From 6a7fb4ad61ea3c60bb02e3c569d0e13e53cc5d84 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sun, 8 Dec 2024 11:52:44 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/13343 --- app/src/protyle/render/searchMarkRender.ts | 5 ++++- app/src/search/util.ts | 9 ++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/src/protyle/render/searchMarkRender.ts b/app/src/protyle/render/searchMarkRender.ts index e7aeec82d..e518cdf02 100644 --- a/app/src/protyle/render/searchMarkRender.ts +++ b/app/src/protyle/render/searchMarkRender.ts @@ -1,6 +1,6 @@ import {Constants} from "../../constants"; -export const searchMarkRender = (protyle: IProtyle, keys: string[], isHL: boolean) => { +export const searchMarkRender = (protyle: IProtyle, keys: string[], isHL: boolean, cb?: () => void) => { if (!isSupportCSSHL()) { return; } @@ -76,6 +76,9 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], isHL: boolea if (isHL) { CSS.highlights.set("search-mark-hl-" + protyle.highlight.styleElement.dataset.uuid, protyle.highlight.markHL); } + if (cb) { + cb(); + } }, protyle.wysiwyg.element.querySelector(".hljs") ? Constants.TIMEOUT_TRANSITION : 0); }; diff --git a/app/src/search/util.ts b/app/src/search/util.ts index a13e0596f..5e843de21 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -1236,20 +1236,19 @@ export const getArticle = (options: { }); const contentRect = options.edit.protyle.contentElement.getBoundingClientRect(); - let matchRectTop: number; if (isSupportCSSHL()) { options.edit.protyle.highlight.rangeIndex = 0; - searchMarkRender(options.edit.protyle, getResponse.data.keywords, true); - matchRectTop = options.edit.protyle.highlight.ranges[0].getBoundingClientRect().top; + searchMarkRender(options.edit.protyle, getResponse.data.keywords, true, () => { + options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + options.edit.protyle.highlight.ranges[0].getBoundingClientRect().top - contentRect.top - contentRect.height / 2; + }); } else { const matchElements = options.edit.protyle.wysiwyg.element.querySelectorAll('span[data-type~="search-mark"]'); if (matchElements.length === 0) { return; } matchElements[0].classList.add("search-mark--hl"); - matchRectTop = matchElements[0].getBoundingClientRect().top; + options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + matchElements[0].getBoundingClientRect().top - contentRect.top - contentRect.height / 2; } - options.edit.protyle.contentElement.scrollTop = options.edit.protyle.contentElement.scrollTop + matchRectTop - contentRect.top - contentRect.height / 2; }); }); });