diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts index 7e24e2d6d..06de392dd 100644 --- a/app/src/protyle/index.ts +++ b/app/src/protyle/index.ts @@ -90,8 +90,8 @@ export class Protyle { if (isSupportCSSHL()) { const styleId = genUUID(); this.protyle.highlight.styleElement.dataset.uuid = styleId; - this.protyle.highlight.styleElement.textContent = `.protyle-wysiwyg::highlight(search-mark-${styleId}) {background-color: var(--b3-highlight-background);color: var(--b3-highlight-color);} - .protyle-wysiwyg::highlight(search-mark-hl-${styleId}) {color: var(--b3-highlight-color);background-color: var(--b3-highlight-current-background)}`; + this.protyle.highlight.styleElement.textContent = `.protyle-content::highlight(search-mark-${styleId}) {background-color: var(--b3-highlight-background);color: var(--b3-highlight-color);} + .protyle-content::highlight(search-mark-hl-${styleId}) {color: var(--b3-highlight-color);background-color: var(--b3-highlight-current-background)}`; } this.protyle.hint = new Hint(this.protyle); diff --git a/app/src/protyle/render/searchMarkRender.ts b/app/src/protyle/render/searchMarkRender.ts index 84207eaa7..1cab32c6f 100644 --- a/app/src/protyle/render/searchMarkRender.ts +++ b/app/src/protyle/render/searchMarkRender.ts @@ -8,6 +8,7 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strin setTimeout(() => { protyle.highlight.markHL.clear(); protyle.highlight.mark.clear(); + protyle.highlight.rangeIndex = 0; protyle.highlight.ranges = []; let isSetHL = false; let hlBlockElement: Element; @@ -20,12 +21,16 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strin }); } + if (!hlBlockElement && hlId === protyle.block.rootID && protyle.title && !protyle.title.element.classList.contains("fn__none")) { + hlBlockElement = protyle.title.element; + } + // 准备一个数组来保存所有文本节点 const textNodes: Node[] = []; const textNodesSize: number[] = []; let currentSize = 0; - const treeWalker = document.createTreeWalker(protyle.wysiwyg.element, NodeFilter.SHOW_TEXT); + const treeWalker = document.createTreeWalker(protyle.contentElement, NodeFilter.SHOW_TEXT); let currentNode = treeWalker.nextNode(); while (currentNode) { textNodes.push(currentNode); @@ -34,7 +39,7 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strin currentNode = treeWalker.nextNode(); } - const text = protyle.wysiwyg.element.textContent; + const text = protyle.contentElement.textContent; const rangeIndexes: { range: Range, startIndex: number, isCurrent: boolean }[] = []; if (keys && keys.length > 0) { keys.forEach(key => { diff --git a/app/src/search/util.ts b/app/src/search/util.ts index 50d0ea2da..cc7a6e893 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -1123,6 +1123,7 @@ export const getArticle = (options: { const contentRect = options.edit.protyle.contentElement.getBoundingClientRect(); if (isSupportCSSHL()) { + let observer:ResizeObserver; searchMarkRender(options.edit.protyle, getResponse.data.keywords, options.id, () => { const highlightKeys = () => { const currentRange = options.edit.protyle.highlight.ranges[options.edit.protyle.highlight.rangeIndex]; @@ -1136,8 +1137,11 @@ export const getArticle = (options: { highlightById(options.edit.protyle, options.id); } }; + if (observer) { + observer.disconnect(); + } highlightKeys(); - const observer = new ResizeObserver(() => { + observer = new ResizeObserver(() => { highlightKeys(); }); observer.observe(options.edit.protyle.wysiwyg.element);