Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Improve mobile keyboard and toolbar transitions https://github.com/siyuan-note/siyuan/issues/19196

Daniel02b06834b2e6
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index d668d31e56..2bf107c6c8 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -26,7 +26,12 @@ import {initAssets} from "../util/assets";
import {bootSync, lockScreen} from "../dialog/processSystem";
import {initMessage, showMessage} from "../dialog/message";
import {goBack} from "./util/MobileBackFoward";
-import {activeBlur, hideKeyboardToolbarByApp, showKeyboardToolbar} from "./util/keyboardToolbar";
+import {
+ activeBlur,
+ hideKeyboardToolbarByApp,
+ hideKeyboardToolbarUtilOnEditorClick,
+ showKeyboardToolbar,
+} from "./util/keyboardToolbar";
import {
getLocalStorage,
initWindowOpenOverride,
@@ -130,6 +135,7 @@ class App {
}, Constants.TIMEOUT_TRANSITION);
}
if (editableElement) {
+ hideKeyboardToolbarUtilOnEditorClick();
// 原生 App 通过桥接主动唤起键盘;移动端浏览器没有桥接,但点击可编辑区域后也会立刻触发 resize,
// 进而调用 activeBlur 关闭键盘(比如三星键盘 https://github.com/siyuan-note/siyuan/issues/18078),所以此处也需要上锁
if (window.JSAndroid && window.JSAndroid.showKeyboard || window.JSHarmony && window.JSHarmony.showKeyboard) {
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 4a84b4f0d5..292cb0d88c 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -100,6 +100,8 @@ let renderKeyboardToolbarTimeout: number;
let scrollSelectionIntoViewTimeout: number;
let clearRenderGutterAfterScroll: () => void;
let showUtil = false;
+let showUtilHeightTimeout: number | undefined;
+let showUtilTimeout: number | undefined;
let preventRender = false;
let preventRenderTimeout: number;
let restoringAndroidBoundedSelection = false;
@@ -716,6 +718,8 @@ const renderSlashMenu = (protyle: IProtyle, toolbarElement: Element) => {
export const showKeyboardToolbarUtil = (oldScrollTop: number) => {
window.siyuan.menus.menu.remove();
showUtil = true;
+ clearTimeout(showUtilHeightTimeout);
+ clearTimeout(showUtilTimeout);
const toolHeight = document.querySelector(".keyboard__bar").clientHeight;
const toolbarElement = document.getElementById("keyboardToolbar");
let keyboardHeight = window.innerHeight / 2 - toolHeight;
@@ -733,17 +737,27 @@ export const showKeyboardToolbarUtil = (oldScrollTop: number) => {
editor.protyle.element.parentElement.style.paddingBottom = keyboardHeight + "px";
editor.protyle.contentElement.scrollTop = oldScrollTop;
}
- setTimeout(() => {
+ showUtilHeightTimeout = window.setTimeout(() => {
+ showUtilHeightTimeout = undefined;
toolbarElement.style.height = keyboardHeight + "px";
updateKeyboardToolbarPosition();
}, Constants.TIMEOUT_TRANSITION); // 防止抖动
- setTimeout(() => {
+ showUtilTimeout = window.setTimeout(() => {
showUtil = false;
+ showUtilTimeout = undefined;
}, 1000); // 防止光标改变后斜杆菜单消失
};
const hideKeyboardToolbarUtil = () => {
+ clearTimeout(showUtilHeightTimeout);
+ clearTimeout(showUtilTimeout);
+ showUtilHeightTimeout = undefined;
+ showUtilTimeout = undefined;
+ showUtil = false;
const toolbarElement = document.getElementById("keyboardToolbar");
+ if (!toolbarElement) {
+ return;
+ }
toolbarElement.style.height = "";
updateKeyboardToolbarPosition();
const editor = getCurrentEditor();
@@ -755,6 +769,14 @@ const hideKeyboardToolbarUtil = () => {
toolbarElement.querySelector('.keyboard__action[data-type="done"] use').setAttribute("xlink:href", "#iconKeyboardHide");
};
+export const hideKeyboardToolbarUtilOnEditorClick = () => {
+ const toolbarElement = document.getElementById("keyboardToolbar");
+ if (!showUtil && (!toolbarElement || toolbarElement.clientHeight <= 100)) {
+ return;
+ }
+ hideKeyboardToolbarUtil();
+};
+
const renderKeyboardToolbar = () => {
clearTimeout(renderKeyboardToolbarTimeout);
renderKeyboardToolbarTimeout = window.setTimeout(() => {
@@ -872,23 +894,22 @@ export const showKeyboardToolbar = () => {
}
const toolbarElement = document.getElementById("keyboardToolbar");
const selection = getSelection();
+ // 空块恢复焦点时 Selection 可能暂时为空,但原生键盘已经显示,仍需隐藏普通底栏。
+ notifyMobileKeyboardChange(true);
if (selection.rangeCount > 0 && (
hasClosestByClassName(selection.getRangeAt(0).startContainer, "protyle-lite-fragment", true) ||
hasClosestByClassName(selection.getRangeAt(0).startContainer, "agent-chat__composer-host", true))) {
// Lite 编辑器使用自己的工具栏,不能显示会作用于下层文档的移动端编辑工具栏。
- notifyMobileKeyboardChange(true);
toolbarElement.classList.add("fn__none");
return;
}
if (!toolbarElement.classList.contains("fn__none")) {
- notifyMobileKeyboardChange(true);
return;
}
if (selection.rangeCount === 0) {
return;
}
toolbarElement.classList.remove("fn__none");
- notifyMobileKeyboardChange(true);
toolbarElement.style.zIndex = (++window.siyuan.zIndex).toString();
updateKeyboardToolbarPosition();
const modelElement = document.getElementById("model");
diff --git a/app/src/mobile/util/mobileKeyboardChange.test.ts b/app/src/mobile/util/mobileKeyboardChange.test.ts
index 23ea33b515..2eda5d083c 100644
--- a/app/src/mobile/util/mobileKeyboardChange.test.ts
+++ b/app/src/mobile/util/mobileKeyboardChange.test.ts
@@ -53,6 +53,17 @@ describe("mobile keyboard change", () => {
assert.deepEqual(notifier.changes, [true]);
});
+ it("keeps the keyboard open when a stale close arrives during reopening", () => {
+ const notifier = createNotifier();
+
+ notifier.notify(true);
+ notifier.notify(false);
+ notifier.notify(true);
+ notifier.runTimers();
+
+ assert.deepEqual(notifier.changes, [true, true]);
+ });
+
it("restarts the close delay when another close notification arrives", () => {
const notifier = createNotifier();