Skip to content

Commit 9873065

Browse files
authored
fix(docs-ui): skip undo redo while editing sheet formulas (#7141)
1 parent 97402e4 commit 9873065

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

packages/docs-ui/src/views/rich-text-editor/hooks/__tests__/editor-undo-redo-keyboard.spec.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ function createCommandService() {
3030

3131
return {
3232
executeCommand,
33-
} satisfies Pick<ICommandService, 'executeCommand'> & {
33+
} as unknown as ICommandService & {
3434
executeCommand: ReturnType<typeof vi.fn>;
3535
};
3636
}
3737

38-
function createUniverInstanceService(focusedUnitId: string | null = null) {
38+
function createUniverInstanceService(focusedUnitId: string | null = null, editorDataStream: string = '') {
3939
let currentFocusedUnitId = focusedUnitId;
4040
const focusUnit = vi.fn((unitId: string) => {
4141
currentFocusedUnitId = unitId;
@@ -46,7 +46,10 @@ function createUniverInstanceService(focusedUnitId: string | null = null) {
4646
getFocusedUnit: vi.fn(() => currentFocusedUnitId == null
4747
? undefined
4848
: { getUnitId: () => currentFocusedUnitId }),
49-
} as unknown as Pick<IUniverInstanceService, 'focusUnit' | 'getFocusedUnit'> & {
49+
getUnit: vi.fn(() => ({
50+
getBody: () => ({ dataStream: editorDataStream }),
51+
})),
52+
} as unknown as IUniverInstanceService & {
5053
focusUnit: ReturnType<typeof vi.fn>;
5154
};
5255
}
@@ -76,10 +79,10 @@ describe('editor undo redo keyboard helper', () => {
7679
expect(univerInstanceService.focusUnit).toHaveBeenNthCalledWith(2, 'host-doc');
7780
});
7881

79-
it('keeps sheet editor undo redo on the existing sheet context', async () => {
82+
it('ignores undo redo shortcuts while editing formulas in sheet editors', async () => {
8083
for (const editorUnitId of [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY]) {
8184
const commandService = createCommandService();
82-
const univerInstanceService = createUniverInstanceService('sheet-unit');
85+
const univerInstanceService = createUniverInstanceService('sheet-unit', '=SUM(A1\r\n');
8386

8487
executeEditorUndoRedoCommand({
8588
commandId: RedoCommand.id,
@@ -91,7 +94,26 @@ describe('editor undo redo keyboard helper', () => {
9194
await waitForCommandFinally();
9295

9396
expect(univerInstanceService.focusUnit).not.toHaveBeenCalled();
94-
expect(commandService.executeCommand).toHaveBeenCalledWith(RedoCommand.id);
97+
expect(commandService.executeCommand).not.toHaveBeenCalled();
98+
}
99+
});
100+
101+
it('keeps undo redo available for non-formula text in sheet editors', async () => {
102+
for (const editorUnitId of [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY]) {
103+
const commandService = createCommandService();
104+
const univerInstanceService = createUniverInstanceService('sheet-unit', 'plain text\r\n');
105+
106+
executeEditorUndoRedoCommand({
107+
commandId: UndoCommand.id,
108+
commandService,
109+
editorUnitId,
110+
univerInstanceService,
111+
});
112+
113+
await waitForCommandFinally();
114+
115+
expect(univerInstanceService.focusUnit).not.toHaveBeenCalled();
116+
expect(commandService.executeCommand).toHaveBeenCalledWith(UndoCommand.id);
95117
}
96118
});
97119

packages/docs-ui/src/views/rich-text-editor/hooks/editor-undo-redo-keyboard.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,45 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { ICommandService, IUniverInstanceService } from '@univerjs/core';
17+
import type { DocumentDataModel, ICommandService, IUniverInstanceService } from '@univerjs/core';
1818
import type { IKeyboardEventConfig } from './use-keyboard-event';
19-
import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, RedoCommand, UndoCommand } from '@univerjs/core';
19+
import { DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';
2020
import { KeyCode, MetaKeys } from '@univerjs/ui';
2121

2222
export interface IExecuteEditorUndoRedoCommandOptions {
2323
commandId: string;
24-
commandService: Pick<ICommandService, 'executeCommand'>;
24+
commandService: ICommandService;
2525
editorUnitId: string;
26-
univerInstanceService: Pick<IUniverInstanceService, 'focusUnit' | 'getFocusedUnit'>;
26+
univerInstanceService: IUniverInstanceService;
2727
}
2828

2929
export interface ICreateEditorUndoRedoKeyboardConfigOptions {
30-
commandService: Pick<ICommandService, 'executeCommand'>;
31-
univerInstanceService: Pick<IUniverInstanceService, 'focusUnit' | 'getFocusedUnit'>;
30+
commandService: ICommandService;
31+
univerInstanceService: IUniverInstanceService;
3232
editorUnitId: string;
3333
keyCodes?: IKeyboardEventConfig['keyCodes'];
3434
handler?: IKeyboardEventConfig['handler'];
3535
}
3636

37+
function isSheetFormulaEditor(editorUnitId: string, univerInstanceService: IUniverInstanceService): boolean {
38+
if (editorUnitId !== DOCS_NORMAL_EDITOR_UNIT_ID_KEY && editorUnitId !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {
39+
return false;
40+
}
41+
42+
const documentModel = univerInstanceService.getUnit<DocumentDataModel>(editorUnitId, UniverInstanceType.UNIVER_DOC);
43+
return documentModel?.getBody()?.dataStream?.startsWith('=') ?? false;
44+
}
45+
3746
export function executeEditorUndoRedoCommand(options: IExecuteEditorUndoRedoCommandOptions): void {
3847
const { commandId, commandService, editorUnitId, univerInstanceService } = options;
3948
const shouldUseSheetEditorContext =
4049
editorUnitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY ||
4150
editorUnitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
51+
52+
if (isSheetFormulaEditor(editorUnitId, univerInstanceService)) {
53+
return;
54+
}
55+
4256
const previousFocusedUnitId = univerInstanceService.getFocusedUnit()?.getUnitId() ?? null;
4357
const shouldRestoreFocus = !shouldUseSheetEditorContext && previousFocusedUnitId !== null && previousFocusedUnitId !== editorUnitId;
4458

0 commit comments

Comments
 (0)