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
feat(base-sheets): Observe Command to auto update worksheet data and refresh
Dushusirc5d09fac0313
diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.tsindex 59cc6cbc98..3ce96dc32b 100644--- a/packages/base-sheets/src/Controller/CellEditorController.ts+++ b/packages/base-sheets/src/Controller/CellEditorController.ts@@ -44,7 +44,7 @@ export class CellEditorController {this._initialize();}- protected _initialize() {+ private _initialize() {this._plugin.getObserver('onSheetContainerDidMountObservable')?.add((sheetContainer: SheetContainer) => {this._sheetContainer = sheetContainer;const mainItem: IMainProps = {diff --git a/packages/base-sheets/src/Controller/InfoBarController.ts b/packages/base-sheets/src/Controller/InfoBarController.tsindex 4ce4daa0b6..f6f3e905c7 100644--- a/packages/base-sheets/src/Controller/InfoBarController.ts+++ b/packages/base-sheets/src/Controller/InfoBarController.ts@@ -46,10 +46,10 @@ export class InfoBarController {onBlur: this.setSheetName,},};- this.init();+ this._initialize();}- init() {+ private _initialize() {const context = this._plugin.context;const manager = context.getObserverManager();diff --git a/packages/base-sheets/src/Controller/RightMenuController.ts b/packages/base-sheets/src/Controller/RightMenuController.tsindex 9db26e4c46..c7e79dc845 100644--- a/packages/base-sheets/src/Controller/RightMenuController.ts+++ b/packages/base-sheets/src/Controller/RightMenuController.ts@@ -1,5 +1,5 @@import { IMouseEvent, IPointerEvent } from '@univer/base-render';-import { Locale, PLUGIN_NAMES, Worksheet } from '@univer/core';+import { Locale, PLUGIN_NAMES } from '@univer/core';import { RightMenuModel, RightMenuProps } from '../Model/RightMenuModel';import { SpreadsheetPlugin } from '../SpreadsheetPlugin';import Style from '../View/UI/RightMenu/index.module.less';@@ -204,10 +204,10 @@ export class RightMenuController {];this._rightMenuModel = new RightMenuModel();- this._init(this._menuList);+ this._initialize(this._menuList);}- private _init(menuList: RightMenuProps[]) {+ private _initialize(menuList: RightMenuProps[]) {const context = this._plugin.getContext();const manager = context.getObserverManager();@@ -364,10 +364,10 @@ export class RightMenuController {return selections;}- private _render(sheet: Worksheet) {- this._plugin.getCanvasView().updateToSheet(sheet);- this._plugin?.getMainComponent().makeDirty(true);- }+ // private _render(sheet: Worksheet) {+ // this._plugin.getCanvasView().updateToSheet(sheet);+ // this._plugin?.getMainComponent().makeDirty(true);+ // }insertRow = (...arg: any) => {arg[1].ref.hideSelect();@@ -376,7 +376,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet?.insertRowBefore(selections[0].startRow, selections[0].endRow - selections[0].startRow + 1);- this._render(sheet);+ // this._render(sheet);}};@@ -387,7 +387,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet?.deleteRows(selections[0].startRow, selections[0].endRow - selections[0].startRow + 1);- this._render(sheet);+ // this._render(sheet);}};@@ -398,7 +398,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet.insertColumnBefore(selections[0].startColumn, selections[0].endColumn - selections[0].startColumn + 1);- this._render(sheet);+ // this._render(sheet);}};@@ -409,7 +409,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet.deleteColumns(selections[0].startColumn, selections[0].endColumn - selections[0].startColumn + 1);- this._render(sheet);+ // this._render(sheet);}};@@ -420,7 +420,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet.getRange(selections[0]).clear();- this._render(sheet);+ // this._render(sheet);}};@@ -432,7 +432,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet.getRange(selections[0]).deleteCells(0);- this._render(sheet);+ // this._render(sheet);}};@@ -444,7 +444,7 @@ export class RightMenuController {if (selections?.length === 1) {const sheet = this._plugin.getContext().getWorkBook().getActiveSheet();sheet.getRange(selections[0]).deleteCells(1);- this._render(sheet);+ // this._render(sheet);}};diff --git a/packages/base-sheets/src/Controller/Selection/SelectionController.ts b/packages/base-sheets/src/Controller/Selection/SelectionController.tsindex f065f4a349..34568c056d 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionController.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionController.ts@@ -1,4 +1,4 @@-import { Rect, Group } from '@univer/base-render';+import { Rect, Group, Spreadsheet } from '@univer/base-render';import { Nullable, ISelection, ICellInfo } from '@univer/core';import { SelectionModel } from '../../Model/SelectionModel';import { SelectionControlDragAndDrop } from './SelectionControlDragDrop';@@ -42,6 +42,8 @@ enum SELECTION_MANAGER_KEY {}export class SelectionControl {+ private _mainComponent: Spreadsheet;+private _leftControl: Rect;private _rightControl: Rect;@@ -275,7 +277,43 @@ export class SelectionControl {this._updateControl();}+ /**+ *When switching to the current sheet+ *+ * 1. Reinitialize the rendering component+ * 2. Calculate the position based on the current skeleton+ * 3. Update data+ * 4. Trigger rendering+ */render() {+ this._initialize();++ let cellInfo = null;+ const main = this._manager.getMainComponent();+ const curCellRange = this._selectionModel.currentCell;++ if (curCellRange) {+ cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);+ }++ const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = this._selectionModel;+ const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);+ const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);++ this._selectionModel.setValue(+ {+ startColumn: finalStartColumn,+ startRow: finalStartRow,+ endColumn: finalEndColumn,+ endRow: finalEndRow,+ startY: startCell?.startY || 0,+ endY: endCell?.endY || 0,+ startX: startCell?.startX || 0,+ endX: endCell?.endX || 0,+ },+ cellInfo+ );+this._updateControl();}diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.tsindex bb132da6b8..8898aaa1b6 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts@@ -44,6 +44,10 @@ export class SelectionManager {return this._sheetView.getContext();}+ getMainComponent() {+ return this._mainComponent;+ }+updateToSheet(worksheet: Worksheet) {this._worksheet = worksheet;const worksheetId = this.getWorksheetId();@@ -147,6 +151,7 @@ export class SelectionManager {constructor(private _sheetView: SheetView) {this._plugin = this._sheetView.getPlugin() as SpreadsheetPlugin;this._initialize();+ this._initializeObserver();}private _initialize() {@@ -405,6 +410,17 @@ export class SelectionManager {});}+ /**+ * Initialize the observer+ */+ private _initializeObserver() {+ const context = this._plugin.getContext();+ context.getContextObserver('onAfterChangeActiveSheetObservable').add(() => {+ // this._plugin.getCanvasView().updateToSheet(this._plugin.getContext().getWorkBook().getActiveSheet()!);+ this._plugin.getSelectionManager().renderCurrentControls();+ });+ }+/*** Returns the list of active ranges in the active sheet or null if there are no active ranges.* If there is a single range selected, this behaves as a getActiveRange() call.diff --git a/packages/base-sheets/src/Controller/SheetBarController.ts b/packages/base-sheets/src/Controller/SheetBarController.tsindex a55870b769..5f4a83dc90 100644--- a/packages/base-sheets/src/Controller/SheetBarController.ts+++ b/packages/base-sheets/src/Controller/SheetBarController.ts@@ -76,8 +76,8 @@ class SheetBarUIController {onClick: () => barControl.moveSheet('right'),},];- this.initializeData();- this.initializeObserver();+ this._initializeData();+ this._initializeObserver();}/**@@ -105,7 +105,7 @@ class SheetBarUIController {/*** 初始化sheet ui*/- initializeObserver() {+ private _initializeObserver() {const plugin = this._barControl.getPlugin();const context = plugin.getContext();const manager = context.getObserverManager();@@ -125,8 +125,6 @@ class SheetBarUIController {const sheet = plugin.getContext().getWorkBook().getSheetBySheetId(this._dataId);if (sheet) {sheet.activate();- plugin.getCanvasView().updateToSheet(sheet);- // plugin.getMainComponent().makeDirty(true);}},contextMenu: (e: MouseEvent) => {@@ -150,7 +148,7 @@ class SheetBarUIController {});// context.getContextObserver('onAfterInsertSheetObservable').add(() => {// // update data- // this.initializeData();+ // this._initializeData();// this._sheetBar.setValue({// sheetList: this._sheetList,// menuList: this._menuList,@@ -158,7 +156,7 @@ class SheetBarUIController {// });// context.getContextObserver('onSheetTabColorChangeObservable').add(() => {// // update data- // this.initializeData();+ // this._initializeData();// this._sheetBar.setValue({// sheetList: this._sheetList,// menuList: this._menuList,@@ -168,7 +166,7 @@ class SheetBarUIController {// const activeSheet = plugin.getContext().getWorkBook().getActiveSheet();// if (activeSheet) {// // update data;- // this.initializeData();+ // this._initializeData();// // set ui bar sheetList;// this._sheetBar.setValue({// sheetList: this._sheetList,@@ -181,7 +179,7 @@ class SheetBarUIController {// });// context.getContextObserver('onAfterChangeSheetNameObservable').add(() => {// // update data;- // this.initializeData();+ // this._initializeData();// // set ui bar sheetList;// this._sheetBar.setValue({// sheetList: this._sheetList,@@ -190,7 +188,7 @@ class SheetBarUIController {// });// context.getContextObserver('onAfterChangeActiveSheetObservable').add(() => {// // update data;- // this.initializeData();+ // this._initializeData();// // set ui bar sheetList;// this._sheetBar.setValue({// sheetList: this._sheetList,@@ -199,7 +197,7 @@ class SheetBarUIController {// });// context.getContextObserver('onShowSheetObservable').add(() => {// // update data;- // this.initializeData();+ // this._initializeData();// // set ui bar sheetList;// this._sheetBar.setValue({// sheetList: this._sheetList,@@ -208,7 +206,7 @@ class SheetBarUIController {// });// context.getContextObserver('onSheetOrderObservable').add(() => {// // update data;- // this.initializeData();+ // this._initializeData();// // set ui bar sheetList;// this._sheetBar.setValue({// sheetList: this._sheetList,@@ -217,7 +215,7 @@ class SheetBarUIController {// });// context.getContextObserver('onHideSheetObservable').add(() => {// // update data;- // this.initializeData();+ // this._initializeData();// // set ui bar sheetList;// this._sheetBar.setValue({// sheetList: this._sheetList,@@ -236,7 +234,7 @@ class SheetBarUIController {case ACTION_NAMES.SET_WORKSHEET_ACTIVATE_ACTION:case ACTION_NAMES.SET_WORKSHEET_STATUS_ACTION: {// update data;- this.initializeData();+ this._initializeData();// set ui bar sheetList;this._sheetBar.setValue({sheetList: this._sheetList,@@ -248,7 +246,7 @@ class SheetBarUIController {});}- initializeData() {+ private _initializeData() {const plugin = this._barControl.getPlugin();const workbook = plugin.getWorkbook();const sheets = workbook.getSheets();@@ -263,8 +261,6 @@ class SheetBarUIController {this._dataId = target.dataset.id as string;sheet.showSheet();sheet.activate();- plugin.getCanvasView().updateToSheet(plugin.getContext().getWorkBook().getActiveSheet()!);- // plugin.getMainComponent().makeDirty(true);},}));this._sheetList = sheets@@ -283,14 +279,18 @@ class SheetBarUIController {onClick: (e: MouseEvent) => {const target = e.currentTarget as HTMLDivElement;this._dataId = target.dataset.id as string;+sheet.activate();- plugin.getSelectionManager().renderCurrentControls();+// // 清空选区// const controls = plugin.getSelectionManager().getCurrentControls();// for (let control of controls!) {// control.dispose();// }- plugin.getCanvasView().updateToSheet(plugin.getContext().getWorkBook().getActiveSheet()!);+ // plugin.getCanvasView().updateToSheet(plugin.getContext().getWorkBook().getActiveSheet()!);++ // plugin.getSelectionManager().renderCurrentControls();+// // plugin.getMainComponent().makeDirty(true);},}));diff --git a/packages/base-sheets/src/Controller/SheetContainerController.ts b/packages/base-sheets/src/Controller/SheetContainerController.tsindex 2becc8a274..4560fb1ffc 100644--- a/packages/base-sheets/src/Controller/SheetContainerController.ts+++ b/packages/base-sheets/src/Controller/SheetContainerController.ts@@ -1,31 +1,25 @@-import { CommandManager } from '@univer/core';+import { BooleanNumber, CommandManager } from '@univer/core';import { SpreadsheetPlugin } from '../SpreadsheetPlugin';-import { SheetContainer } from '../View/UI/SheetContainer';export class SheetContainerController {private _plugin: SpreadsheetPlugin;- private _sheetContainer: SheetContainer;-- private _sheetContainerContent: HTMLElement;-- // private __debounceTimeout: number;-constructor(plugin: SpreadsheetPlugin) {this._plugin = plugin;this._initialize();}- protected _initialize() {+ private _initialize() {// Monitor all command changes and automatically trigger the refresh of the canvas- CommandManager.getActionObservers().add((actionEvent) => {- //In order not to affect the refresh experience and remove the delay, multiple refreshes are acceptable if there is no performance problem-- // window.clearTimeout(this.__debounceTimeout);- // this.__debounceTimeout = window.setTimeout(() => {- this._plugin.getMainComponent().makeDirty(true);- // }, 10);+ CommandManager.getCommandObservers().add(({ actions }) => {+ const worksheet = actions[0].getWorkSheet();++ // Only the currently active worksheet needs to be refreshed+ if (worksheet.getConfig().status === BooleanNumber.TRUE) {+ this._plugin.getCanvasView().updateToSheet(worksheet);+ this._plugin.getMainComponent().makeDirty(true);+ }});}}diff --git a/packages/base-sheets/src/Controller/ToolBarController.ts b/packages/base-sheets/src/Controller/ToolBarController.tsindex 74bfff9d29..2417aedd17 100644--- a/packages/base-sheets/src/Controller/ToolBarController.ts+++ b/packages/base-sheets/src/Controller/ToolBarController.ts@@ -371,10 +371,10 @@ export class ToolBarController {this._toolBarModel.config = config;this._toolBarModel.toolList = toolList;- this.init();+ this._initialize();}- init() {+ private _initialize() {// this._plugin.sheetContainerConfig.toolList = [];// this._plugin.sheetContainerConfig.toolList = this._toolBarModel.toolList;@@ -569,7 +569,7 @@ export class ToolBarController {}// update data- this._plugin.getCanvasView().updateToSheet(this._plugin.getContext().getWorkBook().getActiveSheet()!);+ // this._plugin.getCanvasView().updateToSheet(this._plugin.getContext().getWorkBook().getActiveSheet()!);// update render// this._plugin.getMainComponent().makeDirty(true);}@@ -620,7 +620,7 @@ export class ToolBarController {});// update data- this._plugin.getCanvasView().updateToSheet(this._plugin.getContext().getWorkBook().getActiveSheet()!);+ // this._plugin.getCanvasView().updateToSheet(this._plugin.getContext().getWorkBook().getActiveSheet()!);// update render// this._plugin.getMainComponent().makeDirty(true);}diff --git a/packages/base-sheets/src/SpreadsheetPlugin.tsx b/packages/base-sheets/src/SpreadsheetPlugin.tsxindex 737afa30fa..45a74a93b5 100644--- a/packages/base-sheets/src/SpreadsheetPlugin.tsx+++ b/packages/base-sheets/src/SpreadsheetPlugin.tsx@@ -46,8 +46,16 @@ const DEFAULT_SPREADSHEET_PLUGIN_DATA: ISpreadsheetPluginConfig = {startColumn: 1,endColumn: 2,},+ },+ {+ selection: {+ startRow: 16,+ endRow: 18,+ startColumn: 1,+ endColumn: 2,+ },cell: {- row: 13,+ row: 16,column: 1,},},@@ -56,13 +64,29 @@ const DEFAULT_SPREADSHEET_PLUGIN_DATA: ISpreadsheetPluginConfig = {{selection: {startRow: 17,- endRow: 19,+ endRow: 20,startColumn: 1,endColumn: 2,},+ },+ {+ selection: {+ startRow: 22,+ endRow: 23,+ startColumn: 1,+ endColumn: 2,+ },+ },+ {+ selection: {+ startRow: 25,+ endRow: 27,+ startColumn: 4,+ endColumn: 6,+ },cell: {- row: 17,- column: 1,+ row: 25,+ column: 4,},},],@@ -387,17 +411,6 @@ export class SpreadsheetPlugin extends Plugin<SpreadsheetPluginObserve> {return this._showMainByNameFunc(name, show);}- changeWorksheet(sheetId: string) {- const workbook = this.getWorkbook();- const worksheet = workbook.getSheetBySheetId(sheetId);- if (!worksheet) {- return;- }- this.getCanvasView().updateToSheet(worksheet);-- this.getObserver('onChangeCurrentSheetObserver')?.notifyObservers(sheetId);- }-addRightMenu(item: RightMenuProps[] | RightMenuProps) {this._rightMenuControl.addItem(item);}diff --git a/packages/base-sheets/src/View/Render/CanvasView.ts b/packages/base-sheets/src/View/Render/CanvasView.tsindex 7cd404c207..ddd7d184ac 100644--- a/packages/base-sheets/src/View/Render/CanvasView.ts+++ b/packages/base-sheets/src/View/Render/CanvasView.ts@@ -85,7 +85,7 @@ export class CanvasView {const plugin = this._plugin as SpreadsheetPlugin;this._scene.scale(value.zoomRatio, value.zoomRatio);// this._scene.makeDirty();- // update data+ // update data TODO 增加action后,会自动刷新,此处需要移除plugin.getCanvasView().updateToSheet(this._plugin.getContext().getWorkBook().getActiveSheet()!);// update render// plugin.getMainComponent().makeDirty(true);diff --git a/packages/core/src/Command/CommandBase.ts b/packages/core/src/Command/CommandBase.tsindex 8c88e19698..abee671eb3 100644--- a/packages/core/src/Command/CommandBase.ts+++ b/packages/core/src/Command/CommandBase.ts@@ -3,7 +3,7 @@ import { Workbook } from '../Sheets/Domain';import { CommandManager } from './CommandManager';import { CommandInjector } from './CommandInjectorObservers';import { Class, Nullable } from '../Shared';-import { ActionType } from './ActionObservers';+// import { ActionType } from './ActionObservers';import { ActionExtensionManager } from './ActionExtensionManager';/**@@ -28,10 +28,10 @@ export class CommandBase {const action = new ActionClass(data, this._workbook, observers);this._actions.push(action);});- CommandManager.getCommandObservers().notifyObservers({- type: ActionType.REDO,- actions: this._actions,- });+ // CommandManager.getCommandObservers().notifyObservers({+ // type: ActionType.REDO,+ // actions: this._actions,+ // });}getDoData(): IActionData[] {diff --git a/packages/sheets-plugin-clipboard/src/Domain/Paste.ts b/packages/sheets-plugin-clipboard/src/Domain/Paste.tsindex a8199db591..81d927fac5 100644--- a/packages/sheets-plugin-clipboard/src/Domain/Paste.ts+++ b/packages/sheets-plugin-clipboard/src/Domain/Paste.ts@@ -224,8 +224,8 @@ export class UniverPaste extends Paste {}sheet.getRange(minH, minC, maxH, maxC).setRangeDatas(data);- spreadSheetPlugin.getCanvasView().updateToSheet(spreadSheetPlugin.getContext().getWorkBook().getActiveSheet()!);- spreadsheet.makeDirty(true);+ // spreadSheetPlugin.getCanvasView().updateToSheet(spreadSheetPlugin.getContext().getWorkBook().getActiveSheet()!);+ // spreadsheet.makeDirty(true);}paste(e: ClipboardEvent) {