create-fixed-history-plugin.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { interfaces } from 'inversify';
  6. import { bindContributions } from '@flowgram.ai/utils';
  7. import { HistoryContainerModule, OperationService } from '@flowgram.ai/history';
  8. import { OperationContribution } from '@flowgram.ai/history';
  9. import { FlowOperationBaseService } from '@flowgram.ai/document';
  10. import { FlowDocument } from '@flowgram.ai/document';
  11. import { definePluginCreator } from '@flowgram.ai/core';
  12. import { FixedHistoryPluginOptions } from './types';
  13. import { FixedHistoryService } from './services/fixed-history-service';
  14. import { FixedHistoryOperationService } from './services/fixed-history-operation-service';
  15. import { FixedHistoryFormDataService } from './services';
  16. import { FixedHistoryRegisters } from './fixed-history-registers';
  17. import { FixedHistoryConfig } from './fixed-history-config';
  18. export function registerHistory(bind: interfaces.Bind, rebind: interfaces.Rebind) {
  19. bindContributions(bind, FixedHistoryRegisters, [OperationContribution]);
  20. bind(FixedHistoryService).toSelf().inSingletonScope();
  21. bind(FixedHistoryFormDataService).toSelf().inSingletonScope();
  22. bind(FixedHistoryConfig).toSelf().inSingletonScope();
  23. rebind(FlowOperationBaseService).to(FixedHistoryOperationService).inSingletonScope();
  24. }
  25. export const createFixedHistoryPlugin = definePluginCreator<FixedHistoryPluginOptions<any>>({
  26. onBind: ({ bind, rebind }) => {
  27. registerHistory(bind, rebind);
  28. },
  29. onInit(ctx, opts): void {
  30. const fixedHistoryService = ctx.get<FixedHistoryService>(FixedHistoryService);
  31. fixedHistoryService.setSource(ctx);
  32. const document = ctx.get<FlowDocument>(FlowDocument);
  33. if (opts?.uri) {
  34. fixedHistoryService.historyService.context.uri = opts.uri;
  35. }
  36. if (opts?.getDocumentJSON) {
  37. fixedHistoryService.historyService.config.getSnapshot = opts.getDocumentJSON(ctx);
  38. } else {
  39. fixedHistoryService.historyService.config.getSnapshot = () => document.toJSON();
  40. }
  41. const config = fixedHistoryService.config;
  42. config.init(ctx, opts);
  43. if (opts?.operationMetas) {
  44. fixedHistoryService.registerOperationMetas(opts.operationMetas);
  45. }
  46. if (opts.onApply) {
  47. ctx.get(OperationService).onApply(opts.onApply.bind(null, ctx));
  48. }
  49. },
  50. containerModules: [HistoryContainerModule],
  51. });