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: Render all blocks inserted in one transaction https://github.com/siyuan-note/siyuan/issues/20529
Daniel0ee51f3fdb5f
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex d2b6657d92..3ac9834ae9 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -238,6 +238,23 @@ const getVisibleFoldHeadingHTML = (html: string) => {return template.innerHTML;};+// 按插入前的相邻边界收集新增根元素,避免把已有兄弟块或光标定位块当作完整渲染范围。+const insertTransactionHTML = (element: Element, position: "beforebegin" | "afterend" | "afterbegin", html: string) => {+ const parent = position === "afterbegin" ? element : element.parentElement;+ const previous = position === "beforebegin" ? element.previousElementSibling :+ position === "afterend" ? element : null;+ const next = position === "beforebegin" ? element :+ position === "afterend" ? element.nextElementSibling : element.firstElementChild;+ element.insertAdjacentHTML(position, html);+ const inserted: Element[] = [];+ let current = previous ? previous.nextElementSibling : parent.firstElementChild;+ while (current && current !== next) {+ inserted.push(current);+ current = current.nextElementSibling;+ }+ return inserted;+};+// 用于执行操作,外加处理当前编辑器中块引用、嵌入块的更新const promiseTransaction = (options: {protyle: IProtyle,@@ -465,6 +482,7 @@ const promiseTransaction = (options: {});}const cursorElements: Element[] = [];+ const renderElements: Element[] = [];if (operation.previousID) {Array.from(protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.previousID}"]`)).forEach(item => {const embedElement = isInEmbedBlock(item, false);@@ -483,7 +501,7 @@ const promiseTransaction = (options: {!hasClosestByAttribute(item, "data-node-id", operation.id) &&// 嵌入块后不能插入!item.parentElement.classList.contains("protyle-wysiwyg__embed")) {- item.insertAdjacentHTML("afterend", visibleData);+ renderElements.push(...insertTransactionHTML(item, "afterend", visibleData));cursorElements.push(item.nextElementSibling);}});@@ -503,7 +521,7 @@ const promiseTransaction = (options: {(!range || !item.contains(range.startContainer)) &&!hasClosestByAttribute(item, "data-node-id", operation.id) &&!item.parentElement.classList.contains("protyle-wysiwyg__embed")) {- item.insertAdjacentHTML("beforebegin", visibleData);+ renderElements.push(...insertTransactionHTML(item, "beforebegin", visibleData));cursorElements.push(item.previousElementSibling);}});@@ -524,14 +542,14 @@ const promiseTransaction = (options: {// 列表特殊处理if (item.firstElementChild && item.firstElementChild.classList.contains("protyle-action") &&item.firstElementChild.nextElementSibling?.getAttribute("data-node-id") !== operation.id) {- item.firstElementChild.insertAdjacentHTML("afterend", visibleData);+ renderElements.push(...insertTransactionHTML(item.firstElementChild, "afterend", visibleData));cursorElements.push(item.firstElementChild.nextElementSibling);} else if ((item.classList.contains("callout") || item.classList.contains("tab-item")) &&item.querySelector("[data-node-id]")?.getAttribute("data-node-id") !== operation.id) {- item.querySelector(":scope > .callout-content, :scope > .tab-item-content").insertAdjacentHTML("afterbegin", visibleData);+ renderElements.push(...insertTransactionHTML(item.querySelector(":scope > .callout-content, :scope > .tab-item-content"), "afterbegin", visibleData));cursorElements.push(item.querySelector("[data-node-id]"));} else if (item.firstElementChild.getAttribute("data-node-id") !== operation.id) {- item.insertAdjacentHTML("afterbegin", visibleData);+ renderElements.push(...insertTransactionHTML(item, "afterbegin", visibleData));cursorElements.push(item.firstElementChild);}}@@ -549,11 +567,13 @@ const promiseTransaction = (options: {item.lastElementChild.remove();}});- cursorElements.forEach(item => {+ renderElements.forEach(item => {processRender(item);highlightRender(item);pendingAVElements.add(item);blockRender(protyle, item);+ });+ cursorElements.forEach(item => {item.querySelectorAll("wbr").forEach(wbrItem => {wbrItem.remove();});@@ -1367,18 +1387,20 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUndreturn;}const visibleData = getVisibleFoldHeadingHTML(operation.data);- const cursorElements = [];+ const cursorElements: Element[] = [];+ const renderElements: Element[] = [];if (operation.previousID) {const previousElement = protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.previousID}"]`);if (previousElement.length === 0 && isUndo && protyle.wysiwyg.element.childElementCount === 0) {// https://github.com/siyuan-note/siyuan/issues/15396 操作后撤销protyle.wysiwyg.element.innerHTML = visibleData;+ renderElements.push(...Array.from(protyle.wysiwyg.element.children));cursorElements.push(protyle.wysiwyg.element.firstElementChild);} else if (previousElement.length === 0 && protyle.options.backlinkData && isUndo && getSelection().rangeCount > 0) {// 反链面板删除超级块中的最后一个段落块后撤销const blockElement = hasClosestBlock(getSelection().getRangeAt(0).startContainer);if (blockElement) {- blockElement.insertAdjacentHTML("beforebegin", visibleData);+ renderElements.push(...insertTransactionHTML(blockElement, "beforebegin", visibleData));cursorElements.push(blockElement.previousElementSibling);}} else {@@ -1389,7 +1411,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUndembedElement.removeAttribute("data-render");blockRender(protyle, embedElement);} else {- item.insertAdjacentHTML("afterend", visibleData);+ renderElements.push(...insertTransactionHTML(item, "afterend", visibleData));cursorElements.push(item.nextElementSibling);}});@@ -1402,7 +1424,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUndembedElement.removeAttribute("data-render");blockRender(protyle, embedElement);} else {- item.insertAdjacentHTML("beforebegin", visibleData);+ renderElements.push(...insertTransactionHTML(item, "beforebegin", visibleData));cursorElements.push(item.previousElementSibling);}});@@ -1416,13 +1438,13 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd}});if (!protyle.options.backlinkData && operation.parentID === protyle.block.parentID && !protyle.block.showAll) {- protyle.wysiwyg.element.insertAdjacentHTML("afterbegin", visibleData);+ renderElements.push(...insertTransactionHTML(protyle.wysiwyg.element, "afterbegin", visibleData));cursorElements.push(protyle.wysiwyg.element.firstElementChild);} else if (parentElement.length === 0 && protyle.options.backlinkData && isUndo && getSelection().rangeCount > 0) {// 反链面板删除超级块中的段落块后撤销const blockElement = hasClosestBlock(getSelection().getRangeAt(0).startContainer);if (blockElement) {- blockElement.insertAdjacentHTML("beforebegin", visibleData);+ renderElements.push(...insertTransactionHTML(blockElement, "beforebegin", visibleData));cursorElements.push(blockElement.previousElementSibling);}} else {@@ -1434,13 +1456,13 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd} else {// 列表特殊处理if (item.firstElementChild?.classList.contains("protyle-action")) {- item.firstElementChild.insertAdjacentHTML("afterend", visibleData);+ renderElements.push(...insertTransactionHTML(item.firstElementChild, "afterend", visibleData));cursorElements.push(item.firstElementChild.nextElementSibling);} else if ((item.classList.contains("callout") || item.classList.contains("tab-item"))) {- item.querySelector(":scope > .callout-content, :scope > .tab-item-content").insertAdjacentHTML("afterbegin", visibleData);+ renderElements.push(...insertTransactionHTML(item.querySelector(":scope > .callout-content, :scope > .tab-item-content"), "afterbegin", visibleData));cursorElements.push(item.querySelector("[data-node-id]"));} else {- item.insertAdjacentHTML("afterbegin", visibleData);+ renderElements.push(...insertTransactionHTML(item, "afterbegin", visibleData));cursorElements.push(item.firstElementChild);}}@@ -1465,12 +1487,16 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnditem.removeAttribute(attr);}});+ });+ renderElements.forEach(item => {processRender(item);highlightRender(item);avRender(item, protyle);blockRender(protyle, item);// 插入块后刷新所在超级块的拖拽手柄(撤销/重做/同步)refreshSbs(item);+ });+ cursorElements.forEach(item => {const wbrElement = item.querySelector("wbr");if (isUndo) {if (operation.context?.setRange === "true") {diff --git a/app/src/protyle/wysiwyg/transactionInsertRendering.test.ts b/app/src/protyle/wysiwyg/transactionInsertRendering.test.tsnew file mode 100644index 0000000000..c4edb537b4--- /dev/null+++ b/app/src/protyle/wysiwyg/transactionInsertRendering.test.ts@@ -0,0 +1,309 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";++const compiled = transpileModule(readFileSync("src/protyle/wysiwyg/transaction.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+}).outputText;++type ElementSpec = {attrs?: Record<string, string>; children?: ElementSpec[]; tag?: string};+type InsertAnchor = "previousID" | "nextID" | "parentID";+type ContainerType = "editor" | "blockquote" | "list" | "callout" | "tab" | "superblock";++class TestElement {+ parentElement: TestElement | null = null;+ children: TestElement[] = [];+ readonly nodeType = 1;+ readonly tagName: string;+ private attrs: Record<string, string>;+ classList = {+ contains: (name: string) => (this.attrs.class || "").split(" ").includes(name),+ remove: (name: string) => {+ this.attrs.class = (this.attrs.class || "").split(" ").filter(item => item !== name).join(" ");+ },+ };++ constructor(spec: ElementSpec = {}, private parseHTML: (html: string) => TestElement[] = () => []) {+ this.tagName = spec.tag || "DIV";+ this.attrs = {...spec.attrs};+ this.append(...(spec.children || []).map(child => new TestElement(child, parseHTML)));+ }++ get firstElementChild() { return this.children[0] || null; }+ get lastElementChild() { return this.children[this.children.length - 1] || null; }+ get childElementCount() { return this.children.length; }+ get nextElementSibling() {+ return this.parentElement?.children[this.parentElement.children.indexOf(this) + 1] || null;+ }+ get previousElementSibling() {+ return this.parentElement?.children[this.parentElement.children.indexOf(this) - 1] || null;+ }+ get isConnected(): boolean {+ return this.classList.contains("protyle-wysiwyg") || !!this.parentElement?.isConnected;+ }++ getAttribute(name: string) { return this.attrs[name] ?? null; }+ getAttributeNames() { return Object.keys(this.attrs); }+ hasAttribute(name: string) { return name in this.attrs; }+ setAttribute(name: string, value: string) { this.attrs[name] = value; }+ removeAttribute(name: string) { delete this.attrs[name]; }++ append(...elements: TestElement[]) {+ elements.forEach(element => {+ element.remove();+ element.parentElement = this;+ this.children.push(element);+ });+ }++ remove() {+ if (this.parentElement) {+ this.parentElement.children.splice(this.parentElement.children.indexOf(this), 1);+ this.parentElement = null;+ }+ }++ contains(element: TestElement) {+ for (let current = element; current; current = current.parentElement) {+ if (current === this) {+ return true;+ }+ }+ return false;+ }++ matches(selector: string): boolean {+ if (selector.includes(",")) {+ return selector.split(",").some(part => this.matches(part.trim()));+ }+ if (selector.startsWith(".")) {+ return this.classList.contains(selector.substring(1));+ }+ const attribute = /^\[([^=\]]+)(?:="([^"]*)")?\]$/.exec(selector);+ if (attribute) {+ return attribute[2] === undefined ? this.hasAttribute(attribute[1]) :+ this.getAttribute(attribute[1]) === attribute[2];+ }+ return this.tagName.toLowerCase() === selector;+ }++ closest(selector: string): TestElement | null {+ if (this.matches(selector)) {+ return this;+ }+ return this.parentElement?.closest(selector) || null;+ }++ querySelectorAll(selector: string): TestElement[] {+ if (selector.startsWith(":scope > ")) {+ return this.children.filter(child => child.matches(selector.replaceAll(":scope > ", "")));+ }+ const results: TestElement[] = [];+ const visit = (element: TestElement) => element.children.forEach(child => {+ if (child.matches(selector)) {+ results.push(child);+ }+ visit(child);+ });+ visit(this);+ return results;+ }++ querySelector(selector: string) { return this.querySelectorAll(selector)[0] || null; }++ insertAdjacentHTML(position: "afterbegin" | "afterend" | "beforebegin", html: string) {+ const parent = position === "afterbegin" ? this : this.parentElement;+ const index = position === "afterbegin" ? 0 : parent.children.indexOf(this) + (position === "afterend" ? 1 : 0);+ const inserted = this.parseHTML(html);+ inserted.forEach(element => { element.parentElement = parent; });+ parent.children.splice(index, 0, ...inserted);+ }++ set innerHTML(html: string) {+ this.children.forEach(child => { child.parentElement = null; });+ this.children = [];+ this.append(...this.parseHTML(html));+ }+}++const block = (id: string, type = "NodeParagraph", children: ElementSpec[] = []): ElementSpec => ({+ attrs: {"data-node-id": id, "data-type": type}, children,+});++const fixture = (containerType: ContainerType = "editor") => {+ const snippets = new Map<string, ElementSpec[]>();+ const parseHTML = (html: string) => {+ assert.ok(snippets.has(html), "insertion uses the supplied HTML unchanged");+ return snippets.get(html).map(spec => new TestElement(spec, parseHTML));+ };+ const html = (...specs: ElementSpec[]) => {+ const serialize = (spec: ElementSpec): string => `<${(spec.tag || "DIV").toLowerCase()} ${+ Object.entries(spec.attrs || {}).map(([key, value]) => `${key}="${value}"`).join(" ")}>${+ (spec.children || []).map(serialize).join("")}</${(spec.tag || "DIV").toLowerCase()}>`;+ const value = specs.map(serialize).join("");+ snippets.set(value, specs);+ return value;+ };+ const root = new TestElement({attrs: {class: "protyle-wysiwyg"}}, parseHTML);+ let parent = root;+ if (containerType !== "editor") {+ const types = {blockquote: "NodeBlockquote", list: "NodeListItem", callout: "NodeCallout",+ tab: "NodeTabItem", superblock: "NodeSuperBlock"};+ const classes = {blockquote: "bq", list: "li", callout: "callout", tab: "tab-item", superblock: "sb"};+ parent = new TestElement({attrs: {"data-node-id": "parent", "data-type": types[containerType],+ class: classes[containerType]}}, parseHTML);+ root.append(parent);+ if (containerType === "list") {+ parent.append(new TestElement({attrs: {class: "protyle-action"}}, parseHTML));+ } else if (containerType === "callout" || containerType === "tab") {+ const content = new TestElement({attrs: {class: containerType === "callout" ?+ "callout-content" : "tab-item-content"}}, parseHTML);+ parent.append(content);+ parent = content;+ }+ }+ parent.append(new TestElement(block("before"), parseHTML), new TestElement(block("after"), parseHTML));+ const rendered = {process: [] as string[], highlight: [] as string[], av: [] as string[], block: [] as string[]};+ const focused: string[] = [];+ let commit: () => void;+ const noop = (): void => undefined;+ const identity = (value: string) => value;+ const record = (kind: keyof typeof rendered, element: TestElement) => rendered[kind].push(element.getAttribute("data-node-id"));+ const dependencies = {+ Constants: {CUSTOM_SY_AV_STATIC_TEXT: "custom-sy-av-s-text-"},+ normalizeHTMLAssetIFrameBlockDOM: identity,+ invalidateTrackedRangesByOperations: noop, invalidateViewFoldRequests: noop,+ handleViewFoldSourceOperation: () => false, syncTrackedRanges: noop, applyViewFoldStates: noop,+ queueHeadingNumberRefresh: noop, refreshHeadingFoldIndicators: noop, shouldReloadForHeadingBatch: () => false,+ isInEmbedBlock: () => false, refreshSbResize: noop,+ processRender: (element: TestElement) => record("process", element),+ highlightRender: (element: TestElement) => record("highlight", element),+ avRender: (element: TestElement) => record("av", element),+ blockRender: (_protyle: unknown, element: TestElement) => record("block", element),+ getEditorRange: () => ({}), focusBlock: (element: TestElement) => focused.push(element.getAttribute("data-node-id")),+ focusByWbr: (element: TestElement) => focused.push(element.getAttribute("data-node-id")),+ getNextBlockSibling: (element: TestElement) => element.nextElementSibling,+ getPreviousBlockSibling: (element: TestElement) => element.previousElementSibling,+ hasClosestByAttribute: (element: TestElement, attr: string, value: string) => element.closest(`[${attr}="${value}"]`),+ prepareViewFoldTransaction: (_protyle: unknown, doOperations: IOperation[], undoOperations?: IOperation[]) =>+ ({doOperations, undoOperations}),+ cleanBlockSelectionModeHTML: identity, cleanTableCellRichHTML: identity, cleanTableVirtualizationHTML: identity,+ cleanListMindmapHTML: identity, restoreInlineElementBoundaryHTML: identity, cleanHeadingNumberOperations: noop,+ getBlockSelectionStatusIDs: (): string[] => [], countBlockWord: noop, getEditorTransaction: (value: unknown) => value,+ queueTransaction: (_protyle: unknown, submit: () => void) => submit(),+ fetchPost: (_url: string, data: {transactions: unknown[]}, callback: (response: unknown) => void) => {+ commit = () => callback({data: data.transactions});+ },+ };+ const api = {} as typeof import("./transaction");+ runInNewContext(compiled, {exports: api, require: () => dependencies,+ window: {siyuan: {config: {fileTree: {}, sync: {provider: 0}}}},+ getSelection: () => ({rangeCount: 0}),+ });+ const protyle = {wysiwyg: {element: root}, element: root, options: {},+ block: {rootID: "doc", id: "doc", parentID: "doc"}} as unknown as IProtyle;+ const operation = (anchor: InsertAnchor, ...specs: ElementSpec[]): IOperation => ({+ action: "insert", id: specs[0].attrs["data-node-id"], data: html(...specs),+ ...(anchor === "previousID" ? {previousID: "before"} : anchor === "nextID" ? {nextID: "after"} :+ {parentID: containerType === "editor" ? "doc" : "parent"}),+ context: {setRange: "true"},+ });+ return {api, protyle, root, parent, rendered, focused, operation, commit: () => commit()};+};++const assertRendered = (rendered: ReturnType<typeof fixture>["rendered"], ids: string[], local = false) => {+ for (const kind of local ? ["process", "highlight", "block"] as const :+ ["process", "highlight", "av", "block"] as const) {+ assert.deepEqual(rendered[kind].slice().sort(), ids.slice().sort(), `${kind} initializes only inserted roots`);+ }+};++for (const anchor of ["previousID", "nextID", "parentID"] as const) {+ for (const container of ["editor", "blockquote", "list", "callout", "tab", "superblock"] as const) {+ test(`external ${anchor} insertion in ${container} renders every sibling without changing undo focus`, () => {+ const f = fixture(container);+ f.api.onTransaction(f.protyle, [f.operation(anchor, block("first"), block("second"))], true);+ assertRendered(f.rendered, ["first", "second"]);+ assert.deepEqual(f.focused, [anchor === "nextID" ? "second" : "first"]);+ assert.equal(f.root.querySelectorAll('[data-node-id="first"]').length, 1);+ assert.equal(f.root.querySelectorAll('[data-node-id="second"]').length, 1);+ const ids = f.parent.children.map(item => item.getAttribute("data-node-id")).filter(Boolean);+ assert.deepEqual(ids, anchor === "parentID" ? ["first", "second", "before", "after"] :+ ["before", "first", "second", "after"]);+ });+ }++ test(`external ${anchor} single-root insertion renders once`, () => {+ const f = fixture();+ f.api.onTransaction(f.protyle, [f.operation(anchor, block("first"))], false);+ assertRendered(f.rendered, ["first"]);+ assert.deepEqual(f.focused, []);+ });++ test(`local ${anchor} insertion renders sibling roots and delays database rendering until commit`, () => {+ const f = fixture(anchor === "parentID" ? "list" : "editor");+ f.api.transaction(f.protyle, [f.operation(anchor, block("first"), block("second", "NodeAttributeView"))]);+ assertRendered(f.rendered, ["first", "second"], true);+ assert.deepEqual(f.rendered.av, []);+ f.commit();+ assert.deepEqual(f.rendered.av, ["second"]);+ assert.deepEqual(f.focused, []);+ });+}++test("a single inserted container is rendered once with its descendant blocks", () => {+ const f = fixture();+ f.api.onTransaction(f.protyle, [f.operation("previousID", block("container", "NodeSuperBlock", [+ block("first"), block("second"),+ ]))], false);+ assertRendered(f.rendered, ["container"]);+ assert.equal(f.root.querySelectorAll('[data-node-id="first"]').length, 1);+ assert.equal(f.root.querySelectorAll('[data-node-id="second"]').length, 1);+});++for (const anchor of ["previousID", "nextID", "parentID"] as const) {+ test(`${anchor} insertion renders every root at an open sibling boundary`, () => {+ const f = fixture();+ if (anchor !== "previousID") {+ f.root.querySelector('[data-node-id="before"]').remove();+ }+ if (anchor !== "nextID") {+ f.root.querySelector('[data-node-id="after"]').remove();+ }+ f.api.onTransaction(f.protyle, [f.operation(anchor, block("first"), block("second"))], true);+ assertRendered(f.rendered, ["first", "second"]);+ assert.deepEqual(f.focused, [anchor === "nextID" ? "second" : "first"]);+ assert.deepEqual(f.root.children.map(item => item.getAttribute("data-node-id")),+ anchor === "previousID" ? ["before", "first", "second"] :+ anchor === "nextID" ? ["first", "second", "after"] : ["first", "second"]);+ });++ test(`local ${anchor} insertion does not duplicate blocks that are already in the editor`, () => {+ const f = fixture(anchor === "parentID" ? "list" : "editor");+ const operation = f.operation(anchor, block("first"), block("second"));+ if (anchor === "previousID") {+ f.root.querySelector('[data-node-id="before"]').insertAdjacentHTML("afterend", operation.data as string);+ } else if (anchor === "nextID") {+ f.root.querySelector('[data-node-id="after"]').insertAdjacentHTML("beforebegin", operation.data as string);+ } else {+ f.parent.firstElementChild.insertAdjacentHTML("afterend", operation.data as string);+ }+ const before = f.parent.children.slice();+ f.api.transaction(f.protyle, [operation]);+ assert.equal(f.root.querySelectorAll('[data-node-id="first"]').length, 1);+ assert.equal(f.root.querySelectorAll('[data-node-id="second"]').length, 1);+ assert.deepEqual(f.parent.children, before);+ assertRendered(f.rendered, [], true);+ });+}++test("undo restores and renders every root when the previous anchor and editor content are absent", () => {+ const f = fixture();+ f.root.children.slice().forEach(item => item.remove());+ f.api.onTransaction(f.protyle, [f.operation("previousID", block("first"), block("second"))], true);+ assertRendered(f.rendered, ["first", "second"]);+ assert.deepEqual(f.focused, ["first"]);+ assert.deepEqual(f.root.children.map(item => item.getAttribute("data-node-id")), ["first", "second"]);+});