diff --git a/app/src/mobile/util/showKeyboardToolbar.ts b/app/src/mobile/util/showKeyboardToolbar.ts index 8edb11d63..dd95a4301 100644 --- a/app/src/mobile/util/showKeyboardToolbar.ts +++ b/app/src/mobile/util/showKeyboardToolbar.ts @@ -3,8 +3,7 @@ import {hasClosestBlock, hasClosestByMatchTag} from "../../protyle/util/hasClose import {insertEmptyBlock} from "../../block/util"; import {moveToDown, moveToUp} from "../../protyle/wysiwyg/move"; import {Constants} from "../../constants"; -import {focusByRange} from "../../protyle/util/selection"; -import {scrollCenter} from "../../util/highlightById"; +import {focusByRange, getSelectionPosition} from "../../protyle/util/selection"; export const showKeyboardToolbar = (bottom = 0) => { if (getSelection().rangeCount > 0) { @@ -22,14 +21,26 @@ export const showKeyboardToolbar = (bottom = 0) => { } toolbarElement.classList.remove("fn__none"); toolbarElement.style.bottom = bottom + "px"; - if ("android" === window.siyuan.config.system.container && window.JSAndroid) { - // Android 端事件需要滞后一些,所以这里延迟一下 - setTimeout(() => { - scrollCenter(window.siyuan.mobileEditor.protyle, undefined, false, (window.outerHeight - 65) / 2 - 30); - }, 100); - } else { - scrollCenter(window.siyuan.mobileEditor.protyle, undefined, false, (window.outerHeight - 65) / 2 - 30); + + const contentElement = window.siyuan.mobileEditor.protyle.contentElement + const cursorTop = getSelectionPosition(contentElement).top - contentElement.getBoundingClientRect().top; + if (cursorTop < window.innerHeight - 96) { + return; } + contentElement.scroll({ + top: contentElement.scrollTop + cursorTop - ((window.outerHeight - 65) / 2 - 30), + left: contentElement.scrollLeft, + behavior: "smooth" + }); + alert(cursorTop + "," + window.innerHeight); + // if ("android" === window.siyuan.config.system.container && window.JSAndroid) { + // // Android 端事件需要滞后一些,所以这里延迟一下 + // setTimeout(() => { + // scrollCenter(window.siyuan.mobileEditor.protyle, undefined, false, (window.outerHeight - 65) / 2 - 30); + // }, 100); + // } else { + // scrollCenter(window.siyuan.mobileEditor.protyle, undefined, false, (window.outerHeight - 65) / 2 - 30); + // } }; export const hideKeyboardToolbar = () => { diff --git a/app/src/util/highlightById.ts b/app/src/util/highlightById.ts index e97e70ffb..e31f588f2 100644 --- a/app/src/util/highlightById.ts +++ b/app/src/util/highlightById.ts @@ -38,18 +38,10 @@ export const highlightById = (protyle: IProtyle, id: string, top = false) => { } }; -export const scrollCenter = (protyle: IProtyle, nodeElement?: Element, top = false, offset = 0) => { +export const scrollCenter = (protyle: IProtyle, nodeElement?: Element, top = false) => { if (!top && getSelection().rangeCount > 0 && hasClosestBlock(getSelection().getRangeAt(0).startContainer)) { const editorElement = protyle.contentElement; const cursorTop = getSelectionPosition(editorElement).top - editorElement.getBoundingClientRect().top; - if (offset) { // 仅移动端弹起键盘用到 - editorElement.scroll({ - top: editorElement.scrollTop + cursorTop - offset, - left: editorElement.scrollLeft, - behavior: "smooth" - }); - return; - } if (cursorTop < 0) { editorElement.scrollTop = editorElement.scrollTop + cursorTop; } else if (cursorTop > editorElement.clientHeight - 34) {