Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
refactor(ui): plugin operation service (#104)
Dushusira45bcbb592aa
diff --git a/packages/sheets-plugin-find/src/Controller/FindController.ts b/packages/sheets-plugin-find/src/Controller/FindController.tsindex 3de8bca16f..38b0aa720e 100644--- a/packages/sheets-plugin-find/src/Controller/FindController.ts+++ b/packages/sheets-plugin-find/src/Controller/FindController.ts@@ -1,46 +1,33 @@import { ComponentManager, Icon, IMenuService } from '@univerjs/base-ui';-import { Disposable } from '@univerjs/core';+import { Disposable, ICommandService } from '@univerjs/core';import { IToolbarItemProps, SheetContainerUIController } from '@univerjs/ui-plugin-sheets';-import { Inject, Injector } from '@wendellhu/redi';+import { IDisposable, Inject, Injector } from '@wendellhu/redi';+import { HideModalCommand, ShowModalCommand } from '../commands/show-modal.command';import { FIND_PLUGIN_NAME } from '../Const/PLUGIN_NAME';import { FindModalController } from './FindModalController';import { FindMenuItemFactory } from './menu';-export class FindController extends Disposable {- private _findList: IToolbarItemProps;+export class FindController extends Disposable implements IDisposable {constructor(@Inject(FindModalController) private _findModalController: FindModalController,@Inject(SheetContainerUIController) private _uiController: SheetContainerUIController,@Inject(ComponentManager) private readonly _componentManager: ComponentManager,@Inject(Injector) private readonly _injector: Injector,- @IMenuService private readonly _menuService: IMenuService+ @IMenuService private readonly _menuService: IMenuService,+ @ICommandService private readonly _commandService: ICommandService) {super();- this._findList = {- name: FIND_PLUGIN_NAME,- toolbarType: 1,- tooltip: 'find.findLabel',- label: {- name: 'SearchIcon',- },- show: true,- onClick: () => {- this._findModalController.showModal(true);- },- };this._componentManager.register('SearchIcon', Icon.SearchIcon);const toolbar = this._uiController.getToolbarController();- // toolbar.addToolbarConfig(this._findList);+this._initializeContextMenu();// TODO@Dushusir maybe trigger once in ui-plugin-sheets?toolbar.setToolbar();- }- getFindList() {- return this._findList;+ [ShowModalCommand, HideModalCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));}private _initializeContextMenu() {diff --git a/packages/sheets-plugin-find/src/Controller/FindModalController.ts b/packages/sheets-plugin-find/src/Controller/FindModalController.tsindex e801d6921a..b808d6a978 100644--- a/packages/sheets-plugin-find/src/Controller/FindModalController.ts+++ b/packages/sheets-plugin-find/src/Controller/FindModalController.ts@@ -1,6 +1,7 @@import { ComponentManager, SlotManager } from '@univerjs/base-ui';import { SHEET_UI_PLUGIN_NAME } from '@univerjs/ui-plugin-sheets';import { Inject } from '@wendellhu/redi';+import { TextFinder } from '../Domain';import { FindModal } from '../View/UI/FindModal';diff --git a/packages/sheets-plugin-find/src/Controller/menu.ts b/packages/sheets-plugin-find/src/Controller/menu.tsindex 5233c8fb12..f2bea09437 100644--- a/packages/sheets-plugin-find/src/Controller/menu.ts+++ b/packages/sheets-plugin-find/src/Controller/menu.ts@@ -1,10 +1,11 @@import { IMenuButtonItem, MenuItemType, MenuPosition } from '@univerjs/base-ui';import { IAccessor } from '@wendellhu/redi';+import { ShowModalCommand } from '../commands/show-modal.command';+export function FindMenuItemFactory(accessor: IAccessor): IMenuButtonItem {return {- // TODO@Dushusir find command trigger open modal- id: 'FindCommand.id',+ id: ShowModalCommand.id,type: MenuItemType.BUTTON,icon: 'SearchIcon',title: 'find.findLabel',diff --git a/packages/sheets-plugin-find/src/FindPlugin.ts b/packages/sheets-plugin-find/src/FindPlugin.tsindex 67e2ca30fe..8cee51155d 100644--- a/packages/sheets-plugin-find/src/FindPlugin.ts+++ b/packages/sheets-plugin-find/src/FindPlugin.ts@@ -1,12 +1,14 @@import { CommandManager, ICurrentUniverService, LocaleService, Plugin, PluginType } from '@univerjs/core';// import { TextFinder } from './Domain/TextFind';import { Dependency, Inject, Injector } from '@wendellhu/redi';-import { FIND_PLUGIN_NAME } from './Const/PLUGIN_NAME';+import { FindPluginObserve } from './Basics/Observer';+import { FIND_PLUGIN_NAME } from './Const/PLUGIN_NAME';import { FindController } from './Controller/FindController';-import { en, zh } from './Locale';-import { TextFinder } from './Domain/TextFind';import { FindModalController } from './Controller/FindModalController';+import { TextFinder } from './Domain/TextFind';+import { en, zh } from './Locale';+import { FindService } from './services/find.service';// import { FindPluginObserve, install } from './Basics/Observer';// import { FindModalController } from './Controller/FindModalController';// import { TextFinder } from './Domain';@@ -76,7 +78,7 @@ export class FindPlugin extends Plugin<FindPluginObserve> {}initializeDependencies(sheetInjector: Injector) {- const dependencies: Dependency[] = [[FindController], [TextFinder], [FindModalController]];+ const dependencies: Dependency[] = [[FindController], [TextFinder], [FindModalController], [FindService]];dependencies.forEach((d) => {sheetInjector.add(d);diff --git a/packages/sheets-plugin-find/src/commands/show-modal.command.ts b/packages/sheets-plugin-find/src/commands/show-modal.command.tsnew file mode 100644index 0000000000..61adcf2d28--- /dev/null+++ b/packages/sheets-plugin-find/src/commands/show-modal.command.ts@@ -0,0 +1,23 @@+import { CommandType, ICommand } from '@univerjs/core';+import { IAccessor } from '@wendellhu/redi';++import { FindService } from '../services/find.service';++export const ShowModalCommand: ICommand = {+ id: 'find.command.show-modal',+ type: CommandType.OPERATION,+ handler: async (accessor: IAccessor) => {+ const findService = accessor.get(FindService);+ findService.showModal(true);+ return true;+ },+};+export const HideModalCommand: ICommand = {+ id: 'find.command.hide-modal',+ type: CommandType.OPERATION,+ handler: async (accessor: IAccessor) => {+ const findService = accessor.get(FindService);+ findService.showModal(false);+ return true;+ },+};diff --git a/packages/sheets-plugin-find/src/services/find.service.ts b/packages/sheets-plugin-find/src/services/find.service.tsnew file mode 100644index 0000000000..4d45c7879f--- /dev/null+++ b/packages/sheets-plugin-find/src/services/find.service.ts@@ -0,0 +1,11 @@+import { Inject } from '@wendellhu/redi';++import { FindModalController } from '../Controller/FindModalController';++export class FindService {+ constructor(@Inject(FindModalController) private _findModalController: FindModalController) {}++ showModal(show: boolean) {+ this._findModalController.showModal(show);+ }+}diff --git a/packages/sheets-plugin-image/src/Controller/OverGridImageController.ts b/packages/sheets-plugin-image/src/Controller/OverGridImageController.tsindex 3a961f3240..e06e006c33 100644--- a/packages/sheets-plugin-image/src/Controller/OverGridImageController.ts+++ b/packages/sheets-plugin-image/src/Controller/OverGridImageController.ts@@ -1,76 +1,32 @@-import { ISelectionManager, SelectionManager } from '@univerjs/base-sheets';import { BaseComponentRender, ComponentManager, Icon, IMenuService } from '@univerjs/base-ui';-import { Command, CommandManager, Disposable, ICurrentUniverService, ObserverManager, Tools } from '@univerjs/core';-import { IToolbarItemProps, SheetContainerUIController } from '@univerjs/ui-plugin-sheets';+import { Disposable, ICommandService } from '@univerjs/core';+import { SheetContainerUIController } from '@univerjs/ui-plugin-sheets';import { Inject, Injector } from '@wendellhu/redi';-import { FileSelected, IOverGridImageProperty, OVER_GRID_IMAGE_PLUGIN_NAME, OverGridImageBorderType } from '../Basics';-import { AddOverGridImageAction, IAddOverGridImageActionData } from '../Model';+import { IOverGridImageProperty } from '../Basics';+import { UploadCommand } from '../commands/upload.command';import { IImagePluginData } from '../Symbol';import { ImportImageMenuItemFactory } from './menu';export class OverGridImageController extends Disposable {protected _render: BaseComponentRender;- protected _toolButton: IToolbarItemProps;-constructor(@Inject(Injector) readonly _injector: Injector,@Inject(IImagePluginData) _imagePluginData: Map<string, IOverGridImageProperty>,@Inject(SheetContainerUIController) private readonly _sheetContainerUIController: SheetContainerUIController,- @Inject(CommandManager) private _commandManager: CommandManager,- @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,- @Inject(ObserverManager) private _observerManager: ObserverManager,- @ISelectionManager private readonly _selectionManager: SelectionManager,+ @ICommandService private readonly _commandService: ICommandService,@Inject(ComponentManager) private readonly _componentManager: ComponentManager,@IMenuService private readonly _menuService: IMenuService) {super();- this._toolButton = {- name: OVER_GRID_IMAGE_PLUGIN_NAME,- label: '图片',- toolbarType: 1,- show: true,- tooltip: '导入图片',- onClick: () => {- const rowIndex = _selectionManager.getActiveRange()?.getRowIndex();- const columnIndex = _selectionManager.getActiveRange()?.getColumn();- const workbook = _currentUniverService.getCurrentUniverSheetInstance().getWorkBook();- FileSelected.chooseImage().then((file) => {- const reader = new FileReader();- const img = new Image();- reader.readAsDataURL(file);- reader.onload = () => {- img.src = reader.result as string;- };- img.onload = () => {- const action: IAddOverGridImageActionData = {- actionName: AddOverGridImageAction.NAME,- id: Tools.generateRandomId(),- borderType: OverGridImageBorderType.SOLID,- row: rowIndex || 1,- column: columnIndex || 1,- url: img.src,- radius: 0,- width: img.width,- height: img.height,- borderColor: '#000000',- borderWidth: 1,- sheetId: workbook.getActiveSheet().getSheetId(),- injector: _injector,- };- const command = new Command({ WorkBookUnit: workbook }, action);- this._commandManager.invoke(command);- };- });- },- };this._componentManager.register('ImageIcon', Icon.View.ImageIcon);- // this._sheetContainerUIController.getToolbarController().addToolbarConfig(this._toolButton);const toolbar = this._sheetContainerUIController.getToolbarController();this._initializeContextMenu();toolbar.setToolbar();++ [UploadCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));}private _initializeContextMenu() {diff --git a/packages/sheets-plugin-image/src/Controller/menu.ts b/packages/sheets-plugin-image/src/Controller/menu.tsindex fe04f42c76..02dd7de7f0 100644--- a/packages/sheets-plugin-image/src/Controller/menu.ts+++ b/packages/sheets-plugin-image/src/Controller/menu.ts@@ -1,10 +1,11 @@import { IMenuButtonItem, MenuItemType, MenuPosition } from '@univerjs/base-ui';import { IAccessor } from '@wendellhu/redi';+import { UploadCommand } from '../commands/upload.command';+export function ImportImageMenuItemFactory(accessor: IAccessor): IMenuButtonItem {return {- // TODO@Dushusir trigger image file select- id: 'ImportImageCommand.id',+ id: UploadCommand.id,type: MenuItemType.BUTTON,icon: 'ImageIcon',title: 'Image', // FIXME use translationdiff --git a/packages/sheets-plugin-image/src/ImagePlugin.tsx b/packages/sheets-plugin-image/src/ImagePlugin.tsxindex 1acb9cb51f..20e9aab455 100644--- a/packages/sheets-plugin-image/src/ImagePlugin.tsx+++ b/packages/sheets-plugin-image/src/ImagePlugin.tsx@@ -6,6 +6,7 @@ import { OverImageRender } from './View/Render';import { IImagePluginData } from './Symbol';import { OVER_GRID_IMAGE_PLUGIN_NAME, IOverGridImagePluginConfig, IOverGridImageProperty, install } from './Basics';import { OverGridImageController, CellImageController } from './Controller';+import { UploadService } from './services/upload.service';/*** TODO: 考虑加入单元格图片的情况,@@ -48,7 +49,7 @@ export class ImagePlugin extends Plugin {}private _initializeDependencies(sheetInjector: Injector) {- const dependencies: Dependency[] = [[OverGridImageController], [CellImageController], [OverImageRender]];+ const dependencies: Dependency[] = [[OverGridImageController], [CellImageController], [OverImageRender],[UploadService]];dependencies.forEach((d) => {sheetInjector.add(d);});diff --git a/packages/sheets-plugin-image/src/commands/upload.command.ts b/packages/sheets-plugin-image/src/commands/upload.command.tsnew file mode 100644index 0000000000..c6d11cda28--- /dev/null+++ b/packages/sheets-plugin-image/src/commands/upload.command.ts@@ -0,0 +1,14 @@+import { CommandType, ICommand } from '@univerjs/core';+import { IAccessor } from '@wendellhu/redi';++import { UploadService } from '../services/upload.service';++export const UploadCommand: ICommand = {+ id: 'image.command.upload',+ type: CommandType.OPERATION,+ handler: async (accessor: IAccessor) => {+ const uploadService = accessor.get(UploadService);+ uploadService.upload();+ return true;+ },+};diff --git a/packages/sheets-plugin-image/src/services/upload.service.ts b/packages/sheets-plugin-image/src/services/upload.service.tsnew file mode 100644index 0000000000..9876b2e688--- /dev/null+++ b/packages/sheets-plugin-image/src/services/upload.service.ts@@ -0,0 +1,50 @@+import { ISelectionManager, SelectionManager } from '@univerjs/base-sheets';+import { Command, CommandManager, ICurrentUniverService, Tools } from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';++import { FileSelected, OverGridImageBorderType } from '../Basics';+import { AddOverGridImageAction, IAddOverGridImageActionData } from '../Model';++export class UploadService {+ constructor(+ @Inject(Injector) readonly _injector: Injector,+ @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,+ @Inject(CommandManager) private _commandManager: CommandManager,+ @ISelectionManager private readonly _selectionManager: SelectionManager+ ) {}++ upload() {+ const { _selectionManager, _currentUniverService, _injector } = this;+ const rowIndex = _selectionManager.getActiveRange()?.getRowIndex();+ const columnIndex = _selectionManager.getActiveRange()?.getColumn();+ const workbook = _currentUniverService.getCurrentUniverSheetInstance().getWorkBook();+ FileSelected.chooseImage().then((file) => {+ const reader = new FileReader();+ const img = new Image();+ reader.readAsDataURL(file);+ reader.onload = () => {+ img.src = reader.result as string;+ };+ img.onload = () => {+ // FIXME use command system+ const action: IAddOverGridImageActionData = {+ actionName: AddOverGridImageAction.NAME,+ id: Tools.generateRandomId(),+ borderType: OverGridImageBorderType.SOLID,+ row: rowIndex || 1,+ column: columnIndex || 1,+ url: img.src,+ radius: 0,+ width: img.width,+ height: img.height,+ borderColor: '#000000',+ borderWidth: 1,+ sheetId: workbook.getActiveSheet().getSheetId(),+ injector: _injector,+ };+ const command = new Command({ WorkBookUnit: workbook }, action);+ this._commandManager.invoke(command);+ };+ });+ }+}diff --git a/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts b/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.tsindex e726dc895d..4a84867de6 100644--- a/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts+++ b/packages/sheets-plugin-import-xlsx/src/Controller/ImportXlsxController.ts@@ -1,11 +1,9 @@-import { BasicWorkbookController } from '@univerjs/base-sheets';import { BaseComponentRender, IMenuService } from '@univerjs/base-ui';-import { Disposable, ICurrentUniverService, IKeyValue, migrate, Tools } from '@univerjs/core';+import { Disposable, ICommandService } from '@univerjs/core';import { IToolbarItemProps, SheetContainerUIController } from '@univerjs/ui-plugin-sheets';import { Inject, Injector } from '@wendellhu/redi';-import * as LuckyExcel from 'luckyexcel';-import { IMPORT_XLSX_PLUGIN_NAME } from '../Basics/Const';+import { UploadCommand } from '../commands/upload.command';import { ImportMenuItemFactory } from './menu';export class ImportXlsxController extends Disposable {@@ -15,94 +13,16 @@ export class ImportXlsxController extends Disposable {constructor(@Inject(SheetContainerUIController) private readonly _sheetContainerUIController: SheetContainerUIController,- @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,- @Inject(BasicWorkbookController) private readonly _basicWorkbookController: BasicWorkbookController,@Inject(Injector) private readonly _injector: Injector,- @IMenuService private readonly _menuService: IMenuService+ @IMenuService private readonly _menuService: IMenuService,+ @ICommandService private readonly _commandService: ICommandService) {super();- this._toolButton = {- name: IMPORT_XLSX_PLUGIN_NAME,- toolbarType: 1,- tooltip: 'importXlsx.tooltip',- show: true,- label: 'importXlsx.import',- onClick: () => {- this.upload();- },- };- // this._sheetContainerUIController.getToolbarController().addToolbarConfig(this._toolButton);const toolbar = this._sheetContainerUIController.getToolbarController();this._initializeContextMenu();toolbar.setToolbar();- }-- upload() {- const input = document.createElement('input');- input.type = 'file';- input.accept = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';-- input.addEventListener('change', (evt) => {- const element = evt.currentTarget as HTMLInputElement;- this.handleFiles(element.files);- });-- input.click();- }-- handleFiles(files: File[] | FileList | null) {- if (files == null || files.length === 0) {- console.error('No files wait for import');- return;- }-- const name = files[0].name;- const suffixArr = name.split('.');- const suffix = suffixArr[suffixArr.length - 1];- if (suffix !== 'xlsx') {- console.error('Currently only supports the import of xlsx files');- return;- }- const transformExcelToLucky = LuckyExcel.default.transformExcelToLucky;- transformExcelToLucky(files[0], (exportJson: IKeyValue) => {- if (exportJson.sheets == null || exportJson.sheets.length === 0) {- console.error('Failed to read the content of the excel file, currently does not support xls files!');- return;- }-- const luckysheetConfig = {- container: 'universheet',- data: exportJson.sheets,- title: exportJson.info.name,- };- const univerWorkbookConfig = migrate(luckysheetConfig);-- const sheets = univerWorkbookConfig.sheets;-- if (!sheets) {- return console.error('No content');- }-- const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();- const workbookId = workbook.getUnitId();- const order = Tools.deepClone(workbook.getConfig().sheetOrder);-- // add new sheets- Object.keys(sheets).forEach((sheetId, i) => {- const sheetData = sheets[sheetId];- // workbook.insertSheet(sheetData);- const index = order.length - 1 + i;- this._basicWorkbookController.insertSheet(index, sheetData, workbookId);- });-- // remove other sheets- order.forEach((sheetId: string) => {- // workbook.removeSheetBySheetId(sheetId);- // workbook.removeSheetBySheetId(sheetId);- this._basicWorkbookController.removeSheet(sheetId, workbookId);- });- });+ [UploadCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));}private _initializeContextMenu() {diff --git a/packages/sheets-plugin-import-xlsx/src/Controller/menu.ts b/packages/sheets-plugin-import-xlsx/src/Controller/menu.tsindex 6a5e8fec11..a252a0b044 100644--- a/packages/sheets-plugin-import-xlsx/src/Controller/menu.ts+++ b/packages/sheets-plugin-import-xlsx/src/Controller/menu.ts@@ -1,10 +1,11 @@import { IMenuButtonItem, MenuItemType, MenuPosition } from '@univerjs/base-ui';import { IAccessor } from '@wendellhu/redi';+import { UploadCommand } from '../commands/upload.command';+export function ImportMenuItemFactory(accessor: IAccessor): IMenuButtonItem {return {- // TODO@Dushusir import command trigger open file select- id: 'ImportCommand.id',+ id: UploadCommand.id,type: MenuItemType.BUTTON,title: 'importXlsx.import',tooltip: 'importXlsx.tooltip',diff --git a/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx b/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsxindex 9ad4fcf5da..35f8c437c9 100644--- a/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx+++ b/packages/sheets-plugin-import-xlsx/src/ImportXlsxPlugin.tsx@@ -1,10 +1,12 @@-import { Inject, Injector } from '@wendellhu/redi';-import { LocaleService, Plugin, PluginType } from '@univerjs/core';import { RegisterManager } from '@univerjs/base-ui';-import { zh, en } from './Locale';+import { LocaleService, Plugin, PluginType } from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';+import { IMPORT_XLSX_PLUGIN_NAME } from './Basics';-import { ImportXlsxController } from './Controller/ImportXlsxController';import { DragAndDropExtensionFactory } from './Basics/Register/DragAndDropExtension';+import { ImportXlsxController } from './Controller/ImportXlsxController';+import { en, zh } from './Locale';+import { UploadService } from './services/upload.service';export interface IImportXlsxPluginConfig {}@@ -36,6 +38,7 @@ export class ImportXlsxPlugin extends Plugin<any> {// this._importXlsxController = new ImportXlsxController(this);this._importXlsxController = this._injector.createInstance(ImportXlsxController);this._injector.add([ImportXlsxController, { useValue: this._importXlsxController }]);+ this._injector.add([UploadService]);this.registerExtension();}diff --git a/packages/sheets-plugin-import-xlsx/src/commands/upload.command.ts b/packages/sheets-plugin-import-xlsx/src/commands/upload.command.tsnew file mode 100644index 0000000000..0f37f23793--- /dev/null+++ b/packages/sheets-plugin-import-xlsx/src/commands/upload.command.ts@@ -0,0 +1,14 @@+import { CommandType, ICommand } from '@univerjs/core';+import { IAccessor } from '@wendellhu/redi';++import { UploadService } from '../services/upload.service';++export const UploadCommand: ICommand = {+ id: 'import.command.upload',+ type: CommandType.OPERATION,+ handler: async (accessor: IAccessor) => {+ const uploadService = accessor.get(UploadService);+ uploadService.upload();+ return true;+ },+};diff --git a/packages/sheets-plugin-import-xlsx/src/services/upload.service.ts b/packages/sheets-plugin-import-xlsx/src/services/upload.service.tsnew file mode 100644index 0000000000..43b7565fff--- /dev/null+++ b/packages/sheets-plugin-import-xlsx/src/services/upload.service.ts@@ -0,0 +1,79 @@+import { BasicWorkbookController } from '@univerjs/base-sheets';+import { ICurrentUniverService, IKeyValue, migrate, Tools } from '@univerjs/core';+import { Inject } from '@wendellhu/redi';+import * as LuckyExcel from 'luckyexcel';++export class UploadService {+ constructor(+ @ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,+ @Inject(BasicWorkbookController) private readonly _basicWorkbookController: BasicWorkbookController+ ) {}++ upload() {+ const input = document.createElement('input');+ input.type = 'file';+ input.accept = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';++ input.addEventListener('change', (evt) => {+ const element = evt.currentTarget as HTMLInputElement;+ this.handleFiles(element.files);+ });++ input.click();+ }++ handleFiles(files: File[] | FileList | null) {+ if (files == null || files.length === 0) {+ console.error('No files wait for import');+ return;+ }++ const name = files[0].name;+ const suffixArr = name.split('.');+ const suffix = suffixArr[suffixArr.length - 1];+ if (suffix !== 'xlsx') {+ console.error('Currently only supports the import of xlsx files');+ return;+ }+ const transformExcelToLucky = LuckyExcel.default.transformExcelToLucky;++ transformExcelToLucky(files[0], (exportJson: IKeyValue) => {+ if (exportJson.sheets == null || exportJson.sheets.length === 0) {+ console.error('Failed to read the content of the excel file, currently does not support xls files!');+ return;+ }++ const luckysheetConfig = {+ container: 'universheet',+ data: exportJson.sheets,+ title: exportJson.info.name,+ };+ const univerWorkbookConfig = migrate(luckysheetConfig);++ const sheets = univerWorkbookConfig.sheets;++ if (!sheets) {+ return console.error('No content');+ }++ const workbook = this._currentUniverService.getCurrentUniverSheetInstance().getWorkBook();+ const workbookId = workbook.getUnitId();+ const order = Tools.deepClone(workbook.getConfig().sheetOrder);++ // add new sheets+ Object.keys(sheets).forEach((sheetId, i) => {+ const sheetData = sheets[sheetId];+ // workbook.insertSheet(sheetData);+ const index = order.length - 1 + i;+ this._basicWorkbookController.insertSheet(index, sheetData, workbookId);+ });++ // remove other sheets+ order.forEach((sheetId: string) => {+ // workbook.removeSheetBySheetId(sheetId);+ // workbook.removeSheetBySheetId(sheetId);+ this._basicWorkbookController.removeSheet(sheetId, workbookId);+ });+ });+ }+}diff --git a/packages/sheets-plugin-numfmt/src/Basics/Const/DEFAULT_DATA.ts b/packages/sheets-plugin-numfmt/src/Basics/Const/DEFAULT_DATA.tsindex a0e30dcdf8..be25ef8419 100644--- a/packages/sheets-plugin-numfmt/src/Basics/Const/DEFAULT_DATA.ts+++ b/packages/sheets-plugin-numfmt/src/Basics/Const/DEFAULT_DATA.ts@@ -162,14 +162,14 @@ export const DEFAULT_DATA = [export const MORE_FORMATS_SELECTIONS = [{label: 'format.moreCurrency',- value: 'MoreCurrency',+ value: 'currency',},{label: 'format.moreDateTime',- value: 'MoreDateTime',+ value: 'date',},{label: 'format.moreNumber',- value: 'MoreNumber',+ value: 'number',},];diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.tsindex a69b27cbc4..124ff4940c 100644--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts@@ -1,88 +1,29 @@-import { ISelectionManager, SelectionManager } from '@univerjs/base-sheets';import { BaseComponentRender, ComponentManager, IMenuService } from '@univerjs/base-ui';import {ACTION_NAMES as CORE_ACTION_NAME,Command,CommandManager,Disposable,+ ICommandService,ICurrentUniverService,IRangeData,ObjectMatrix,ObjectMatrixPrimitiveType,Range,} from '@univerjs/core';-import { IToolbarItemProps, SheetContainerUIController } from '@univerjs/ui-plugin-sheets';import { Inject, Injector } from '@wendellhu/redi';-import { DEFAULT_DATA, NUMFMT_PLUGIN_NAME, NumfmtConfig } from '../Basics';+import { NUMFMT_PLUGIN_NAME } from '../Basics/Const';+import { SetNumfmtRangeDataCommand } from '../commands/set-numfmt-range-data.command';+import { ShowModalCommand } from '../commands/show-modal.command';import { NumfmtModel } from '../Model';import { INumfmtPluginData } from '../Symbol';import { FormatItem } from '../View/UI/FormatItem';-import styles from '../View/UI/index.module.less';import { NumfmtRangeDataMenuItemFactory, OpenMoreFormatsModalMenuItemFactory } from './menu';import { NumfmtModalController } from './NumfmtModalController';-export class NumfmtController extends Disposable {- protected _numfmtList: IToolbarItemProps;-- protected _render: BaseComponentRender;-- constructor(- @Inject(ISelectionManager) private readonly _selectionManager: SelectionManager,- @Inject(CommandManager) private readonly _commandManager: CommandManager,- @Inject(SheetContainerUIController) private readonly _sheetContainerUIController: SheetContainerUIController,- @Inject(ComponentManager) private readonly _componentManager: ComponentManager,- @Inject(Injector) private readonly _injector: Injector,- @Inject(INumfmtPluginData) private _numfmtPluginData: NumfmtModel,- @Inject(ICurrentUniverService) private readonly _currentUniverService: ICurrentUniverService,- @Inject(NumfmtModalController) private readonly _numfmtModalController: NumfmtModalController,- @IMenuService private readonly _menuService: IMenuService- ) {- // const executeFormatter = (type: string): void => {- // const manager = this._sheetPlugin.getSelectionManager();- // const workbook = this._plugin.getContext().getWorkBook();- // const activeSheet = workbook.getActiveSheet();- // const activeRange = manager.getActiveRangeList();- // const cellMatrix = activeSheet.getCellMatrix();- // if (activeRange == null) {- // return;- // }- // // update cell data- // activeRange.getRangeList().forEach((range) => {- // let matrix = new ObjectMatrix<ICellData>();- // Range.foreach(range, (row, col) => {- // const cell = cellMatrix.getValue(row, col);- // if (cell) {- // const formatter = numfmt(type);- // matrix.setValue(row, col, { v: cell.v, m: formatter(cell.v) });- // }- // });- // const setRangeDataAction: ISetRangeDataActionData = {- // sheetId: activeSheet.getSheetId(),- // cellValue: matrix.getData(),- // actionName: CORE_ACTION_NAME.SET_RANGE_DATA_ACTION,- // };- // const newSetRangeDataAction = ActionOperation.make<ISetRangeDataActionData>(setRangeDataAction).removeExtension().getAction();- // const cmd = new Command({ WorkBookUnit: workbook }, newSetRangeDataAction);- // workbook.getCommandManager().invoke(cmd);- // });- // // update numfmt data- // activeRange.getRangeList().forEach((range) => {- // let matrix = new ObjectMatrix();- // Range.foreach(range, (row, col) => {- // matrix.setValue(row, col, type);- // });- // const setNumfmtRangeDataAction = {- // sheetId: activeSheet.getSheetId(),- // actionName: ACTION_NAMES.SET_NUMFMT_RANGE_DATA_ACTION,- // numfmtMatrix: matrix.getData(),- // };- // const cmd = new Command({ WorkBookUnit: workbook }, setNumfmtRangeDataAction);- // cmd.invoke();- // });- // };- super();- const CHILDREN_DATA = DEFAULT_DATA.map((item, index) => ({+/**+ * const CHILDREN_DATA = DEFAULT_DATA.map((item, index) => ({onClick: () => {switch (index) {case 0: {@@ -129,14 +70,17 @@ export class NumfmtController extends Disposable {//executeFormatter('h:mm');break;}+ // eslint-disable-next-line no-magic-numberscase 11: {//executeFormatter('yyyy-mm-dd h:mm AM/PM');break;}+ // eslint-disable-next-line no-magic-numberscase 12: {//executeFormatter('yyyy-mm-dd h:mm');break;}+ // eslint-disable-next-line no-magic-numberscase 13: {break;}@@ -144,48 +88,27 @@ export class NumfmtController extends Disposable {},...item,}));- this._numfmtList = {- name: NUMFMT_PLUGIN_NAME,- type: 0,- label: 'toolbar.moreFormats',- className: styles.customFormat,- show: NumfmtConfig.show,- border: true,- children: [- ...CHILDREN_DATA,- {- label: 'defaultFmt.CustomFormats.text',- suffix: { name: 'RightIcon' },- className: styles.customFormatMore,- children: [- {- label: 'format.moreCurrency',- onClick: () => {- this._numfmtModalController.showModal('currency', true);- },- },- {- label: 'format.moreDateTime',- onClick: () => {- this._numfmtModalController.showModal('date', true);- },- },- {- label: 'format.moreNumber',- onClick: () => {- this._numfmtModalController.showModal('number', true);- },- },- ],- },- ],- };- // TODO@Dushusir remove this after refactoring to new toolbar- // this._sheetContainerUIController.getToolbarController().addToolbarConfig(this._numfmtList);+ */+export class NumfmtController extends Disposable {+ protected _render: BaseComponentRender;++ constructor(+ @Inject(CommandManager) private readonly _commandManager: CommandManager,+ @Inject(ComponentManager) private readonly _componentManager: ComponentManager,+ @Inject(Injector) private readonly _injector: Injector,+ @Inject(INumfmtPluginData) private _numfmtPluginData: NumfmtModel,+ @Inject(ICurrentUniverService) private readonly _currentUniverService: ICurrentUniverService,+ @Inject(NumfmtModalController) private readonly _numfmtModalController: NumfmtModalController,+ @IMenuService private readonly _menuService: IMenuService,+ @ICommandService private readonly _commandService: ICommandService+ ) {+ super();this._componentManager.register(NUMFMT_PLUGIN_NAME + FormatItem.name, FormatItem);this._initializeContextMenu();++ [ShowModalCommand, SetNumfmtRangeDataCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));}getNumfmtBySheetIdConfig(sheetId: string): ObjectMatrixPrimitiveType<string> {diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.tsindex 52a28095e3..ed5f121d31 100644--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtModalController.ts@@ -1,10 +1,11 @@import { ComponentChildren, ComponentManager } from '@univerjs/base-ui';import { LocaleService, ObserverManager } from '@univerjs/core';import { Inject } from '@wendellhu/redi';++import { CURRENCYDETAIL, DATEFMTLISG, NUMBERFORMAT, NUMFMT_PLUGIN_NAME } from '../Basics/Const';+import { INumfmtPluginData } from '../Symbol';import { FormatContent } from '../View/UI/FormatContent';-import { NUMBERFORMAT, NUMFMT_PLUGIN_NAME, CURRENCYDETAIL, DATEFMTLISG } from '../Basics';import { NumfmtModal } from '../View/UI/NumfmtModal';-import { INumfmtPluginData } from '../Symbol';interface GroupProps {locale: string;diff --git a/packages/sheets-plugin-numfmt/src/Controller/menu.ts b/packages/sheets-plugin-numfmt/src/Controller/menu.tsindex 5951d6c9ac..5af546d02d 100644--- a/packages/sheets-plugin-numfmt/src/Controller/menu.ts+++ b/packages/sheets-plugin-numfmt/src/Controller/menu.ts@@ -4,6 +4,8 @@ import { ICommandService, IPermissionService } from '@univerjs/core';import { IAccessor } from '@wendellhu/redi';import { DEFAULT_DATA, MORE_FORMATS_SELECTIONS } from '../Basics/Const/DEFAULT_DATA';+import { SetNumfmtRangeDataCommand } from '../commands/set-numfmt-range-data.command';+import { ShowModalCommand } from '../commands/show-modal.command';export function NumfmtRangeDataMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {const permissionService = accessor.get(IPermissionService);@@ -11,8 +13,7 @@ export function NumfmtRangeDataMenuItemFactory(accessor: IAccessor): IMenuSelectconst selectionManager = accessor.get(ISelectionManager);return {- // TODO replace with real command- id: 'SetNumfmtRangeDataCommand.id',+ id: SetNumfmtRangeDataCommand.id,title: 'toolbar.moreFormats',tooltip: 'toolbar.moreFormats',selectType: SelectTypes.NEO,@@ -64,11 +65,10 @@ export function OpenMoreFormatsModalMenuItemFactory(accessor: IAccessor): IMenuSconst commandService = accessor.get(ICommandService);const selectionManager = accessor.get(ISelectionManager);return {- // TODO@Dushusir 1. replace with real command// 2. suffix- id: 'OpenMoreFormatsModalCommand.id',+ id: ShowModalCommand.id,title: 'defaultFmt.CustomFormats.text',- positions: 'SetNumfmtRangeDataCommand.id',+ positions: SetNumfmtRangeDataCommand.id,display: DisplayTypes.LABEL,selectType: SelectTypes.NEO,type: MenuItemType.SELECTOR,diff --git a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.tsindex 400ae1a70c..28894fd1d3 100644--- a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts+++ b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts@@ -8,6 +8,7 @@ import { INumfmtPluginConfig } from './Interfaces';import en from './Locale/en';import zh from './Locale/zh';import { NumfmtModel } from './Model';+import { NumfmtService } from './services/numfmt.service';import { INumfmtPluginData } from './Symbol';export class NumfmtPlugin extends Plugin<NumfmtPluginObserve> {@@ -82,7 +83,7 @@ export class NumfmtPlugin extends Plugin<NumfmtPluginObserve> {// [NumfmtController],// [NumfmtModalController],// ]);- const dependencies: Dependency[] = [[NumfmtController], [NumfmtModalController]];+ const dependencies: Dependency[] = [[NumfmtController], [NumfmtModalController], [NumfmtService]];dependencies.forEach((d) => {sheetInjector.add(d);});diff --git a/packages/sheets-plugin-numfmt/src/View/UI/FormatItem.tsx b/packages/sheets-plugin-numfmt/src/View/UI/FormatItem.tsxindex b8bf0462fb..bd23e8e62e 100644--- a/packages/sheets-plugin-numfmt/src/View/UI/FormatItem.tsx+++ b/packages/sheets-plugin-numfmt/src/View/UI/FormatItem.tsx@@ -9,7 +9,6 @@ export interface BaseFormatItemProps extends BaseMenuItem {labelText?: string;suffix?: ComponentChildren;border?: boolean;- onValueChange: (v: string | number) => void;}function getLocale(context: Partial<AppContextValues>, name: string) {@@ -20,11 +19,11 @@ function getLocale(context: Partial<AppContextValues>, name: string) {* FormatItem*/export function FormatItem(props: BaseFormatItemProps): JSX.Element {- const { selected, labelText, suffix, disabled, value, onValueChange } = props;+ const { selected, labelText, suffix, disabled, value } = props;const context = useContext(AppContext);return (- <div className={joinClassNames(styles.formatItem, disabled ? styles.selectDisabledItem : '')} onClick={() => onValueChange(value)}>+ <div className={joinClassNames(styles.formatItem, disabled ? styles.selectDisabledItem : '')}>{selected && (<span className={styles.formatItemSelected}><Icon.CorrectIcon />diff --git a/packages/sheets-plugin-numfmt/src/commands/set-numfmt-range-data.command.ts b/packages/sheets-plugin-numfmt/src/commands/set-numfmt-range-data.command.tsnew file mode 100644index 0000000000..fe6f81c298--- /dev/null+++ b/packages/sheets-plugin-numfmt/src/commands/set-numfmt-range-data.command.ts@@ -0,0 +1,16 @@+import { CommandType, ICommand } from '@univerjs/core';+import { IAccessor } from '@wendellhu/redi';++export interface SetNumfmtRangeDataCommandParams {+ value: string;+}++export const SetNumfmtRangeDataCommand: ICommand<SetNumfmtRangeDataCommandParams> = {+ id: 'numfmt.command.set-range-data',+ type: CommandType.COMMAND,+ handler: async (accessor: IAccessor, params: SetNumfmtRangeDataCommandParams) => {+ const { value } = params;+ console.info('set-numfmt-range-data', value);+ return true;+ },+};diff --git a/packages/sheets-plugin-numfmt/src/commands/show-modal.command.ts b/packages/sheets-plugin-numfmt/src/commands/show-modal.command.tsnew file mode 100644index 0000000000..dd39b63306--- /dev/null+++ b/packages/sheets-plugin-numfmt/src/commands/show-modal.command.ts@@ -0,0 +1,20 @@+import { CommandType, ICommand } from '@univerjs/core';+import { IAccessor } from '@wendellhu/redi';++import { NumfmtService } from '../services/numfmt.service';++export interface ShowModalCommandParams {+ value: string;+}++export const ShowModalCommand: ICommand<ShowModalCommandParams> = {+ id: 'numfmt.command.show-modal',+ type: CommandType.OPERATION,+ handler: async (accessor: IAccessor, params: ShowModalCommandParams) => {+ const { value } = params;+ const numfmtService = accessor.get(NumfmtService);+ console.info('numfmt-command-show-modal', value);+ numfmtService.showModal(value);+ return true;+ },+};diff --git a/packages/sheets-plugin-numfmt/src/services/numfmt.service.ts b/packages/sheets-plugin-numfmt/src/services/numfmt.service.tsnew file mode 100644index 0000000000..1440937188--- /dev/null+++ b/packages/sheets-plugin-numfmt/src/services/numfmt.service.ts@@ -0,0 +1,11 @@+import { Inject } from '@wendellhu/redi';++import { NumfmtModalController } from '../Controller/NumfmtModalController';++export class NumfmtService {+ constructor(@Inject(NumfmtModalController) private _numfmtModalController: NumfmtModalController) {}++ showModal(type: string) {+ this._numfmtModalController.showModal(type, true);+ }+}