Skip to content

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

:recycle: Reuse shared list conversion actions https://github.com/siyuan-note/siyuan/issues/20627

Danielb4c320446c05
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 9e6db752d9..39d3bfc384 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -5,6 +5,7 @@ import {fetchSyncPost} from "../../../util/fetch";
import {normalizeHTMLAssetIFrameBlockDOM} from "../../../asset/html";
import {updateTransaction, transaction} from "../../wysiwyg/transaction";
import {genListItemElement, setTaskListItemMarker} from "../../wysiwyg/list";
+import {getListConversionType, type TListSubtype} from "../../wysiwyg/listContext";
import {nextTaskListMarker, nextTaskListStatus} from "../../wysiwyg/taskListMarker";
import {getTaskStatusItems} from "../../wysiwyg/taskStatusDialog";
import {getTabTask} from "../tabsRender";
@@ -88,7 +89,8 @@ export const toggleListMindmap = async (owner: IProtyle, list: HTMLElement) => {
if (!source || !canEdit(owner, list)) {
return;
}
- const type = {o: "UL2OL", t: "UL2TL", u: "OL2UL"}[source.getAttribute("data-subtype")] || "OL2UL";
+ const type = getListConversionType("u", source.getAttribute("data-subtype") as TListSubtype) ||
+ getListConversionType("o", "u");
const html = convertListMindmapToList(source, type, owner.lute);
const template = list.ownerDocument.createElement("template");
template.innerHTML = html;
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index cd1cf89001..d23d6fa72b 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -7,12 +7,13 @@ import {execFile} from "node:child_process";
import {promisify} from "node:util";
import type {ListMindmapLayoutNode, ListMindmapNode} from "./model";
import type {ListMindmapFoldTarget} from "./fold";
+import {getListConversionType, type TListSubtype} from "../../wysiwyg/listContext";
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");
+const {layoutListMindmap, parseListMindmapMetadata, isListMindmapListConversion}: typeof import("./model") = require("./model");
buildGlobals.forEach(({name, descriptor}) => {
if (descriptor) {
Object.defineProperty(globalThis, name, descriptor);
@@ -24,6 +25,26 @@ buildGlobals.forEach(({name, descriptor}) => {
const measured = (id: string, width = 100, height = 40,
children: ListMindmapLayoutNode[] = []): ListMindmapLayoutNode => ({id, width, height, children});
+test("mindmap list conversion accepts exactly the shared list conversion actions", () => {
+ const conversions = new Set<string>();
+ const subtypes: TListSubtype[] = ["u", "o", "t"];
+ for (const from of subtypes) {
+ for (const to of subtypes) {
+ const conversion = getListConversionType(from, to);
+ if (conversion) {
+ conversions.add(conversion);
+ }
+ }
+ }
+ for (const legacy of [false, true]) {
+ const element = {getAttribute: (name: string) => name === "data-type" ?
+ (legacy ? "NodeList" : "NodeMindmap") : legacy ? "1" : null} as unknown as Element;
+ const candidates = ["OL2UL", "UL2OL", "UL2TL", "OL2TL", "TL2UL", "TL2OL",
+ "UL2UL", "OL2OL", "TL2TL", "CancelList", "Blocks2UL", "invalid", ""];
+ assert.deepEqual(new Set(candidates.filter(type => isListMindmapListConversion(element, type))), conversions);
+ }
+});
+
test("relation label spacing only expands the requested branch boundary", () => {
const root = measured("root", 100, 40, [measured("a"), measured("b"), measured("c")]);
const expanded = layoutListMindmap(root, {verticalGaps: new Map([["b", 64]])}).nodes;
diff --git a/app/src/protyle/render/listMindmap/model.ts b/app/src/protyle/render/listMindmap/model.ts
index a7675fd49f..866ca668d8 100644
--- a/app/src/protyle/render/listMindmap/model.ts
+++ b/app/src/protyle/render/listMindmap/model.ts
@@ -1,5 +1,5 @@
import {Constants} from "../../../constants";
-import {getOrderedListMarkerUpdates} from "../../wysiwyg/listContext";
+import {getOrderedListMarkerUpdates, isListConversionType} from "../../wysiwyg/listContext";
import type {MindmapManualRoute, MindmapRelationAnchor} from "./routing";
import {getListMindmapSiblingIDs, isListMindmapSummaryCrossing, normalizeListMindmapSummaries} from "./summary";
import type {ListMindmapSummary} from "./summary";
@@ -438,7 +438,7 @@ export const materializeListMindmapRoot = (list: HTMLElement, lute: Lute): boole
export const isListMindmapListConversion = (element: Element, type: string) =>
(element.getAttribute("data-type") === "NodeMindmap" ||
element.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) === "1") &&
- ["OL2UL", "UL2OL", "UL2TL", "OL2TL", "TL2UL", "TL2OL"].includes(type);
+ isListConversionType(type);
// 转换列表类型时退出脑图显示,保留节点、连接元数据及原 DOM 供撤销使用。
export const convertListMindmapToList = (element: Element, type: string, lute: Lute): string | undefined => {
diff --git a/app/src/protyle/render/listMindmap/rootConversion.test.ts b/app/src/protyle/render/listMindmap/rootConversion.test.ts
index b757f5570e..dca2831a3d 100644
--- a/app/src/protyle/render/listMindmap/rootConversion.test.ts
+++ b/app/src/protyle/render/listMindmap/rootConversion.test.ts
@@ -4,6 +4,7 @@ import {createRequire} from "node:module";
import * as path from "node:path";
import test from "node:test";
import {createSourceFile, isVariableStatement, ScriptTarget, transpileModule} from "typescript";
+import {isListConversionType} from "../../wysiwyg/listContext";
// 使用项目已有的 HTML 解析器承载节点操作,Lute 负责真实块 DOM 的生成和转换。
const parse5 = createRequire(require.resolve("html-loader"))("parse5");
@@ -114,11 +115,11 @@ lute.SetKramdownIAL(true);
lute.SetProtyleWYSIWYG(true);
const source = readFileSync(path.join(__dirname, "model.ts"), "utf8")
.replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, "");
-const api = new Function("Constants", "Lute", "document", "NodeFilter", transpileModule(source, {
+const api = new Function("Constants", "Lute", "document", "NodeFilter", "isListConversionType", transpileModule(source, {
compilerOptions: {target: ScriptTarget.ES2021},
}).outputText + "; return {convertListMindmapToList, readListMindmap, retagMindmapBranch, listMindmapConversionSource, cleanListMindmapHTML};")({
CUSTOM_SY_LIST_MINDMAP: "custom-sy-list-mindmap", CUSTOM_SY_LIST_MINDMAP_DATA: "custom-sy-list-mindmap-data",
-}, Lute, documentFixture, {SHOW_COMMENT: 8});
+}, Lute, documentFixture, {SHOW_COMMENT: 8}, isListConversionType);
const parse = (html: string) => wrap(parse5.parseFragment(html)).firstElementChild;
const ids = (element: ElementFixture) => [element, ...element.querySelectorAll<ElementFixture>("[data-node-id]")]
.map(item => item.dataset.nodeId);
diff --git a/app/src/protyle/render/listMindmap/toggle.test.ts b/app/src/protyle/render/listMindmap/toggle.test.ts
index ac753c2780..5387be0c62 100644
--- a/app/src/protyle/render/listMindmap/toggle.test.ts
+++ b/app/src/protyle/render/listMindmap/toggle.test.ts
@@ -4,6 +4,7 @@ import test from "node:test";
import {runInNewContext} from "node:vm";
import {createSourceFile, isVariableStatement, ScriptTarget, transpileModule} from "typescript";
import {isProtyleListItemFragment, registerProtyleRuntimeCapabilities} from "../../runtimeCapabilities";
+import {getListConversionType} from "../../wysiwyg/listContext";
const source = createSourceFile("index.ts", readFileSync("src/protyle/render/listMindmap/index.ts", "utf8"),
ScriptTarget.ES2021, true);
@@ -42,6 +43,7 @@ const setup = (embedded = true) => {
const context: any = {
Constants: {CB_GET_HISTORY: "history", CUSTOM_SY_LIST_MINDMAP: attribute},
isProtyleListItemFragment,
+ getListConversionType,
roots: new WeakMap(), hideElements: () => {}, readListMindmap: () => {},
hasListMindmapRootTitle: () => false,
cleanListMindmapHTML: (html: string) => html,
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 21099d2e96..5332b8acb1 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -2359,7 +2359,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
protyle,
nodeElement: selectsElement[0],
id: selectsElement[0].dataset.nodeId,
- type: isMatchCheck ? "UL2TL" : (isMatchList ? "OL2UL" : "UL2OL"),
+ type: getListConversionType("u", targetSubtype) || getListConversionType("o", "u"),
});
} else if (listContext) {
const isListItemFocused = Boolean(protyle.block.showAll &&
diff --git a/app/src/protyle/wysiwyg/listContext.ts b/app/src/protyle/wysiwyg/listContext.ts
index c6334df34a..d40bf8cdb8 100644
--- a/app/src/protyle/wysiwyg/listContext.ts
+++ b/app/src/protyle/wysiwyg/listContext.ts
@@ -25,6 +25,10 @@ const LIST_CONVERSION_TYPES: Record<TListSubtype, Partial<Record<TListSubtype, s
t: {u: "TL2UL", o: "TL2OL"},
};
+const LIST_CONVERSION_TYPE_SET = new Set(Object.values(LIST_CONVERSION_TYPES).flatMap(Object.values));
+
+export const isListConversionType = (type: string) => LIST_CONVERSION_TYPE_SET.has(type);
+
export const getAppendListContext = (nodeElement: HTMLElement,
editorElement: HTMLElement): TAppendListContext | undefined => {
let currentElement: HTMLElement | null = nodeElement;