fix(core): workflow document reload addNode api (#353)

This commit is contained in:
Louis Young 2025-06-10 15:47:19 +08:00 committed by GitHub
parent 004b8ac8bd
commit 73b2090bd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 86 additions and 19 deletions

View File

@ -226,12 +226,7 @@ export class FlowDocument<T = FlowDocumentJSON> implements Disposable {
* @param data * @param data
* @param addedNodes * @param addedNodes
*/ */
addNode( addNode(data: AddNodeData, addedNodes?: FlowNodeEntity[]): FlowNodeEntity {
data: AddNodeData,
addedNodes?: FlowNodeEntity[],
ignoreCreateAndUpdateEvent?: boolean,
ignoreBlocks?: boolean
): FlowNodeEntity {
const { id, type = 'block', originParent, parent, meta, hidden, index } = data; const { id, type = 'block', originParent, parent, meta, hidden, index } = data;
let node = this.getNode(id); let node = this.getNode(id);
let isNew = false; let isNew = false;
@ -279,7 +274,7 @@ export class FlowDocument<T = FlowDocumentJSON> implements Disposable {
if (extendNodes && addedNodes) { if (extendNodes && addedNodes) {
addedNodes.push(...extendNodes); addedNodes.push(...extendNodes);
} }
} else if (data.blocks && data.blocks.length > 0 && !ignoreBlocks) { } else if (data.blocks && data.blocks.length > 0) {
// 兼容老的写法 // 兼容老的写法
if (!data.blocks[0].type) { if (!data.blocks[0].type) {
this.addInlineBlocks(node, data.blocks, addedNodes); this.addInlineBlocks(node, data.blocks, addedNodes);
@ -288,16 +283,14 @@ export class FlowDocument<T = FlowDocumentJSON> implements Disposable {
} }
} }
if (!ignoreCreateAndUpdateEvent) { if (isNew) {
if (isNew) { this.onNodeCreateEmitter.fire({
this.onNodeCreateEmitter.fire({ node,
node, data,
data, json: data,
json: data, });
}); } else {
} else { this.onNodeUpdateEmitter.fire({ node, data, json: data });
this.onNodeUpdateEmitter.fire({ node, data, json: data });
}
} }
return node; return node;

View File

@ -2,7 +2,12 @@ import { customAlphabet } from 'nanoid';
import { inject, injectable, optional, postConstruct } from 'inversify'; import { inject, injectable, optional, postConstruct } from 'inversify';
import { Emitter, type IPoint } from '@flowgram.ai/utils'; import { Emitter, type IPoint } from '@flowgram.ai/utils';
import { NodeEngineContext } from '@flowgram.ai/form-core'; import { NodeEngineContext } from '@flowgram.ai/form-core';
import { FlowDocument, FlowNodeBaseType, FlowNodeTransformData } from '@flowgram.ai/document'; import {
AddNodeData,
FlowDocument,
FlowNodeBaseType,
FlowNodeTransformData,
} from '@flowgram.ai/document';
import { import {
injectPlaygroundContext, injectPlaygroundContext,
PlaygroundConfigEntity, PlaygroundConfigEntity,
@ -170,7 +175,6 @@ export class WorkflowDocument extends FlowDocument {
parent, parent,
}, },
undefined, undefined,
true,
true true
) as WorkflowNodeEntity; ) as WorkflowNodeEntity;
@ -293,6 +297,76 @@ export class WorkflowDocument extends FlowDocument {
return node; return node;
} }
/**
*
* @param data
* @param addedNodes
*/
addNode(
data: AddNodeData,
addedNodes?: WorkflowNodeEntity[],
ignoreCreateAndUpdateEvent?: boolean
): WorkflowNodeEntity {
const { id, type = 'block', originParent, parent, meta, hidden, index } = data;
let node = this.getNode(id);
let isNew = false;
const register = this.getNodeRegistry(type, data.originParent);
// node 类型变化则全部删除重新来
if (node && node.flowNodeType !== data.type) {
node.dispose();
node = undefined;
}
if (!node) {
const { dataRegistries } = register;
node = this.entityManager.createEntity<WorkflowNodeEntity>(WorkflowNodeEntity, {
id,
document: this,
flowNodeType: type,
originParent,
meta,
});
const datas = dataRegistries
? this.nodeDataRegistries.concat(...dataRegistries)
: this.nodeDataRegistries;
node.addInitializeData(datas);
node.onDispose(() => this.onNodeDisposeEmitter.fire({ node: node! }));
this.options.fromNodeJSON?.(node, data, true);
isNew = true;
} else {
this.options.fromNodeJSON?.(node, data, false);
}
// 初始化数据重制
node.initData({
originParent,
parent,
meta,
hidden,
index,
});
addedNodes?.push(node);
// 自定义创建逻辑
if (register.onCreate) {
const extendNodes = register.onCreate(node, data);
if (extendNodes && addedNodes) {
addedNodes.push(...extendNodes);
}
}
if (!ignoreCreateAndUpdateEvent) {
if (isNew) {
this.onNodeCreateEmitter.fire({
node,
data,
json: data,
});
} else {
this.onNodeUpdateEmitter.fire({ node, data, json: data });
}
}
return node;
}
get layout(): FreeLayout { get layout(): FreeLayout {
const layout = this.layouts.find((layout) => layout.name == this.currentLayoutKey); const layout = this.layouts.find((layout) => layout.name == this.currentLayoutKey);
if (!layout) { if (!layout) {