create-node-core-plugin.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { FormModelV2 } from '@flowgram.ai/node';
  6. import {
  7. createNodeContainerModules,
  8. createNodeEntityDatas,
  9. FlowNodeFormData,
  10. FormManager,
  11. NodeManager,
  12. } from '@flowgram.ai/form-core';
  13. import { FlowDocument, FlowNodeEntity } from '@flowgram.ai/document';
  14. import { definePluginCreator, EntityManager } from '@flowgram.ai/core';
  15. import { registerNodeMaterial } from './utils';
  16. import { NodeEngineMaterialOptions } from './types';
  17. export interface NodeCorePluginOptions {
  18. materials?: NodeEngineMaterialOptions;
  19. }
  20. export const createNodeCorePlugin = definePluginCreator<NodeCorePluginOptions>({
  21. onInit(ctx, options) {
  22. /**
  23. * 注册NodeEngine 相关 EntityData 到flowDocument
  24. */
  25. ctx.get<FlowDocument>(FlowDocument).registerNodeDatas(...createNodeEntityDatas());
  26. const formModelFactory = (entity: FlowNodeEntity) => new FormModelV2(entity);
  27. const entityManager = ctx.get<EntityManager>(EntityManager);
  28. entityManager.registerEntityData(
  29. FlowNodeFormData,
  30. () =>
  31. ({
  32. formModelFactory: formModelFactory,
  33. } as any)
  34. );
  35. if (!options.materials) {
  36. return;
  37. }
  38. const nodeManager = ctx.get<NodeManager>(NodeManager);
  39. const formManager = ctx.get<FormManager>(FormManager);
  40. if (!nodeManager || !formManager) {
  41. throw new Error('NodeCorePlugin Error: nodeManager or formManager not found');
  42. }
  43. registerNodeMaterial({ nodeManager, formManager, material: options.materials! });
  44. },
  45. onDispose(ctx) {
  46. ctx.get<FormManager>(FormManager)?.dispose();
  47. },
  48. containerModules: createNodeContainerModules(),
  49. // onBind: ({ bind }) => {
  50. // bindContributions(bind, FormNodeContribution, [NodeContribution]);
  51. // },
  52. });