create-container.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { interfaces } from 'inversify';
  6. import { HistoryService } from '@flowgram.ai/fixed-history-plugin';
  7. import {
  8. createPlaygroundContainer,
  9. Playground,
  10. loadPlugins,
  11. PluginContext,
  12. createPluginContextDefault,
  13. FlowDocument,
  14. EditorProps,
  15. } from '@flowgram.ai/editor';
  16. import {
  17. FixedLayoutPluginContext,
  18. FixedLayoutProps,
  19. FlowOperationService,
  20. createFixedLayoutPreset,
  21. } from '../src';
  22. export function createContainer(opts: FixedLayoutProps): interfaces.Container {
  23. const container = createPlaygroundContainer();
  24. const playground = container.get(Playground);
  25. const preset = createFixedLayoutPreset(opts);
  26. const customPluginContext = (container: interfaces.Container) =>
  27. ({
  28. ...createPluginContextDefault(container),
  29. get document(): FlowDocument {
  30. return container.get<FlowDocument>(FlowDocument);
  31. },
  32. } as FixedLayoutPluginContext);
  33. const ctx = customPluginContext(container);
  34. container.rebind(PluginContext).toConstantValue(ctx);
  35. loadPlugins(preset(ctx), container);
  36. playground.init();
  37. return container;
  38. }
  39. export function createHistoryContainer(props: EditorProps = {}) {
  40. const container = createContainer({
  41. history: {
  42. enable: true,
  43. },
  44. ...props,
  45. });
  46. const flowDocument = container.get<FlowDocument>(FlowDocument);
  47. const flowOperationService = container.get<FlowOperationService>(FlowOperationService);
  48. const historyService = container.get<HistoryService>(HistoryService);
  49. return {
  50. flowDocument,
  51. flowOperationService,
  52. historyService,
  53. };
  54. }