From 080d28ba1ad099332e60d0b463e6265e51ab1621 Mon Sep 17 00:00:00 2001 From: xiamidaxia Date: Fri, 27 Jun 2025 14:26:53 +0800 Subject: [PATCH] fix: node.toJSON use document.toNodeJSON (#418) --- .../document/src/entities/flow-node-entity.ts | 32 +---------------- .../document/src/flow-document.ts | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/packages/canvas-engine/document/src/entities/flow-node-entity.ts b/packages/canvas-engine/document/src/entities/flow-node-entity.ts index 6b0ee55b..e4e63dda 100644 --- a/packages/canvas-engine/document/src/entities/flow-node-entity.ts +++ b/packages/canvas-engine/document/src/entities/flow-node-entity.ts @@ -337,37 +337,7 @@ export class FlowNodeEntity extends Entity { * @param newId */ toJSON(): FlowNodeJSON { - if (this.document.options.toNodeJSON) { - return this.document.options.toNodeJSON(this); - } - const nodesMap: Record = {}; - let startNodeJSON: FlowNodeJSON; - this.document.traverse((node) => { - const isSystemNode = node.id.startsWith('$'); - if (isSystemNode) return; - const nodeJSONData = this.getJSONData(); - const nodeJSON: FlowNodeJSON = { - id: node.id, - type: node.flowNodeType, - }; - if (nodeJSONData !== undefined) { - nodeJSON.data = nodeJSONData; - } - if (!startNodeJSON) startNodeJSON = nodeJSON; - let { parent } = node; - if (parent && parent.id.startsWith('$')) { - parent = parent.originParent; - } - const parentJSON = parent ? nodesMap[parent.id] : undefined; - if (parentJSON) { - if (!parentJSON.blocks) { - parentJSON.blocks = []; - } - parentJSON.blocks.push(nodeJSON); - } - nodesMap[node.id] = nodeJSON; - }, this); - return startNodeJSON!; + return this.document.toNodeJSON(this); } get isVertical(): boolean { diff --git a/packages/canvas-engine/document/src/flow-document.ts b/packages/canvas-engine/document/src/flow-document.ts index 61a53cde..5922c991 100644 --- a/packages/canvas-engine/document/src/flow-document.ts +++ b/packages/canvas-engine/document/src/flow-document.ts @@ -593,6 +593,40 @@ export class FlowDocument implements Disposable { return result; } + toNodeJSON(node: FlowNodeEntity): FlowNodeJSON { + if (this.options.toNodeJSON) { + return this.options.toNodeJSON(node); + } + const nodesMap: Record = {}; + let startNodeJSON: FlowNodeJSON; + this.traverse((node) => { + const isSystemNode = node.id.startsWith('$'); + if (isSystemNode) return; + const nodeJSONData = node.getJSONData(); + const nodeJSON: FlowNodeJSON = { + id: node.id, + type: node.flowNodeType, + }; + if (nodeJSONData !== undefined) { + nodeJSON.data = nodeJSONData; + } + if (!startNodeJSON) startNodeJSON = nodeJSON; + let { parent } = node; + if (parent && parent.id.startsWith('$')) { + parent = parent.originParent; + } + const parentJSON = parent ? nodesMap[parent.id] : undefined; + if (parentJSON) { + if (!parentJSON.blocks) { + parentJSON.blocks = []; + } + parentJSON.blocks.push(nodeJSON); + } + nodesMap[node.id] = nodeJSON; + }, node); + return startNodeJSON!; + } + /** * 移动节点 * @param param0