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
chore(core): command set undo redo
Dushusir939080e7a952
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.tsindex 80c3db0b45..c4e7d3fb34 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts@@ -30,6 +30,8 @@ import { SelectionControl, SELECTION_TYPE } from './SelectionController';* SelectionManager 维护model数据list,action也是修改这一层数据,obs监听到数据变动后,自动刷新(control仍然可以持有数据)*/export class SelectionManager {+ hasSelection: boolean = false;+private _mainComponent: Spreadsheet;private _rowComponent: SpreadsheetRowTitle;@@ -62,7 +64,11 @@ export class SelectionManager {private _dragLineControl: DragLineController;- hasSelection: boolean = false;+ constructor(private _sheetView: SheetView) {+ this._plugin = this._sheetView.getPlugin() as SheetPlugin;+ this._initialize();+ this._initializeObserver();+ }getSheetView() {return this._sheetView;@@ -405,25 +411,192 @@ export class SelectionManager {break;}+ const cellInfo = this._mainComponent.getCellByIndex(row, column);++ const selectionData = makeCellToSelection(cellInfo);++ if (!selectionData) return;++ const { startRow, endRow, startColumn, endColumn } = selectionData;+let rangeData: IRangeData = {- startRow: row,- endRow: row,- startColumn: column,- endColumn: column,+ startRow,+ endRow,+ startColumn,+ endColumn,};let currentCellData: IRangeCellData = {- row,- column,+ row: startRow,+ column: startColumn,};this.clearSelectionControls();this.addControlToCurrentByRangeData(rangeData, currentCellData);}- constructor(private _sheetView: SheetView) {- this._plugin = this._sheetView.getPlugin() as SheetPlugin;- this._initialize();- this._initializeObserver();+ /**+ * When mousedown and mouseup need to go to the coordination and undo stack, when mousemove does not need to go to the coordination and undo stack+ * @param moveEvt+ * @param selectionControl+ * @returns+ */+ moving(moveEvt: IPointerEvent | IMouseEvent, selectionControl: Nullable<SelectionControl>) {+ // console.log('moving');+ const main = this._mainComponent;+ const { offsetX: moveOffsetX, offsetY: moveOffsetY, clientX, clientY } = moveEvt;+ const { startRow, startColumn, endRow, endColumn } = this._startSelectionRange;++ const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);+ const moveCellInfo = main.calculateCellIndexByPosition(moveOffsetX, moveOffsetY, scrollXY);+ const moveActualSelection = makeCellToSelection(moveCellInfo);++ if (!moveActualSelection) {+ return false;+ }+ const { startRow: moveStartRow, startColumn: moveStartColumn, endColumn: moveEndColumn, endRow: moveEndRow } = moveActualSelection;++ const newStartRow = Math.min(moveStartRow, startRow);+ const newStartColumn = Math.min(moveStartColumn, startColumn);+ const newEndRow = Math.max(moveEndRow, endRow);+ const newEndColumn = Math.max(moveEndColumn, endColumn);+ const newBounding = main.getSelectionBounding(newStartRow, newStartColumn, newEndRow, newEndColumn);++ if (!newBounding) {+ return false;+ }+ const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = newBounding;++ const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);+ const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);++ const newSelectionRange: ISelection = {+ startColumn: finalStartColumn,+ startRow: finalStartRow,+ endColumn: finalEndColumn,+ endRow: finalEndRow,+ startY: startCell?.startY || 0,+ endY: endCell?.endY || 0,+ startX: startCell?.startX || 0,+ endX: endCell?.endX || 0,+ };+ // Only notify when the selection changes+ const {+ startRow: oldStartRow,+ endRow: oldEndRow,+ startColumn: oldStartColumn,+ endColumn: oldEndColumn,+ } = selectionControl?.model || { startRow: -1, endRow: -1, startColumn: -1, endColumn: -1 };++ if (oldStartColumn !== finalStartColumn || oldStartRow !== finalStartRow || oldEndColumn !== finalEndColumn || oldEndRow !== finalEndRow) {+ selectionControl && selectionControl.update(newSelectionRange);++ // update model+ // this.setSelectionModel();+ selectionControl && this._plugin.getObserver('onChangeSelectionObserver')?.notifyObservers(selectionControl);+ }+ }++ /**+ * pointer up event+ * @param moveEvt+ * @param selectionControl+ * @returns+ */+ up() {+ // update model+ this.setSelectionModel();+ }++ /**+ * Get all range data of the current selection+ * @returns+ */+ getActiveRangeListData(): Nullable<IRangeData[]> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ const selections = models?.map((model: SelectionModel) => ({+ startRow: model.startRow,+ startColumn: model.startColumn,+ endRow: model.endRow,+ endColumn: model.endColumn,+ }));++ return selections;+ }+ }++ /**+ * 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.+ *+ * @returns+ */+ getActiveRangeList(): Nullable<RangeList> {+ const rangeListData = this.getActiveRangeListData();+ return rangeListData && this._worksheet?.getRangeList(rangeListData);+ }++ /**+ * Get the range of the currently active cell+ * @returns+ */+ getActiveRangeData(): Nullable<IRangeData> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ for (const model of models) {+ if (model.currentCell) {+ return {+ startRow: model.startRow,+ startColumn: model.startColumn,+ endRow: model.endRow,+ endColumn: model.endColumn,+ };+ }+ }+ }+ }++ /**+ * Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.+ * @returns+ */+ getActiveRange(): Nullable<Range> {+ const rangeData = this.getActiveRangeData();+ return rangeData && this._worksheet?.getRange(rangeData);+ }++ /**+ * get active selection control+ * @returns+ */+ getCurrentCellData(): Nullable<IRangeData> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ for (const model of models) {+ const currentCell = model.currentCell;+ if (currentCell) {+ return {+ startRow: currentCell.row,+ startColumn: currentCell.column,+ endRow: currentCell.row,+ endColumn: currentCell.column,+ };+ }+ }+ }+ }++ /**+ * get active selection control+ * @returns+ */+ getCurrentCell(): Nullable<Range> {+ const rangeData = this.getCurrentCellData();+ return rangeData && this._worksheet?.getRange(rangeData);+ }++ getDragLineControl() {+ return this._dragLineControl;}private _initialize() {@@ -550,7 +723,7 @@ export class SelectionManager {scrollTimer.startScroll(evtOffsetX, evtOffsetY);// update model- this.setSelectionModel();+ // this.setSelectionModel();// Notification toolbar updates button state and valuethis._plugin.getObserver('onChangeSelectionObserver')?.notifyObservers(selectionControl);@@ -584,78 +757,6 @@ export class SelectionManager {});}- /**- * When mousedown and mouseup need to go to the coordination and undo stack, when mousemove does not need to go to the coordination and undo stack- * @param moveEvt- * @param selectionControl- * @returns- */- moving(moveEvt: IPointerEvent | IMouseEvent, selectionControl: Nullable<SelectionControl>) {- // console.log('moving');- const main = this._mainComponent;- const { offsetX: moveOffsetX, offsetY: moveOffsetY, clientX, clientY } = moveEvt;- const { startRow, startColumn, endRow, endColumn } = this._startSelectionRange;-- const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);- const moveCellInfo = main.calculateCellIndexByPosition(moveOffsetX, moveOffsetY, scrollXY);- const moveActualSelection = makeCellToSelection(moveCellInfo);-- if (!moveActualSelection) {- return false;- }- const { startRow: moveStartRow, startColumn: moveStartColumn, endColumn: moveEndColumn, endRow: moveEndRow } = moveActualSelection;-- const newStartRow = Math.min(moveStartRow, startRow);- const newStartColumn = Math.min(moveStartColumn, startColumn);- const newEndRow = Math.max(moveEndRow, endRow);- const newEndColumn = Math.max(moveEndColumn, endColumn);- const newBounding = main.getSelectionBounding(newStartRow, newStartColumn, newEndRow, newEndColumn);-- if (!newBounding) {- return false;- }- const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = newBounding;-- const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);- const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);-- const newSelectionRange: ISelection = {- startColumn: finalStartColumn,- startRow: finalStartRow,- endColumn: finalEndColumn,- endRow: finalEndRow,- startY: startCell?.startY || 0,- endY: endCell?.endY || 0,- startX: startCell?.startX || 0,- endX: endCell?.endX || 0,- };- // Only notify when the selection changes- const {- startRow: oldStartRow,- endRow: oldEndRow,- startColumn: oldStartColumn,- endColumn: oldEndColumn,- } = selectionControl?.model || { startRow: -1, endRow: -1, startColumn: -1, endColumn: -1 };-- if (oldStartColumn !== finalStartColumn || oldStartRow !== finalStartRow || oldEndColumn !== finalEndColumn || oldEndRow !== finalEndRow) {- selectionControl && selectionControl.update(newSelectionRange);- // update model- this.setSelectionModel();- selectionControl && this._plugin.getObserver('onChangeSelectionObserver')?.notifyObservers(selectionControl);- }- }-- /**- * pointer up event- * @param moveEvt- * @param selectionControl- * @returns- */- up() {- // update model- this.setSelectionModel();- }-private _rowEventInitial() {const row = this._rowComponent;row.onPointerEnterObserver.add((evt: IPointerEvent | IMouseEvent) => {@@ -801,96 +902,4 @@ export class SelectionManager {this.renderCurrentControls();});}-- /**- * Get all range data of the current selection- * @returns- */- getActiveRangeListData(): Nullable<IRangeData[]> {- const models = this.getCurrentModels();- if (models && models.length > 0) {- const selections = models?.map((model: SelectionModel) => ({- startRow: model.startRow,- startColumn: model.startColumn,- endRow: model.endRow,- endColumn: model.endColumn,- }));-- return selections;- }- }-- /**- * 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.- *- * @returns- */- getActiveRangeList(): Nullable<RangeList> {- const rangeListData = this.getActiveRangeListData();- return rangeListData && this._worksheet?.getRangeList(rangeListData);- }-- /**- * Get the range of the currently active cell- * @returns- */- getActiveRangeData(): Nullable<IRangeData> {- const models = this.getCurrentModels();- if (models && models.length > 0) {- for (const model of models) {- if (model.currentCell) {- return {- startRow: model.startRow,- startColumn: model.startColumn,- endRow: model.endRow,- endColumn: model.endColumn,- };- }- }- }- }-- /**- * Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.- * @returns- */- getActiveRange(): Nullable<Range> {- const rangeData = this.getActiveRangeData();- return rangeData && this._worksheet?.getRange(rangeData);- }-- /**- * get active selection control- * @returns- */- getCurrentCellData(): Nullable<IRangeData> {- const models = this.getCurrentModels();- if (models && models.length > 0) {- for (const model of models) {- const currentCell = model.currentCell;- if (currentCell) {- return {- startRow: currentCell.row,- startColumn: currentCell.column,- endRow: currentCell.row,- endColumn: currentCell.column,- };- }- }- }- }-- /**- * get active selection control- * @returns- */- getCurrentCell(): Nullable<Range> {- const rangeData = this.getCurrentCellData();- return rangeData && this._worksheet?.getRange(rangeData);- }-- getDragLineControl() {- return this._dragLineControl;- }}diff --git a/packages/core/src/Command/ActionBase.ts b/packages/core/src/Command/ActionBase.tsindex f32e0fff5d..b598a21492 100644--- a/packages/core/src/Command/ActionBase.ts+++ b/packages/core/src/Command/ActionBase.ts@@ -67,7 +67,7 @@ export abstract class ActionBase<return this._doActionData;}- getOldActionDaa() {+ getOldActionData() {return this._oldActionData;}diff --git a/packages/core/src/Command/Command.ts b/packages/core/src/Command/Command.tsindex 82a6ae2241..edb56be9d1 100644--- a/packages/core/src/Command/Command.ts+++ b/packages/core/src/Command/Command.ts@@ -6,6 +6,7 @@ import {ActionType,CommandInjector,CommandManager,+ ActionOperation,} from './index';import { DocumentModel } from '../Docs/Domain/DocumentModel';@@ -38,7 +39,7 @@ export class Command {}getOldData(): IActionData[] {- return this._actionList.map((action) => action.getOldActionDaa());+ return this._actionList.map((action) => action.getOldActionData());}getInjector(): CommandInjector {@@ -64,7 +65,11 @@ export class Command {}redo(): void {- this._actionList.forEach((action) => action.redo());+ this._actionList.forEach((action) => {+ if (ActionOperation.hasUndo(action.getDoActionData())) {+ action.redo();+ }+ });CommandManager.getCommandObservers().notifyObservers({type: ActionType.REDO,actions: this._actionList,@@ -76,7 +81,11 @@ export class Command {// 1. removeColumn C:E 2.insertColumnData A,// when undo, it should be// 1. removeColumn A, 2. insertColumnData C:E- this._actionList.reverse().forEach((action) => action.undo());+ this._actionList.reverse().forEach((action) => {+ if (ActionOperation.hasUndo(action.getOldActionData())) {+ action.undo();+ }+ });CommandManager.getCommandObservers().notifyObservers({type: ActionType.UNDO,actions: this._actionList,diff --git a/packages/core/src/Command/CommandManager.ts b/packages/core/src/Command/CommandManager.tsindex cd1c17bea9..338578a583 100644--- a/packages/core/src/Command/CommandManager.ts+++ b/packages/core/src/Command/CommandManager.ts@@ -6,8 +6,8 @@ import {ActionObservers,CommandObservers,CommandInjectorObservers,- ActionOperation,Command,+ ActionOperation,} from './index';import { Class } from '../Shared';import { ContextBase } from '../Basics';@@ -89,18 +89,20 @@ export class CommandManager {const { _actionDataList, _unit, _actionList } = command;// const server = _workbook.getServer();this._actionExtensionManager.handle(_actionDataList);+_actionDataList.forEach((data) => {const ActionClass = CommandManager.getAction(data.actionName);const observers = CommandManager.getActionObservers();const action = new ActionClass(data, _unit, observers);- if (ActionOperation.hasUndo(data)) {- _actionList.push(action);- }+ // if (ActionOperation.hasUndo(data)) {+ _actionList.push(action);++ // }});command.invoke();- if (_actionList.length === 0) {- return;- }+ // if (_actionList.length === 0) {+ // return;+ // }_undoManager.push(command);// server.pushMessageQueue(command.getDoData());}diff --git a/packages/core/src/Docs/Action/DeleteTextAction.ts b/packages/core/src/Docs/Action/DeleteTextAction.tsindex f74cf6658c..6035d08f78 100644--- a/packages/core/src/Docs/Action/DeleteTextAction.ts+++ b/packages/core/src/Docs/Action/DeleteTextAction.ts@@ -8,7 +8,7 @@ import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';export interface IDeleteTextActionDataextends IDocActionData,- ITextSelectionRangeParam {+ ITextSelectionRangeParam {text: string;}@@ -63,7 +63,7 @@ export class DeleteTextAction extends DocActionBase<undo(): void {// TODO ...- const actionData = this.getOldActionDaa();+ const actionData = this.getOldActionData();const document = this.getDocument();// const { text, start, length } = actionData;InsertTextApply(document, { ...actionData });diff --git a/packages/core/src/Docs/Action/InsertTextAction.ts b/packages/core/src/Docs/Action/InsertTextAction.tsindex b7e700e952..60ee96c5e3 100644--- a/packages/core/src/Docs/Action/InsertTextAction.ts+++ b/packages/core/src/Docs/Action/InsertTextAction.ts@@ -54,7 +54,7 @@ export class InsertTextAction extends DocActionBase<}undo(): void {- const actionData = this.getOldActionDaa();+ const actionData = this.getOldActionData();const document = this.getDocument();// const { length, start, segmentId } = actionData;DeleteTextApply(document, { ...actionData });diff --git a/packages/core/src/Docs/Action/TextIndexAdjustAction.ts b/packages/core/src/Docs/Action/TextIndexAdjustAction.tsindex a10d055ff9..d699586f47 100644--- a/packages/core/src/Docs/Action/TextIndexAdjustAction.ts+++ b/packages/core/src/Docs/Action/TextIndexAdjustAction.ts@@ -5,7 +5,7 @@ import { textIndexAdjustApply } from '../Apply/TextIndexAdjustApply';export interface ITextIndexAdjustActionextends IDocActionData,- ITextSelectionRangeParam {}+ ITextSelectionRangeParam { }export class TextIndexAdjustAction extends DocActionBase<ITextIndexAdjustAction,@@ -39,7 +39,7 @@ export class TextIndexAdjustAction extends DocActionBase<}undo(): void {- const actionData = this.getOldActionDaa();+ const actionData = this.getOldActionData();const document = this.getDocument();textIndexAdjustApply(document, actionData);diff --git a/packages/core/src/Sheets/Action/SetRangeDataAction.ts b/packages/core/src/Sheets/Action/SetRangeDataAction.tsindex 8ee12fc647..f8d6fae517 100644--- a/packages/core/src/Sheets/Action/SetRangeDataAction.ts+++ b/packages/core/src/Sheets/Action/SetRangeDataAction.ts@@ -10,12 +10,25 @@ import {} from '../../Command';import { ICopyToOptionsData, ICellData } from '../../Interfaces';+/**+ * 设置数据时的类型+ */+enum SetRangeDataType {+ DEFAULT = 'default',++ /**+ *+ */+ PASTE = 'paste',+}+/*** @internal*/export interface ISetRangeDataActionData extends ISheetActionData {cellValue: ObjectMatrixPrimitiveType<ICellData>;options?: ICopyToOptionsData;+ type?: SetRangeDataType;}/**