create-panel-manager-plugin.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { definePluginCreator } from '@flowgram.ai/core';
  6. import {
  7. PanelEntityFactory,
  8. PanelEntity,
  9. PanelEntityFactoryConstant,
  10. PanelEntityConfigConstant,
  11. } from './services/panel-factory';
  12. import { defineConfig } from './services/panel-config';
  13. import {
  14. PanelManager,
  15. PanelManagerConfig,
  16. PanelLayer,
  17. PanelRestore,
  18. PanelRestoreImpl,
  19. } from './services';
  20. export const createPanelManagerPlugin = definePluginCreator<Partial<PanelManagerConfig>>({
  21. onBind: ({ bind }, opt) => {
  22. bind(PanelManager).to(PanelManager).inSingletonScope();
  23. bind(PanelRestore).to(PanelRestoreImpl).inSingletonScope();
  24. bind(PanelManagerConfig).toConstantValue(defineConfig(opt));
  25. bind(PanelEntityFactory).toFactory(
  26. (context) =>
  27. ({
  28. factory,
  29. config,
  30. }: {
  31. factory: PanelEntityFactoryConstant;
  32. config: PanelEntityConfigConstant;
  33. }) => {
  34. const container = context.container.createChild();
  35. container.bind(PanelEntityFactoryConstant).toConstantValue(factory);
  36. container.bind(PanelEntityConfigConstant).toConstantValue(config);
  37. const panel = container.resolve(PanelEntity);
  38. panel.init();
  39. return panel;
  40. }
  41. );
  42. },
  43. onInit(ctx) {
  44. ctx.playground.registerLayer(PanelLayer);
  45. const panelManager = ctx.container.get<PanelManager>(PanelManager);
  46. panelManager.init();
  47. },
  48. });