Skip to content

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

:bug: Toggle document tree more menus on repeated clicks https://github.com/siyuan-note/siyuan/issues/19285

Danielb9f98299af14
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index 0ce29f1417..4b6f0c46da 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -7,6 +7,7 @@ import {Constants} from "../../constants";
import {getDocDisplayName, isMoveTargetAllowed, pathPosix, setNoteBook} from "../../util/pathName";
import {newFileInTree} from "../../util/newFile";
import {initFileMenu, initNavigationMenu, sortMenu} from "../../menus/navigation";
+import {toggleFileTreeMenu} from "../../menus/fileTreeMenu";
import {isDocTreeDragSelectionAllowed} from "../../menus/navigationSelection";
import {MenuItem} from "../../menus/Menu";
import {showMessage} from "../../dialog/message";
@@ -109,7 +110,7 @@ export class Files extends Model {
<svg><use xlink:href="#iconContract"></use></svg>
</span>
<div class="fn__space${window.siyuan.config.readonly ? " fn__none" : ""}"></div>
- <div data-type="more" class="ariaLabel block__icon${window.siyuan.config.readonly ? " fn__none" : ""}" data-position="north" aria-label="${window.siyuan.languages.more}">
+ <div data-type="more" data-menu="true" class="ariaLabel block__icon${window.siyuan.config.readonly ? " fn__none" : ""}" data-position="north" aria-label="${window.siyuan.languages.more}">
<svg><use xlink:href="#iconMore"></use></svg>
</div>
<span class="fn__space"></span>
@@ -219,8 +220,10 @@ export class Files extends Model {
event.preventDefault();
break;
} else if (type === "more") {
- const rect = target.getBoundingClientRect();
- this.initMoreMenu().popup({x: rect.left, y: rect.bottom, h: rect.height});
+ toggleFileTreeMenu(target, () => {
+ const rect = target.getBoundingClientRect();
+ this.initMoreMenu().popup({x: rect.left, y: rect.bottom, h: rect.height});
+ });
event.preventDefault();
event.stopPropagation();
break;
@@ -349,20 +352,24 @@ export class Files extends Model {
if (type === "new") {
newFileInTree(options.app, notebookId, pathString);
} else if (type === "more-root") {
+ toggleFileTreeMenu(target, () => {
+ const rect = target.getBoundingClientRect();
+ initNavigationMenu(options.app, target.parentElement).popup({
+ x: rect.left,
+ y: rect.bottom,
+ h: rect.height,
+ });
+ });
+ }
+ }
+ if (type === "more-file") {
+ toggleFileTreeMenu(target, () => {
const rect = target.getBoundingClientRect();
- initNavigationMenu(options.app, target.parentElement).popup({
+ initFileMenu(options.app, notebookId, pathString, target.parentElement).popup({
x: rect.left,
y: rect.bottom,
h: rect.height,
});
- }
- }
- if (type === "more-file") {
- const rect = target.getBoundingClientRect();
- initFileMenu(options.app, notebookId, pathString, target.parentElement).popup({
- x: rect.left,
- y: rect.bottom,
- h: rect.height,
});
}
event.preventDefault();
@@ -1481,7 +1488,7 @@ data-type="navigation-root" data-path="/" data-count="${item.subFileCount || 0}"
${emojiHTML}
${switchHTML}
<span class="b3-list-item__text ariaLabel" data-position="parentE">${escapeHtml(item.name)}</span>
- <span data-type="more-root" class="b3-list-item__action b3-tooltips b3-tooltips__w${(window.siyuan.config.readonly) ? " fn__none" : ""}" aria-label="${window.siyuan.languages.more}">
+ <span data-type="more-root" data-menu="true" class="b3-list-item__action b3-tooltips b3-tooltips__w${(window.siyuan.config.readonly) ? " fn__none" : ""}" aria-label="${window.siyuan.languages.more}">
<svg><use xlink:href="#iconMore"></use></svg>
</span>
<span data-type="new" class="b3-list-item__action b3-tooltips b3-tooltips__w${(window.siyuan.config.readonly) ? " fn__none" : ""}" aria-label="${window.siyuan.languages.newSubDoc}">
@@ -2165,7 +2172,7 @@ class="b3-list-item b3-list-item--hide-action${actionClasses}" data-path="${item
<span class="b3-list-item__switch b3-tooltips b3-tooltips__n${editingPublishAccess ? "" : " fn__none"}" aria-label="${window.siyuan.languages.publishAccess}">${getPublishAccessOptionByLevel("public").iconHTML}</span>
<span class="b3-list-item__text ariaLabel" data-delay="200" data-position="parentE"
aria-label="${ariaLabel}">${getDocDisplayName(item.name, item.titleEmpty, true)}</span>
- <span data-type="more-file" class="b3-list-item__action b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.more}">
+ <span data-type="more-file" data-menu="true" class="b3-list-item__action b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.more}">
<svg><use xlink:href="#iconMore"></use></svg>
</span>
<span data-type="new" class="b3-list-item__action b3-tooltips b3-tooltips__nw${window.siyuan.config.readonly ? " fn__none" : ""}" aria-label="${window.siyuan.languages.newSubDoc}">
diff --git a/app/src/layout/dock/PinnedDocs.test.ts b/app/src/layout/dock/PinnedDocs.test.ts
index d2c8605075..bb6dff9db8 100644
--- a/app/src/layout/dock/PinnedDocs.test.ts
+++ b/app/src/layout/dock/PinnedDocs.test.ts
@@ -49,15 +49,25 @@ const loadPanel = (fetchCode = 0) => {
const config = {readonly: false, fileTree: {docIconClickMode: 0, parentDocTitleClickMode: 0}};
const calls: {kind: string, args: unknown[]}[] = [];
const timers: (() => void)[] = [];
+ let menuVisible = false;
+ const menu = {
+ data: undefined as Element | undefined,
+ element: {classList: {contains: () => !menuVisible}},
+ remove: () => { menuVisible = false; menu.data = undefined; },
+ };
const runtime = {config, notebooks: [] as unknown[], languages: {pinDoc: "Pin", dragTipMoveChild: "Into ${x}",
dragTipMoveBefore: "Before ${x}", dragTipMoveAfter: "After ${x}"}, dragElement: undefined as {innerText: string},
- dragTitle: "Document", touchDragActive: false, touchDragGhost: undefined as unknown};
+ dragTitle: "Document", touchDragActive: false, touchDragGhost: undefined as unknown, menus: {menu}};
const tips: unknown[][] = [];
const record = (kind: string) => async (...args: unknown[]) => {
calls.push({kind, args});
return {code: kind === "http" ? fetchCode : 0, data: args[0] === "/api/filetree/listDocsByPath" ? childData : docs};
};
const exports: {PinnedDocs?: {prototype: object}} = {};
+ const fileTreeMenu = {};
+ runInNewContext(ts.transpileModule(readFileSync(join(__dirname, "../../menus/fileTreeMenu.ts"), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText, {exports: fileTreeMenu, window: {siyuan: runtime}});
const source = ts.transpileModule(readFileSync(join(__dirname, "PinnedDocs.ts"), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
}).outputText;
@@ -74,6 +84,8 @@ const loadPanel = (fetchCode = 0) => {
remove() { calls.push({kind: "removeGhost", args: [this]}); },
})},
require: (name: string) => {
+ if (name.endsWith("/fileTreeMenu")) { return fileTreeMenu; }
+ if (name.endsWith("/menuClick")) { return {globalClickHideMenu: () => {}}; }
if (name.endsWith("/dragTip")) { return {setDragTipGhost: record("dragTipGhost"),
showDragTip: (...args: unknown[]) => tips.push(args), hideDragTip: () => tips.push([])}; }
if (name.endsWith("/fileTreeAnimation")) { return {setFileTreeVisibility: (element: HTMLElement, visible: boolean) => element.classList.toggle("fn__none", !visible)}; }
@@ -85,8 +97,12 @@ const loadPanel = (fetchCode = 0) => {
if (name.endsWith("/emoji")) { return {openEmojiPanel: record("icon")}; }
if (name.endsWith("/navigation")) {
return {initFileMenu: (...args: unknown[]) => {
+ menu.remove();
calls.push({kind: "menu", args});
- return {popup: record("popup"), fullscreen: record("fullscreen")};
+ return {
+ popup: (...position: unknown[]) => { menuVisible = true; return record("popup")(...position); },
+ fullscreen: (...position: unknown[]) => { menuVisible = true; return record("fullscreen")(...position); },
+ };
}};
}
if (name.endsWith("/pinnedDocs")) { return {updatePinnedDocs: record("pin"), pinnedDocIDs: new Set<string>()}; }
@@ -132,14 +148,15 @@ test("collapse clears descendant expansion and persists the closed section", asy
assert.equal(calls[0].args[0], "/api/filetree/listDocsByPath");
});
-test("pinned document more actions open the source document menu", () => {
+test("pinned document more actions open the source document menu and toggle on repeated clicks", () => {
const {panel, calls} = loadPanel();
const row = {dataset: {nodeId: "document", notebook: "notebook", path: "/document.sy"}};
const button = {getBoundingClientRect: () => ({left: 180, bottom: 120, height: 24})};
- panel.click({
+ const event = {
stopPropagation: () => {}, clientX: 10, clientY: 20,
target: {closest: (selector: string) => selector === "[data-pin-row]" ? row : selector === "[data-pin-more]" ? button : null},
- });
+ };
+ panel.click(event);
assert.equal(calls[0].kind, "menu");
assert.deepEqual(calls[0].args.slice(1), ["notebook", "/document.sy", row]);
assert.equal(calls[1].kind, "popup");
@@ -147,6 +164,12 @@ test("pinned document more actions open the source document menu", () => {
panel.mobile = true;
panel.menu(row, {x: 180, y: 120, h: 24});
assert.equal(calls[3].kind, "fullscreen");
+ panel.click(event);
+ assert.equal(calls.length, 6);
+ panel.click(event);
+ assert.equal(calls.length, 6);
+ panel.click(event);
+ assert.equal(calls.length, 8);
});
test("pinned roots share one list and retain each document notebook without wrapper lists", async () => {
diff --git a/app/src/layout/dock/PinnedDocs.ts b/app/src/layout/dock/PinnedDocs.ts
index 1f067c0567..0d5efaeddc 100644
--- a/app/src/layout/dock/PinnedDocs.ts
+++ b/app/src/layout/dock/PinnedDocs.ts
@@ -7,6 +7,8 @@ import {getFileTreeIconHTML} from "../../emoji/fileTreeIcon";
import {openEmojiPanel} from "../../emoji";
import {escapeHtml} from "../../util/escape";
import {initFileMenu} from "../../menus/navigation";
+import {toggleFileTreeMenu} from "../../menus/fileTreeMenu";
+import {globalClickHideMenu} from "../../menus/menuClick";
import {parseDocumentTabDragData, parseDocumentTreeDragData} from "../../util/fileTreeMove";
import {reorderSortedFileTree} from "../../util/fileTreeReorder";
import {dragOverScroll, stopScrollAnimation} from "../../boot/globalEvent/dragover";
@@ -325,7 +327,7 @@ export class PinnedDocs {
// 桌面端点击会修改图标时给出提示,移动端保留无障碍标签
const iconLabel = this.getDocIconLabel(doc.subFileCount);
if (!doc.icon) { row.dataset.defaultIcon = doc.subFileCount ? "folder" : "file"; }
- row.innerHTML = `<span data-pin-toggle="true" style="padding-left:${paddingLeft}px" class="b3-list-item__toggle b3-list-item__toggle--hl${doc.subFileCount ? "" : " fn__hidden"}"><svg class="b3-list-item__arrow"><use xlink:href="#iconRight"></use></svg></span><span class="b3-list-item__icon${iconLabel ? " ariaLabel" : ""}" data-position="8east"${iconLabel ? ` aria-label="${iconLabel}"` : ""}>${getFileTreeIconHTML(doc.icon, doc.subFileCount ? "folder" : "file")}</span><span class="b3-list-item__text">${escapeHtml(doc.name)}${doc.unavailable ? ` (${window.siyuan.languages.closeNotebook})` : ""}</span><span data-pin-more="true" class="b3-list-item__action" aria-label="${window.siyuan.languages.more}"><svg><use xlink:href="#iconMore"></use></svg></span>`;
+ row.innerHTML = `<span data-pin-toggle="true" style="padding-left:${paddingLeft}px" class="b3-list-item__toggle b3-list-item__toggle--hl${doc.subFileCount ? "" : " fn__hidden"}"><svg class="b3-list-item__arrow"><use xlink:href="#iconRight"></use></svg></span><span class="b3-list-item__icon${iconLabel ? " ariaLabel" : ""}" data-position="8east"${iconLabel ? ` aria-label="${iconLabel}"` : ""}>${getFileTreeIconHTML(doc.icon, doc.subFileCount ? "folder" : "file")}</span><span class="b3-list-item__text">${escapeHtml(doc.name)}${doc.unavailable ? ` (${window.siyuan.languages.closeNotebook})` : ""}</span><span data-pin-more="true" data-menu="true" class="b3-list-item__action" aria-label="${window.siyuan.languages.more}"><svg><use xlink:href="#iconMore"></use></svg></span>`;
if (!doc.unavailable && !window.siyuan.config.readonly) {
const add = document.createElement("span");
add.className = "b3-list-item__action";
@@ -439,6 +441,7 @@ export class PinnedDocs {
event.stopPropagation();
if (this.suppressClick) { event.preventDefault(); return; }
const target = event.target as Element;
+ globalClickHideMenu(target as HTMLElement);
if (!target.closest(".b3-list-item__text") || event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) {
this.parentDocClick.cancel();
}
@@ -458,8 +461,10 @@ export class PinnedDocs {
}
const moreButton = target.closest("[data-pin-more]");
if (moreButton) {
- const rect = moreButton.getBoundingClientRect();
- this.menu(row, {x: rect.left, y: rect.bottom, h: rect.height});
+ toggleFileTreeMenu(moreButton, () => {
+ const rect = moreButton.getBoundingClientRect();
+ this.menu(row, {x: rect.left, y: rect.bottom, h: rect.height});
+ });
} else if (target.closest("[data-pin-new]")) {
newFileInTree(this.app, row.dataset.notebook, row.dataset.path);
} else if (target.closest("[data-pin-toggle]")) {
diff --git a/app/src/menus/fileTreeMenu.test.ts b/app/src/menus/fileTreeMenu.test.ts
new file mode 100644
index 0000000000..61a296d419
--- /dev/null
+++ b/app/src/menus/fileTreeMenu.test.ts
@@ -0,0 +1,89 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const fixture = () => {
+ let visible = false;
+ const opened: Element[] = [];
+ const menu = {
+ data: undefined as Element | undefined,
+ element: {
+ classList: {contains: () => !visible},
+ contains: () => false,
+ },
+ remove: () => { visible = false; menu.data = undefined; },
+ };
+ const context = {
+ window: {siyuan: {menus: {menu}}},
+ document: {activeElement: null as HTMLElement | null},
+ getSelection: () => ({rangeCount: 0}),
+ require: () => ({hasClosestByAttribute: (element: {menuTrigger?: boolean}) => element.menuTrigger}),
+ };
+ const load = <T>(file: string): T => {
+ const exports = {} as T;
+ runInNewContext(transpileModule(readFileSync(__dirname + file, "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},
+ }).outputText, {...context, exports});
+ return exports;
+ };
+ const {toggleFileTreeMenu} = load<typeof import("./fileTreeMenu")>("/fileTreeMenu.ts");
+ const {globalClickHideMenu} = load<typeof import("./menuClick")>("/menuClick.ts");
+ const click = (button: Element) => toggleFileTreeMenu(button, () => {
+ menu.remove();
+ opened.push(button);
+ visible = true;
+ });
+ return {click, opened, menu, dismiss: globalClickHideMenu, isVisible: () => visible,
+ openContextMenu: () => { menu.remove(); visible = true; }};
+};
+
+test("repeated clicks on a file tree more button open, close and reopen its menu", () => {
+ const f = fixture();
+ const button = {} as Element;
+ f.click(button);
+ assert.equal(f.isVisible(), true);
+ f.click(button);
+ assert.equal(f.isVisible(), false);
+ assert.equal(f.opened.length, 1);
+ f.click(button);
+ assert.equal(f.isVisible(), true);
+ assert.equal(f.opened.length, 2);
+});
+
+test("switching between panel, notebook, document and pinned more buttons opens the selected menu", () => {
+ const f = fixture();
+ const buttons = Array.from({length: 4}, () => ({} as Element));
+ buttons.forEach(button => f.click(button));
+ assert.equal(f.isVisible(), true);
+ assert.deepEqual(f.opened, buttons);
+ f.click(buttons[3]);
+ assert.equal(f.isVisible(), false);
+});
+
+test("outside dismissal permits reopening while trigger clicks retain toggle handling", () => {
+ const f = fixture();
+ const button = {menuTrigger: true} as unknown as HTMLElement;
+ f.click(button);
+ f.dismiss(button);
+ assert.equal(f.isVisible(), true);
+ f.click(button);
+ assert.equal(f.isVisible(), false);
+ f.click(button);
+ f.dismiss({} as HTMLElement);
+ assert.equal(f.isVisible(), false);
+ f.click(button);
+ assert.equal(f.isVisible(), true);
+ assert.equal(f.opened.length, 3);
+});
+
+test("a context menu replacing a more menu does not consume the next more-button click", () => {
+ const f = fixture();
+ const button = {} as Element;
+ f.click(button);
+ f.openContextMenu();
+ f.click(button);
+ assert.equal(f.opened.length, 2);
+ assert.equal(f.menu.data, button);
+});
diff --git a/app/src/menus/fileTreeMenu.ts b/app/src/menus/fileTreeMenu.ts
new file mode 100644
index 0000000000..833b175442
--- /dev/null
+++ b/app/src/menus/fileTreeMenu.ts
@@ -0,0 +1,10 @@
+export const toggleFileTreeMenu = (button: Element, openMenu: () => void) => {
+ const menu = window.siyuan.menus.menu;
+ if (!menu.element.classList.contains("fn__none") && menu.data === button) {
+ menu.remove();
+ return;
+ }
+ openMenu();
+ // 记录具体入口,使同一按钮关闭菜单,其他按钮切换操作对象。
+ menu.data = button;
+};
diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts
index dabafc2476..d21cd138e8 100644
--- a/app/src/mobile/dock/MobileFiles.ts
+++ b/app/src/mobile/dock/MobileFiles.ts
@@ -5,6 +5,7 @@ import {Model} from "../../layout/Model";
import {Constants} from "../../constants";
import {getDocDisplayName, isMoveTargetAllowed, pathPosix, setNoteBook} from "../../util/pathName";
import {initFileMenu, initNavigationMenu, sortMenu} from "../../menus/navigation";
+import {toggleFileTreeMenu} from "../../menus/fileTreeMenu";
import {
getPublishAccessLevel,
getPublishAccessOptionByLevel,
@@ -297,15 +298,19 @@ export class MobileFiles extends Model {
if (type === "new") {
newFileInTree(app, notebookId, pathString);
} else if (type === "more-root") {
- initNavigationMenu(app, target.parentElement);
- this.insertMultiSelectMenu(target.parentElement);
- window.siyuan.menus.menu.fullscreen("bottom");
+ toggleFileTreeMenu(target, () => {
+ initNavigationMenu(app, target.parentElement);
+ this.insertMultiSelectMenu(target.parentElement);
+ window.siyuan.menus.menu.fullscreen("bottom");
+ });
}
}
if (type === "more-file") {
- initFileMenu(app, notebookId, pathString, target.parentElement);
- this.insertMultiSelectMenu(target.parentElement);
- window.siyuan.menus.menu.fullscreen("bottom");
+ toggleFileTreeMenu(target, () => {
+ initFileMenu(app, notebookId, pathString, target.parentElement);
+ this.insertMultiSelectMenu(target.parentElement);
+ window.siyuan.menus.menu.fullscreen("bottom");
+ });
}
}
event.preventDefault();
@@ -993,7 +998,7 @@ export class MobileFiles extends Model {
${emojiHTML}
${switchHTML}
<span class="b3-list-item__text${item.closed ? " ft__on-surface" : ""}">${escapeHtml(item.name)}</span>
- <span data-type="more-root" class="b3-list-item__action${(window.siyuan.config.readonly || item.closed) ? " fn__none" : ""}">
+ <span data-type="more-root" data-menu="true" class="b3-list-item__action${(window.siyuan.config.readonly || item.closed) ? " fn__none" : ""}">
<svg><use xlink:href="#iconMore"></use></svg>
</span>
<span data-type="new" class="b3-list-item__action${(window.siyuan.config.readonly || item.closed) ? " fn__none" : ""}">
@@ -1732,7 +1737,7 @@ export class MobileFiles extends Model {
const paddingLeft = (item.path.split("/").length - 1) * 20;
const editingPublishAccess = this.actionsElement.querySelector('[data-type="publish-access"]').classList.contains("block__icon--active");
const defaultIcon = item.subFileCount === 0 ? "file" : "folder";
- const actionHTML = `<span data-type="more-file" class="b3-list-item__action b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.more}">
+ const actionHTML = `<span data-type="more-file" data-menu="true" class="b3-list-item__action b3-tooltips b3-tooltips__nw" aria-label="${window.siyuan.languages.more}">
<svg><use xlink:href="#iconMore"></use></svg>
</span>
<span data-type="new" class="b3-list-item__action b3-tooltips b3-tooltips__nw${window.siyuan.config.readonly ? " fn__none" : ""}" aria-label="${window.siyuan.languages.newSubDoc}">