Skip to content

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.

feat(sheet): support undo and redo in sheet cell (#512)

Ran Luo736384d9a125
diff --git a/packages/base-docs/src/controllers/doc-render.controller.ts b/packages/base-docs/src/controllers/doc-render.controller.ts
index 61f94dbf43..53f012c65e 100644
--- a/packages/base-docs/src/controllers/doc-render.controller.ts
+++ b/packages/base-docs/src/controllers/doc-render.controller.ts
@@ -1,14 +1,8 @@
+import { Documents, DocumentSkeleton, IRender, IRenderManagerService, PageLayoutType } from '@univerjs/base-render';
import {
+ Disposable,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- Documents,
- DocumentSkeleton,
- IRender,
- IRenderManagerService,
- PageLayoutType,
-} from '@univerjs/base-render';
-import {
- Disposable,
ICommandInfo,
ICommandService,
IUniverInstanceService,
diff --git a/packages/base-docs/src/controllers/floating-object.controller.ts b/packages/base-docs/src/controllers/floating-object.controller.ts
index a8d285e950..98def19c18 100644
--- a/packages/base-docs/src/controllers/floating-object.controller.ts
+++ b/packages/base-docs/src/controllers/floating-object.controller.ts
@@ -1,15 +1,9 @@
-import {
- DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
- DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- Documents,
- DocumentSkeleton,
- IRender,
- IRenderManagerService,
- Liquid,
-} from '@univerjs/base-render';
+import { Documents, DocumentSkeleton, IRender, IRenderManagerService, Liquid } from '@univerjs/base-render';
import {
DEFAULT_DOCUMENT_SUB_COMPONENT_ID,
Disposable,
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
ICommandInfo,
ICommandService,
IFloatingObjectManagerParam,
diff --git a/packages/base-docs/src/controllers/page-render.controller.ts b/packages/base-docs/src/controllers/page-render.controller.ts
index 7f426b39a6..a02867af43 100644
--- a/packages/base-docs/src/controllers/page-render.controller.ts
+++ b/packages/base-docs/src/controllers/page-render.controller.ts
@@ -1,12 +1,13 @@
+import { Documents, IPageRenderConfig, IRenderManagerService, Rect } from '@univerjs/base-render';
import {
+ Disposable,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- Documents,
- IPageRenderConfig,
- IRenderManagerService,
- Rect,
-} from '@univerjs/base-render';
-import { Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle } from '@univerjs/core';
+ ICommandService,
+ IUniverInstanceService,
+ LifecycleStages,
+ OnLifecycle,
+} from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
diff --git a/packages/base-docs/src/controllers/zoom.controller.ts b/packages/base-docs/src/controllers/zoom.controller.ts
index 86a162f8a9..d2c5471d00 100644
--- a/packages/base-docs/src/controllers/zoom.controller.ts
+++ b/packages/base-docs/src/controllers/zoom.controller.ts
@@ -1,11 +1,8 @@
+import { IRenderManagerService, IWheelEvent } from '@univerjs/base-render';
import {
+ Disposable,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- IRenderManagerService,
- IWheelEvent,
-} from '@univerjs/base-render';
-import {
- Disposable,
ICommandInfo,
ICommandService,
IUniverInstanceService,
diff --git a/packages/base-render/src/basics/const.ts b/packages/base-render/src/basics/const.ts
index 725df510c1..79600e84d4 100644
--- a/packages/base-render/src/basics/const.ts
+++ b/packages/base-render/src/basics/const.ts
@@ -290,11 +290,6 @@ export enum RENDER_CLASS_TYPE {
GROUP = 'Group',
}
-// FIXME: this const should not be put on base-render but in core
-export const DOCS_NORMAL_EDITOR_UNIT_ID_KEY = '__defaultDocumentNormalEditorSpecialUnitId_20231006__';
-
-export const DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY = '__defaultDocumentFormulaBarEditorSpecialUnitId_20231012__';
-
export const DEFAULT_SELECTION_LAYER_INDEX = 1000;
export const MAXIMUM_ROW_HEIGHT = 2000;
diff --git a/packages/core/src/common/const.ts b/packages/core/src/common/const.ts
new file mode 100644
index 0000000000..93c98a128d
--- /dev/null
+++ b/packages/core/src/common/const.ts
@@ -0,0 +1,3 @@
+export const DOCS_NORMAL_EDITOR_UNIT_ID_KEY = '__defaultDocumentNormalEditorSpecialUnitId_20231006__';
+
+export const DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY = '__defaultDocumentFormulaBarEditorSpecialUnitId_20231012__';
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 8698dae500..29d99d8d48 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -1,5 +1,6 @@
export * from './basics';
export { dedupe, remove } from './common/array';
+export { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY } from './common/const';
export * from './docs/domain';
export * from './observer';
export { Plugin, PluginType } from './plugin/plugin';
diff --git a/packages/core/src/services/instance/instance.service.ts b/packages/core/src/services/instance/instance.service.ts
index 10ee88b342..403c6bb101 100644
--- a/packages/core/src/services/instance/instance.service.ts
+++ b/packages/core/src/services/instance/instance.service.ts
@@ -14,6 +14,7 @@ export const enum DocumentType {
UNKNOWN = 0,
DOC = 1,
SHEET = 2,
+ // eslint-disable-next-line no-magic-numbers
SLIDE = 3,
}
@@ -149,10 +150,12 @@ export class UniverInstanceService extends Disposable implements IUniverInstance
changeDoc(unitId: string, doc: DocumentModel): void {
const oldDoc = this._docs.find((doc) => doc.getUnitId() === unitId);
+
if (oldDoc != null) {
const index = this._docs.indexOf(oldDoc);
this._docs.splice(index, 1);
}
+
this.addDoc(doc);
}
diff --git a/packages/core/src/services/undoredo/undoredo.service.ts b/packages/core/src/services/undoredo/undoredo.service.ts
index 0349c1b23d..7199555e07 100644
--- a/packages/core/src/services/undoredo/undoredo.service.ts
+++ b/packages/core/src/services/undoredo/undoredo.service.ts
@@ -1,8 +1,11 @@
import { createIdentifier, IAccessor, IDisposable } from '@wendellhu/redi';
import { BehaviorSubject, Observable } from 'rxjs';
+import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY } from '../../common/const';
import { Disposable, toDisposable } from '../../shared/lifecycle';
import { CommandType, ICommand, ICommandService, IMutationInfo, sequenceExecute } from '../command/command.service';
+import { FOCUSING_EDITOR, FOCUSING_EDITOR_INPUT_FORMULA } from '../context/context';
+import { IContextService } from '../context/context.service';
import { IUniverInstanceService } from '../instance/instance.service';
// TODO: an undo redo element may be merge-able to another undo redo element
@@ -57,8 +60,9 @@ export class LocalUndoRedoService extends Disposable implements IUndoRedoService
protected readonly _redoStacks = new Map<string, IUndoRedoItem[]>();
constructor(
- @IUniverInstanceService protected readonly _currentUniverSheet: IUniverInstanceService,
- @ICommandService protected readonly _commandService: ICommandService
+ @IUniverInstanceService protected readonly _univerInstanceService: IUniverInstanceService,
+ @ICommandService protected readonly _commandService: ICommandService,
+ @IContextService private readonly _contextService: IContextService
) {
super();
@@ -67,7 +71,7 @@ export class LocalUndoRedoService extends Disposable implements IUndoRedoService
this.disposeWithMe(this._commandService.registerCommand(UndoCommand));
this.disposeWithMe(this._commandService.registerCommand(RedoCommand));
this.disposeWithMe(toDisposable(() => this._undoRedoStatus$.complete()));
- this.disposeWithMe(toDisposable(this._currentUniverSheet.focused$.subscribe(() => this._updateStatus())));
+ this.disposeWithMe(toDisposable(this._univerInstanceService.focused$.subscribe(() => this._updateStatus())));
}
pushUndoRedo(item: IUndoRedoItem): void {
@@ -145,7 +149,7 @@ export class LocalUndoRedoService extends Disposable implements IUndoRedoService
}
protected _updateStatus(): void {
- const unitID = this._currentUniverSheet.getFocusedUniverInstance()?.getUnitId();
+ const unitID = this._getFocusedUniverInstanceId();
const undos = (unitID && this._undoStacks.get(unitID)?.length) || 0;
const redos = (unitID && this._redoStacks.get(unitID)?.length) || 0;
@@ -180,7 +184,8 @@ export class LocalUndoRedoService extends Disposable implements IUndoRedoService
}
protected _getUndoStackForFocused(): IUndoRedoItem[] {
- const unitID = this._currentUniverSheet.getFocusedUniverInstance()?.getUnitId();
+ const unitID = this._getFocusedUniverInstanceId();
+
if (!unitID) {
throw new Error('No focused univer instance!');
}
@@ -189,13 +194,28 @@ export class LocalUndoRedoService extends Disposable implements IUndoRedoService
}
protected _getRedoStackForFocused(): IUndoRedoItem[] {
- const unitID = this._currentUniverSheet.getFocusedUniverInstance()?.getUnitId();
+ const unitID = this._getFocusedUniverInstanceId();
+
if (!unitID) {
throw new Error('No focused univer instance!');
}
return this._getRedoStack(unitID, true);
}
+
+ private _getFocusedUniverInstanceId() {
+ let unitID: string = '';
+
+ if (this._contextService.getContextValue(FOCUSING_EDITOR)) {
+ unitID = DOCS_NORMAL_EDITOR_UNIT_ID_KEY;
+ } else if (this._contextService.getContextValue(FOCUSING_EDITOR_INPUT_FORMULA)) {
+ unitID = DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
+ } else {
+ unitID = this._univerInstanceService.getFocusedUniverInstance()?.getUnitId() ?? '';
+ }
+
+ return unitID;
+ }
}
abstract class MultiImplementationCommand implements IDisposable {
diff --git a/packages/sheets-plugin-formula-ui/src/controllers/prompt.controller.ts b/packages/sheets-plugin-formula-ui/src/controllers/prompt.controller.ts
index 6e6e1b115f..7ac286640f 100644
--- a/packages/sheets-plugin-formula-ui/src/controllers/prompt.controller.ts
+++ b/packages/sheets-plugin-formula-ui/src/controllers/prompt.controller.ts
@@ -9,13 +9,7 @@ import {
normalizeSheetName,
sequenceNodeType,
} from '@univerjs/base-formula-engine';
-import {
- DeviceInputEventType,
- DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
- DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- IRenderManagerService,
- ITextSelectionRenderManager,
-} from '@univerjs/base-render';
+import { DeviceInputEventType, IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/base-render';
import {
convertSelectionDataToRange,
getNormalSelectionStyle,
@@ -29,6 +23,8 @@ import {
Direction,
Disposable,
DisposableCollection,
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
FOCUSING_EDITOR_INPUT_FORMULA,
ICommandInfo,
ICommandService,
diff --git a/packages/ui-plugin-docs/src/doc-ui-plugin.ts b/packages/ui-plugin-docs/src/doc-ui-plugin.ts
index ab649cee3c..96e97ef9eb 100644
--- a/packages/ui-plugin-docs/src/doc-ui-plugin.ts
+++ b/packages/ui-plugin-docs/src/doc-ui-plugin.ts
@@ -1,5 +1,12 @@
-import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY } from '@univerjs/base-render';
-import { IUniverInstanceService, LocaleService, Plugin, PluginType, Tools } from '@univerjs/core';
+import {
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ IUniverInstanceService,
+ LocaleService,
+ Plugin,
+ PluginType,
+ Tools,
+} from '@univerjs/core';
import { Dependency, Inject, Injector } from '@wendellhu/redi';
import { DefaultDocUiConfig, IDocUIPluginConfig } from './basics';
@@ -55,7 +62,7 @@ export class DocUIPlugin extends Plugin {
const doc = currentService.getCurrentUniverDocInstance();
const id = doc.getUnitId();
- if (id !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY && id !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
+ if (id !== DOCS_NORMAL_EDITOR_UNIT_ID_KEY && id !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
currentService.focusUniverInstance(doc.getUnitId());
}
}
diff --git a/packages/ui-plugin-sheets/src/controllers/editor/end-edit.controller.ts b/packages/ui-plugin-sheets/src/controllers/editor/end-edit.controller.ts
index 17221d3302..918d3a3795 100644
--- a/packages/ui-plugin-sheets/src/controllers/editor/end-edit.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/editor/end-edit.controller.ts
@@ -1,18 +1,14 @@
import { MoveCursorOperation } from '@univerjs/base-docs';
import { FormulaEngineService, matchToken } from '@univerjs/base-formula-engine';
-import {
- DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
- DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- IMouseEvent,
- IPointerEvent,
- IRenderManagerService,
-} from '@univerjs/base-render';
+import { IMouseEvent, IPointerEvent, IRenderManagerService } from '@univerjs/base-render';
import { SetRangeValuesCommand } from '@univerjs/base-sheets';
import { KeyCode } from '@univerjs/base-ui';
import {
DEFAULT_EMPTY_DOCUMENT_VALUE,
Direction,
Disposable,
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
DocumentModel,
FOCUSING_EDITOR,
FOCUSING_EDITOR_BUT_HIDDEN,
@@ -22,6 +18,7 @@ import {
ICommandService,
IContextService,
isFormulaString,
+ IUndoRedoService,
IUniverInstanceService,
LifecycleStages,
Nullable,
@@ -65,7 +62,8 @@ export class EndEditController extends Disposable {
@IEditorBridgeService private readonly _editorBridgeService: IEditorBridgeService,
@IContextService private readonly _contextService: IContextService,
@ICellEditorManagerService private readonly _cellEditorManagerService: ICellEditorManagerService,
- @Inject(FormulaEngineService) private readonly _formulaEngineService: FormulaEngineService
+ @Inject(FormulaEngineService) private readonly _formulaEngineService: FormulaEngineService,
+ @IUndoRedoService private _undoRedoService: IUndoRedoService
) {
super();
@@ -200,6 +198,8 @@ export class EndEditController extends Disposable {
this._cellEditorManagerService.setState({
show: param.visible,
});
+ this._undoRedoService.clearUndoRedo(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);
+ this._undoRedoService.clearUndoRedo(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
}
private _moveCursor(keycode?: KeyCode) {
diff --git a/packages/ui-plugin-sheets/src/controllers/editor/initialize-editor.controller.ts b/packages/ui-plugin-sheets/src/controllers/editor/initialize-editor.controller.ts
index 4a983cca3c..88c0a3fcbe 100644
--- a/packages/ui-plugin-sheets/src/controllers/editor/initialize-editor.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/editor/initialize-editor.controller.ts
@@ -1,10 +1,14 @@
import { getDocObject } from '@univerjs/base-docs';
+import { IRenderManagerService } from '@univerjs/base-render';
import {
+ Disposable,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- IRenderManagerService,
-} from '@univerjs/base-render';
-import { Disposable, IUniverInstanceService, LifecycleStages, Nullable, OnLifecycle } from '@univerjs/core';
+ IUniverInstanceService,
+ LifecycleStages,
+ Nullable,
+ OnLifecycle,
+} from '@univerjs/core';
import { Subscription } from 'rxjs';
@OnLifecycle(LifecycleStages.Rendered, InitializeEditorController)
@@ -30,21 +34,17 @@ export class InitializeEditorController extends Disposable {
}
private _initialize() {
+ // create univer doc sheet cell editor instance
this._currentUniverService.createDoc({
id: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
documentStyle: {},
});
- // this._currentUniverService.focusUniverInstance(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
-
// create univer doc formula bar editor instance
-
this._currentUniverService.createDoc({
id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
documentStyle: {},
});
-
- // this._currentUniverService.focusUniverInstance(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);
}
private _commandExecutedListener() {}
diff --git a/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts b/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts
index c54dda9ddd..3da678941b 100644
--- a/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts
@@ -9,8 +9,6 @@ import {
} from '@univerjs/base-docs';
import {
DeviceInputEventType,
- DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
- DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
DocumentSkeleton,
IDocumentLayoutObject,
IEditorInputConfig,
@@ -24,6 +22,8 @@ import { KeyCode } from '@univerjs/base-ui';
import {
DEFAULT_EMPTY_DOCUMENT_VALUE,
Disposable,
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
DocumentModel,
FOCUSING_EDITOR,
FOCUSING_EDITOR_BUT_HIDDEN,
diff --git a/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx b/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx
index 74c7970c3d..5aa50d953e 100644
--- a/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx
+++ b/packages/ui-plugin-sheets/src/views/editor-container/EditorContainer.tsx
@@ -1,4 +1,5 @@
-import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY, IRenderManagerService } from '@univerjs/base-render';
+import { IRenderManagerService } from '@univerjs/base-render';
+import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY } from '@univerjs/core';
import { useDependency } from '@wendellhu/redi/react-bindings';
import { useEffect, useRef, useState } from 'react';
import { switchMap } from 'rxjs/operators';