create-container.ts 1.5 KB

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