Skip to content

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

:recycle: Centralize mind map custom attribute constants https://github.com/siyuan-note/siyuan/issues/18831

Vanessa4a68468b6bfa
diff --git a/app/src/constants.ts b/app/src/constants.ts
index b053b526c9..6e1700b1df 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -79,6 +79,8 @@ export abstract class Constants {
public static readonly CUSTOM_SY_HEADING_NUMBER: string = "custom-sy-heading-number";
public static readonly CUSTOM_SY_AV_VIEW: string = "custom-sy-av-view";
public static readonly CUSTOM_SY_AV_VISIBLE_VIEWS: string = "custom-sy-av-visible-views";
+ public static readonly CUSTOM_SY_LIST_MINDMAP: string = "custom-sy-list-mindmap";
+ public static readonly CUSTOM_SY_LIST_MINDMAP_DATA: string = "custom-sy-list-mindmap-data";
public static readonly CUSTOM_SY_TITLE_EMPTY: string = "custom-sy-title-empty";
public static readonly CUSTOM_SY_CODE_TAB_SPACES: string = "custom-sy-code-tab-spaces";
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index d4ff39c280..1787a468ab 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -20,7 +20,7 @@ import {
import {openInlineStyleDialog} from "../../toolbar/inlineStyleDialog";
import {
addListMindmapNode, cleanListMindmapHTML, deleteListMindmapNode,
- LIST_MINDMAP_VIEW_ATTRIBUTE, moveListMindmapNode, readListMindmap, replaceListMindmapContent,
+ moveListMindmapNode, readListMindmap, replaceListMindmapContent,
writeListMindmapMetadata,
} from "./model";
import type {ListMindmapMetadata, ListMindmapModel} from "./model";
@@ -38,7 +38,7 @@ export const toggleListMindmap = (owner: IProtyle, list: HTMLElement) => {
if (list.dataset.type !== "NodeList" || !canEdit(owner, list)) {
return;
}
- const previous = list.getAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE) || "";
+ const previous = list.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) || "";
const next = previous === "1" ? "" : "1";
if (next) {
try {
@@ -50,10 +50,10 @@ export const toggleListMindmap = (owner: IProtyle, list: HTMLElement) => {
}
}
hideElements(["gutter", "toolbar", "hint"], owner);
- list.setAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE, next);
+ list.setAttribute(Constants.CUSTOM_SY_LIST_MINDMAP, next);
transaction(owner, [{action: "setAttrs", id: list.dataset.nodeId,
- data: JSON.stringify({[LIST_MINDMAP_VIEW_ATTRIBUTE]: next})}], [{action: "setAttrs", id: list.dataset.nodeId,
- data: JSON.stringify({[LIST_MINDMAP_VIEW_ATTRIBUTE]: previous})}]);
+ data: JSON.stringify({[Constants.CUSTOM_SY_LIST_MINDMAP]: next})}], [{action: "setAttrs", id: list.dataset.nodeId,
+ data: JSON.stringify({[Constants.CUSTOM_SY_LIST_MINDMAP]: previous})}]);
roots.get(owner)?.refresh();
};
@@ -376,7 +376,7 @@ export const initListMindmaps = (owner: IProtyle) => {
return;
}
const mount = () => {
- if (disposed || !root.contains(list) || list.getAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE) !== "1") {
+ if (disposed || !root.contains(list) || list.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) !== "1") {
return;
}
try {
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 741e08c623..48c58e3ef0 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -5,9 +5,21 @@ import {tmpdir} from "node:os";
import * as path from "node:path";
import {execFile} from "node:child_process";
import {promisify} from "node:util";
-import {layoutListMindmap, parseListMindmapMetadata} from "./model";
import type {ListMindmapLayoutNode} from "./model";
+const buildGlobals = ["SIYUAN_VERSION", "NODE_ENV"].map(name => ({
+ name, descriptor: Object.getOwnPropertyDescriptor(globalThis, name),
+}));
+buildGlobals.forEach(({name}) => Object.defineProperty(globalThis, name, {configurable: true, value: "test"}));
+const {layoutListMindmap, parseListMindmapMetadata}: typeof import("./model") = require("./model");
+buildGlobals.forEach(({name, descriptor}) => {
+ if (descriptor) {
+ Object.defineProperty(globalThis, name, descriptor);
+ } else {
+ Reflect.deleteProperty(globalThis, name);
+ }
+});
+
const measured = (id: string, width = 100, height = 40,
children: ListMindmapLayoutNode[] = []): ListMindmapLayoutNode => ({id, width, height, children});
@@ -93,7 +105,8 @@ const browserCases = async (sourceCode: string, css: string) => {
formula.innerHTML = '<span class="katex">rendered formula</span>';
formula.setAttribute("data-render", "true");
});
- }, {TIMEOUT_DBLCLICK: 190});
+ }, {TIMEOUT_DBLCLICK: 190, CUSTOM_SY_LIST_MINDMAP: "custom-sy-list-mindmap",
+ CUSTOM_SY_LIST_MINDMAP_DATA: "custom-sy-list-mindmap-data"});
const lute = Lute.New();
lute.SetKramdownIAL(true);
lute.SetProtyleWYSIWYG(true);
diff --git a/app/src/protyle/render/listMindmap/model.ts b/app/src/protyle/render/listMindmap/model.ts
index 8c104ec367..36e99c232e 100644
--- a/app/src/protyle/render/listMindmap/model.ts
+++ b/app/src/protyle/render/listMindmap/model.ts
@@ -1,8 +1,6 @@
+import {Constants} from "../../../constants";
import {getOrderedListMarkerUpdates} from "../../wysiwyg/listContext";
-export const LIST_MINDMAP_VIEW_ATTRIBUTE = "custom-sy-list-mindmap";
-export const LIST_MINDMAP_META_ATTRIBUTE = "custom-sy-list-mindmap-data";
-
export interface ListMindmapNodeStyle {
textColor?: string;
backgroundColor?: string;
@@ -121,21 +119,21 @@ export const parseListMindmapMetadata = (value: string | null): ListMindmapMetad
};
export const writeListMindmapMetadata = (list: HTMLElement, metadata: ListMindmapMetadata) => {
- parseListMindmapMetadata(list.getAttribute(LIST_MINDMAP_META_ATTRIBUTE));
+ parseListMindmapMetadata(list.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA));
const value = JSON.stringify(metadata);
parseListMindmapMetadata(value);
- list.setAttribute(LIST_MINDMAP_META_ATTRIBUTE, value);
+ list.setAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA, value);
};
// 复制块树时同步替换节点样式和关系线端点,先校验全部配置再写入,避免出现部分改写。
export const remapListMindmapIDs = (root: Element, ids: Map<string, string>) => {
- const lists = Array.from(root.querySelectorAll<HTMLElement>(`[${LIST_MINDMAP_META_ATTRIBUTE}]`)).filter(list =>
+ const lists = Array.from(root.querySelectorAll<HTMLElement>(`[${Constants.CUSTOM_SY_LIST_MINDMAP_DATA}]`)).filter(list =>
!list.closest(".list-mindmap"));
- if (root.hasAttribute(LIST_MINDMAP_META_ATTRIBUTE)) {
+ if (root.hasAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA)) {
lists.unshift(root as HTMLElement);
}
const updates = lists.map(list => {
- const metadata = parseListMindmapMetadata(list.getAttribute(LIST_MINDMAP_META_ATTRIBUTE));
+ const metadata = parseListMindmapMetadata(list.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA));
const validIds = new Set(readListMindmap(list).nodes.keys());
validIds.add(list.dataset.nodeId);
const nodes: Record<string, ListMindmapNodeStyle> = Object.create(null);
@@ -155,7 +153,7 @@ export const remapListMindmapIDs = (root: Element, ids: Map<string, string>) =>
to: ids.get(relation.to) || relation.to,
})).filter(relation => validIds.has(relation.from) && validIds.has(relation.to))}};
});
- updates.forEach(({list, metadata}) => list.setAttribute(LIST_MINDMAP_META_ATTRIBUTE, JSON.stringify(metadata)));
+ updates.forEach(({list, metadata}) => list.setAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA, JSON.stringify(metadata)));
cleanListMindmapDOM(root);
};
@@ -172,7 +170,7 @@ export const readListMindmap = (list: HTMLElement): ListMindmapModel => {
if (list.getAttribute("data-type") !== "NodeList" || !list.getAttribute("data-node-id")) {
throw new Error("A list mindmap requires a list block");
}
- const metadata = parseListMindmapMetadata(list.getAttribute(LIST_MINDMAP_META_ATTRIBUTE));
+ const metadata = parseListMindmapMetadata(list.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA));
const nodes = new Map<string, ListMindmapNode>();
const virtualRoot: ListMindmapNode = {
id: list.getAttribute("data-node-id"),
@@ -488,7 +486,7 @@ export const deleteListMindmapNode = (list: HTMLElement, id: string): boolean =>
removedIds.forEach(removedId => delete model.metadata.nodes[removedId]);
model.metadata.relations = model.metadata.relations.filter(relation =>
!removedIds.has(relation.from) && !removedIds.has(relation.to));
- if (list.hasAttribute(LIST_MINDMAP_META_ATTRIBUTE)) {
+ if (list.hasAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA)) {
writeListMindmapMetadata(list, model.metadata);
}
return true;
diff --git a/app/src/protyle/render/listMindmap/render.ts b/app/src/protyle/render/listMindmap/render.ts
index 51f914b0b8..4c72e48947 100644
--- a/app/src/protyle/render/listMindmap/render.ts
+++ b/app/src/protyle/render/listMindmap/render.ts
@@ -1,4 +1,5 @@
-import {LIST_MINDMAP_VIEW_ATTRIBUTE, readListMindmap} from "./model";
+import {Constants} from "../../../constants";
+import {readListMindmap} from "./model";
import {ListMindmapView} from "./view";
const editorRoots = new WeakMap<Element, () => void>();
@@ -10,7 +11,7 @@ export const registerListMindmapRoot = (root: Element, refresh: () => void) => {
};
export const getListMindmapElements = (root: Element) => {
- const selector = `[data-type="NodeList"][${LIST_MINDMAP_VIEW_ATTRIBUTE}="1"]`;
+ const selector = `[data-type="NodeList"][${Constants.CUSTOM_SY_LIST_MINDMAP}="1"]`;
const lists = Array.from(root.querySelectorAll<HTMLElement>(selector));
if (root.matches(selector)) {
lists.unshift(root as HTMLElement);
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 720c9c10e2..fe772fdc22 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -74,7 +74,7 @@ import {
} from "./blockSelection";
import {isEmptyParagraph} from "./emptyTextBlock";
import {cleanTableCellRichHTML, retainTableCellRichMetadata} from "../util/tableCellRich";
-import {cleanListMindmapHTML, LIST_MINDMAP_META_ATTRIBUTE, LIST_MINDMAP_VIEW_ATTRIBUTE} from "../render/listMindmap/model";
+import {cleanListMindmapHTML} from "../render/listMindmap/model";
import {completeTabsListSource, convertTabsList, isTabsListConversion} from "./tabsList";
import {waitForPendingTransactions} from "../util/transactionQueue";
import {
@@ -147,8 +147,8 @@ const syncBlockAttrs = (element: Element, operation: Extract<IOperation, {action
const attrs = JSON.parse(operation.data);
const hasFold = Object.prototype.hasOwnProperty.call(attrs, "fold");
const hasStyle = Object.prototype.hasOwnProperty.call(attrs, "style");
- const hasMindmapView = Object.prototype.hasOwnProperty.call(attrs, LIST_MINDMAP_VIEW_ATTRIBUTE);
- const hasMindmapMetadata = Object.prototype.hasOwnProperty.call(attrs, LIST_MINDMAP_META_ATTRIBUTE);
+ const hasMindmapView = Object.prototype.hasOwnProperty.call(attrs, Constants.CUSTOM_SY_LIST_MINDMAP);
+ const hasMindmapMetadata = Object.prototype.hasOwnProperty.call(attrs, Constants.CUSTOM_SY_LIST_MINDMAP_DATA);
const tabsAttrs = ["tabs-active-id", "tabs-position", "tabs-task"]
.filter(name => Object.prototype.hasOwnProperty.call(attrs, name));
if (!hasFold && !hasStyle && !hasMindmapView && !hasMindmapMetadata && tabsAttrs.length === 0) {
@@ -156,18 +156,18 @@ const syncBlockAttrs = (element: Element, operation: Extract<IOperation, {action
}
element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {
if (hasMindmapMetadata) {
- if (attrs[LIST_MINDMAP_META_ATTRIBUTE]) {
- item.setAttribute(LIST_MINDMAP_META_ATTRIBUTE, attrs[LIST_MINDMAP_META_ATTRIBUTE]);
+ if (attrs[Constants.CUSTOM_SY_LIST_MINDMAP_DATA]) {
+ item.setAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA, attrs[Constants.CUSTOM_SY_LIST_MINDMAP_DATA]);
} else {
- item.removeAttribute(LIST_MINDMAP_META_ATTRIBUTE);
+ item.removeAttribute(Constants.CUSTOM_SY_LIST_MINDMAP_DATA);
}
}
// 同步脑图视图属性,由列表监听器刷新各分屏中的视图。
if (hasMindmapView) {
- if (attrs[LIST_MINDMAP_VIEW_ATTRIBUTE]) {
- item.setAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE, attrs[LIST_MINDMAP_VIEW_ATTRIBUTE]);
+ if (attrs[Constants.CUSTOM_SY_LIST_MINDMAP]) {
+ item.setAttribute(Constants.CUSTOM_SY_LIST_MINDMAP, attrs[Constants.CUSTOM_SY_LIST_MINDMAP]);
} else {
- item.removeAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE);
+ item.removeAttribute(Constants.CUSTOM_SY_LIST_MINDMAP);
}
}
if (hasFold) {
diff --git a/app/src/protyle/wysiwyg/transactionAttrs.test.ts b/app/src/protyle/wysiwyg/transactionAttrs.test.ts
index 1cbc7bdc00..ee5090802f 100644
--- a/app/src/protyle/wysiwyg/transactionAttrs.test.ts
+++ b/app/src/protyle/wysiwyg/transactionAttrs.test.ts
@@ -10,7 +10,8 @@ const declaration = source.statements.find(statement => isVariableStatement(stat
statement.declarationList.declarations.some(item => item.name.getText(source) === "syncBlockAttrs"));
const attribute = "custom-sy-list-mindmap";
const metadataAttribute = "custom-sy-list-mindmap-data";
-const context = {LIST_MINDMAP_VIEW_ATTRIBUTE: attribute, LIST_MINDMAP_META_ATTRIBUTE: metadataAttribute, sync: undefined as any};
+const context = {Constants: {CUSTOM_SY_LIST_MINDMAP: attribute, CUSTOM_SY_LIST_MINDMAP_DATA: metadataAttribute},
+ sync: undefined as any};
runInNewContext(transpileModule(declaration.getText(source) + "\nglobalThis.sync = syncBlockAttrs;", {
compilerOptions: {target: ScriptTarget.ES2021},
}).outputText, context);