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(sheets-ui): configure drag overwrite warning (#8146)

白熱f2f0e9200f29
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/move-range-confirm.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/move-range-confirm.command.spec.ts
index 00f098f116..9c6640a1b5 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/move-range-confirm.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/move-range-confirm.command.spec.ts
@@ -17,11 +17,14 @@
import type { Injector, IWorkbookData, Univer, Workbook } from '@univerjs/core';
import {
ICommandService,
+ IConfigService,
IConfirmService,
IUndoRedoService,
LocaleService,
LocaleType,
+ RedoCommand,
TestConfirmService,
+ UndoCommand,
} from '@univerjs/core';
import {
MoveRangeCommand,
@@ -30,6 +33,7 @@ import {
SheetInterceptorService,
} from '@univerjs/sheets';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../../config/config';
import { MoveRangeConfirmCommand } from '../move-range-confirm.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -65,7 +69,8 @@ describe('MoveRangeConfirmCommand', () => {
univer.dispose();
});
- it('rejects an invalid move before asking to overwrite target content', async () => {
+ it.each([undefined, false])('rejects an invalid move with warnBeforeDragOverwrite=%s', async (warnBeforeDragOverwrite) => {
+ get(IConfigService).setConfig(SHEETS_UI_PLUGIN_CONFIG_KEY, { warnBeforeDragOverwrite });
const confirm = vi.spyOn(confirmService, 'confirm').mockResolvedValue(true);
const history = get(IUndoRedoService).getUndoRedoStatus('test');
get(SheetInterceptorService).interceptBeforeCommand({
@@ -110,27 +115,28 @@ describe('MoveRangeConfirmCommand', () => {
fromRange: FROM_RANGE,
toRange: TO_RANGE,
})).toBeFalsy();
- expect(confirm).toHaveBeenCalledWith({
- id: MoveRangeConfirmCommand.id,
- title: { title: 'Warning' },
- children: {
- title: 'There is already data in the target area. This action will overwrite the existing data. Do you want to continue?',
- },
- cancelText: 'Cancel',
- confirmText: 'OK',
- });
+ expect(confirm).toHaveBeenCalledTimes(1);
expect(getCellValue(0, 0)).toBe('source');
expect(getCellValue(0, 1)).toBe('target');
});
- it('overwrites target content after confirmation', async () => {
+ it.each([undefined, true, false])('preserves undo/redo when overwriting with warnBeforeDragOverwrite=%s', async (warnBeforeDragOverwrite) => {
+ get(IConfigService).setConfig(SHEETS_UI_PLUGIN_CONFIG_KEY, { warnBeforeDragOverwrite });
const confirm = vi.spyOn(confirmService, 'confirm').mockResolvedValue(true);
expect(await commandService.executeCommand(MoveRangeConfirmCommand.id, {
fromRange: FROM_RANGE,
toRange: TO_RANGE,
})).toBeTruthy();
- expect(confirm).toHaveBeenCalledTimes(1);
+ expect(confirm).toHaveBeenCalledTimes(warnBeforeDragOverwrite === false ? 0 : 1);
+ expect(getCellValue(0, 0)).toBeUndefined();
+ expect(getCellValue(0, 1)).toBe('source');
+
+ expect(await commandService.executeCommand(UndoCommand.id)).toBe(true);
+ expect(getCellValue(0, 0)).toBe('source');
+ expect(getCellValue(0, 1)).toBe('target');
+
+ expect(await commandService.executeCommand(RedoCommand.id)).toBe(true);
expect(getCellValue(0, 0)).toBeUndefined();
expect(getCellValue(0, 1)).toBe('source');
});
diff --git a/packages/sheets-ui/src/commands/commands/move-range-confirm.command.ts b/packages/sheets-ui/src/commands/commands/move-range-confirm.command.ts
index 5722e7d49d..6d1e28c223 100644
--- a/packages/sheets-ui/src/commands/commands/move-range-confirm.command.ts
+++ b/packages/sheets-ui/src/commands/commands/move-range-confirm.command.ts
@@ -16,10 +16,10 @@
import type { ICommand } from '@univerjs/core';
import type { IMoveRangeCommandParams } from '@univerjs/sheets';
-import type { LocaleKey } from '../../locale/types';
import {
CommandType,
ICommandService,
+ IConfigService,
IConfirmService,
IUniverInstanceService,
LocaleService,
@@ -31,6 +31,9 @@ import {
MoveRangeCommand,
SheetInterceptorService,
} from '@univerjs/sheets';
+import type { IUniverSheetsUIConfig } from '../../config/config';
+import type { LocaleKey } from '../../locale/types';
+import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../config/config';
export const MoveRangeConfirmCommand: ICommand<IMoveRangeCommandParams> = {
type: CommandType.COMMAND,
@@ -49,6 +52,10 @@ export const MoveRangeConfirmCommand: ICommand<IMoveRangeCommandParams> = {
}
const commandService = accessor.get(ICommandService);
+ if (accessor.get(IConfigService).getConfig<IUniverSheetsUIConfig>(SHEETS_UI_PLUGIN_CONFIG_KEY)?.warnBeforeDragOverwrite === false) {
+ return commandService.executeCommand(MoveRangeCommand.id, params);
+ }
+
const univerInstanceService = accessor.get(IUniverInstanceService);
const sourceTarget = getSheetCommandTarget(univerInstanceService, {
unitId: params.fromUnitId,
diff --git a/packages/sheets-ui/src/config/config.ts b/packages/sheets-ui/src/config/config.ts
index 590152115b..56452ab8f5 100644
--- a/packages/sheets-ui/src/config/config.ts
+++ b/packages/sheets-ui/src/config/config.ts
@@ -109,6 +109,13 @@ export interface IUniverSheetsUIConfig {
hidePasteOptions?: boolean;
};
+ /**
+ * Whether to ask for confirmation before a dragged range overwrites existing cell content.
+ * Disabling the warning does not bypass move validation or undo/redo.
+ * @default true
+ */
+ warnBeforeDragOverwrite?: boolean;
+
/** The config of the scroll bar. */
scrollConfig?: IScrollBarProps;
diff --git a/presets/packages/preset-sheets-core/src/preset.ts b/presets/packages/preset-sheets-core/src/preset.ts
index 97f17240e8..b01b361b8d 100644
--- a/presets/packages/preset-sheets-core/src/preset.ts
+++ b/presets/packages/preset-sheets-core/src/preset.ts
@@ -145,6 +145,7 @@ export function UniverSheetsCorePreset(config: Partial<IUniverSheetsCorePresetCo
maxAutoHeightCount: sheets?.maxAutoHeightCount,
enableFillHandle: sheets?.enableFillHandle,
clipboardConfig: sheets?.clipboardConfig,
+ warnBeforeDragOverwrite: sheets?.warnBeforeDragOverwrite,
scrollConfig: sheets?.scrollConfig,
protectedRangeShadow: sheets?.protectedRangeShadow ?? true,
protectedRangeUserSelector: sheets?.protectedRangeUserSelector,
diff --git a/presets/packages/preset-sheets-core/src/types.ts b/presets/packages/preset-sheets-core/src/types.ts
index c21700dfcf..2993601737 100644
--- a/presets/packages/preset-sheets-core/src/types.ts
+++ b/presets/packages/preset-sheets-core/src/types.ts
@@ -42,6 +42,7 @@ export interface IUniverSheetsPresetConfig extends
'maxAutoHeightCount'
| 'enableFillHandle'
| 'clipboardConfig'
+ | 'warnBeforeDragOverwrite'
| 'scrollConfig'
| 'protectedRangeShadow'
| 'protectedRangeUserSelector'