From a3ec74f72c07fa360caeb0cec455358a20717e22 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Wed, 20 Dec 2023 10:10:49 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/protyle/util/onGet.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.ts index 94ee3ce6c..222ce62f0 100644 --- a/app/src/protyle/util/onGet.ts +++ b/app/src/protyle/util/onGet.ts @@ -4,7 +4,7 @@ import {fetchPost} from "../../util/fetch"; import {processRender} from "./processCode"; import {highlightRender} from "../render/highlightRender"; import {blockRender} from "../render/blockRender"; -import {bgFade} from "../../util/highlightById"; +import {bgFade, scrollCenter} from "../../util/highlightById"; /// #if !MOBILE import {pushBack} from "../../util/backForward"; /// #endif @@ -391,21 +391,31 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScr } /// #endif } - if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) { - focusElement.scrollIntoView(); - } else if (scrollAttr && scrollAttr.scrollTop) { + if (scrollAttr && typeof scrollAttr.scrollTop === "number") { protyle.contentElement.scrollTop = scrollAttr.scrollTop; + } + if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) { + const contentRect = protyle.contentElement.getBoundingClientRect(); + const focusRect = focusElement.getBoundingClientRect(); + if (contentRect.top > focusRect.top || contentRect.bottom < focusRect.bottom) { + scrollCenter(protyle, focusElement); + } } else { protyle.observerLoad?.disconnect(); return; } // 加强定位 protyle.observerLoad = new ResizeObserver(() => { - if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) { - focusElement.scrollIntoView(); - } else if (scrollAttr && scrollAttr.scrollTop) { + if (scrollAttr && typeof scrollAttr.scrollTop === "number") { protyle.contentElement.scrollTop = scrollAttr.scrollTop; } + if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) { + const contentRect = protyle.contentElement.getBoundingClientRect(); + const focusRect = focusElement.getBoundingClientRect(); + if (contentRect.top > focusRect.top || contentRect.bottom < focusRect.bottom) { + scrollCenter(protyle, focusElement); + } + } }); protyle.observerLoad.observe(protyle.wysiwyg.element); protyle.observer.unobserve(protyle.wysiwyg.element);