Skip to content

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

:bug: Improve mobile bottom bar display timing when closing the keyboard https://github.com/siyuan-note/siyuan/issues/19192

Daniel7b1a5ec4736d
diff --git a/app/src/mobile/util/keyboardToolbar.ts b/app/src/mobile/util/keyboardToolbar.ts
index 268618bb5e..4a84b4f0d5 100644
--- a/app/src/mobile/util/keyboardToolbar.ts
+++ b/app/src/mobile/util/keyboardToolbar.ts
@@ -69,6 +69,7 @@ import {
getInlineFontFamilyValue,
renderMobileFontFamilyMenu,
} from "../../protyle/toolbar/fontFamilyMenu";
+import {notifyMobileKeyboardChange} from "./mobileKeyboardChange";
type TAndroidBoundedSelection = {
container: HTMLElement,
@@ -875,19 +876,19 @@ export const showKeyboardToolbar = () => {
hasClosestByClassName(selection.getRangeAt(0).startContainer, "protyle-lite-fragment", true) ||
hasClosestByClassName(selection.getRangeAt(0).startContainer, "agent-chat__composer-host", true))) {
// Lite 编辑器使用自己的工具栏,不能显示会作用于下层文档的移动端编辑工具栏。
- window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: true}));
+ notifyMobileKeyboardChange(true);
toolbarElement.classList.add("fn__none");
return;
}
if (!toolbarElement.classList.contains("fn__none")) {
- window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: true}));
+ notifyMobileKeyboardChange(true);
return;
}
if (selection.rangeCount === 0) {
return;
}
toolbarElement.classList.remove("fn__none");
- window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: true}));
+ notifyMobileKeyboardChange(true);
toolbarElement.style.zIndex = (++window.siyuan.zIndex).toString();
updateKeyboardToolbarPosition();
const modelElement = document.getElementById("model");
@@ -989,7 +990,7 @@ export const hideKeyboardToolbar = () => {
if (modelElement.style.transform === "translateX(0px)") {
modelElement.style.paddingBottom = "";
}
- window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: false}));
+ notifyMobileKeyboardChange(false);
};
export const hideKeyboardToolbarByApp = (preserveSelection = false) => {
diff --git a/app/src/mobile/util/mobileAppUtil.ts b/app/src/mobile/util/mobileAppUtil.ts
index 9873b7a760..3385f457a6 100644
--- a/app/src/mobile/util/mobileAppUtil.ts
+++ b/app/src/mobile/util/mobileAppUtil.ts
@@ -1,4 +1,5 @@
import {hasClosestByAttribute, hasClosestByClassName} from "../../protyle/util/hasClosest";
+import {notifyMobileKeyboardChange} from "./mobileKeyboardChange";
export let keyboardLockUntil = 0;
@@ -10,7 +11,7 @@ export const armKeyboardLock = () => {
export const callMobileAppShowKeyboard = () => {
armKeyboardLock();
- window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: true}));
+ notifyMobileKeyboardChange(true);
if (window.JSAndroid && window.JSAndroid.showKeyboard) {
window.JSAndroid.showKeyboard();
diff --git a/app/src/mobile/util/mobileKeyboardChange.test.ts b/app/src/mobile/util/mobileKeyboardChange.test.ts
new file mode 100644
index 0000000000..23ea33b515
--- /dev/null
+++ b/app/src/mobile/util/mobileKeyboardChange.test.ts
@@ -0,0 +1,66 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {createMobileKeyboardChangeNotifier} from "./mobileKeyboardChange";
+
+const createNotifier = () => {
+ const changes: boolean[] = [];
+ const timers = new Map<number, () => void>();
+ let timer = 0;
+ const notify = createMobileKeyboardChangeNotifier({
+ dispatch: (open) => changes.push(open),
+ setTimer: (callback) => {
+ timer++;
+ timers.set(timer, callback);
+ return timer;
+ },
+ clearTimer: (timerID) => timers.delete(timerID),
+ closeDelay: 300,
+ });
+ return {
+ changes,
+ notify,
+ runTimers: () => {
+ const callbacks = [...timers.values()];
+ timers.clear();
+ callbacks.forEach(callback => callback());
+ },
+ timers,
+ };
+};
+
+describe("mobile keyboard change", () => {
+ it("reports opening immediately and closing after the transition", () => {
+ const notifier = createNotifier();
+
+ notifier.notify(true);
+ assert.deepEqual(notifier.changes, [true]);
+
+ notifier.notify(false);
+ assert.deepEqual(notifier.changes, [true]);
+ assert.equal(notifier.timers.size, 1);
+
+ notifier.runTimers();
+ assert.deepEqual(notifier.changes, [true, false]);
+ });
+
+ it("cancels a pending close when the keyboard opens again", () => {
+ const notifier = createNotifier();
+
+ notifier.notify(false);
+ notifier.notify(true);
+ notifier.runTimers();
+
+ assert.deepEqual(notifier.changes, [true]);
+ });
+
+ it("restarts the close delay when another close notification arrives", () => {
+ const notifier = createNotifier();
+
+ notifier.notify(false);
+ notifier.notify(false);
+ assert.equal(notifier.timers.size, 1);
+
+ notifier.runTimers();
+ assert.deepEqual(notifier.changes, [false]);
+ });
+});
diff --git a/app/src/mobile/util/mobileKeyboardChange.ts b/app/src/mobile/util/mobileKeyboardChange.ts
new file mode 100644
index 0000000000..2890c3dc8c
--- /dev/null
+++ b/app/src/mobile/util/mobileKeyboardChange.ts
@@ -0,0 +1,35 @@
+interface IMobileKeyboardChangeNotifierOptions {
+ dispatch: (open: boolean) => void,
+ setTimer?: (callback: () => void, delay: number) => number,
+ clearTimer?: (timer: number) => void,
+ closeDelay?: number,
+}
+
+// 等待系统软键盘关闭动画结束,避免底栏跟随仍在变化的视口向下移动。
+export const MOBILE_KEYBOARD_CLOSE_DELAY = 300;
+
+export const createMobileKeyboardChangeNotifier = (options: IMobileKeyboardChangeNotifierOptions) => {
+ const setTimer = options.setTimer || ((callback, delay) => window.setTimeout(callback, delay));
+ const clearTimer = options.clearTimer || ((timer) => window.clearTimeout(timer));
+ const closeDelay = options.closeDelay ?? MOBILE_KEYBOARD_CLOSE_DELAY;
+ let closeTimer: number | undefined;
+
+ return (open: boolean) => {
+ if (closeTimer !== undefined) {
+ clearTimer(closeTimer);
+ closeTimer = undefined;
+ }
+ if (open) {
+ options.dispatch(true);
+ return;
+ }
+ closeTimer = setTimer(() => {
+ closeTimer = undefined;
+ options.dispatch(false);
+ }, closeDelay);
+ };
+};
+
+export const notifyMobileKeyboardChange = createMobileKeyboardChangeNotifier({
+ dispatch: (open) => window.dispatchEvent(new CustomEvent("siyuan-mobile-keyboard-change", {detail: open})),
+});