index.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {
  2. LocaleService,
  3. Plugin,
  4. Inject,
  5. Injector,
  6. UniverInstanceType,
  7. IConfigService,
  8. Univer,
  9. } from '@univerjs/presets';
  10. import type { ICommand, Dependency, IAccessor } from '@univerjs/presets';
  11. import type { IMenuButtonItem, IShortcutItem } from '@univerjs/preset-sheets-core';
  12. import zhCN from './locale/zh-CN';
  13. import enUS from './locale/en-US';
  14. import { CustomMenuController } from './controllers/custom-menu.controller';
  15. const SHEET_CUSTOM_MENU_PLUGIN = 'SHEET_CUSTOM_MENU_PLUGIN';
  16. export interface ICustomMenuPluginConfig {
  17. instance: Univer;
  18. menu: Array<{
  19. operation: ICommand<object, boolean>;
  20. shortcut?: IShortcutItem<object>;
  21. menu: () => IMenuButtonItem<string>;
  22. icon?: { name: string; component: any };
  23. }>;
  24. }
  25. export interface UniverMenuConfig {
  26. id: string;
  27. operation: ICommand<object, boolean>;
  28. shortcut?: IShortcutItem<object>;
  29. menu?: (accessor: IAccessor) => IMenuButtonItem<string>;
  30. icon?: { name: string; component: any };
  31. onlyOperation?: boolean;
  32. }
  33. export interface ICustomMenuPulginParams {
  34. [key: string]: any;
  35. before?: () => void | Promise<any>;
  36. after?: (param?: any) => void;
  37. }
  38. export const CUSTOM_PLUGIN_CONFIG = 'CUSTOM_PLUGIN_CONFIG';
  39. const AllCustomController = [CustomMenuController];
  40. export class UniverSheetsCustomMenuPlugin extends Plugin {
  41. static override type = UniverInstanceType.UNIVER_SHEET;
  42. static override pluginName = SHEET_CUSTOM_MENU_PLUGIN;
  43. constructor(
  44. config: ICustomMenuPluginConfig,
  45. @Inject(Injector) protected readonly _injector: Injector,
  46. @Inject(LocaleService) private readonly _localeService: LocaleService,
  47. @IConfigService private readonly _configService: IConfigService
  48. ) {
  49. super();
  50. this._localeService.load({
  51. zhCN,
  52. enUS,
  53. });
  54. this._configService.setConfig(CUSTOM_PLUGIN_CONFIG, config);
  55. }
  56. onReady(): void {
  57. AllCustomController.forEach((d) => this._injector.get(d));
  58. }
  59. override onStarting(): void {
  60. ([AllCustomController] as Dependency[]).forEach((d) =>
  61. this._injector.add(d)
  62. );
  63. }
  64. }