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.

fix(docs-ui): skip undo redo while editing sheet formulas (#7141)

wpxp123456987306526ea8
diff --git a/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/editor-undo-redo-keyboard.spec.ts b/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/editor-undo-redo-keyboard.spec.ts
index 91888f7efd..a0010488f2 100644
--- a/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/editor-undo-redo-keyboard.spec.ts
+++ b/packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/editor-undo-redo-keyboard.spec.ts
@@ -30,12 +30,12 @@ function createCommandService() {
return {
executeCommand,
- } satisfies Pick<ICommandService, 'executeCommand'> & {
+ } as unknown as ICommandService & {
executeCommand: ReturnType<typeof vi.fn>;
};
}
-function createUniverInstanceService(focusedUnitId: string | null = null) {
+function createUniverInstanceService(focusedUnitId: string | null = null, editorDataStream: string = '') {
let currentFocusedUnitId = focusedUnitId;
const focusUnit = vi.fn((unitId: string) => {
currentFocusedUnitId = unitId;
@@ -46,7 +46,10 @@ function createUniverInstanceService(focusedUnitId: string | null = null) {
getFocusedUnit: vi.fn(() => currentFocusedUnitId == null
? undefined
: { getUnitId: () => currentFocusedUnitId }),
- } as unknown as Pick<IUniverInstanceService, 'focusUnit' | 'getFocusedUnit'> & {
+ getUnit: vi.fn(() => ({
+ getBody: () => ({ dataStream: editorDataStream }),
+ })),
+ } as unknown as IUniverInstanceService & {
focusUnit: ReturnType<typeof vi.fn>;
};
}
@@ -76,10 +79,10 @@ describe('editor undo redo keyboard helper', () => {
expect(univerInstanceService.focusUnit).toHaveBeenNthCalledWith(2, 'host-doc');
});
- it('keeps sheet editor undo redo on the existing sheet context', async () => {
+ it('ignores undo redo shortcuts while editing formulas in sheet editors', async () => {
for (const editorUnitId of [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY]) {
const commandService = createCommandService();
- const univerInstanceService = createUniverInstanceService('sheet-unit');
+ const univerInstanceService = createUniverInstanceService('sheet-unit', '=SUM(A1\r\n');
executeEditorUndoRedoCommand({
commandId: RedoCommand.id,
@@ -91,7 +94,26 @@ describe('editor undo redo keyboard helper', () => {
await waitForCommandFinally();
expect(univerInstanceService.focusUnit).not.toHaveBeenCalled();
- expect(commandService.executeCommand).toHaveBeenCalledWith(RedoCommand.id);
+ expect(commandService.executeCommand).not.toHaveBeenCalled();
+ }
+ });
+
+ it('keeps undo redo available for non-formula text in sheet editors', async () => {
+ for (const editorUnitId of [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY]) {
+ const commandService = createCommandService();
+ const univerInstanceService = createUniverInstanceService('sheet-unit', 'plain text\r\n');
+
+ executeEditorUndoRedoCommand({
+ commandId: UndoCommand.id,
+ commandService,
+ editorUnitId,
+ univerInstanceService,
+ });
+
+ await waitForCommandFinally();
+
+ expect(univerInstanceService.focusUnit).not.toHaveBeenCalled();
+ expect(commandService.executeCommand).toHaveBeenCalledWith(UndoCommand.id);
}
});
diff --git a/packages/docs-ui/src/views/rich-text-editor/hooks/editor-undo-redo-keyboard.ts b/packages/docs-ui/src/views/rich-text-editor/hooks/editor-undo-redo-keyboard.ts
index 5ef9be2949..70edc5bcb8 100644
--- a/packages/docs-ui/src/views/rich-text-editor/hooks/editor-undo-redo-keyboard.ts
+++ b/packages/docs-ui/src/views/rich-text-editor/hooks/editor-undo-redo-keyboard.ts
@@ -14,31 +14,45 @@
* limitations under the License.
*/
-import type { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import type { DocumentDataModel, ICommandService, IUniverInstanceService } from '@univerjs/core';
import type { IKeyboardEventConfig } from './use-keyboard-event';
-import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, RedoCommand, UndoCommand } from '@univerjs/core';
+import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';
import { KeyCode, MetaKeys } from '@univerjs/ui';
export interface IExecuteEditorUndoRedoCommandOptions {
commandId: string;
- commandService: Pick<ICommandService, 'executeCommand'>;
+ commandService: ICommandService;
editorUnitId: string;
- univerInstanceService: Pick<IUniverInstanceService, 'focusUnit' | 'getFocusedUnit'>;
+ univerInstanceService: IUniverInstanceService;
}
export interface ICreateEditorUndoRedoKeyboardConfigOptions {
- commandService: Pick<ICommandService, 'executeCommand'>;
- univerInstanceService: Pick<IUniverInstanceService, 'focusUnit' | 'getFocusedUnit'>;
+ commandService: ICommandService;
+ univerInstanceService: IUniverInstanceService;
editorUnitId: string;
keyCodes?: IKeyboardEventConfig['keyCodes'];
handler?: IKeyboardEventConfig['handler'];
}
+function isSheetFormulaEditor(editorUnitId: string, univerInstanceService: IUniverInstanceService): boolean {
+ if (editorUnitId !== DOCS_NORMAL_EDITOR_UNIT_ID_KEY && editorUnitId !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
+ return false;
+ }
+
+ const documentModel = univerInstanceService.getUnit<DocumentDataModel>(editorUnitId, UniverInstanceType.UNIVER_DOC);
+ return documentModel?.getBody()?.dataStream?.startsWith('=') ?? false;
+}
+
export function executeEditorUndoRedoCommand(options: IExecuteEditorUndoRedoCommandOptions): void {
const { commandId, commandService, editorUnitId, univerInstanceService } = options;
const shouldUseSheetEditorContext =
editorUnitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY ||
editorUnitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
+
+ if (isSheetFormulaEditor(editorUnitId, univerInstanceService)) {
+ return;
+ }
+
const previousFocusedUnitId = univerInstanceService.getFocusedUnit()?.getUnitId() ?? null;
const shouldRestoreFocus = !shouldUseSheetEditorContext && previousFocusedUnitId !== null && previousFocusedUnitId !== editorUnitId;