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): add Ctrl+D (Copy Down) and Ctrl+R (Copy Right) shortcuts (#6690)

technical-founderc4988baea55a
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index 20b9b35ed3..c38c5af6a9 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -97,6 +97,7 @@ import { CELL_POPUP_COMPONENT_KEY } from '../views/cell-popup/config';
import { DEFINED_NAME_CONTAINER } from '../views/defined-name/component-name';
import { DefinedNameContainer } from '../views/defined-name/DefinedNameContainer';
import { RenderSheetContent, RenderSheetFooter, RenderSheetHeader } from '../views/sheet-container/SheetContainer';
+import { CopyDownShortcutItem, CopyRightShortcutItem } from './shortcuts/copy-fill.shortcut';
import {
EditorBreakLineShortcut,
EditorCursorCtrlEnterShortcut,
@@ -329,6 +330,10 @@ export class SheetUIController extends Disposable {
EditorCursorCtrlEnterShortcut,
ShiftEditorDeleteLeftShortcut,
+ // copy fill shortcuts
+ CopyDownShortcutItem,
+ CopyRightShortcutItem,
+
// operation shortcuts
SetRowHiddenShortcutItem,
SetColHiddenShortcutItem,
diff --git a/packages/sheets-ui/src/controllers/shortcuts/copy-fill.shortcut.ts b/packages/sheets-ui/src/controllers/shortcuts/copy-fill.shortcut.ts
new file mode 100644
index 0000000000..eabf7b3de6
--- /dev/null
+++ b/packages/sheets-ui/src/controllers/shortcuts/copy-fill.shortcut.ts
@@ -0,0 +1,32 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IShortcutItem } from '@univerjs/ui';
+import { SheetCopyDownCommand, SheetCopyRightCommand } from '@univerjs/sheets';
+import { KeyCode, MetaKeys } from '@univerjs/ui';
+import { whenSheetEditorFocused } from './utils';
+
+export const CopyDownShortcutItem: IShortcutItem = {
+ id: SheetCopyDownCommand.id,
+ preconditions: (contextService) => whenSheetEditorFocused(contextService),
+ binding: KeyCode.D | MetaKeys.CTRL_COMMAND,
+};
+
+export const CopyRightShortcutItem: IShortcutItem = {
+ id: SheetCopyRightCommand.id,
+ preconditions: (contextService) => whenSheetEditorFocused(contextService),
+ binding: KeyCode.R | MetaKeys.CTRL_COMMAND,
+};
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 8588fac83e..37f9a29454 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -168,3 +168,4 @@ export { SheetBar } from './views/sheet-bar/SheetBar';
export { RenderSheetContent, RenderSheetFooter, RenderSheetHeader } from './views/sheet-container/SheetContainer';
export { type IStatisticItem } from './views/status-bar/CopyableStatisticItem';
export { functionDisplayNames } from './views/status-bar/CopyableStatisticItem';
+export { SheetCopyDownCommand, SheetCopyRightCommand } from '@univerjs/sheets';
diff --git a/packages/sheets/src/commands/commands/__tests__/auto-fill.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/auto-fill.command.spec.ts
new file mode 100644
index 0000000000..15e4d04174
--- /dev/null
+++ b/packages/sheets/src/commands/commands/__tests__/auto-fill.command.spec.ts
@@ -0,0 +1,159 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IAccessor } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { describe, expect, it, vi } from 'vitest';
+import { IAutoFillService } from '../../../services/auto-fill/auto-fill.service';
+import { AUTO_FILL_APPLY_TYPE } from '../../../services/auto-fill/type';
+import { SheetsSelectionsService } from '../../../services/selections/selection.service';
+import { AutoFillCommand, SheetCopyDownCommand, SheetCopyRightCommand } from '../auto-fill.command';
+
+function createAccessor(pairs: Array<[unknown, unknown]>): IAccessor {
+ const map = new Map<unknown, unknown>(pairs);
+
+ return {
+ get(token: unknown) {
+ if (!map.has(token)) {
+ throw new Error(`Unknown token: ${String(token)}`);
+ }
+
+ return map.get(token);
+ },
+ } as IAccessor;
+}
+
+function createCommandAccessor(range?: { startRow: number; endRow: number; startColumn: number; endColumn: number }, executeResult = true) {
+ const executeCommand = vi.fn(async () => executeResult);
+ const setShowMenu = vi.fn();
+ const worksheet = { getSheetId: () => 'sheet-1' };
+ const workbook = {
+ getUnitId: () => 'unit-1',
+ getActiveSheet: () => worksheet,
+ };
+
+ const accessor = createAccessor([
+ [SheetsSelectionsService, { getCurrentLastSelection: () => (range ? { range } : null) }],
+ [IUniverInstanceService, { getCurrentUnitOfType: () => workbook }],
+ [ICommandService, { executeCommand }],
+ [IAutoFillService, { setShowMenu }],
+ ]);
+
+ return {
+ accessor,
+ executeCommand,
+ setShowMenu,
+ };
+}
+
+describe('Sheet copy fill commands', () => {
+ it('copies down from the row above for a single-row selection', async () => {
+ const { accessor, executeCommand, setShowMenu } = createCommandAccessor({
+ startRow: 2,
+ endRow: 2,
+ startColumn: 1,
+ endColumn: 3,
+ });
+
+ expect(await SheetCopyDownCommand.handler(accessor)).toBe(true);
+ expect(executeCommand).toHaveBeenCalledWith(AutoFillCommand.id, {
+ sourceRange: { startRow: 1, endRow: 1, startColumn: 1, endColumn: 3 },
+ targetRange: { startRow: 1, endRow: 2, startColumn: 1, endColumn: 3 },
+ unitId: 'unit-1',
+ subUnitId: 'sheet-1',
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ });
+ expect(setShowMenu).toHaveBeenCalledWith(false);
+ });
+
+ it('copies down from the first selected row for a multi-row selection', async () => {
+ const { accessor, executeCommand } = createCommandAccessor({
+ startRow: 1,
+ endRow: 4,
+ startColumn: 0,
+ endColumn: 2,
+ });
+
+ expect(await SheetCopyDownCommand.handler(accessor)).toBe(true);
+ expect(executeCommand).toHaveBeenCalledWith(AutoFillCommand.id, {
+ sourceRange: { startRow: 1, endRow: 1, startColumn: 0, endColumn: 2 },
+ targetRange: { startRow: 1, endRow: 4, startColumn: 0, endColumn: 2 },
+ unitId: 'unit-1',
+ subUnitId: 'sheet-1',
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ });
+ });
+
+ it('copies right from the column to the left for a single-column selection', async () => {
+ const { accessor, executeCommand, setShowMenu } = createCommandAccessor({
+ startRow: 1,
+ endRow: 3,
+ startColumn: 2,
+ endColumn: 2,
+ });
+
+ expect(await SheetCopyRightCommand.handler(accessor)).toBe(true);
+ expect(executeCommand).toHaveBeenCalledWith(AutoFillCommand.id, {
+ sourceRange: { startRow: 1, endRow: 3, startColumn: 1, endColumn: 1 },
+ targetRange: { startRow: 1, endRow: 3, startColumn: 1, endColumn: 2 },
+ unitId: 'unit-1',
+ subUnitId: 'sheet-1',
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ });
+ expect(setShowMenu).toHaveBeenCalledWith(false);
+ });
+
+ it('copies right from the first selected column for a multi-column selection', async () => {
+ const { accessor, executeCommand } = createCommandAccessor({
+ startRow: 2,
+ endRow: 4,
+ startColumn: 1,
+ endColumn: 3,
+ });
+
+ expect(await SheetCopyRightCommand.handler(accessor)).toBe(true);
+ expect(executeCommand).toHaveBeenCalledWith(AutoFillCommand.id, {
+ sourceRange: { startRow: 2, endRow: 4, startColumn: 1, endColumn: 1 },
+ targetRange: { startRow: 2, endRow: 4, startColumn: 1, endColumn: 3 },
+ unitId: 'unit-1',
+ subUnitId: 'sheet-1',
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ });
+ });
+
+ it('returns false for top-row and leftmost-column no-op selections', async () => {
+ const topRow = createCommandAccessor({
+ startRow: 0,
+ endRow: 0,
+ startColumn: 1,
+ endColumn: 1,
+ });
+ const leftColumn = createCommandAccessor({
+ startRow: 1,
+ endRow: 1,
+ startColumn: 0,
+ endColumn: 0,
+ });
+
+ expect(await SheetCopyDownCommand.handler(topRow.accessor)).toBe(false);
+ expect(topRow.executeCommand).not.toHaveBeenCalled();
+ expect(topRow.setShowMenu).not.toHaveBeenCalled();
+
+ expect(await SheetCopyRightCommand.handler(leftColumn.accessor)).toBe(false);
+ expect(leftColumn.executeCommand).not.toHaveBeenCalled();
+ expect(leftColumn.setShowMenu).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/sheets/src/commands/commands/auto-fill.command.ts b/packages/sheets/src/commands/commands/auto-fill.command.ts
index 4c913bff34..bc7f55d114 100644
--- a/packages/sheets/src/commands/commands/auto-fill.command.ts
+++ b/packages/sheets/src/commands/commands/auto-fill.command.ts
@@ -15,11 +15,11 @@
*/
import type { IAccessor, ICommand, IRange } from '@univerjs/core';
-import type { AUTO_FILL_APPLY_TYPE } from '../../services/auto-fill/type';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, sequenceExecute } from '@univerjs/core';
import { generateNullCellValue } from '../../basics/utils';
import { IAutoFillService } from '../../services/auto-fill/auto-fill.service';
+import { AUTO_FILL_APPLY_TYPE } from '../../services/auto-fill/type';
import { SheetsSelectionsService } from '../../services/selections';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
@@ -48,6 +48,93 @@ export const AutoFillCommand: ICommand = {
},
};
+function getSheetCopyFillRange(accessor: IAccessor, direction: 'down' | 'right'): IAutoFillCommandParams | null {
+ const selection = accessor.get(SheetsSelectionsService).getCurrentLastSelection();
+ if (!selection) {
+ return null;
+ }
+
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
+ if (!target) {
+ return null;
+ }
+
+ const { unitId, subUnitId } = target;
+ const { startRow, endRow, startColumn, endColumn } = selection.range;
+
+ if (direction === 'down') {
+ if (startRow === endRow) {
+ if (startRow === 0) {
+ return null;
+ }
+
+ return {
+ sourceRange: { startRow: startRow - 1, endRow: startRow - 1, startColumn, endColumn },
+ targetRange: { startRow: startRow - 1, endRow, startColumn, endColumn },
+ unitId,
+ subUnitId,
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ };
+ }
+
+ return {
+ sourceRange: { startRow, endRow: startRow, startColumn, endColumn },
+ targetRange: { startRow, endRow, startColumn, endColumn },
+ unitId,
+ subUnitId,
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ };
+ }
+
+ if (startColumn === endColumn) {
+ if (startColumn === 0) {
+ return null;
+ }
+
+ return {
+ sourceRange: { startRow, endRow, startColumn: startColumn - 1, endColumn: startColumn - 1 },
+ targetRange: { startRow, endRow, startColumn: startColumn - 1, endColumn },
+ unitId,
+ subUnitId,
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ };
+ }
+
+ return {
+ sourceRange: { startRow, endRow, startColumn, endColumn: startColumn },
+ targetRange: { startRow, endRow, startColumn, endColumn },
+ unitId,
+ subUnitId,
+ applyType: AUTO_FILL_APPLY_TYPE.COPY,
+ };
+}
+
+async function executeSheetCopyFill(accessor: IAccessor, direction: 'down' | 'right') {
+ const params = getSheetCopyFillRange(accessor, direction);
+ if (!params) {
+ return false;
+ }
+
+ const result = await accessor.get(ICommandService).executeCommand(AutoFillCommand.id, params);
+ if (result) {
+ accessor.get(IAutoFillService).setShowMenu(false);
+ }
+
+ return result;
+}
+
+export const SheetCopyDownCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.copy-down',
+ handler: async (accessor: IAccessor) => executeSheetCopyFill(accessor, 'down'),
+};
+
+export const SheetCopyRightCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.copy-right',
+ handler: async (accessor: IAccessor) => executeSheetCopyFill(accessor, 'right'),
+};
+
export interface IAutoClearContentCommand {
clearRange: IRange;
selectionRange: IRange;
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index 3efda32d95..e4b6ae69db 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -22,7 +22,7 @@ import { AddWorksheetMergeAllCommand, AddWorksheetMergeCommand, AddWorksheetMerg
import { AddWorksheetProtectionCommand } from '../commands/commands/add-worksheet-protection.command';
import { SetWorksheetRangeThemeStyleCommand } from '../commands/commands/add-worksheet-range-theme.command';
import { AppendRowCommand } from '../commands/commands/append-row.command';
-import { AutoClearContentCommand, AutoFillCommand } from '../commands/commands/auto-fill.command';
+import { AutoClearContentCommand, AutoFillCommand, SheetCopyDownCommand, SheetCopyRightCommand } from '../commands/commands/auto-fill.command';
import { ClearSelectionAllCommand } from '../commands/commands/clear-selection-all.command';
import { ClearSelectionContentCommand } from '../commands/commands/clear-selection-content.command';
import { ClearSelectionFormatCommand } from '../commands/commands/clear-selection-format.command';
@@ -381,6 +381,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
RemoveRangeThemeMutation,
AutoFillCommand,
+ SheetCopyDownCommand,
+ SheetCopyRightCommand,
AutoClearContentCommand,
RefillCommand,
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index b9c17f554a..7bad97f5ac 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -70,7 +70,7 @@ export {
export { AddWorksheetProtectionCommand } from './commands/commands/add-worksheet-protection.command';
export { SetWorksheetRangeThemeStyleCommand } from './commands/commands/add-worksheet-range-theme.command';
export { AppendRowCommand, type IAppendRowCommandParams } from './commands/commands/append-row.command';
-export { AutoClearContentCommand, AutoFillCommand, type IAutoClearContentCommand, type IAutoFillCommandParams } from './commands/commands/auto-fill.command';
+export { AutoClearContentCommand, AutoFillCommand, type IAutoClearContentCommand, type IAutoFillCommandParams, SheetCopyDownCommand, SheetCopyRightCommand } from './commands/commands/auto-fill.command';
export { ClearSelectionAllCommand } from './commands/commands/clear-selection-all.command';
export { ClearSelectionContentCommand, type IClearSelectionContentCommandParams } from './commands/commands/clear-selection-content.command';
export { ClearSelectionFormatCommand } from './commands/commands/clear-selection-format.command';