vue3關(guān)鍵字高亮指令的實現(xiàn)詳解
前言
因為業(yè)務(wù)需要,要在當前項目上做一個將搜索結(jié)果所存在的關(guān)鍵字進行高亮顯示的需求,然后在網(wǎng)上找了一下類似解決方案,最后在這篇文章中找到了一個解決方案,所以這次指令的制作其實就是將這篇文章進行一個指令的制作而已。(經(jīng)過作者了同意)
簡單間講下上面文章的思路
總結(jié)一下這篇文章的兩種方案:
- 插入替換標簽方式。就是通過正則匹配,匹配出對應(yīng)的關(guān)鍵字然后通過將關(guān)鍵字包裹在一層span標簽中替換關(guān)鍵字,重新渲染視圖。
- 渲染層貼標簽方式。就是找到關(guān)鍵字渲染的DOM,在這個DOM之上創(chuàng)建一個貼圖的渲染層,通過提供的DOM2范圍API(可以參考紅寶書第16章的范圍,有很詳細的講解)確定貼圖的位置,創(chuàng)建對應(yīng)的span貼圖標簽放到渲染層上。
我將上面兩種方案都實現(xiàn)了,核心代碼就是這篇文章提供的,大家可以去參考,我就不多贅述了。
實現(xiàn)
代碼里面有注釋就不一行一行解釋了
/** * 關(guān)鍵字高亮 * 使用方式:v-highlight="{ keyWord: '要高亮的文本', textDomSelectors: ['p'], // 要高亮的文本所在的標簽選擇器 如:<p>123123213要高亮的文本13123</p> renderWay: RenderWay.ALTERNATE // 類型 }" */ import { debounced, escString } from '@/utils/utils' export enum RenderWay { ALTERNATE, // 替換文本為span標簽?zāi)J? LABELLING, // 貼標簽?zāi)J? SVG// SVG模式,這個方式有渲染問題,暫不做(動態(tài)插入的的標簽無法渲染,估計是DOM改變后,但是沒有更新視圖) } export type HighlightParamsType = { keyWord: string // 關(guān)鍵字 textDomSelectors: string[] // DOM選擇器(于需要高亮的文本所在的節(jié)點) renderWay?: RenderWay // 渲染方式 } export type CDomRectType = (DOMRect & { tLeft?: number, tTop?: number }) /** * 創(chuàng)建高亮貼標簽區(qū)域 * @param targetEl */ function createHighlightArea(targetEl: HTMLElement, renderWay: RenderWay = RenderWay.LABELLING) { return new Promise<HTMLElement | null>((res, rej) => { let tagName = renderWay !== RenderWay.SVG ? 'div' : 'svg' if (!targetEl) rej() targetEl.style.setProperty('position', 'relative') const { offsetWidth: width, offsetHeight: height } = targetEl let area: any = targetEl.querySelector('#highlight-area') // 查看目標元素節(jié)點下是否存在ID為highlight-area的元素 if (area) area.innerHTML = '' else if (width && height) { area = document.createElement(tagName) area.setAttribute('id', 'highlight-area') area.style.setProperty('position', 'absolute') area.style.setProperty('top', '0') area.style.setProperty('left', '0') area.style.setProperty('right', '0') area.style.setProperty('bottom', '0') area.style.setProperty('pointer-events', 'none') area.style.setProperty('z-index', '10') if (renderWay === RenderWay.SVG) { area.setAttribute('width', String(width)) area.setAttribute('height', String(height)) area.setAttribute('xmlns', 'http://www.w3.org/2000/svg') // area.setAttribute('viewBox', `0 0 ${width} ${height}`) } console.log('創(chuàng)建渲染層:', area) targetEl.appendChild(area) } res(area) }) } /** * 匹配DOM節(jié)點中所有關(guān)鍵字 * @param word * @param el */ function matchingAllKeyWord(value: HighlightParamsType): CDomRectType[] | HTMLElement[] { let result: any = [] value.textDomSelectors.forEach((selector: string) => { const doms = document.querySelectorAll(selector) Array.from(doms).forEach((dom: any) => { if (!dom) return dom.style.setProperty('position', 'relative') dom.style.setProperty('z-index', '100') if (value.renderWay === RenderWay.ALTERNATE) { result.push(dom) } else { const rectItemArr = createRangeRectItem(value.keyWord, dom) if (rectItemArr.length) result = [...result, ...rectItemArr] } }) }) return result } function insertLabel(keyWord: string, dom: HTMLElement) { const regExp = new RegExp(keyWord, 'gi') dom.innerHTML = dom.innerHTML.replace(regExp, `<span style="background: yellow">${keyWord}</span>`) } /** * 使用貼標簽方式,創(chuàng)建一個range的rect信息 * @param keyWord 需要匹配的關(guān)鍵字 * @param reg 正則 * @param dom */ function createRangeRectItem(keyWord: string, dom: HTMLElement): CDomRectType[] { const textDom: any = dom.firstChild let matchResult: any = null const reg: RegExp = new RegExp(escString(keyWord), 'gi') let result: CDomRectType[] = [] const range = document.createRange() while (matchResult = reg.exec(dom.innerText)) { const { index } = matchResult if (textDom.length < index + keyWord.length) continue // 確定范圍邊界(注意:下面兩個方法的第一個參數(shù)需要傳入的確切的值,比如某個標簽里面的文本,只要文本,不能有其他內(nèi)容) range.setStart(textDom, index) range.setEnd(textDom, index + keyWord.length) // 獲取這個范圍的Rect信息 const recItem = range.getBoundingClientRect() if (recItem) result = handleMultiLineRectItem(recItem, dom, keyWord.length, matchResult) } range.detach() return result } /** * 處理跨行高亮數(shù)據(jù) * @param rectItem * @param lineHeight * @returns CDomRectType | CDomRectType[] */ function handleMultiLineRectItem(rectItem: CDomRectType, dom: HTMLElement, len: number, result: RegExpExecArray): CDomRectType[] { const textDom: any = dom.firstChild const standardRange = document.createRange() standardRange.setStart(textDom, 0) standardRange.setEnd(textDom, 0) const standardRangeReact = standardRange.getBoundingClientRect() const lineHeight = standardRangeReact.height if (lineHeight === rectItem.height) return [rectItem] else { /** * 文本:我要顯示高亮的文本 * 關(guān)鍵字:keyWord=顯示高亮的 * 解釋:高字在第一行,亮字在第二行,所以這里涉及到了換行 * 定義兩個指針i和j,i指向keyWord的0坐標(顯),j指向1坐標(示) * 在循環(huán)中不斷創(chuàng)建范圍,校驗第i到第j個字符所創(chuàng)建的范圍中的高度是否是一行內(nèi)容的高度 * 如果是,則將這個范圍加入到結(jié)果數(shù)組中,然后i++,j++,繼續(xù)循環(huán) * 注意:因為只需要一個貼圖來渲染不換行的文案,比如(顯示高)只修要一個span標簽顯示,但是之前的循環(huán)中灰創(chuàng)建(顯,顯示,顯示高)三個范圍 * 實際上只需要(顯示高)這個范圍的數(shù)據(jù)而已,所以要在j!==1的時候刪除掉前面的內(nèi)容,只保留最后一個 * 如果找到了換行的那個字符(亮),這個時候i就會重新賦值為i=j-1(此時的j為坐標為4,對應(yīng)‘的'字符)在重復(fù)上訴過程就能拆分出來兩行內(nèi)容所需的數(shù)據(jù)了 * * 推薦一個優(yōu)化方案: * 可以結(jié)合二分查找進行優(yōu)化,比較忙,就不魔改了 * */ let resultArr: CDomRectType[] = [] // 處理多行 let i = 0 let j = 1 while (j <= len) { const subRange = document.createRange() subRange.setStart(textDom, result.index + i) subRange.setEnd(textDom, result.index + j) const subRangeReact = subRange.getBoundingClientRect() if (subRangeReact.height === lineHeight) { if (j !== 1) resultArr.pop() // 只保留單行內(nèi)容的最后一個 j++ } else { i = j - 1 } resultArr.push(subRangeReact) subRange.detach() } return resultArr } } /** * 計算要生成高亮區(qū)域的范圍的真實渲染位置 * 注意:因為getBoundingClientRect拿到的是當前節(jié)點和視口之間的關(guān)系 * 所以需要計算出當前節(jié)點rect數(shù)據(jù)和當前節(jié)點父級元素rect數(shù)據(jù)之間的相對位置 * 推薦的文章中有解釋 * @param rectArr */ function calcHighlightRealPosition(rectArr: CDomRectType[], parent: HTMLElement): CDomRectType[] { const parentRect = parent.getBoundingClientRect() if (!parentRect) return rectArr rectArr.forEach((rect: CDomRectType) => { rect['tLeft'] = rect.left - parentRect.left rect['tTop'] = rect.top - parentRect.top }) return rectArr } /** * 創(chuàng)建span標簽,用于高亮背景顯示 * @param rect */ function createdSpanBgDom(rect: CDomRectType): HTMLSpanElement { const span = document.createElement('span') span.style.setProperty('position', 'absolute') span.style.setProperty('left', `${rect.tLeft}px`) span.style.setProperty('top', `${rect.tTop}px`) span.style.setProperty('width', `${rect.width}px`) span.style.setProperty('height', `${rect.height}px`) span.style.setProperty('z-index', `-1`) span.style.setProperty('background-color', 'rgba(255, 255, 0,.4)') return span } /** * todo 測試代碼,因為有無法渲染的問題,先不做 * @param rect */ function createSvgPath(rect: CDomRectType[]) { const path = document.createElementNS('http://www.w3.org/2000/svg', 'path') const d = 'M 10 10 L 50 40 L 100 10' path.setAttribute('d', d) path.setAttribute('stroke', 'red') path.setAttribute('fill', 'red') return path } export default debounced( async function(el: HTMLElement, binding: any, vnode: any, prevVnode: any) { const params = binding.value as HighlightParamsType const arr = matchingAllKeyWord(params) if (params.renderWay !== RenderWay.ALTERNATE) { if (!params.renderWay) params['renderWay'] = RenderWay.LABELLING const areaDom = await createHighlightArea(el, params.renderWay) await nextTick() if (!areaDom) return const rangeDomRectInfoArr = calcHighlightRealPosition( arr as CDomRectType[], areaDom ) if (params.renderWay === RenderWay.LABELLING) { rangeDomRectInfoArr.forEach((rect: DOMRect) => { const span = createdSpanBgDom(rect) areaDom.appendChild(span) }) } else if (params.renderWay === RenderWay.SVG) { console.log('創(chuàng)建path', areaDom) // todo:SVG方式有渲染問題,待解決 // const path = createSvgPath(rangeDomRectInfoArr) // areaDom.appendChild(path) } } else { arr.forEach((dom) => { insertLabel(params.keyWord, dom as HTMLElement) }) } }, 500 )
工具函數(shù)
/** * * 添加轉(zhuǎn)義字符 * @param value */ export function escString(value:string) { let arr = ['(', '[', '{', '/', '^', '$', '|', '}', ']', ')', '?', '*', '+', '.', "'", '"'] for (let i = 0; i < arr.length; i++) { if (value) { if (value.indexOf(arr[i]) > -1) { const reg = (str:string) => str.replace(/[[]/{}()*'"\|+?.\^$|]/g, "\$&") value = reg(value) } } } return value; }
/** * @des 防抖 ,多次只執(zhí)行最后一次 * @param func 需要包裝的函數(shù) * @param delay 延遲時間,單位ms * @param immediate 是否默認執(zhí)行一次(第一次不延遲) * 返回值為any不為Function主要是為了解決使用window對象上的某些監(jiān)聽返回的錯誤: Type 'Function' provides no match for the signature '(this: GlobalEventHandlers, ev: UIEvent): any' */ export const debounced = ( func: Function, delay: number = 500, immediate: boolean = false ): any => { let timer: any return (...args: any) => { if (immediate) { func.apply(this, args) immediate = false return } clearTimeout(timer) timer = setTimeout(() => { func.apply(this, args) }, delay) } }
效果展示
插入替換標簽方式
渲染層貼標簽方式
結(jié)語
這個指令只能算是簡單的指令,基本滿足了我當前的業(yè)務(wù)需求,通用性不是很大,大家可以參考,還有很多優(yōu)化的方面沒做,比如可以在方案2中,重新搜索時,要移除之前渲染過的節(jié)點,或者對之前的節(jié)點進行重復(fù)利用,在校驗換行的方法中進行二分查找優(yōu)化等,這兩種方案都是對DOM進行操作,感覺都不是最優(yōu),原來是想使用svg做,但是在動態(tài)插入path等標簽的時候會有無法正常渲染的問題,估計是vue沒有通知視圖更新的原因,也可以使用canvas(搜結(jié)果索數(shù)據(jù)量很大的時候可以這么做),主要還是看業(yè)務(wù)需求,所以看大伙選擇。
到此這篇關(guān)于vue3關(guān)鍵字高亮指令的實現(xiàn)詳解的文章就介紹到這了,更多相關(guān)vue3關(guān)鍵字高亮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3實現(xiàn)模擬地圖站點名稱按需顯示的功能(車輛模擬地圖)
最近在做車輛模擬地圖,在實現(xiàn)控制站點名稱按需顯示,下面通過本文給大家分享vue3實現(xiàn)模擬地圖站點名稱按需顯示的功能,感興趣的朋友跟隨小編一起看看吧2024-06-06Vue監(jiān)控路由與路由參數(shù), 刷新當前頁面數(shù)據(jù)的方法匯總
這篇文章主要介紹了Vue監(jiān)控路由與路由參數(shù), 刷新當前頁面數(shù)據(jù)的幾種方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10淺談vuejs實現(xiàn)數(shù)據(jù)驅(qū)動視圖原理
這篇文章主要介紹了淺談vuejs實現(xiàn)數(shù)據(jù)驅(qū)動視圖原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02vue使用v-for循環(huán)獲取數(shù)組最后一項
這篇文章主要介紹了vue使用v-for循環(huán)獲取數(shù)組最后一項問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03