import { LocaleService, Plugin, Inject, Injector, UniverInstanceType, IConfigService, Univer, } from '@univerjs/presets'; import type { ICommand, Dependency, IAccessor } from '@univerjs/presets'; import type { IMenuButtonItem, IShortcutItem } from '@univerjs/preset-sheets-core'; import zhCN from './locale/zh-CN'; import enUS from './locale/en-US'; import { CustomMenuController } from './controllers/custom-menu.controller'; const SHEET_CUSTOM_MENU_PLUGIN = 'SHEET_CUSTOM_MENU_PLUGIN'; export interface ICustomMenuPluginConfig { instance: Univer; menu: Array<{ operation: ICommand; shortcut?: IShortcutItem; menu: () => IMenuButtonItem; icon?: { name: string; component: any }; }>; } export interface UniverMenuConfig { id: string; operation: ICommand; shortcut?: IShortcutItem; menu?: (accessor: IAccessor) => IMenuButtonItem; icon?: { name: string; component: any }; onlyOperation?: boolean; } export interface ICustomMenuPulginParams { [key: string]: any; before?: () => void | Promise; after?: (param?: any) => void; } export const CUSTOM_PLUGIN_CONFIG = 'CUSTOM_PLUGIN_CONFIG'; const AllCustomController = [CustomMenuController]; export class UniverSheetsCustomMenuPlugin extends Plugin { static override type = UniverInstanceType.UNIVER_SHEET; static override pluginName = SHEET_CUSTOM_MENU_PLUGIN; constructor( config: ICustomMenuPluginConfig, @Inject(Injector) protected readonly _injector: Injector, @Inject(LocaleService) private readonly _localeService: LocaleService, @IConfigService private readonly _configService: IConfigService ) { super(); this._localeService.load({ zhCN, enUS, }); this._configService.setConfig(CUSTOM_PLUGIN_CONFIG, config); } onReady(): void { AllCustomController.forEach((d) => this._injector.get(d)); } override onStarting(): void { ([AllCustomController] as Dependency[]).forEach((d) => this._injector.add(d) ); } }