Skip to content

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

:bug: Refine mobile reference menu and drawer interactions https://github.com/siyuan-note/siyuan/issues/20115

Daniel654f5ddeb35c
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index 35340fb05d..703a5ae43c 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -750,7 +750,9 @@ export const refMenu = (protyle: IProtyle, element: HTMLElement) => {
window.siyuan.menus.menu.data = element;
window.siyuan.menus.menu.element.setAttribute("data-from", popoverElement ? popoverElement.dataset.level + "popover" : "app");
if (!protyle.disabled) {
- window.siyuan.menus.menu.element.querySelector("input").select();
+ if (!isMobile()) {
+ window.siyuan.menus.menu.element.querySelector("input").select();
+ }
window.siyuan.menus.menu.removeCB = () => {
if (nodeElement.outerHTML !== oldHTML) {
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
diff --git a/app/src/menus/refMenu.test.ts b/app/src/menus/refMenu.test.ts
new file mode 100644
index 0000000000..518d14665c
--- /dev/null
+++ b/app/src/menus/refMenu.test.ts
@@ -0,0 +1,89 @@
+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, isVariableStatement, ScriptTarget, transpileModule} from "typescript";
+
+const {parse} = require("ifdef-loader/preprocessor");
+
+const setup = (mobile: boolean, disabled = false) => {
+ const source = createSourceFile("protyle.ts", parse(readFileSync(join(__dirname, "protyle.ts"), "utf8"),
+ {MOBILE: mobile, BROWSER: true}, false, true, "protyle.ts"), ScriptTarget.Latest, true);
+ const declaration = source.statements.filter(isVariableStatement).flatMap(statement =>
+ Array.from(statement.declarationList.declarations)).find(item => item.name.getText(source) === "refMenu");
+ const code = transpileModule("const refMenu = " + declaration.initializer.getText(source) + "; refMenu(protyle, element);",
+ {compilerOptions: {target: ScriptTarget.ES2022}}).outputText;
+ const events: string[] = [];
+ const items: IMenu[] = [];
+ const transactions: string[] = [];
+ const listeners = new Map<string, () => void>();
+ const input = {value: "", select: () => events.push("select"),
+ addEventListener: (type: string, listener: () => void) => listeners.set(type, listener)};
+ const attributes = new Map([["data-id", "target"], ["data-subtype", "d"]]);
+ const element = {innerHTML: "Reference", textContent: "Reference",
+ parentElement: {textContent: "Text before Reference", tagName: "DIV"},
+ getAttribute: (name: string) => attributes.get(name),
+ setAttribute: (name: string, value: string) => attributes.set(name, value),
+ getBoundingClientRect: () => ({left: 10, top: 20})};
+ const block = {get outerHTML() { return `<div>${element.innerHTML}</div>`; },
+ getAttribute: () => "source", setAttribute() {}};
+ const menu = {element: {setAttribute() {}, querySelector: () => {
+ assert.ok(!disabled, "Readonly menus have no anchor input");
+ return input;
+ }}, remove() {}, append: (item: IMenu) => items.push(item),
+ fullscreen: () => events.push("fullscreen"), popup: () => events.push("popup"), removeCB: undefined as (() => void)};
+ const protyle = {disabled, lite: true, app: {}, element: {}, toolbar: {range: {}}};
+ const keymap = Object.fromEntries(["openBy", "refTab", "insertRight", "insertBottom", "backlinks", "graphView"]
+ .map(name => [name, {custom: ""}]));
+ runInNewContext(code, {
+ protyle, element, isMobile: () => mobile,
+ Constants: {MENU_INLINE_REF: "ref", ATTRIBUTE_MENU_KEYMAP: "data-keymap"},
+ window: {siyuan: {languages: {}, menus: {menu}, config: {keymap: {editor: {general: keymap}}}}},
+ hasClosestBlock: () => block, hasTopClosestByClassName: (): undefined => undefined,
+ hideElements() {}, emitOpenMenu() {}, updateHotkeyTip: (value: string) => value,
+ escapeHtmlTextAndAttr: (value: string) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;"),
+ updateTransaction: (_protyle: unknown, _block: unknown, previous: string) => transactions.push(previous),
+ getSelection: () => ({rangeCount: 0}), dayjs: () => ({format: () => "20261007100000"}),
+ openMobileReference: (source: unknown, id: string) => {
+ assert.equal(source, protyle);
+ assert.equal(id, "target");
+ events.push("reference");
+ },
+ MenuItem: class {
+ element: IMenu;
+ constructor(options: IMenu) {
+ this.element = options;
+ options.bind?.({querySelector: () => input} as unknown as HTMLElement);
+ }
+ },
+ });
+ return {events, items, input, listeners, menu, transactions, element, attributes};
+};
+
+for (const mobile of [false, true]) {
+ for (const disabled of [false, true]) {
+ test(`reference menu preserves keyboard intent (${mobile ? "mobile" : "desktop"}, ${disabled ? "readonly" : "editable"})`, () => {
+ const fixture = setup(mobile, disabled);
+ assert.deepEqual(fixture.events, [mobile ? "fullscreen" : "popup", ...!mobile && !disabled ? ["select"] : []]);
+ assert.equal(fixture.items.some(item => item.id === "anchor"), !disabled);
+ assert.equal(typeof fixture.menu.removeCB, disabled ? "undefined" : "function");
+ if (mobile) {
+ fixture.items.find(item => item.id === "viewRefContent").click(null, null);
+ assert.equal(fixture.events.at(-1), "reference");
+ }
+ });
+ }
+}
+
+test("mobile anchor edits still commit when the reference menu closes", () => {
+ const fixture = setup(true);
+ fixture.input.value = "Edited <anchor>";
+ fixture.listeners.get("input")();
+ assert.equal(fixture.element.innerHTML, "Edited &lt;anchor&gt;");
+ assert.equal(fixture.attributes.get("data-subtype"), "s");
+ assert.deepEqual(fixture.transactions, []);
+ fixture.menu.removeCB();
+ assert.deepEqual(fixture.transactions, ["<div>Reference</div>"]);
+ assert.ok(!fixture.events.includes("select"));
+});
diff --git a/app/src/mobile/util/openReference.test.ts b/app/src/mobile/util/openReference.test.ts
index 54ac645064..08647e2989 100644
--- a/app/src/mobile/util/openReference.test.ts
+++ b/app/src/mobile/util/openReference.test.ts
@@ -250,6 +250,7 @@ test("reference editors load whole blocks or ordinary documents and preserve rea
for (const blockId of ["target-block", "target-root"]) {
const fixture = setup(readonly);
await fixture.open(blockId);
+ assert.equal(fixture.dialogs[0].options.hideCloseIcon, true);
const editor = fixture.editors[0];
assert.deepEqual(Array.from(editor.options.action), [blockId === "target-root" ? "context" : "all"]);
assert.equal(editor.options.render.title, blockId === "target-root");
diff --git a/app/src/mobile/util/openReference.ts b/app/src/mobile/util/openReference.ts
index 026cdddae4..50c7b853de 100644
--- a/app/src/mobile/util/openReference.ts
+++ b/app/src/mobile/util/openReference.ts
@@ -123,6 +123,7 @@ export const openMobileReference = async (protyle: IProtyle, blockId: string) =>
content: '<div class="mobile-reference-editor fn__flex-1"></div>',
width: "100vw",
height: "60%",
+ hideCloseIcon: true,
containerClassName: "mobile-reference-sheet",
destroyCallback: () => {
disposeSheet();