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小時內與您取得聯系。