This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import { FormModelV2 } from '@flowgram.ai/node';
|
||||
import {
|
||||
createNodeContainerModules,
|
||||
createNodeEntityDatas,
|
||||
FlowNodeFormData,
|
||||
FormManager,
|
||||
NodeManager,
|
||||
} from '@flowgram.ai/form-core';
|
||||
import { FlowDocument, FlowNodeEntity } from '@flowgram.ai/document';
|
||||
import { definePluginCreator, EntityManager } from '@flowgram.ai/core';
|
||||
|
||||
import { registerNodeMaterial } from './utils';
|
||||
import { NodeEngineMaterialOptions } from './types';
|
||||
|
||||
export interface NodeCorePluginOptions {
|
||||
materials?: NodeEngineMaterialOptions;
|
||||
}
|
||||
|
||||
export const createNodeCorePlugin = definePluginCreator<NodeCorePluginOptions>({
|
||||
onInit(ctx, options) {
|
||||
/**
|
||||
* 注册NodeEngine 相关 EntityData 到flowDocument
|
||||
*/
|
||||
ctx.get<FlowDocument>(FlowDocument).registerNodeDatas(...createNodeEntityDatas());
|
||||
|
||||
const formModelFactory = (entity: FlowNodeEntity) => new FormModelV2(entity);
|
||||
const entityManager = ctx.get<EntityManager>(EntityManager);
|
||||
entityManager.registerEntityData(
|
||||
FlowNodeFormData,
|
||||
() =>
|
||||
({
|
||||
formModelFactory: formModelFactory,
|
||||
} as any)
|
||||
);
|
||||
|
||||
if (!options.materials) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nodeManager = ctx.get<NodeManager>(NodeManager);
|
||||
const formManager = ctx.get<FormManager>(FormManager);
|
||||
|
||||
if (!nodeManager || !formManager) {
|
||||
throw new Error('NodeCorePlugin Error: nodeManager or formManager not found');
|
||||
}
|
||||
|
||||
registerNodeMaterial({ nodeManager, formManager, material: options.materials! });
|
||||
},
|
||||
onDispose(ctx) {
|
||||
ctx.get<FormManager>(FormManager)?.dispose();
|
||||
},
|
||||
containerModules: createNodeContainerModules(),
|
||||
// onBind: ({ bind }) => {
|
||||
// bindContributions(bind, FormNodeContribution, [NodeContribution]);
|
||||
// },
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import { injectable } from 'inversify';
|
||||
import { NodeContribution, NodeManager, PLUGIN_KEY } from '@flowgram.ai/form-core';
|
||||
|
||||
import { FormRender } from './form-render';
|
||||
|
||||
@injectable()
|
||||
export class FormNodeContribution implements NodeContribution {
|
||||
onRegister(nodeManager: NodeManager) {
|
||||
nodeManager.registerPluginRender(PLUGIN_KEY.FORM, FormRender);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { FlowNodeFormData, FormModel } from '@flowgram.ai/form-core';
|
||||
import { FlowNodeEntity } from '@flowgram.ai/document';
|
||||
import { PlaygroundContext, useRefresh } from '@flowgram.ai/core';
|
||||
|
||||
interface FormRenderProps {
|
||||
node: FlowNodeEntity;
|
||||
playgroundContext?: PlaygroundContext;
|
||||
}
|
||||
|
||||
function getFormModelFromNode(node: FlowNodeEntity) {
|
||||
return node.getData(FlowNodeFormData)?.getFormModel<FormModel>();
|
||||
}
|
||||
|
||||
export function FormRender({ node }: FormRenderProps): any {
|
||||
const refresh = useRefresh();
|
||||
const formModel = getFormModelFromNode(node);
|
||||
|
||||
useEffect(() => {
|
||||
const disposable = formModel?.onInitialized(() => {
|
||||
refresh();
|
||||
});
|
||||
return () => {
|
||||
disposable.dispose();
|
||||
};
|
||||
}, [formModel]);
|
||||
|
||||
return formModel?.initialized ? formModel.render() : null;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
export * from './create-node-core-plugin';
|
||||
export * from './utils';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import {
|
||||
DecoratorExtension,
|
||||
EffectExtension,
|
||||
NodeErrorRender,
|
||||
NodePlaceholderRender,
|
||||
SetterExtension,
|
||||
ValidationExtension,
|
||||
} from '@flowgram.ai/form-core';
|
||||
|
||||
export interface NodeEngineMaterialOptions {
|
||||
/**
|
||||
* 节点项的渲染物料
|
||||
*/
|
||||
setters?: SetterExtension[];
|
||||
/**
|
||||
* 节点项的渲染装饰器物料
|
||||
*/
|
||||
decorators?: DecoratorExtension[];
|
||||
/**
|
||||
* 副作用物料
|
||||
*/
|
||||
effects?: EffectExtension[];
|
||||
/**
|
||||
* 校验物料
|
||||
*/
|
||||
validators?: ValidationExtension[];
|
||||
/**
|
||||
* 节点内部报错的渲染组件
|
||||
*/
|
||||
nodeErrorRender?: NodeErrorRender;
|
||||
/**
|
||||
* 节点无内容时的渲染组件
|
||||
*/
|
||||
nodePlaceholderRender?: NodePlaceholderRender;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import {
|
||||
DecoratorAbility,
|
||||
DecoratorExtension,
|
||||
EffectAbility,
|
||||
EffectExtension,
|
||||
FormManager,
|
||||
NodeManager,
|
||||
registerNodeErrorRender,
|
||||
registerNodePlaceholderRender,
|
||||
SetterAbility,
|
||||
SetterExtension,
|
||||
ValidationAbility,
|
||||
ValidationExtension,
|
||||
} from '@flowgram.ai/form-core';
|
||||
|
||||
import { NodeEngineMaterialOptions } from './types';
|
||||
|
||||
interface RegisterNodeMaterialProps {
|
||||
nodeManager: NodeManager;
|
||||
formManager: FormManager;
|
||||
material: NodeEngineMaterialOptions;
|
||||
}
|
||||
|
||||
export function registerNodeMaterial({
|
||||
nodeManager,
|
||||
formManager,
|
||||
material,
|
||||
}: RegisterNodeMaterialProps) {
|
||||
const {
|
||||
setters = [],
|
||||
decorators = [],
|
||||
effects = [],
|
||||
validators = [],
|
||||
nodeErrorRender,
|
||||
nodePlaceholderRender,
|
||||
} = material;
|
||||
|
||||
if (nodeErrorRender) {
|
||||
registerNodeErrorRender(nodeManager, nodeErrorRender);
|
||||
}
|
||||
if (nodePlaceholderRender) {
|
||||
registerNodePlaceholderRender(nodeManager, nodePlaceholderRender);
|
||||
}
|
||||
setters.forEach((setter: SetterExtension) => {
|
||||
formManager.registerAbilityExtension(SetterAbility.type, setter);
|
||||
});
|
||||
decorators.forEach((decorator: DecoratorExtension) => {
|
||||
formManager.registerAbilityExtension(DecoratorAbility.type, decorator);
|
||||
});
|
||||
effects.forEach((effect: EffectExtension) => {
|
||||
formManager.registerAbilityExtension(EffectAbility.type, effect);
|
||||
});
|
||||
validators.forEach((validator: ValidationExtension) => {
|
||||
formManager.registerAbilityExtension(ValidationAbility.type, validator);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user