Skip to content

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

:bug: Hide mobile bottom bar before opening the keyboard from images https://github.com/siyuan-note/siyuan/issues/20138

Danielb1a52a3f1d59
diff --git a/app/src/protyle/wysiwyg/imageClickKeyboard.test.ts b/app/src/protyle/wysiwyg/imageClickKeyboard.test.ts
new file mode 100644
index 0000000000..7e27c38b45
--- /dev/null
+++ b/app/src/protyle/wysiwyg/imageClickKeyboard.test.ts
@@ -0,0 +1,110 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, forEachChild, isIfStatement, Node as TSNode, ScriptTarget, transpileModule} from "typescript";
+import {canInput} from "../../mobile/util/mobileAppUtil";
+import {createMobileKeyboardChangeNotifier} from "../../mobile/util/mobileKeyboardChange";
+
+const source = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest);
+let imageClick: string;
+const findImageClick = (node: TSNode) => {
+ if (isIfStatement(node) && node.expression.getText(source) === "!event.shiftKey && !ctrlIsPressed && imgElement") {
+ imageClick = node.getText(source);
+ }
+ forEachChild(node, findImageClick);
+};
+findImageClick(source);
+assert.ok(imageClick);
+const compiled = transpileModule(`() => {${imageClick}}`, {compilerOptions: {target: ScriptTarget.ES2020}}).outputText;
+
+const fixture = (options: {mobile?: boolean, disabled?: boolean, readonly?: boolean, sibling?: boolean,
+ elementSibling?: boolean, nonEditableSibling?: boolean, shift?: boolean, ctrl?: boolean} = {}) => {
+ const element = (attributes: Record<string, string>, className = "", parentElement?: HTMLElement) => ({
+ nodeType: 1,
+ tagName: "DIV",
+ parentElement,
+ classList: {contains: (name: string) => name === className},
+ getAttribute: (name: string) => attributes[name] ?? null,
+ hasAttribute: (name: string) => Object.prototype.hasOwnProperty.call(attributes, name),
+ } as unknown as HTMLElement);
+ const root = element({"data-readonly": options.readonly ? "true" : "false"}, "protyle-wysiwyg", element({}));
+ const editable = element({contenteditable: options.readonly ? "false" : "true"}, "", root);
+ const nextSibling = options.elementSibling ? Object.assign(
+ element({contenteditable: options.nonEditableSibling ? "false" : "true"}, "", editable),
+ {textContent: "after"}) : {nodeType: 3, parentElement: editable, textContent: "\u200bafter"};
+ const timers = new Map<number, () => void>();
+ const calls: string[] = [];
+ let hidden = false;
+ const notify = createMobileKeyboardChangeNotifier({
+ dispatch: open => {hidden = open; calls.push(open ? "hide-bottom" : "show-bottom");},
+ setTimer: callback => {timers.set(1, callback); return 1;},
+ clearTimer: timer => {timers.delete(timer);},
+ });
+ let start: {node: unknown, offset: number};
+ let collapsed: boolean;
+ let hiddenAtFocus: boolean;
+ const click = runInNewContext(compiled, {
+ event: {shiftKey: !!options.shift}, ctrlIsPressed: !!options.ctrl,
+ imgElement: {classList: {add: () => calls.push("select-image")}},
+ hasNextSibling: () => options.sibling === false ? false : nextSibling,
+ Constants: {ZWSP: "\u200b"},
+ range: {setStart: (node: unknown, offset: number) => {start = {node, offset};},
+ collapse: (value: boolean) => {collapsed = value;}},
+ protyle: {disabled: !!options.disabled, options: {render: {breadcrumb: true}},
+ breadcrumb: {render: () => calls.push("breadcrumb")}},
+ focusByRange: () => {hiddenAtFocus = hidden; calls.push("focus");},
+ isMobile: () => options.mobile !== false,
+ canInput,
+ notifyMobileKeyboardChange: notify,
+ });
+ return {click, calls, nextSibling, notify, timers, start: () => start, collapsed: () => collapsed,
+ hiddenAtFocus: () => hiddenAtFocus};
+};
+
+test("mobile image selection hides navigation before restoring the caret and opening the keyboard", () => {
+ const f = fixture();
+ f.click();
+ assert.equal(f.hiddenAtFocus(), true);
+ assert.deepEqual(f.calls, ["select-image", "hide-bottom", "focus", "breadcrumb"]);
+ assert.deepEqual(f.start(), {node: f.nextSibling, offset: 1});
+ assert.equal(f.collapsed(), true);
+});
+
+test("image selection cancels a pending keyboard close before the native keyboard reopens", () => {
+ const f = fixture();
+ f.notify(false);
+ f.click();
+ [...f.timers.values()].forEach(callback => callback());
+ assert.equal(f.timers.size, 0);
+ assert.equal(f.hiddenAtFocus(), true);
+ assert.equal(f.calls.includes("show-bottom"), false);
+});
+
+test("desktop and readonly image selection retain navigation visibility", () => {
+ for (const options of [{mobile: false}, {disabled: true}, {readonly: true}]) {
+ const f = fixture(options);
+ f.click();
+ assert.equal(f.hiddenAtFocus(), false);
+ assert.deepEqual(f.calls, ["select-image", "focus", "breadcrumb"]);
+ }
+});
+
+test("the caret target respects editable boundaries for element siblings", () => {
+ for (const nonEditableSibling of [false, true]) {
+ const f = fixture({elementSibling: true, nonEditableSibling});
+ f.click();
+ assert.equal(f.hiddenAtFocus(), !nonEditableSibling);
+ assert.deepEqual(f.start(), {node: f.nextSibling, offset: 0});
+ }
+});
+
+test("missing caret targets and modified clicks do not report keyboard opening", () => {
+ for (const options of [{sibling: false}, {shift: true}, {ctrl: true}]) {
+ const f = fixture(options);
+ f.click();
+ assert.equal(f.calls.includes("hide-bottom"), false);
+ assert.equal(f.calls.includes("focus"), false);
+ }
+});
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 9d9e6c137d..bddf1ae15d 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -154,6 +154,8 @@ import {removeSearchMark} from "../toolbar/util";
import {getTableCellTextStyleMenus} from "../toolbar/tableCell";
import {isNestedListCrossBlockSelection, NESTED_LIST_PASTE_MARKER} from "../util/pasteSource";
import {activeBlur} from "../../mobile/util/keyboardToolbar";
+import {canInput} from "../../mobile/util/mobileAppUtil";
+import {notifyMobileKeyboardChange} from "../../mobile/util/mobileKeyboardChange";
import {commonClick} from "./commonClick";
import {avClick, avContextmenu, switchAVView, updateAVName} from "../render/av/action";
import {selectRow, stickyRow, updateHeader} from "../render/av/row";
@@ -5295,6 +5297,11 @@ export class WYSIWYG {
range.setStart(nextSibling, 0);
}
range.collapse(true);
+ if (isMobile() && !protyle.disabled && canInput(nextSibling.nodeType === 3 ?
+ nextSibling.parentElement : nextSibling as HTMLElement)) {
+ // 恢复图片后的光标前隐藏普通底栏,避免底栏随软键盘弹起而上移。
+ notifyMobileKeyboardChange(true);
+ }
focusByRange(range);
// 需等待 range 更新再次进行渲染
if (protyle.options.render.breadcrumb) {