n2=n+1
在markdown中写法:
n<sup>2</sup>=n+1
a=log2b
在markdown中写法:
a=log<sub>2</sub>b
? ? ° ? ? ? ? √ " &
在markdown中写法:
? ? ° ? ? ? ? √ \" \&
居中::-:
箭头符号:
在markdown中写法:
$\Rightarrow$ $\Leftarrow$
在 Markdown 文档中,可以直接采用 HTML 标记插入空格(blank space),而且无需任何其他前缀或分隔符。具体如下所示:
插入一个空格 或 或 (non-breaking space)
插入两个空格 ?或?或?(en space)
插入四个空格 ?或?或?(em space)
插入细的空格 ?或?或?(thin space)
在markdown中写法:
插入一个空格
或 或
插入两个空格
?或?或?
插入四个空格
?或?或?
插入细的空格
?或?或?
注意:不要漏掉分号。
当用户进行鼠标框选选择了页面上的内容时,把选择的内容进行上报。
虽然这需求就一句话的事,但是很显然,没那么简单...
因为鼠标框选说起来简单,就是选择的内容,但是这包含很多中情况,比如:只选择文案、选择图片、选择输入框、输入框中的内容选择、iframe、等。
简单总结,分为以下几点:
鼠标框选包含以下几点:
老生常谈的技术点了,这里不能用节流,因为肯定不能你鼠标选择的时候,隔一段时间返回一段内容,肯定是选择之后一起返回。
这里用 debounce 主要也是用在事件监听和事件处理上。
事件监听,因为鼠标选择,不仅仅是鼠标按下到鼠标抬起,还包括双击、右键、全选。
需要使用事件监听对事件作处理。
Range 接口表示一个包含节点与文本节点的一部分的文档片段。
Range 是浏览器原生的对象。
<body>
<ul>
<li>Vite</li>
<li>Vue</li>
<li>React</li>
<li>VitePress</li>
<li>NaiveUI</li>
</ul>
</body>
<script>
// 创建 Range 对象
const range=new Range()
const liDoms=document.querySelectorAll("li");
// Range 起始位置在 li 2
range.setStartBefore(liDoms[1]);
// Range 结束位置在 li 3
range.setEndAfter(liDoms[2]);
// 获取 selection 对象
const selection=window.getSelection();
// 添加光标选择的范围
selection.addRange(range);
</script>
可以看到,选择内容为第二行和第三行
只选择 li 中的 itePres
可以看出 range 属性对应的值
const range=document.createRange();
const range=window.getSelection().getRangeAt(0)
if (document.caretRangeFromPoint) {
range=document.caretRangeFromPoint(e.clientX, e.clientY);
}
const range=new Range()
Selection 对象表示用户选择的文本范围或插入符号的当前位置。它代表页面中的文本选区,可能横跨多个元素。
window.getSelection()
锚指的是一个选区的起始点(不同于 HTML 中的锚点链接)。当我们使用鼠标框选一个区域的时候,锚点就是我们鼠标按下瞬间的那个点。在用户拖动鼠标时,锚点是不会变的。
选区的焦点是该选区的终点,当你用鼠标框选一个选区的时候,焦点是你的鼠标松开瞬间所记录的那个点。随着用户拖动鼠标,焦点的位置会随着改变。
范围指的是文档中连续的一部分。一个范围包括整个节点,也可以包含节点的一部分,例如文本节点的一部分。用户通常下只能选择一个范围,但是有的时候用户也有可能选择多个范围。
一个用户可编辑的元素(例如一个使用 contenteditable 的 HTML 元素,或是在启用了 designMode 的 Document 的子元素)。
首先要清楚,选择的起点称为锚点(anchor),终点称为焦点(focus)。
function debounce (fn, time=500) {
let timeout=null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout) // 每当触发时,把前一个 定时器 clear 掉
timeout=setTimeout(()=> { // 创建一个新的 定时器,并赋值给 timeout
fn.apply(this, arguments)
}, time)
}
}
/**
* debounce 函数类型
*/
type DebouncedFunction<F extends (...args: any[])=> any>=(...args: Parameters<F>)=> void
/**
* debounce 防抖函数
* @param {Function} func 函数
* @param {number} wait 等待时间
* @param {false} immediate 是否立即执行
* @returns {DebouncedFunction}
*/
function debounce<F extends (...args: any[])=> any>(
func: F,
wait=500,
immediate=false
): DebouncedFunction<F> {
let timeout: ReturnType<typeof setTimeout> | null
return function (this: ThisParameterType<F>, ...args: Parameters<F>) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context=this
const later=function () {
timeout=null
if (!immediate) {
func.apply(context, args)
}
}
const callNow=immediate && !timeout
if (timeout) {
clearTimeout(timeout)
}
timeout=setTimeout(later, wait)
if (callNow) {
func.apply(context, args)
}
}
}
nterface IGetSelectContentProps {
type: 'html' | 'text'
content: string
}
/**
* 获取选择的内容
* @returns {null | IGetSelectContentProps} 返回选择的内容
*/
const getSelectContent=(): null | IGetSelectContentProps=> {
const selection=window.getSelection()
if (selection) {
// 1. 是焦点在 input 输入框
// 2. 没有选中
// 3. 选择的是输入框
if (selection.isCollapsed) {
return selection.toString().trim().length
? {
type: 'text',
content: selection.toString().trim()
}
: null
}
// 获取选择范围
const range=selection.getRangeAt(0)
// 获取选择内容
const rangeClone=range.cloneContents()
// 判断选择内容里面有没有节点
if (rangeClone.childElementCount > 0) {
// 创建 div 标签
const container=document.createElement('div')
// div 标签 append 复制节点
container.appendChild(rangeClone)
// 如果复制的内容长度为 0
if (!selection.toString().trim().length) {
// 判断是否有选择特殊节点
const isSpNode=hasSpNode(container)
return isSpNode
? {
type: 'html',
content: container.innerHTML
}
: null
}
return {
type: 'html',
content: container.innerHTML
}
} else {
return selection.toString().trim().length
? {
type: 'text',
content: selection.toString().trim()
}
: null
}
} else {
return null
}
}
/**
* 判断是否包含特殊元素
* @param {Element} parent 父元素
* @returns {boolean} 是否包含特殊元素
*/
const hasSpNode=(parent: Element): boolean=> {
const nodeNameList=['iframe', 'svg', 'img', 'audio', 'video']
const inpList=['input', 'textarea', 'select']
return Array.from(parent.children).some((node)=> {
if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
if (
inpList.includes(node.nodeName.toLocaleLowerCase()) &&
(node as HTMLInputElement).value.trim().length
)
return true
if (node.children) {
return hasSpNode(node)
}
return false
})
}
/**
* 获取框选的文案内容
* @returns {string} 返回框选的内容
*/
const getSelectTextContent=(): string=> {
const selection=window.getSelection()
return selection?.toString().trim() || ''
}
// 是否时鼠标点击动作
let selectionchangeMouseTrack: boolean=false
const selectionChangeFun=debounce(()=> {
const selectContent=getSelectContent()
console.log('selectContent', selectContent)
// todo... 处理上报
selectionchangeMouseTrack=false
})
// 添加 mousedown 监听事件
document.addEventListener('mousedown', ()=> {
selectionchangeMouseTrack=true
})
// 添加 mouseup 监听事件
document.addEventListener(
'mouseup',
debounce(()=> {
selectionChangeFun()
}, 100)
)
// 添加 selectionchange 监听事件
document.addEventListener(
'selectionchange',
debounce(()=> {
if (selectionchangeMouseTrack) return
selectionChangeFun()
})
)
// 添加 dblclick 监听事件
document.addEventListener('dblclick', ()=> {
selectionChangeFun()
})
// 添加 contextmenu 监听事件
document.addEventListener(
'contextmenu',
debounce(()=> {
selectionChangeFun()
})
)
也可以进行封装
/**
* addEventlistener function 类型
*/
export interface IEventHandlerProps {
[eventName: string]: EventListenerOrEventListenerObject
}
let selectionchangeMouseTrack: boolean=false
const eventHandlers: IEventHandlerProps={
// 鼠标 down 事件
mousedown: ()=> {
selectionchangeMouseTrack=true
},
// 鼠标 up 事件
mouseup: debounce(()=> selectionChangeFun(), 100),
// 选择事件
selectionchange: debounce(()=> {
if (selectionchangeMouseTrack) return
selectionChangeFun()
}),
// 双击事件
dblclick: ()=> selectionChangeFun(),
// 右键事件
contextmenu: debounce(()=> selectionChangeFun())
}
Object.keys(eventHandlers).forEach((event)=> {
document.addEventListener(event, eventHandlers[event])
})
function debounce (fn, time=500) {
let timeout=null; // 创建一个标记用来存放定时器的返回值
return function () {
clearTimeout(timeout) // 每当触发时,把前一个 定时器 clear 掉
timeout=setTimeout(()=> { // 创建一个新的 定时器,并赋值给 timeout
fn.apply(this, arguments)
}, time)
}
}
let selectionchangeMouseTrack=false
document.addEventListener('mousedown', (e)=> {
selectionchangeMouseTrack=true
console.log('mousedown', e)
})
document.addEventListener('mouseup', debounce((e)=> {
console.log('mouseup', e)
selectionChangeFun()
}, 100))
document.addEventListener('selectionchange', debounce((e)=> {
console.log('selectionchange', e)
if (selectionchangeMouseTrack) return
selectionChangeFun()
}))
document.addEventListener('dblclick', (e)=> {
console.log('dblclick', e)
selectionChangeFun()
})
document.addEventListener('contextmenu',debounce(()=> {
selectionChangeFun()
}))
const selectionChangeFun=debounce(()=> {
const selectContent=getSelectContent()
selectionchangeMouseTrack=false
console.log('selectContent', selectContent)
})
const getSelectContent=()=> {
const selection=window.getSelection();
if (selection) {
// 1. 是焦点在 input 输入框
// 2. 没有选中
// 3. 选择的是输入框
if (selection.isCollapsed) {
return selection.toString().trim().length ? {
type: 'text',
content: selection.toString().trim()
} : null
}
// 获取选择范围
const range=selection.getRangeAt(0);
// 获取选择内容
const rangeClone=range.cloneContents()
// 判断选择内容里面有没有节点
if (rangeClone.childElementCount > 0) {
const container=document.createElement('div');
container.appendChild(rangeClone);
if (!selection.toString().trim().length) {
const hasSpNode=getSpNode(container)
return hasSpNode ? {
type: 'html',
content: container.innerHTML
} : null
}
return {
type: 'html',
content: container.innerHTML
}
} else {
return selection.toString().trim().length ? {
type: 'text',
content: selection.toString().trim()
} : null
}
} else {
return null
}
}
const getSpNode=(parent)=> {
const nodeNameList=['iframe', 'svg', 'img', 'audio', 'video']
const inpList=['input', 'textarea', 'select']
return Array.from(parent.children).some((node)=> {
if (nodeNameList.includes(node.nodeName.toLocaleLowerCase())) return true
if (inpList.includes(node.nodeName.toLocaleLowerCase()) && node.value.trim().length) return true
if (node.children) {
return getSpNode(node)
}
return false
})
}
们今天来分析解释一下这个表达式string hrefPattern=@"href\s*=\s*(?:""'[""']|(?<1>[^>\s]+))";,并用实例演示用法。这个正则表达式用于从文本中提取href属性的值,这些值可以是被单引号或双引号包围的,或者是不包含大于符号和空白字符的文本。我们分解这个正则表达式来详细解释它的各个部分:
1. href\s*=\s*: 这部分匹配 href 关键字,后面可以跟着零个或多个空白字符,然后是一个等号,再然后又是零个或多个空白字符。其中href: 直接匹配文本中的"href",这是HTML中表示链接地址的属性名称。\s*=\s*: 匹配等号(=),等号前后可以有0个或多个空白字符(包括空格、制表符、换行符等)。
2. (?:...): 这是一个非捕获组,意味着它会匹配括号内的内容,但不会为其创建一个捕获组。这意味着我们不能直接从匹配结果中提取这部分内容。
3. [""'](?<1>[^""']*)[""']: 这部分匹配被单引号或双引号包围的任何内容。具体来说:
1. [""']: 匹配一个单引号或双引号。
2. (?<1>[^\"']*): 创建了一个命名捕获组,名为1,用来捕获在引号之间的任何非引号字符序列,这就是href属性的值。(?<1>...): 这是一个命名捕获组,但这里它被放在了一个非捕获组内,这意味着它不会捕获匹配的内容。
3. [^""']*: 匹配任何不是单引号或双引号的字符零次或多次。
4. [""']: 再次匹配一个单引号或双引号。
4. |: 或者操作符,表示前面的模式和后面的模式中的任何一个可以匹配。又叫管道符号,代表逻辑“或”操作,也就是表示前面的模式与后面的模式任一满足即可。
5. (?<1>[^>\s]+): 这部分匹配任何不是大于符号或空白字符的字符一次或多次。这也是一个命名捕获组,但同样,它被放在了一个非捕获组内。当href值没有被引号包围时使用。也就是这部分匹配不是大于符号(>)和空白字符的任何字符1次或多次,但不包括引号。
综上所述,此正则表达式能够处理以下两种格式的href属性及其值:
1. 被引号包围的情况:<a href="http://example.com">...</a> 或 <a href='http://example.com'>...</a>
2. 未被引号包围的情况:<a href=http://example.com>...</a>
实例演示用法:
using System.Text.RegularExpressions;
namespace ConsoleAppC
{
internal class Program
{
static void Main(string[] args)
{
string inputString=@"<a href=""http://example.com"">Link</a>
<a href='http://another.example.com'>Another Link</a>
<a href=http://noquotes.example.com>No Quotes Link</a>";
string hrefPattern=@"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))";
MatchCollection matches=Regex.Matches(inputString, hrefPattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value); // 输出匹配到的href属性值
Console.WriteLine($"Found href value: {match.Groups[1].Value} at index: {match.Groups[1].Index}");
}
}
}
}
运行这段代码后,将输出如下结果:
href="http://example.com"
Found href value: http://example.com at index: 9
href='http://another.example.com'
Found href value: http://another.example.com at index: 72
href=http://noquotes.example.com
Found href value: http://noquotes.example.com at index: 150
为了给大家演示如何使用这个正则表达式,我们再看以下例子:
假设我们有以下的HTML片段:
<a href="https://www.example.com">Click here</a>
<a href='https://www.example.org'>Go there</a>
<a href="https://www.example.net" target="_blank">Open external link</a>
使用上述的正则表达式,我们可以提取所有的href属性值:
string input=@"<a href=\""https://www.example.com\"">Click here</a>
<a href='https://www.example.org'>Go there</a>
<a href=\""https://www.example.net\"" target=\""_blank\"">Open external link</a>";
代码为:
string hrefPattern=@"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))";
Regex regex=new Regex(hrefPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches=regex.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine($"Found href: {match.Groups["1"].Value}");
}
string input=@"<a href=\""https://www.example.com\"">Click here</a>
输出将是:
Found href: \"https://www.example.com\"
Found href: https://www.example.org
Found href: \"https://www.example.net\"
注意,这个正则表达式并不完美,它可能无法处理所有可能的HTML格式,但对于简单的用途来说可能已经足够了。
*请认真填写需求信息,我们会在24小时内与您取得联系。