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: Defer database rendering until template insertion completes https://github.com/siyuan-note/siyuan/issues/19216
Daniel4eb2008c6282
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.tsindex 023e3d741c..c0a3d43bb0 100644--- a/app/src/protyle/hint/extend.ts+++ b/app/src/protyle/hint/extend.ts@@ -25,7 +25,6 @@ import {zoomOut} from "../../menus/protyle";import {hideElements} from "../ui/hideElements";import {genAssetHTML} from "../../asset/renderAssets";import {unicode2Emoji} from "../../emoji";-import {avRender} from "../render/av/render";import {addWidgetCacheVersion} from "../util/widgetCache";import {getEntryCatalogNode,@@ -675,7 +674,6 @@ export const hintRenderTemplate = (value: string, protyle: IProtyle, nodeElementblockRender(protyle, protyle.wysiwyg.element);processRender(protyle.wysiwyg.element);highlightRender(protyle.wysiwyg.element);- avRender(protyle.wysiwyg.element, protyle);hideElements(["util"], protyle);};const docTreePlan = response.data.docTreePlan as ITemplateDocTreePlan | undefined;diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex 45a1395664..f3f326db21 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -192,6 +192,7 @@ const promiseTransaction = (options: {const protyle = options.protyle;// 受影响的嵌入块需推迟到事务提交后再渲染,否则其查询请求会早于写入到达内核而拿到旧数据const pendingEmbedElements = new Set<Element>();+ const pendingAVElements = new Set<Element>();/// #if MOBILEif (((0 !== window.siyuan.config.sync.provider && isPaidUser()) ||(0 === window.siyuan.config.sync.provider && !needSubscribe(""))) &&@@ -279,7 +280,7 @@ const promiseTransaction = (options: {if (updatedEmbed) {processRender(protyle.wysiwyg.element);highlightRender(protyle.wysiwyg.element);- avRender(protyle.wysiwyg.element, protyle);+ pendingAVElements.add(protyle.wysiwyg.element);}focusRestoredBlockSelectionMode(restoredSelectionModeElement);return;@@ -488,7 +489,7 @@ const promiseTransaction = (options: {cursorElements.forEach(item => {processRender(item);highlightRender(item);- avRender(item, protyle);+ pendingAVElements.add(item);blockRender(protyle, item);item.querySelectorAll("wbr").forEach(wbrItem => {wbrItem.remove();@@ -553,6 +554,29 @@ const promiseTransaction = (options: {templateDocTreePlanID: options.templateDocTreePlanID,},callback: (responseTransaction: {doOperations: IOperation[]}) => {+ // 新增和更新的数据库载体必须在事务成功后渲染,确保内核已登记块及其笔记本归属。+ responseTransaction.doOperations.forEach(operation => {+ if (operation.action === "insert" || operation.action === "update") {+ protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {+ pendingAVElements.add(item);+ });+ }+ });+ const avElements = new Set<Element>();+ pendingAVElements.forEach(item => {+ if (item.getAttribute("data-type") === "NodeAttributeView") {+ avElements.add(item);+ } else {+ item.querySelectorAll('[data-type="NodeAttributeView"]').forEach(avElement => {+ avElements.add(avElement);+ });+ }+ });+ avElements.forEach(item => {+ if (item.isConnected) {+ avRender(item, protyle);+ }+ });invalidateViewFoldRequests(protyle);const ids = getBlockSelectionStatusIDs(protyle.wysiwyg.element);countBlockWord(ids, protyle, true);diff --git a/app/src/protyle/wysiwyg/transactionAV.test.ts b/app/src/protyle/wysiwyg/transactionAV.test.tsnew file mode 100644index 0000000000..2b0e35c2c9--- /dev/null+++ b/app/src/protyle/wysiwyg/transactionAV.test.ts@@ -0,0 +1,97 @@+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";+import {queueTransaction, queueTransactionBatch, waitForPendingTransactions} from "../util/transactionQueue";++const compiled = transpileModule(readFileSync("src/protyle/wysiwyg/transaction.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+}).outputText;++const fixture = () => {+ const rendered: Element[] = [];+ const carriers = [+ {isConnected: true, closest: (): Element => null, getAttribute: () => "NodeAttributeView"},+ {isConnected: true, closest: (): Element => null, getAttribute: () => "NodeAttributeView"},+ ];+ let finish: (success: boolean) => void;+ const exports: any = {};+ const dependencies = {+ Constants: {},+ queueTransaction,+ queueTransactionBatch,+ prepareViewFoldTransaction: (_protyle: IProtyle, doOperations: IOperation[], undoOperations: IOperation[]) =>+ ({doOperations, undoOperations}),+ cleanBlockSelectionModeHTML: (html: string) => html,+ cleanTableCellRichHTML: (html: string) => html,+ normalizeHTMLAssetIFrameBlockDOM: (html: string) => html,+ cleanHeadingNumberOperations: (): void => undefined,+ needSubscribe: () => true,+ isInEmbedBlock: (): void => undefined,+ syncTrackedRanges: (): void => undefined,+ avRender: (element: Element) => rendered.push(element),+ invalidateViewFoldRequests: (): void => undefined,+ getBlockSelectionStatusIDs: (): string[] => [],+ countBlockWord: (): void => undefined,+ handleViewFoldSourceOperation: () => false,+ queueHeadingNumberRefresh: (): void => undefined,+ applyViewFoldStates: (): void => undefined,+ fetchPost: (_url: string, data: {transactions: unknown[]}, callback: (response: unknown) => void) =>+ new Promise<void>(resolve => {+ finish = success => {+ if (success) {+ callback({data: data.transactions});+ }+ resolve();+ };+ }),+ };+ runInNewContext(compiled, {+ exports,+ require: () => dependencies,+ window: {siyuan: {config: {sync: {provider: 0}}}},+ getSelection: () => ({rangeCount: 0}),+ });+ const element = {+ childElementCount: 1,+ querySelectorAll: (selector: string) => selector === '[data-node-id="carrier"]' ?+ carriers.filter(item => item.isConnected) : [],+ querySelector: () => carriers[0],+ };+ const protyle = {wysiwyg: {element}, options: {}} as unknown as IProtyle;+ const insert = () => exports.transaction(protyle, [{+ action: "insert", id: "carrier", previousID: "paragraph", data: '<div data-type="NodeAttributeView"></div>',+ }]);+ return {protyle, carriers, rendered, insert, finish: (success: boolean) => finish(success)};+};++test("本地模板数据库及其副本在插入事务成功后才渲染", async () => {+ const f = fixture();+ f.insert();+ assert.equal(f.rendered.length, 0);+ await new Promise(resolve => setImmediate(resolve));+ assert.equal(f.rendered.length, 0);+ f.finish(true);+ await waitForPendingTransactions(f.protyle);+ assert.deepEqual(f.rendered, f.carriers);+});++test("插入事务失败时不渲染尚未保存的数据库", async () => {+ const f = fixture();+ f.insert();+ await new Promise(resolve => setImmediate(resolve));+ f.finish(false);+ await waitForPendingTransactions(f.protyle);+ assert.equal(f.rendered.length, 0);+});++test("等待插入事务期间移除的数据库副本不再渲染", async () => {+ const f = fixture();+ f.insert();+ await new Promise(resolve => setImmediate(resolve));+ f.carriers[0].isConnected = false;+ f.finish(true);+ await waitForPendingTransactions(f.protyle);+ assert.deepEqual(f.rendered, [f.carriers[1]]);+});