An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Restore mind map conversions and task toggles https://github.com/siyuan-note/siyuan/issues/19818
Vanessabda95e3f787f
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.tsindex de70a8e9ad..79cd249c78 100644--- a/app/src/protyle/gutter/index.ts+++ b/app/src/protyle/gutter/index.ts@@ -1797,7 +1797,7 @@ export class Gutter {type: "Blocks2Hs",}));}- } else if (type === "NodeList" && allowStructuralMutation) {+ } else if ((type === "NodeList" || type === "NodeMindmap") && allowStructuralMutation) {turnIntoSubmenu.push(this.turnsOneInto({menuId: "paragraph",id,@@ -1835,7 +1835,8 @@ export class Gutter {type: "List2Tabs"}));const listSubtype = nodeElement.getAttribute("data-subtype");- const isMindmap = nodeElement.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) === "1";+ const isMindmap = type === "NodeMindmap" ||+ nodeElement.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) === "1";if (isMindmap) {[{menuId: "list", icon: "iconList", label: "list", type: "OL2UL"},diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.tsindex dc9c544799..b37dd0ff07 100644--- a/app/src/protyle/render/listMindmap/model.test.ts+++ b/app/src/protyle/render/listMindmap/model.test.ts@@ -127,9 +127,9 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,inputSource: string) => {const check = require("node:assert/strict");const api = new Function("mathRender", "Constants", "highlightRender", sourceCode + "; return {readListMindmap, moveListMindmapNode, addListMindmapNode, " +- "deleteListMindmapNode, replaceListMindmapContent, cleanListMindmapHTML, convertListMindmapToList, remapListMindmapIDs, writeListMindmapMetadata, retagMindmapBranch, " ++ "deleteListMindmapNode, replaceListMindmapContent, cleanListMindmapHTML, convertListMindmapToList, listMindmapConversionSource, remapListMindmapIDs, writeListMindmapMetadata, retagMindmapBranch, " +"normalizeLegacyMindmapCodes, replaceLegacyMindmapHTML, spinListMindmapDOM, focusListMindmap, " +- "tabsRender, destroyTabsRender, getTabTask, getListMindmapTabItem, ListMindmapView};")(+ "tabsRender, destroyTabsRender, getTabTask, getListMindmapTabItem, convertTabsList, ListMindmapView};")(async (element: Element) => {const formulas = element.querySelectorAll('[data-subtype="math"]:not([data-render="true"])');await Promise.resolve();@@ -669,6 +669,44 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,check.ok(result.textContent.includes("Alpha") && result.textContent.includes("Beta"));}}+ const typedSource = document.createElement("div");+ typedSource.innerHTML = lute.Md2BlockDOM("* Alpha\n * Nested\n* Beta\n");+ const typedList = typedSource.firstElementChild as HTMLElement;+ const typedIDs = ids(typedList);+ const typedMetadata = JSON.stringify({version: 1, nodes: {}, relations: [], rootTitle: "Example"});+ typedList.setAttribute("custom-sy-list-mindmap-data", typedMetadata);+ api.retagMindmapBranch(typedList, true);+ typedList.append(derived.cloneNode(true));+ const typedBefore = typedList.outerHTML;+ const normalized = api.listMindmapConversionSource(typedList);+ check.equal(normalized.getAttribute("data-type"), "NodeList");+ check.deepEqual(ids(normalized), typedIDs);+ check.equal(normalized.querySelector(".mindmap-view"), null);+ check.equal(typedList.outerHTML, typedBefore, "preparing a conversion never mutates the undo source");+ for (const [conversion, subtype] of [["OL2UL", "u"], ["UL2OL", "o"], ["UL2TL", "t"]]) {+ const converted = document.createElement("div");+ converted.innerHTML = api.convertListMindmapToList(typedList, conversion, lute);+ const result = converted.firstElementChild as HTMLElement;+ check.equal(result.getAttribute("data-type"), "NodeList");+ check.equal(result.getAttribute("data-subtype"), subtype);+ check.equal(result.querySelector("[data-type=\"NodeMindmapItem\"]"), null);+ check.equal(result.querySelector("[data-type=\"NodeMindmap\"]"), null);+ check.deepEqual(ids(result), typedIDs);+ check.equal(result.getAttribute("custom-sy-list-mindmap-data"), typedMetadata);+ check.equal(result.querySelector(".mindmap-view"), null);+ check.ok(result.textContent.includes("Nested"));+ }+ const paragraphs = document.createElement("div");+ // @ts-expect-error Lute 的类型声明未包含列表取消方法。+ paragraphs.innerHTML = lute.CancelList(normalized.outerHTML);+ check.ok(paragraphs.textContent.includes("Alpha") && paragraphs.textContent.includes("Nested"));+ check.equal(paragraphs.querySelector('[data-type="NodeMindmap"]'), null);+ lute.SetTabs(true);+ const convertedTabs = api.convertTabsList(normalized, "List2Tabs", lute);+ check.equal(convertedTabs.getAttribute("data-type"), "NodeTabs");+ check.equal(convertedTabs.querySelectorAll(':scope > [data-type="NodeTabItem"]').length, 2);+ check.ok(convertedTabs.textContent.includes("Nested"));+ check.equal(typedList.outerHTML, typedBefore);check.equal(cleaned.includes("Derived editor"), false);check.equal(cleaned.includes("data-mindmap-view-rendered"), false);check.equal(cleaned.includes("data-mindmap-view-editing"), false);@@ -730,7 +768,6 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,let interactions = 0;let undo = 0;let redo = 0;- let exits = 0;let finishAllowed: boolean | Promise<boolean> = true;const options = {host, model,@@ -755,21 +792,12 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,onRelationAdd: (...args: unknown[]) => relationAdditions.push(args),onRelationChange: (...args: unknown[]) => relationChanges.push(args),onRelationDelete: (id: string) => relationDeletions.push(id),- onExit: () => exits++,+ onExit: (): void => undefined,};const frame = () => new Promise<void>(resolve => requestAnimationFrame(() => resolve()));const settle = async () => { await frame(); await frame(); };const view = new api.ListMindmapView(options);await settle();- const listButton = host.querySelector<HTMLButtonElement>('.mindmap-view__toolbar button[aria-label="listBlock"]');- check.ok(listButton);- check.equal(listButton.querySelector("use").getAttribute("xlink:href"), "#iconList");- finishAllowed = false;- listButton.click();- check.equal(exits, 0, "conversion waits for pending node edits");- finishAllowed = true;- listButton.click();- check.equal(exits, 1, "the toolbar restores the list conversion action");const viewport = host.querySelector<HTMLElement>(".mindmap-view__viewport");check.equal(viewport.getAttribute("data-prevent-swipe"), "true");const originalCapture = HTMLElement.prototype.setPointerCapture;@@ -1826,7 +1854,6 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,const readonly = new api.ListMindmapView({...options, readOnly: true});await settle();- check.equal(host.querySelector('.mindmap-view__toolbar [aria-label="listBlock"]'), null);check.equal(host.querySelector('.mindmap-view__toolbar [aria-label="listMindmapChild"]'), null);check.equal(host.querySelector('[aria-label="undo"]'), null);nodeElement(beta).dispatchEvent(new MouseEvent("dblclick", {bubbles: true}));@@ -2775,7 +2802,8 @@ test("list mindmap mutations preserve block data in the real DOM and Lute", {["../../../util/escape.ts", "../tabsState.ts", "../tabsDrag.ts", "../tabsAttributes.ts", "../tabsRender.ts"].map(file => compile(path.join(__dirname, file))).join("\n") +"return {tabsRender, destroyTabsRender, getTabTask};})();\n";- const source = tabsSource + compile(path.join(__dirname, "../av/richTextValue.ts")) + compile(path.join(__dirname, "../../wysiwyg/listContext.ts")) ++ const source = tabsSource + compile(path.join(__dirname, "../../wysiwyg/tabsList.ts")) ++ compile(path.join(__dirname, "../av/richTextValue.ts")) + compile(path.join(__dirname, "../../wysiwyg/listContext.ts")) +compile(path.join(__dirname, "model.ts")) + compile(path.join(__dirname, "fold.ts")) +compile(path.join(__dirname, "routing.ts")) + compile(path.join(__dirname, "view.ts")) +compile(path.join(__dirname, "legacy.ts")) + compile(path.join(__dirname, "migrate.ts")) +diff --git a/app/src/protyle/render/listMindmap/model.ts b/app/src/protyle/render/listMindmap/model.tsindex 622162f7f5..c4352ca8b0 100644--- a/app/src/protyle/render/listMindmap/model.ts+++ b/app/src/protyle/render/listMindmap/model.ts@@ -282,16 +282,26 @@ export const cleanListMindmapHTML = (html: string): string => {return template.innerHTML;};+// 转换前将思维导图块还原为列表结构,并移除视图节点。+export const listMindmapConversionSource = (element: Element): Element => {+ const template = document.createElement("template");+ template.innerHTML = cleanListMindmapHTML(element.outerHTML);+ const source = template.content.firstElementChild;+ if (source.getAttribute("data-type") === "NodeMindmap") {+ retagMindmapBranch(source, false);+ }+ source.removeAttribute(Constants.CUSTOM_SY_LIST_MINDMAP);+ return source;+};+// 转换列表类型时退出脑图显示,保留节点、连接元数据及原 DOM 供撤销使用。export const convertListMindmapToList = (element: Element, type: string, lute: Lute): string | undefined => {- if (element.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) !== "1" ||+ if (element.getAttribute("data-type") !== "NodeMindmap" &&+ element.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) !== "1" ||!["OL2UL", "UL2OL", "UL2TL", "OL2TL", "TL2UL", "TL2OL"].includes(type)) {return;}- const template = document.createElement("template");- template.innerHTML = cleanListMindmapHTML(element.outerHTML);- const source = template.content.firstElementChild;- source.removeAttribute(Constants.CUSTOM_SY_LIST_MINDMAP);+ const source = listMindmapConversionSource(element);const from = {o: "OL", t: "TL", u: "UL"}[source.getAttribute("data-subtype")] || "UL";const to = type.split("2")[1];if (from === to) {diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.tsindex 1dcc19e17e..9283a76d0a 100644--- a/app/src/protyle/render/listMindmap/view.ts+++ b/app/src/protyle/render/listMindmap/view.ts@@ -373,9 +373,6 @@ export class ListMindmapView {this.buttons.set(id, button);this.toolbar.append(button);};- if (!this.options.readOnly) {- add("list", "listBlock", "iconList", () => this.options.onExit());- }this.toolbar.append(createElement("span", "mindmap-view__spacer fn__flex-1"));if (!this.options.readOnly) {add("relation", "connect", "iconRoute", () => {diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex a356ad534b..48a543d0b1 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -75,7 +75,7 @@ import {} from "./blockSelection";import {isEmptyParagraph} from "./emptyTextBlock";import {cleanTableCellRichHTML, retainTableCellRichMetadata} from "../util/tableCellRich";-import {cleanListMindmapHTML, convertListMindmapToList} from "../render/listMindmap/model";+import {cleanListMindmapHTML, convertListMindmapToList, listMindmapConversionSource} from "../render/listMindmap/model";import {getProtyleTransactionOwner} from "../runtimeCapabilities";import {completeTabsListSource, convertTabsList, isTabsListConversion} from "./tabsList";import {waitForPendingTransactions} from "../util/transactionQueue";@@ -2284,15 +2284,18 @@ export const turnsOneInto = async (options: {source = completeTabsListSource(source, full);oldHTML = source.outerHTML;}- const converted = convertTabsList(source, options.type, options.protyle.lute);+ const converted = convertTabsList(source.getAttribute("data-type") === "NodeMindmap" ?+ listMindmapConversionSource(source) : source, options.type, options.protyle.lute);if (!converted) {return;}newHTML = converted.outerHTML;} else {const listHTML = convertListMindmapToList(options.nodeElement, options.type, options.protyle.lute);+ const sourceHTML = options.type === "CancelList" && options.nodeElement.getAttribute("data-type") === "NodeMindmap" ?+ listMindmapConversionSource(options.nodeElement).outerHTML : cleanListMindmapHTML(options.nodeElement.outerHTML);// @ts-ignore- newHTML = listHTML ?? options.protyle.lute[options.type](cleanListMindmapHTML(options.nodeElement.outerHTML), options.level);+ newHTML = listHTML ?? options.protyle.lute[options.type](sourceHTML, options.level);}disposeCustomBlocksInElement(options.nodeElement);options.nodeElement.insertAdjacentHTML("afterend", newHTML);diff --git a/kernel/model/block_update.go b/kernel/model/block_update.goindex f8bd867eaf..627897b02c 100644--- a/kernel/model/block_update.go+++ b/kernel/model/block_update.go@@ -243,12 +243,16 @@ func resolveBlockUpdateNode(oldNode, root *ast.Node) (updatedNode *ast.Node, errif nil == updatedNode {return nil, errors.New("parse tree failed")}- if ast.NodeListItem == oldNode.Type && ast.NodeList == updatedNode.Type {- listItem := firstContentBlock(updatedNode)- if nil == listItem || ast.NodeListItem != listItem.Type {- return nil, errors.New("list block has no list item")+ if ast.NodeListItem == oldNode.Type && ast.NodeList == updatedNode.Type ||+ ast.NodeMindmapItem == oldNode.Type && ast.NodeMindmap == updatedNode.Type {+ item := firstContentBlock(updatedNode)+ if nil == item || oldNode.Type != item.Type {+ if ast.NodeListItem == oldNode.Type {+ return nil, errors.New("list block has no list item")+ }+ return nil, errors.New("mind map block has no mind map item")}- updatedNode = listItem+ updatedNode = item}return}diff --git a/kernel/model/block_update_test.go b/kernel/model/block_update_test.goindex 4c4f0aba79..9dacbcda0f 100644--- a/kernel/model/block_update_test.go+++ b/kernel/model/block_update_test.go@@ -17,6 +17,7 @@package modelimport (+ "strings""testing""github.com/88250/lute/ast"@@ -82,6 +83,37 @@ func TestResolveSuperBlockListItemAfterBlockDOMRoundTrip(t *testing.T) {}}+func TestResolveMindmapItemAfterBlockDOMRoundTrip(t *testing.T) {+ const oldID = "20260924140000-mindmapitem"+ luteEngine := util.NewLute()+ blockDOM, _ := luteEngine.Md2BlockDOMTree("* [ ] updated", true)+ blockDOM = strings.ReplaceAll(blockDOM, `data-type="NodeList"`, `data-type="NodeMindmap"`)+ blockDOM = strings.ReplaceAll(blockDOM, `data-type="NodeListItem"`, `data-type="NodeMindmapItem"`)+ oldMindmap := &ast.Node{Type: ast.NodeMindmap, ListData: &ast.ListData{}}+ oldItem := &ast.Node{Type: ast.NodeMindmapItem, ID: oldID, ListData: &ast.ListData{}}+ oldMindmap.AppendChild(oldItem)+ normalizedTree, updatedItem, err := normalizeBlockUpdateTree(oldItem, luteEngine.BlockDOM2Tree(blockDOM), luteEngine)+ if err != nil {+ t.Fatal(err)+ }+ updatedItem.SetIALAttr("id", oldID)+ normalizedDOM := luteEngine.Tree2BlockDOM(normalizedTree, luteEngine.RenderOptions, luteEngine.ParseOptions)+ transactionTree := luteEngine.BlockDOM2Tree(normalizedDOM)+ if ast.NodeMindmap != firstContentBlock(transactionTree.Root).Type {+ t.Fatal("standalone mind map item DOM should be wrapped in a mind map when parsed")+ }+ updatedNode, err := resolveBlockUpdateNode(oldItem, transactionTree.Root)+ if err != nil {+ t.Fatal(err)+ }+ if ast.NodeMindmapItem != updatedNode.Type || oldID != updatedNode.ID {+ t.Fatalf("unexpected resolved node [%s] [%s]", updatedNode.Type.String(), updatedNode.ID)+ }+ if err = treenode.ValidateBlockReplacement(oldItem, updatedNode); err != nil {+ t.Fatalf("mind map item replacement should be valid: %s", err)+ }+}+func TestBuildBlockUpdateOperationsCachesTrees(t *testing.T) {const (boxID = "20260731010100-box0001"