Skip to content

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

:art: Improve entry visibility context menus with side filtering and icons https://github.com/siyuan-note/siyuan/issues/19305

Danield7b6e8834eef
diff --git a/app/src/config/entryVisibility/menu.test.ts b/app/src/config/entryVisibility/menu.test.ts
index f2c3bd53d2..a2b7dbf7e0 100644
--- a/app/src/config/entryVisibility/menu.test.ts
+++ b/app/src/config/entryVisibility/menu.test.ts
@@ -28,6 +28,7 @@ const createRuntime = (options: {
readonly?: boolean;
hidden?: string[];
reverseOrder?: boolean;
+ icons?: Record<string, Pick<IMenu, "icon" | "iconHTML">>;
} = {}) => {
const hidden = new Set(options.hidden || []);
const calls: Array<{path: string; visible: boolean}> = [];
@@ -40,6 +41,7 @@ const createRuntime = (options: {
setEntryVisibilityValue: (path, visible) => {
calls.push({path, visible});
},
+ getEntryIcon: (path) => options.icons?.[path] || {iconHTML: ""},
readonly: options.readonly || false,
languages: {entryHide: "Hide", entryShow: "Show"},
};
@@ -80,6 +82,23 @@ test("dock blank menu lists configurable dock entries only", () => {
});
});
+test("blank menu keeps only entries accepted by the filter", () => {
+ withWindow(() => {
+ const {runtime} = createRuntime();
+ const items = buildEntryVisibilityMenuItems("dock", runtime, (key) => key === "file" || key === "outline");
+ assert.deepEqual(items.map((item) => item.id), ["dock.file", "dock.outline"]);
+ });
+});
+
+test("blank menu applies entry icons", () => {
+ withWindow(() => {
+ const {runtime} = createRuntime({icons: {"dock.file": {icon: "iconFile"}}});
+ const items = buildEntryVisibilityMenuItems("dock", runtime);
+ assert.equal(items.find((item) => item.id === "dock.file")!.icon, "iconFile");
+ assert.equal(items.find((item) => item.id === "dock.outline")!.iconHTML, "");
+ });
+});
+
test("menu item click toggles the entry visibility", () => {
withWindow(() => {
const {runtime, calls} = createRuntime({hidden: ["topBar.barSync"]});
diff --git a/app/src/config/entryVisibility/menu.ts b/app/src/config/entryVisibility/menu.ts
index fc0d2bbdb9..92207ce8cf 100644
--- a/app/src/config/entryVisibility/menu.ts
+++ b/app/src/config/entryVisibility/menu.ts
@@ -2,12 +2,62 @@ import {buildEntryVisibilityMenuItems as buildMenuItems,
buildEntryVisibilityToggleItem as buildToggleItem,
IEntryVisibilityMenuRuntime,
} from "./menuItems";
+import {getDockEntryKey, TOP_BAR_ROOT_PATH} from "./catalog";
import {getEntryOrder, isEntryVisible, setEntryVisibilityValue} from "./runtime";
+const findEntryElement = (path: string) => {
+ const separatorIndex = path.indexOf(".");
+ const scope = path.substring(0, separatorIndex);
+ const key = path.substring(separatorIndex + 1);
+ if (scope === TOP_BAR_ROOT_PATH) {
+ return Array.from(document.querySelectorAll<HTMLElement>("[data-topbar-entry]"))
+ .find((item) => item.getAttribute("data-topbar-entry") === key);
+ }
+ if (scope === "dock") {
+ return Array.from(document.querySelectorAll<HTMLElement>(".dock__item[data-type]"))
+ .find((item) => getDockEntryKey(item) === key);
+ }
+ return undefined;
+};
+
+const getEntryIcon = (path: string): Pick<IMenu, "icon" | "iconHTML"> => {
+ if (path === `${TOP_BAR_ROOT_PATH}.toolbarVIP` || path === `${TOP_BAR_ROOT_PATH}.toolbarTitle`) {
+ return {icon: "iconAccount"};
+ }
+ const element = findEntryElement(path);
+ if (!element) {
+ return {iconHTML: ""};
+ }
+ const customElement = element.querySelector(":scope > .b3-menu__icon--custom");
+ if (customElement) {
+ const iconElement = customElement.cloneNode(true) as HTMLElement;
+ iconElement.classList.add("b3-menu__icon");
+ return {iconHTML: iconElement.outerHTML};
+ }
+ const href = element.querySelector("use")?.getAttribute("xlink:href");
+ if (href) {
+ return {icon: href.substring(1)};
+ }
+ const svgElement = element.querySelector("svg");
+ if (svgElement) {
+ const iconElement = svgElement.cloneNode(true) as HTMLElement;
+ iconElement.classList.add("b3-menu__icon");
+ return {iconHTML: iconElement.outerHTML};
+ }
+ const childElement = element.firstElementChild;
+ if (childElement) {
+ const iconElement = childElement.cloneNode(true) as HTMLElement;
+ iconElement.classList.add("b3-menu__icon");
+ return {iconHTML: iconElement.outerHTML};
+ }
+ return {iconHTML: ""};
+};
+
const getRuntime = (): IEntryVisibilityMenuRuntime => ({
getEntryOrder,
isEntryVisible,
setEntryVisibilityValue,
+ getEntryIcon,
readonly: window.siyuan.config.readonly,
languages: window.siyuan.languages,
});
@@ -15,5 +65,19 @@ const getRuntime = (): IEntryVisibilityMenuRuntime => ({
export const buildEntryVisibilityMenuItems = (parentPath: string) =>
buildMenuItems(parentPath, getRuntime());
+export const buildDockEntryVisibilityMenuItems = (container?: Element) => {
+ if (!container) {
+ return buildMenuItems("dock", getRuntime());
+ }
+ const keys = new Set<string>();
+ container.querySelectorAll(".dock__item[data-type]").forEach((item) => {
+ const key = getDockEntryKey(item);
+ if (key) {
+ keys.add(key);
+ }
+ });
+ return buildMenuItems("dock", getRuntime(), (key) => keys.has(key));
+};
+
export const buildEntryVisibilityToggleItem = (path: string) =>
buildToggleItem(path, getRuntime());
diff --git a/app/src/config/entryVisibility/menuItems.ts b/app/src/config/entryVisibility/menuItems.ts
index fe9dcbcfa5..02b29f09a1 100644
--- a/app/src/config/entryVisibility/menuItems.ts
+++ b/app/src/config/entryVisibility/menuItems.ts
@@ -9,6 +9,7 @@ export interface IEntryVisibilityMenuRuntime {
getEntryOrder: (parentPath: string) => string[];
isEntryVisible: (path: string) => boolean;
setEntryVisibilityValue: (path: string, visible: boolean) => void;
+ getEntryIcon: (path: string) => Pick<IMenu, "icon" | "iconHTML">;
readonly: boolean;
languages: { [key: string]: any };
}
@@ -22,21 +23,24 @@ const getConfigurableEntries = (parentPath: string, runtime: IEntryVisibilityMen
Boolean(item && item.type === "entry" && isEntryCatalogNodeConfigurable(item)));
};
-export const buildEntryVisibilityMenuItems = (parentPath: string, runtime: IEntryVisibilityMenuRuntime): IMenu[] => {
- return getConfigurableEntries(parentPath, runtime).map((item) => {
- const path = `${parentPath}.${item.key}`;
- const visible = runtime.isEntryVisible(path);
- return {
- id: path,
- iconHTML: "",
- label: item.label(),
- checked: visible,
- disabled: runtime.readonly,
- click: () => {
- runtime.setEntryVisibilityValue(path, !visible);
- },
- };
- });
+export const buildEntryVisibilityMenuItems = (parentPath: string, runtime: IEntryVisibilityMenuRuntime,
+ filter?: (key: string) => boolean): IMenu[] => {
+ return getConfigurableEntries(parentPath, runtime)
+ .filter((item) => !filter || filter(item.key))
+ .map((item) => {
+ const path = `${parentPath}.${item.key}`;
+ const visible = runtime.isEntryVisible(path);
+ return {
+ id: path,
+ ...runtime.getEntryIcon(path),
+ label: item.label(),
+ checked: visible,
+ disabled: runtime.readonly,
+ click: () => {
+ runtime.setEntryVisibilityValue(path, !visible);
+ },
+ };
+ });
};
export const buildEntryVisibilityToggleItem = (path: string, runtime: IEntryVisibilityMenuRuntime): IMenu | undefined => {
diff --git a/app/src/menus/dock.ts b/app/src/menus/dock.ts
index 19c7bd84bb..ffe5dc3be2 100644
--- a/app/src/menus/dock.ts
+++ b/app/src/menus/dock.ts
@@ -1,6 +1,6 @@
import {MenuItem} from "./Menu";
import {Constants} from "../constants";
-import {buildEntryVisibilityMenuItems, buildEntryVisibilityToggleItem} from "../config/entryVisibility/menu";
+import {buildDockEntryVisibilityMenuItems, buildEntryVisibilityToggleItem} from "../config/entryVisibility/menu";
import {getDockEntryKey, refreshDockCatalog} from "../config/entryVisibility/catalog";
const moveMenuItem = (label: string, target: Element) => {
@@ -20,7 +20,7 @@ const moveMenuItem = (label: string, target: Element) => {
});
};
-export const initDockMenu = (target?: Element) => {
+export const initDockMenu = (target?: Element, container?: Element) => {
window.siyuan.menus.menu.remove();
window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_DOCK);
refreshDockCatalog(window.siyuan.ws?.app?.plugins || []);
@@ -38,7 +38,7 @@ export const initDockMenu = (target?: Element) => {
window.siyuan.menus.menu.append(new MenuItem(item).element);
}
} else {
- buildEntryVisibilityMenuItems("dock").forEach((item) => {
+ buildDockEntryVisibilityMenuItems(container).forEach((item) => {
window.siyuan.menus.menu.append(new MenuItem(item).element);
});
}
diff --git a/app/src/menus/index.ts b/app/src/menus/index.ts
index 88706136eb..de83941338 100644
--- a/app/src/menus/index.ts
+++ b/app/src/menus/index.ts
@@ -124,7 +124,10 @@ export class Menus {
} else if (target.classList.contains("dock") || target.classList.contains("dock__items") ||
target.classList.contains("dock__item--space")) {
hideTooltip();
- initDockMenu().popup({x: event.clientX, y: event.clientY});
+ initDockMenu(undefined, target.closest(".dock") || undefined).popup({
+ x: event.clientX,
+ y: event.clientY
+ });
event.stopPropagation();
break;
} else if (dataType === "textMenu") {