This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
const { defineFlatConfig } = require('@flowgram.ai/eslint-config');
|
||||
|
||||
module.exports = defineFlatConfig({
|
||||
preset: 'web',
|
||||
packageRoot: __dirname,
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"name": "@flowgram.ai/node-core-plugin",
|
||||
"version": "0.1.8",
|
||||
"homepage": "https://flowgram.ai/",
|
||||
"repository": "https://github.com/bytedance/flowgram.ai",
|
||||
"license": "MIT",
|
||||
"exports": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/esm/index.js",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/esm/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "npm run build:fast -- --dts-resolve",
|
||||
"build:fast": "tsup src/index.ts --format cjs,esm --sourcemap --legacy-output",
|
||||
"build:watch": "npm run build:fast -- --dts-resolve",
|
||||
"clean": "rimraf dist",
|
||||
"test": "exit 0",
|
||||
"test:cov": "exit 0",
|
||||
"ts-check": "tsc --noEmit",
|
||||
"watch": "npm run build:fast -- --dts-resolve --watch --ignore-watch dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"@flowgram.ai/core": "workspace:*",
|
||||
"@flowgram.ai/document": "workspace:*",
|
||||
"@flowgram.ai/form-core": "workspace:*",
|
||||
"@flowgram.ai/node": "workspace:*",
|
||||
"inversify": "^6.0.1",
|
||||
"reflect-metadata": "~0.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@flowgram.ai/eslint-config": "workspace:*",
|
||||
"@flowgram.ai/ts-config": "workspace:*",
|
||||
"@types/bezier-js": "4.1.3",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
"@types/react": "^18",
|
||||
"@types/react-dom": "^18",
|
||||
"@vitest/coverage-v8": "^3.2.4",
|
||||
"eslint": "^9.0.0",
|
||||
"react": "^18",
|
||||
"react-dom": "^18",
|
||||
"tsup": "^8.0.1",
|
||||
"typescript": "^5.8.3",
|
||||
"vitest": "^3.2.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": ">=16.8",
|
||||
"react-dom": ">=16.8"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "@flowgram.ai/ts-config/tsconfig.flow.path.json",
|
||||
"compilerOptions": {
|
||||
},
|
||||
"include": ["./src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
import { defineConfig } from 'vitest/config';
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
commonjsOptions: {
|
||||
transformMixedEsModules: true,
|
||||
},
|
||||
},
|
||||
test: {
|
||||
globals: true,
|
||||
mockReset: false,
|
||||
environment: 'jsdom',
|
||||
setupFiles: [path.resolve(__dirname, './vitest.setup.ts')],
|
||||
include: ['**/?(*.){test,spec}.?(c|m)[jt]s?(x)'],
|
||||
exclude: [
|
||||
'**/__mocks__**',
|
||||
'**/node_modules/**',
|
||||
'**/dist/**',
|
||||
'**/lib/**', // lib 编译结果忽略掉
|
||||
'**/cypress/**',
|
||||
'**/.{idea,git,cache,output,temp}/**',
|
||||
'**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*',
|
||||
],
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
import 'reflect-metadata';
|
||||
Reference in New Issue
Block a user