Skip to content

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

:art: Improve outline expand action order and state display https://github.com/siyuan-note/siyuan/issues/20309

Daniel1c339293cb7f
diff --git a/app/src/editor/outlineMenu.test.ts b/app/src/editor/outlineMenu.test.ts
new file mode 100644
index 0000000000..a8e8f59167
--- /dev/null
+++ b/app/src/editor/outlineMenu.test.ts
@@ -0,0 +1,41 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+
+for (const path of ["src/layout/dock/Outline.ts", "src/mobile/dock/MobileOutline.ts"]) {
+ test(`${path}: outline expand menu lists actions first without stale state`, () => {
+ const source = createSourceFile(path, readFileSync(path, "utf8"), ScriptTarget.ES2021, true);
+ const method = source.statements.find(isClassDeclaration).members.find(member => member.name?.getText(source) === "showExpandLevelMenu");
+ const compiled = transpileModule(`class Harness {${method.getText(source)}}; globalThis.Harness = Harness;`, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+ }).outputText;
+ const items: IMenu[] = [];
+ const locations: unknown[] = [];
+ const menu = {element: {setAttribute: () => {}}, remove: () => {}, append: (item: IMenu) => items.push(item),
+ popup: (position: unknown) => locations.push(position), fullscreen: (position: unknown) => locations.push(position)};
+ const context = {
+ window: {siyuan: {menus: {menu}, languages: new Proxy({}, {get: (_target, key) => String(key)}),
+ config: {keymap: {editor: {general: {expand: {custom: "Ctrl+Down"}, collapse: {custom: "Ctrl+Up"}}}}}}},
+ Constants: {MENU_OUTLINE_EXPAND_LEVEL: "outline-expand-level"},
+ MenuItem: class {element: IMenu; constructor(item: IMenu) { this.element = item; }},
+ };
+ const Harness = runInNewContext(`${compiled}\nHarness;`, context);
+ const harness = new Harness();
+ const actions: unknown[] = [];
+ harness.setAllExpanded = (expanded: boolean) => actions.push(expanded);
+ harness.expandToLevel = (level: number) => actions.push(level);
+ harness.showExpandLevelMenu({getBoundingClientRect: () => ({left: 10, bottom: 20, height: 30})});
+ assert.deepEqual(items.map(item => item.id), ["expandAll", "foldAll", "separator_all", "heading1", "heading2", "heading3", "heading4", "heading5", "heading6"]);
+ assert.ok(items.every(item => item.current === undefined));
+ items.forEach(item => item.click?.(undefined, undefined));
+ assert.deepEqual(actions, [true, false, 1, 2, 3, 4, 5, 6]);
+ if (path.includes("mobile")) {
+ assert.deepEqual(locations, ["bottom"]);
+ } else {
+ assert.deepEqual(items.slice(0, 2).map(item => item.accelerator), ["Ctrl+Down", "Ctrl+Up"]);
+ assert.deepEqual(JSON.parse(JSON.stringify(locations)), [{x: 10, y: 20, h: 30}]);
+ }
+ });
+}
diff --git a/app/src/layout/dock/Outline.ts b/app/src/layout/dock/Outline.ts
index ba722ce60e..39e08ab3ea 100644
--- a/app/src/layout/dock/Outline.ts
+++ b/app/src/layout/dock/Outline.ts
@@ -908,16 +908,6 @@ export class Outline extends Model {
private showExpandLevelMenu(target: HTMLElement) {
window.siyuan.menus.menu.remove();
window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_OUTLINE_EXPAND_LEVEL);
- for (let i = 1; i <= 6; i++) {
- window.siyuan.menus.menu.append(new MenuItem({
- id: `heading${i}`,
- icon: `iconH${i}`,
- label: window.siyuan.languages[`heading${i}`],
- current: window.siyuan.storage[Constants.LOCAL_OUTLINE].expandLevel === i,
- click: () => this.expandToLevel(i)
- }).element);
- }
- window.siyuan.menus.menu.append(new MenuItem({id: "separator_all", type: "separator"}).element);
window.siyuan.menus.menu.append(new MenuItem({
id: "expandAll",
icon: "iconExpand",
@@ -932,6 +922,15 @@ export class Outline extends Model {
accelerator: window.siyuan.config.keymap.editor.general.collapse.custom,
click: () => this.setAllExpanded(false)
}).element);
+ window.siyuan.menus.menu.append(new MenuItem({id: "separator_all", type: "separator"}).element);
+ for (let i = 1; i <= 6; i++) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: `heading${i}`,
+ icon: `iconH${i}`,
+ label: window.siyuan.languages[`heading${i}`],
+ click: () => this.expandToLevel(i)
+ }).element);
+ }
const rect = target.getBoundingClientRect();
window.siyuan.menus.menu.popup({
x: rect.left,
diff --git a/app/src/mobile/dock/MobileOutline.ts b/app/src/mobile/dock/MobileOutline.ts
index 65d1415086..dfc4c1edd3 100644
--- a/app/src/mobile/dock/MobileOutline.ts
+++ b/app/src/mobile/dock/MobileOutline.ts
@@ -782,16 +782,6 @@ export class MobileOutline extends Model {
private showExpandLevelMenu() {
window.siyuan.menus.menu.remove();
window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_OUTLINE_EXPAND_LEVEL);
- for (let i = 1; i <= 6; i++) {
- window.siyuan.menus.menu.append(new MenuItem({
- id: `heading${i}`,
- icon: `iconH${i}`,
- label: window.siyuan.languages[`heading${i}`],
- current: window.siyuan.storage[Constants.LOCAL_OUTLINE].expandLevel === i,
- click: () => this.expandToLevel(i)
- }).element);
- }
- window.siyuan.menus.menu.append(new MenuItem({id: "separator_all", type: "separator"}).element);
window.siyuan.menus.menu.append(new MenuItem({
id: "expandAll",
icon: "iconExpand",
@@ -804,6 +794,15 @@ export class MobileOutline extends Model {
label: window.siyuan.languages.foldAll,
click: () => this.setAllExpanded(false)
}).element);
+ window.siyuan.menus.menu.append(new MenuItem({id: "separator_all", type: "separator"}).element);
+ for (let i = 1; i <= 6; i++) {
+ window.siyuan.menus.menu.append(new MenuItem({
+ id: `heading${i}`,
+ icon: `iconH${i}`,
+ label: window.siyuan.languages[`heading${i}`],
+ click: () => this.expandToLevel(i)
+ }).element);
+ }
window.siyuan.menus.menu.fullscreen("bottom");
return window.siyuan.menus.menu;
}