fix: node.toJSON use document.toNodeJSON (#418)

This commit is contained in:
xiamidaxia 2025-06-27 14:26:53 +08:00 committed by GitHub
parent bf69e6cb89
commit 080d28ba1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 31 deletions

View File

@ -337,37 +337,7 @@ export class FlowNodeEntity extends Entity<FlowNodeEntityConfig> {
* @param newId * @param newId
*/ */
toJSON(): FlowNodeJSON { toJSON(): FlowNodeJSON {
if (this.document.options.toNodeJSON) { return this.document.toNodeJSON(this);
return this.document.options.toNodeJSON(this);
}
const nodesMap: Record<string, FlowNodeJSON> = {};
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!;
} }
get isVertical(): boolean { get isVertical(): boolean {

View File

@ -593,6 +593,40 @@ export class FlowDocument<T = FlowDocumentJSON> implements Disposable {
return result; return result;
} }
toNodeJSON(node: FlowNodeEntity): FlowNodeJSON {
if (this.options.toNodeJSON) {
return this.options.toNodeJSON(node);
}
const nodesMap: Record<string, FlowNodeJSON> = {};
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 * @param param0