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.

test(sheet:selection): finish selection test (#208)

Wenzhao Hu0e88ad717c68
diff --git a/packages/base-sheets/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/base-sheets/src/commands/commands/__tests__/create-command-test-bed.ts
index 4b4ed15c5e..91a9dc0188 100644
--- a/packages/base-sheets/src/commands/commands/__tests__/create-command-test-bed.ts
+++ b/packages/base-sheets/src/commands/commands/__tests__/create-command-test-bed.ts
@@ -1,4 +1,12 @@
-import { ICurrentUniverService, IWorkbookConfig, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import {
+ ICurrentUniverService,
+ ILogService,
+ IWorkbookConfig,
+ LocaleType,
+ Plugin,
+ PluginType,
+ Univer,
+} from '@univerjs/core';
import { Inject, Injector } from '@wendellhu/redi';
import { BorderStyleManagerService } from '../../../services/border-style-manager.service';
@@ -76,6 +84,9 @@ export function createCommandTestBed(workbookConfig?: IWorkbookConfig) {
const currentUniverService = get(ICurrentUniverService);
currentUniverService.focusUniverInstance('test');
+ const logService = get(ILogService);
+ logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+
return {
univer,
get,
diff --git a/packages/base-sheets/src/commands/commands/__tests__/create-selection-command-test-bed.ts b/packages/base-sheets/src/commands/commands/__tests__/create-selection-command-test-bed.ts
index 0a9c8944df..223487be45 100644
--- a/packages/base-sheets/src/commands/commands/__tests__/create-selection-command-test-bed.ts
+++ b/packages/base-sheets/src/commands/commands/__tests__/create-selection-command-test-bed.ts
@@ -1,10 +1,25 @@
import { ICommandService, IWorkbookConfig, LocaleType } from '@univerjs/core';
import { SetSelectionsOperation } from '../../operations/selection.operation';
-import { ChangeSelectionCommand, ExpandSelectionCommand, SelectAllCommand } from '../set-selections.command';
+import { ExpandSelectionCommand, MoveSelectionCommand, SelectAllCommand } from '../set-selections.command';
import { createCommandTestBed } from './create-command-test-bed';
-const SELECTION_COMMAND_TEST_WORKBOOK_DATA: IWorkbookConfig = {
+export function createSelectionCommandTestBed(workbookConfig?: IWorkbookConfig) {
+ const { univer, get, sheet } = createCommandTestBed(workbookConfig || SIMPLE_SELECTION_WORKBOOK_DATA);
+
+ const commandService = get(ICommandService);
+ [MoveSelectionCommand, ExpandSelectionCommand, SelectAllCommand, SetSelectionsOperation].forEach((c) => {
+ commandService.registerCommand(c);
+ });
+
+ return {
+ univer,
+ get,
+ sheet,
+ };
+}
+
+export const SELECTION_WITH_EMPTY_CELLS_DATA: IWorkbookConfig = {
id: 'test',
appVersion: '3.0.0-alpha',
sheets: {
@@ -16,38 +31,114 @@ const SELECTION_COMMAND_TEST_WORKBOOK_DATA: IWorkbookConfig = {
v: 'A1',
},
'1': {
- v: 'A2',
+ v: 'B1',
+ },
+ '2': {
+ v: 'C1',
+ },
+ '3': {
+ v: '',
+ },
+ '4': {
+ v: '',
+ },
+ '5': {
+ v: 'F1',
+ },
+ '6': {
+ v: '', // merged to F1
+ },
+ '7': {
+ v: '',
+ },
+ '8': {
+ v: '',
},
},
- '1': {
+ },
+ mergeData: [{ startRow: 0, startColumn: 5, endColumn: 6, endRow: 1 }],
+ },
+ },
+ createdTime: '',
+ creator: '',
+ extensions: [],
+ lastModifiedBy: '',
+ locale: LocaleType.EN,
+ modifiedTime: '',
+ name: '',
+ namedRanges: [],
+ sheetOrder: [],
+ styles: {},
+ timeZone: '',
+};
+
+export const SELECTION_WITH_MERGED_CELLS_DATA: IWorkbookConfig = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ '0': {
'0': {
- v: 'B1',
+ v: 'A1',
},
'1': {
- v: 'B2',
+ v: 'B1',
+ },
+ '2': {
+ v: 'C1',
},
},
-
- '3': {
- '3': {
- v: 'D4',
+ '1': {
+ '0': {
+ v: 'A2',
},
- '4': {
- v: 'E4',
+ '1': {
+ v: '', // merged to B1
},
- '5': {
- v: 'F4',
+ '2': {
+ v: 'C2',
},
},
- '4': {
- '3': {
- v: 'D5',
+ },
+ mergeData: [{ startRow: 0, startColumn: 1, endRow: 1, endColumn: 1 }],
+ },
+ },
+ createdTime: '',
+ creator: '',
+ extensions: [],
+ lastModifiedBy: '',
+ locale: LocaleType.EN,
+ modifiedTime: '',
+ name: '',
+ namedRanges: [],
+ sheetOrder: [],
+ styles: {},
+ timeZone: '',
+};
+
+export const SIMPLE_SELECTION_WORKBOOK_DATA: IWorkbookConfig = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ '0': {
+ '0': {
+ v: 'A1',
},
- '4': {
- v: 'E5',
+ '1': {
+ v: 'A2',
},
- '5': {
- v: 'F5',
+ },
+ '1': {
+ '0': {
+ v: 'B1',
+ },
+ '1': {
+ v: 'B2',
},
},
},
@@ -68,18 +159,3 @@ const SELECTION_COMMAND_TEST_WORKBOOK_DATA: IWorkbookConfig = {
styles: {},
timeZone: '',
};
-
-export function createSelectionCommandTestBed(workbookConfig?: IWorkbookConfig) {
- const { univer, get, sheet } = createCommandTestBed(workbookConfig || SELECTION_COMMAND_TEST_WORKBOOK_DATA);
-
- const commandService = get(ICommandService);
- [ChangeSelectionCommand, ExpandSelectionCommand, SelectAllCommand, SetSelectionsOperation].forEach((c) => {
- commandService.registerCommand(c);
- });
-
- return {
- univer,
- get,
- sheet,
- };
-}
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-selections.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-selections.command.spec.ts
index abedfd73f7..1e63d7d0ba 100644
--- a/packages/base-sheets/src/commands/commands/__tests__/set-selections.command.spec.ts
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-selections.command.spec.ts
@@ -1,31 +1,30 @@
-import { Direction, ICommandService, RANGE_TYPE, Univer } from '@univerjs/core';
+/* eslint-disable no-magic-numbers */
+
+import { Direction, ICommandService, IWorkbookConfig, RANGE_TYPE, Univer } from '@univerjs/core';
import { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../../services/selection-manager.service';
import {
- ChangeSelectionCommand,
- IChangeSelectionCommandParams,
+ ExpandSelectionCommand,
+ IExpandSelectionCommandParams,
+ IMoveSelectionCommandParams,
ISelectAllCommandParams,
+ MoveSelectionCommand,
SelectAllCommand,
} from '../set-selections.command';
-import { createSelectionCommandTestBed } from './create-selection-command-test-bed';
+import {
+ createSelectionCommandTestBed,
+ SELECTION_WITH_EMPTY_CELLS_DATA,
+ SELECTION_WITH_MERGED_CELLS_DATA,
+} from './create-selection-command-test-bed';
describe('Test commands used for change selections', () => {
- let univer: Univer;
+ let univer: Univer | null = null;
let get: Injector['get'];
let commandService: ICommandService;
let selectionManagerService: SelectionManagerService;
- beforeEach(() => {
- const testBed = createSelectionCommandTestBed();
- univer = testBed.univer;
- get = testBed.get;
-
- commandService = get(ICommandService);
- selectionManagerService = get(SelectionManagerService);
- });
-
function select00() {
selectionManagerService.setCurrentSelection({
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
@@ -51,7 +50,16 @@ describe('Test commands used for change selections', () => {
]);
}
- function select(startRow: number, startColumn: number, endRow: number, endColumn: number) {
+ function select(
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number,
+ actualRow: number,
+ actualColumn: number,
+ isMerged: boolean,
+ isMergedMainCell: boolean
+ ) {
selectionManagerService.setCurrentSelection({
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
unitId: 'test',
@@ -66,10 +74,10 @@ describe('Test commands used for change selections', () => {
startColumn,
endRow,
endColumn,
- actualRow: startRow,
- actualColumn: startColumn,
- isMerged: false,
- isMergedMainCell: false,
+ actualRow,
+ actualColumn,
+ isMerged,
+ isMergedMainCell,
},
style: null,
},
@@ -86,76 +94,290 @@ describe('Test commands used for change selections', () => {
});
}
- afterEach(() => {
- univer.dispose();
- });
+ function disposeTestBed() {
+ univer?.dispose();
+ univer = null;
+ }
+
+ function prepareTestBed(snapshot?: IWorkbookConfig) {
+ const testBed = createSelectionCommandTestBed(snapshot);
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ selectionManagerService = get(SelectionManagerService);
+ }
+
+ afterEach(disposeTestBed);
+
+ describe('Simple movement to next cell', () => {
+ beforeEach(() => prepareTestBed());
- describe('Simple movement', () => {
it('Should move selection with command', async () => {
select00();
- await commandService.executeCommand<IChangeSelectionCommandParams>(ChangeSelectionCommand.id, {
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
direction: Direction.LEFT,
});
expectSelectionToBe(0, 0, 0, 0);
- await commandService.executeCommand<IChangeSelectionCommandParams>(ChangeSelectionCommand.id, {
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
direction: Direction.RIGHT,
});
expectSelectionToBe(0, 1, 0, 1);
- await commandService.executeCommand<IChangeSelectionCommandParams>(ChangeSelectionCommand.id, {
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
direction: Direction.DOWN,
});
expectSelectionToBe(1, 1, 1, 1);
- await commandService.executeCommand<IChangeSelectionCommandParams>(ChangeSelectionCommand.id, {
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
direction: Direction.LEFT,
});
expectSelectionToBe(1, 0, 1, 0);
- await commandService.executeCommand<IChangeSelectionCommandParams>(ChangeSelectionCommand.id, {
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
direction: Direction.UP,
});
expectSelectionToBe(0, 0, 0, 0);
});
+ });
+
+ describe('Move cell to/through merged cells', () => {
+ beforeEach(() => prepareTestBed(SELECTION_WITH_MERGED_CELLS_DATA));
+
+ /**
+ * A1 | B1 | C1
+ * ---| |----
+ * A2 | | C2
+ *
+ * When user clicks on C2 and move cursor left twice, A2 should not selected not A1.
+ */
+ it('Should select merged cell and move to next cell', async () => {
+ select00();
- describe('With merged cells', () => {
- it('Should select merged cell and move to next cell', async () => {
- // FIXME: 在跨越选区的时候,需要考虑选区的合并单元格,特别是这样的情形
- // D4 | E4 | F4
- // ---| |---
- // D5 | E5 | F5
- // 从 F5 向左移动两次,应该选中 D5 而非 D4
- // 所以一个选区信息可能有三个部分,选区的范围,选区的单元格范围,选区的合并单元格范围
- // Google Sheet 和腾讯文档 Sheet 的行为都是这样
- expect(true).toBeTruthy();
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 0, 0);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 1, 1, 1);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.RIGHT,
});
+ expectSelectionToBe(0, 2, 0, 2);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.DOWN,
+ });
+ expectSelectionToBe(1, 2, 1, 2);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 1, 1, 1);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(1, 0, 1, 0);
});
});
- describe('Move to next gap cell', () => {
- it('placeholder', () => {
- expect(true).toBeTruthy();
+ describe('Move to next cell that has value (skip cell)', () => {
+ beforeEach(() => prepareTestBed(SELECTION_WITH_EMPTY_CELLS_DATA));
+
+ it('Works on move', async () => {
+ select00();
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 2, 0, 2);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 5, 1, 6);
+
+ await commandService.executeCommand<IMoveSelectionCommandParams>(MoveSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 19, 0, 19);
});
});
- describe('Expand to next selection or shrink', () => {
- it('placeholder', () => {
- expect(true).toBeTruthy();
+ describe('Expand to next selection or shrink to previous selection', () => {
+ beforeEach(() => prepareTestBed(SELECTION_WITH_EMPTY_CELLS_DATA));
+
+ // it('Should not expand when hitting worksheet boundary', () => {})
+
+ it('Works on expand', async () => {
+ select00();
+
+ // expand
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 0, 0, 1); // A1:B1
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 0, 0, 2); // A1:C1
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 0, 0, 3); // A1:D1
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 0, 0, 4); // A1:E1
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 0, 1, 6); // A1:G2, because that is a merged cell
+
+ // shrink
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 4); // A1:E2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 3); // A1:D2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 2); // A1:C2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 1); // A1:B2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 0); // A1:A2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 0); // A1:A2, remain unchanged when hitting boundary
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.UP,
+ });
+ expectSelectionToBe(0, 0, 0, 0); // A1:A1
+ });
+ });
+
+ /**
+ * A1 | B1 | C1
+ * ---| |----
+ * A2 | | C2
+ *
+ * When A1:C1 is selected and B2 is the primary cell, shrink should only shrink to one side.
+ */
+ describe('Shrink edge case', () => {
+ beforeEach(() => prepareTestBed(SELECTION_WITH_MERGED_CELLS_DATA));
+
+ it('Should shrink on side when primary is in the middle of selections', async () => {
+ select(0, 0, 1, 2, 1, 1, true, false);
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ });
+ expectSelectionToBe(0, 0, 1, 1);
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ });
+ expectSelectionToBe(0, 1, 1, 1);
});
});
- describe('Expand to next gap position or shrink', () => {
- it('placeholder', () => {
- expect(true).toBeTruthy();
+ describe('Expand to next gap position or shrink to previous gap', () => {
+ beforeEach(() => prepareTestBed(SELECTION_WITH_EMPTY_CELLS_DATA));
+
+ it('Works on gap expand', async () => {
+ select00();
+
+ // expand
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 0, 0, 2); // A1:C1
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 0, 1, 6); // A1:G2, because that is a merged cell
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.RIGHT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 0, 1, 19);
+
+ // shrink
+
+ console.log('debug', selectionManagerService.getLast());
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 0, 1, 6); // A1:G2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 0, 1, 2); // A1:C2
+
+ await commandService.executeCommand<IExpandSelectionCommandParams>(ExpandSelectionCommand.id, {
+ direction: Direction.LEFT,
+ jumpOver: true,
+ });
+ expectSelectionToBe(0, 0, 1, 0); // A1:A2
});
});
- describe('Expand selection', () => {
+ describe('Select all', () => {
+ beforeEach(() => prepareTestBed());
+
it('Should first select all neighbor cells, and then the whole sheet', async () => {
select00();
+ const unchangedPrimaryInfo = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 0,
+ endColumn: 0,
+ actualRow: 0,
+ actualColumn: 0,
+ isMerged: false,
+ isMergedMainCell: false,
+ };
+
await commandService.executeCommand<ISelectAllCommandParams>(SelectAllCommand.id, {
loop: true,
expandToGapFirst: true,
@@ -167,6 +389,7 @@ describe('Test commands used for change selections', () => {
endColumn: 1,
rangeType: RANGE_TYPE.NORMAL,
});
+ expect(selectionManagerService.getLast()!.primary).toEqual(unchangedPrimaryInfo);
await commandService.executeCommand<ISelectAllCommandParams>(SelectAllCommand.id, {
loop: true,
@@ -179,6 +402,7 @@ describe('Test commands used for change selections', () => {
endColumn: 19,
rangeType: RANGE_TYPE.NORMAL,
});
+ expect(selectionManagerService.getLast()!.primary).toEqual(unchangedPrimaryInfo);
await commandService.executeCommand<ISelectAllCommandParams>(SelectAllCommand.id, {
loop: true,
@@ -191,6 +415,7 @@ describe('Test commands used for change selections', () => {
endColumn: 0,
rangeType: RANGE_TYPE.NORMAL,
});
+ expect(selectionManagerService.getLast()!.primary).toEqual(unchangedPrimaryInfo);
});
it('Should directly select all if `expandToGapFirst` is false', async () => {
diff --git a/packages/base-sheets/src/commands/commands/set-selections.command.ts b/packages/base-sheets/src/commands/commands/set-selections.command.ts
index ad9784263a..1a375c9277 100644
--- a/packages/base-sheets/src/commands/commands/set-selections.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-selections.command.ts
@@ -5,65 +5,83 @@ import {
ICommandService,
ICurrentUniverService,
IRange,
+ RANGE_TYPE,
Rectangle,
} from '@univerjs/core';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../services/selection-manager.service';
import { SetSelectionsOperation } from '../operations/selection.operation';
import {
+ checkIfShrink,
expandToContinuousRange,
expandToNextCell,
- expandToNextGapCell,
+ expandToNextGapRange,
expandToWholeSheet,
findNextGapRange,
findNextRange,
- getRangeAtPosition,
+ getCellAtRowCol,
+ getStartRange,
shrinkToNextCell,
- shrinkToNextGapCell,
+ shrinkToNextGapRange,
} from './utils/selection-util';
// TODO@wzhudev: we also need to handle when the current selection is the whole spreadsheet, whole rows or whole columns
-export interface IChangeSelectionCommandParams {
+export interface IMoveSelectionCommandParams {
direction: Direction;
jumpOver?: boolean;
}
-export const ChangeSelectionCommand: ICommand<IChangeSelectionCommandParams> = {
- id: 'sheet.command.change-selection',
+/**
+ * Move selection
+ */
+export const MoveSelectionCommand: ICommand<IMoveSelectionCommandParams> = {
+ id: 'sheet.command.move-selection',
type: CommandType.COMMAND,
handler: async (accessor, params) => {
- const currentUniverService = accessor.get(ICurrentUniverService);
- const currentWorkbook = currentUniverService.getCurrentUniverSheetInstance().getWorkBook();
- const currentWorksheet = currentWorkbook.getActiveSheet();
- const selectionManagerService = accessor.get(SelectionManagerService);
- const commandService = accessor.get(ICommandService);
+ if (!params) {
+ return false;
+ }
- const selection = selectionManagerService.getLast();
- if (!selection || !params) {
+ const workbook = accessor.get(ICurrentUniverService).getCurrentUniverSheetInstance().getWorkBook();
+ const worksheet = workbook.getActiveSheet();
+ const selection = accessor.get(SelectionManagerService).getLast();
+ if (!selection) {
return false;
}
const { direction, jumpOver } = params;
- const startRange = selection.range;
- let destRange = jumpOver
- ? findNextGapRange(startRange, direction, currentWorksheet)
- : findNextRange(startRange, direction, currentWorksheet);
+ const { range, primary } = selection;
+ const startRange = getStartRange(range, primary, direction);
- destRange = getRangeAtPosition(destRange.startRow, destRange.startColumn, currentWorksheet);
+ // the start range is from the primary selection range
+ const next = jumpOver
+ ? findNextGapRange(startRange, direction, worksheet)
+ : findNextRange(startRange, direction, worksheet);
+ const destRange = getCellAtRowCol(next.startRow, next.startColumn, worksheet);
if (Rectangle.equals(destRange, startRange)) {
return false;
}
- return commandService.executeCommand(SetSelectionsOperation.id, {
- unitId: currentWorkbook.getUnitId(),
- sheetId: currentWorksheet.getSheetId(),
+ return accessor.get(ICommandService).executeCommand(SetSelectionsOperation.id, {
+ unitId: workbook.getUnitId(),
+ sheetId: worksheet.getSheetId(),
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
selections: [
{
range: Rectangle.clone(destRange),
- primary: getRangeAtPosition(destRange.startRow, destRange.startColumn, currentWorksheet),
+ primary: {
+ startRow: destRange.startRow,
+ startColumn: destRange.startColumn,
+ endRow: destRange.endRow,
+ endColumn: destRange.endColumn,
+ actualRow: next.startRow,
+ actualColumn: next.startColumn,
+ isMerged: destRange.isMerged,
+ isMergedMainCell:
+ destRange.startRow === next.startRow && destRange.startColumn === next.startColumn,
+ },
},
],
});
@@ -82,64 +100,48 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
id: 'sheet.command.expand-selection',
type: CommandType.COMMAND,
handler: async (accessor, params) => {
- const currentUniverService = accessor.get(ICurrentUniverService);
- const currentWorkbook = currentUniverService.getCurrentUniverSheetInstance().getWorkBook();
- const currentWorksheet = currentWorkbook.getActiveSheet();
- const selectionManagerService = accessor.get(SelectionManagerService);
- const commandService = accessor.get(ICommandService);
-
- const selection = selectionManagerService.getLast();
- if (!selection || !params) {
+ if (!params) {
return false;
}
- const startRange = selection.range;
- const primary = selection.primary;
- if (!primary) {
- return false;
- }
+ const currentWorkbook = accessor.get(ICurrentUniverService).getCurrentUniverSheetInstance().getWorkBook();
+ const currentWorksheet = currentWorkbook.getActiveSheet();
- if (primary.startRow === -1) {
- // FIXME@DR-Univer
- // It maybe be related to click event handling. Try to click on C10 cell and use shift+arrow to expand selection.
- throw new Error('startRow is -1! @DR-Univer');
+ const selection = accessor.get(SelectionManagerService).getLast();
+ if (!selection) {
+ return false;
}
+ const { range: startRange, primary } = selection;
const { jumpOver, direction } = params;
- const isShrink = (function isShrink() {
- switch (direction) {
- case Direction.DOWN:
- return startRange.startRow < primary.startRow;
- case Direction.UP:
- return startRange.endRow > primary.endRow;
- case Direction.LEFT:
- return startRange.endColumn > primary.endColumn;
- case Direction.RIGHT:
- return startRange.startColumn < primary.startColumn;
- }
- })();
+
+ const isShrink = checkIfShrink(selection, direction, currentWorksheet);
const destRange = !isShrink
? jumpOver
- ? expandToNextGapCell(startRange, direction, currentWorksheet)
+ ? expandToNextGapRange(startRange, direction, currentWorksheet)
: expandToNextCell(startRange, direction, currentWorksheet)
: jumpOver
- ? shrinkToNextGapCell(startRange, primary, direction, currentWorksheet)
- : shrinkToNextCell(startRange, primary, direction, currentWorksheet);
+ ? shrinkToNextGapRange(
+ startRange,
+ // TODO: should fix on SelectionManagerService's side
+ { ...Rectangle.clone(primary), rangeType: RANGE_TYPE.NORMAL },
+ direction,
+ currentWorksheet
+ )
+ : shrinkToNextCell(startRange, direction, currentWorksheet);
if (Rectangle.equals(destRange, startRange)) {
return false;
}
- return commandService.executeCommand(SetSelectionsOperation.id, {
+ return accessor.get(ICommandService).executeCommand(SetSelectionsOperation.id, {
unitId: currentWorkbook.getUnitId(),
sheetId: currentWorksheet.getSheetId(),
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
selections: [
{
range: destRange,
- // primary: expand
- // ? selection.primary
primary, // this remains unchanged
},
],
@@ -167,44 +169,41 @@ export const SelectAllCommand: ICommand<ISelectAllCommandParams> = {
SELECTED_RANGE_WORKSHEET = '';
},
handler: async (accessor, params = { expandToGapFirst: true, loop: false }) => {
- const selectionManager = accessor.get(SelectionManagerService);
- const currentUniverService = accessor.get(ICurrentUniverService);
-
- const currentSelection = selectionManager.getLast();
- const currentWorkbook = currentUniverService.getCurrentUniverSheetInstance()?.getWorkBook();
- const currentWorksheet = currentWorkbook.getActiveSheet();
+ const selection = accessor.get(SelectionManagerService).getLast();
+ const workbook = accessor.get(ICurrentUniverService).getCurrentUniverSheetInstance()?.getWorkBook();
+ const worksheet = workbook.getActiveSheet();
- if (!currentSelection || !currentWorksheet) {
+ if (!selection || !worksheet) {
return false;
}
- const id = `${currentWorkbook.getUnitId()}|${currentWorksheet.getSheetId()}`;
+ const id = `${workbook.getUnitId()}|${worksheet.getSheetId()}`;
if (id !== SELECTED_RANGE_WORKSHEET) {
RANGES_STACK = [];
SELECTED_RANGE_WORKSHEET = id;
}
- const commandService = accessor.get(ICommandService);
- const maxRow = currentWorksheet.getMaxRows();
- const maxCol = currentWorksheet.getMaxColumns();
+ const maxRow = worksheet.getMaxRows();
+ const maxCol = worksheet.getMaxColumns();
const { expandToGapFirst, loop } = params;
- const currentRange = currentSelection.range;
+ const { range, primary } = selection;
const isWholeSheetSelected =
- currentRange.endColumn === maxCol - 1 &&
- currentRange.endRow === maxRow - 1 &&
- currentRange.startRow === 0 &&
- currentRange.startColumn === 0;
+ range.endColumn === maxCol - 1 &&
+ range.endRow === maxRow - 1 &&
+ range.startRow === 0 &&
+ range.startColumn === 0;
- if (!RANGES_STACK.some((s) => Rectangle.equals(s, currentRange))) {
+ if (!RANGES_STACK.some((s) => Rectangle.equals(s, range))) {
RANGES_STACK = [];
- RANGES_STACK.push(currentRange);
+ RANGES_STACK.push(range);
}
let destRange: IRange;
+ // determined what kind of adjustment it should get
if (isWholeSheetSelected) {
if (loop) {
- const currentSelectionIndex = RANGES_STACK.findIndex((s) => Rectangle.equals(s, currentRange));
+ const currentSelectionIndex = RANGES_STACK.findIndex((s) => Rectangle.equals(s, range));
if (currentSelectionIndex !== RANGES_STACK.length - 1) {
return false;
}
@@ -214,40 +213,27 @@ export const SelectAllCommand: ICommand<ISelectAllCommandParams> = {
return false;
}
} else if (expandToGapFirst) {
- destRange = expandToContinuousRange(
- currentRange,
- { left: true, right: true, up: true, down: true },
- currentWorksheet
- );
-
- if (Rectangle.equals(destRange, currentRange)) {
- destRange = expandToWholeSheet(currentWorksheet);
+ destRange = expandToContinuousRange(range, { left: true, right: true, up: true, down: true }, worksheet);
+ if (Rectangle.equals(destRange, range)) {
+ // already to gaps
+ destRange = expandToWholeSheet(worksheet);
}
} else {
- destRange = expandToWholeSheet(currentWorksheet);
+ destRange = expandToWholeSheet(worksheet);
}
if (!RANGES_STACK.some((s) => Rectangle.equals(s, destRange))) {
RANGES_STACK.push(destRange);
}
- return commandService.executeCommand(SetSelectionsOperation.id, {
- unitId: currentWorkbook.getUnitId(),
- sheetId: currentWorksheet.getSheetId(),
+ return accessor.get(ICommandService).executeCommand(SetSelectionsOperation.id, {
+ unitId: workbook.getUnitId(),
+ sheetId: worksheet.getSheetId(),
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
selections: [
{
range: destRange,
- primary: {
- row: destRange.startRow,
- column: destRange.startColumn,
- isMerged: false,
- isMergedMainCell: false,
- startRow: destRange.startRow,
- startColumn: destRange.startColumn,
- endRow: destRange.endRow,
- endColumn: destRange.endColumn,
- },
+ primary, // this remains unchanged
},
],
});
diff --git a/packages/base-sheets/src/commands/commands/utils/selection-util.ts b/packages/base-sheets/src/commands/commands/utils/selection-util.ts
index c00d3324bc..5c7eead499 100644
--- a/packages/base-sheets/src/commands/commands/utils/selection-util.ts
+++ b/packages/base-sheets/src/commands/commands/utils/selection-util.ts
@@ -3,6 +3,7 @@ import {
getReverseDirection,
ICellData,
IRange,
+ ISelection,
ISelectionCell,
ObjectMatrix,
RANGE_TYPE,
@@ -18,7 +19,7 @@ export interface IExpandParams {
down?: boolean;
}
-export function getRangeAtPosition(row: number, col: number, worksheet: Worksheet): ISelectionCell {
+export function getCellAtRowCol(row: number, col: number, worksheet: Worksheet): ISelectionCell {
let destRange: ISelectionCell | null = null;
const matrix = worksheet.getMatrixWithMergedCells(row, col, row, col);
@@ -34,10 +35,22 @@ export function getRangeAtPosition(row: number, col: number, worksheet: Workshee
endColumn: col + (value.colSpan !== undefined ? value.colSpan - 1 : 0),
rangeType: RANGE_TYPE.NORMAL,
};
+
+ return false;
});
if (!destRange) {
- throw new Error();
+ return {
+ actualColumn: col,
+ actualRow: row,
+ startRow: row,
+ startColumn: col,
+ endRow: row,
+ endColumn: col,
+ isMerged: false,
+ isMergedMainCell: false,
+ rangeType: RANGE_TYPE.NORMAL,
+ };
}
return destRange;
@@ -70,10 +83,13 @@ export function findNextRange(startRange: IRange, direction: Direction, workshee
}
export function findNextGapRange(startRange: IRange, direction: Direction, worksheet: Worksheet): IRange {
- let destRange = { ...startRange };
- const maxRow = worksheet.getMaxRows();
+ const destRange = { ...startRange };
+
+ const lastRow = worksheet.getMaxRows() - 1;
+ const lastColumn = worksheet.getMaxColumns() - 1;
+
+ const { startRow, startColumn, endRow, endColumn } = getEdgeOfRange(startRange, direction, worksheet);
- const { startRow, startColumn, endRow, endColumn } = getLastArrayOfRange(startRange, direction, worksheet);
let currentPositionHasValue = rangeHasValue(worksheet, startRow, startColumn, endRow, endColumn).hasValue;
let firstMove = true;
let shouldContinue = true;
@@ -85,48 +101,7 @@ export function findNextGapRange(startRange: IRange, direction: Direction, works
break;
}
- const destRow = destRange.startRow - 1; // it may decrease if there are merged cell
- const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
- worksheet,
- destRow,
- destRange.startColumn,
- destRow,
- destRange.endColumn
- );
-
- if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
- shouldContinue = false;
- break;
- } else {
- const r = {
- startColumn: destRange.startColumn,
- endColumn: destRange.endColumn,
- startRow: 1_000_000_000,
- endRow: 0,
- };
- matrix.forValue((row, _, value) => {
- r.startRow = Math.min(row, r.startRow);
- r.endRow = Math.max(row + (value.rowSpan || 1) - 1, r.endRow);
- });
- destRange = r;
-
- if (!currentPositionHasValue && nextRangeHasValue) {
- shouldContinue = false;
- break;
- }
-
- currentPositionHasValue = nextRangeHasValue;
- firstMove = false;
- }
- }
-
- if (Direction.DOWN === direction) {
- if (destRange.endRow === maxRow - 1) {
- shouldContinue = false;
- break;
- }
-
- const nextRow = destRange.endRow + 1;
+ const nextRow = destRange.startRow - 1; // it may decrease if there are merged cell
const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
worksheet,
nextRow,
@@ -139,168 +114,31 @@ export function findNextGapRange(startRange: IRange, direction: Direction, works
shouldContinue = false;
break;
} else {
- const r = {
- startColumn: destRange.startColumn,
- endColumn: destRange.endColumn,
- startRow: 1_000_000_000,
- endRow: 0,
- };
- matrix.forValue((row, _, value) => {
- r.startRow = Math.min(r.startRow, row);
- r.endRow = Math.max(row + (value.rowSpan || 1) - 1, r.endRow);
- });
- destRange = r;
-
- if (!currentPositionHasValue && nextRangeHasValue) {
- shouldContinue = false;
- break;
+ if (matrix.getLength() !== 0) {
+ // update searching ranges
+ let min = nextRow;
+ matrix.forValue((row) => {
+ min = Math.min(row, min);
+ });
+ destRange.startRow = min;
+ } else {
+ destRange.startRow = nextRow;
}
- currentPositionHasValue = nextRangeHasValue;
- firstMove = false;
- }
- }
-
- if (Direction.LEFT === direction) {
- if (destRange.startColumn === 0) {
- shouldContinue = false;
- break;
- }
-
- const destCol = destRange.startColumn - 1;
- const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
- worksheet,
- destRange.startRow,
- destCol,
- destRange.endRow,
- destCol
- );
-
- if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
- shouldContinue = false;
- break;
- } else {
- const r = {
- startRow: destRange.startRow,
- endRow: destRange.endRow,
- startColumn: 1_000_000_000,
- endColumn: 0,
- };
- matrix.forValue((_, col, value) => {
- r.startColumn = Math.min(col, r.startColumn);
- r.endColumn = Math.max(col + (value.colSpan || 1) - 1, r.endColumn);
- });
-
- destRange = r;
-
- if (!currentPositionHasValue && nextRangeHasValue) {
- shouldContinue = false;
- break;
- }
-
- currentPositionHasValue = nextRangeHasValue;
- firstMove = false;
- }
- }
-
- if (Direction.RIGHT === direction) {
- if (destRange.endColumn === worksheet.getMaxColumns() - 1) {
- shouldContinue = false;
- break;
- }
-
- const destCol = destRange.endColumn + 1;
- const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
- worksheet,
- destRange.startRow,
- destCol,
- destRange.endRow,
- destCol
- );
-
- if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
- shouldContinue = false;
- break;
- } else {
- const r = {
- startRow: destRange.startRow,
- endRow: destRange.endRow,
- startColumn: 1_000_000_000,
- endColumn: 0,
- };
- matrix.forValue((_, col, value) => {
- r.startColumn = Math.min(col, r.startColumn);
- r.endColumn = Math.max(col + (value.colSpan || 1) - 1, r.endColumn);
- });
- destRange = r;
-
- if (!currentPositionHasValue && nextRangeHasValue) {
- shouldContinue = false;
- break;
- }
-
- currentPositionHasValue = nextRangeHasValue;
- firstMove = false;
- }
- }
- }
-
- return destRange;
-}
-
-export function expandToNextGapCell(startRange: IRange, direction: Direction, worksheet: Worksheet): IRange {
- const destRange = { ...startRange };
- const maxRow = worksheet.getMaxRows();
-
- const { startRow, startColumn, endRow, endColumn } = getLastArrayOfRange(startRange, direction, worksheet);
- let currentPositionHasValue = rangeHasValue(worksheet, startRow, startColumn, endRow, endColumn).hasValue;
- let firstMove = true;
- let shouldContinue = true;
-
- while (shouldContinue) {
- if (Direction.UP === direction) {
- if (destRange.startRow === 0) {
- shouldContinue = false;
- break;
- }
-
- const destRow = destRange.startRow - 1; // it may decrease if there are merged cell
- const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
- worksheet,
- destRow,
- destRange.startColumn,
- destRow,
- destRange.endColumn
- );
-
- // case 1
- if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
- shouldContinue = false;
- break;
- } else {
- matrix.forValue((row, col, value) => {
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.startColumn = Math.min(col, destRange.startColumn);
- destRange.endColumn = Math.max(
- col + (value.colSpan === undefined ? 0 : value.colSpan - 1),
- destRange.endColumn
- );
- });
+ destRange.endRow = destRange.startRow;
- // case 2
if (!currentPositionHasValue && nextRangeHasValue) {
shouldContinue = false;
break;
}
- // other cases
currentPositionHasValue = nextRangeHasValue;
firstMove = false;
}
}
if (Direction.DOWN === direction) {
- if (destRange.endRow === maxRow - 1) {
+ if (destRange.endRow === lastRow) {
shouldContinue = false;
break;
}
@@ -318,11 +156,17 @@ export function expandToNextGapCell(startRange: IRange, direction: Direction, wo
shouldContinue = false;
break;
} else {
- matrix.forValue((row, col, value) => {
- destRange.startColumn = Math.min(col, destRange.startColumn);
- destRange.endRow = Math.max(row + (value.rowSpan || 1) - 1, destRange.endRow);
- destRange.endColumn = Math.max(col + (value.colSpan || 1) - 1, destRange.endColumn);
- });
+ if (matrix.getLength() !== 0) {
+ let max = nextRow;
+ matrix.forValue((row, _, value) => {
+ max = Math.max(row + (value.rowSpan || 1) - 1, max);
+ });
+ destRange.endRow = max;
+ } else {
+ destRange.endRow = nextRow;
+ }
+
+ destRange.startRow = destRange.endRow;
if (!currentPositionHasValue && nextRangeHasValue) {
shouldContinue = false;
@@ -340,27 +184,30 @@ export function expandToNextGapCell(startRange: IRange, direction: Direction, wo
break;
}
- const destCol = destRange.startColumn - 1;
+ const nextCol = destRange.startColumn - 1;
const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
worksheet,
destRange.startRow,
- destCol,
+ nextCol,
destRange.endRow,
- destCol
+ nextCol
);
if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
shouldContinue = false;
break;
} else {
- matrix.forValue((row, col, value) => {
- destRange.startColumn = Math.min(col, destRange.startColumn);
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.endRow = Math.max(
- row + (value.rowSpan === undefined ? 0 : value.rowSpan - 1),
- destRange.endRow
- );
- });
+ if (matrix.getLength() !== 0) {
+ let min = nextCol;
+ matrix.forValue((_, col) => {
+ min = Math.min(col, min);
+ });
+ destRange.startColumn = min;
+ } else {
+ destRange.startColumn = nextCol;
+ }
+
+ destRange.endColumn = destRange.startColumn;
if (!currentPositionHasValue && nextRangeHasValue) {
shouldContinue = false;
@@ -373,35 +220,35 @@ export function expandToNextGapCell(startRange: IRange, direction: Direction, wo
}
if (Direction.RIGHT === direction) {
- if (destRange.endColumn === worksheet.getMaxColumns() - 1) {
+ if (destRange.endColumn === lastColumn) {
shouldContinue = false;
break;
}
- const destCol = destRange.endColumn + 1;
+ const nextCol = destRange.endColumn + 1;
const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
worksheet,
destRange.startRow,
- destCol,
+ nextCol,
destRange.endRow,
- destCol
+ nextCol
);
if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
shouldContinue = false;
break;
} else {
- matrix.forValue((row, col, value) => {
- destRange.startRow = Math.min(row, destRange.startRow);
- destRange.endRow = Math.max(
- row + (value.rowSpan === undefined ? 0 : value.rowSpan - 1),
- destRange.endRow
- );
- destRange.endColumn = Math.max(
- col + (value.colSpan === undefined ? 0 : value.colSpan - 1),
- destRange.endColumn
- );
- });
+ if (matrix.getLength() !== 0) {
+ let max = nextCol;
+ matrix.forValue((_, col, value) => {
+ max = Math.max(col + (value.colSpan || 1) - 1, max);
+ });
+ destRange.endColumn = max;
+ } else {
+ destRange.endColumn = nextCol;
+ }
+
+ destRange.startColumn = destRange.endColumn;
if (!currentPositionHasValue && nextRangeHasValue) {
shouldContinue = false;
@@ -414,7 +261,12 @@ export function expandToNextGapCell(startRange: IRange, direction: Direction, wo
}
}
- return destRange;
+ return alignToMergedCellsBorders(destRange, worksheet, true);
+}
+
+export function expandToNextGapRange(startRange: IRange, direction: Direction, worksheet: Worksheet): IRange {
+ const next = findNextGapRange(startRange, direction, worksheet);
+ return alignToMergedCellsBorders(Rectangle.union(next, startRange), worksheet, true);
}
/**
@@ -468,7 +320,7 @@ export function expandToNextCell(startRange: IRange, direction: Direction, works
* @param direction
* @param worksheet
*/
-export function shrinkToNextGapCell(
+export function shrinkToNextGapRange(
startRange: IRange,
anchorRange: IRange,
direction: Direction,
@@ -476,11 +328,7 @@ export function shrinkToNextGapCell(
): IRange {
// use `moveToNextGapCell` reversely to get the next going to cell
const reversedDirection = getReverseDirection(direction);
- const nextGap = findNextGapRange(
- getLastArrayOfRange(startRange, reversedDirection, worksheet),
- direction,
- worksheet
- );
+ const nextGap = findNextGapRange(getEdgeOfRange(startRange, reversedDirection, worksheet), direction, worksheet);
// if next exceed the startRange we should just expand anchorRange with
if (direction === Direction.UP && nextGap.startRow <= startRange.startRow) {
@@ -511,7 +359,7 @@ export function shrinkToNextGapCell(
true
);
}
- return Rectangle.union(anchorRange, nextGap);
+ return Rectangle.union(Rectangle.clone(anchorRange), nextGap);
}
/**
@@ -520,25 +368,13 @@ export function shrinkToNextGapCell(
* @param direction
* @param worksheet
*/
-export function shrinkToNextCell(
- startRange: IRange,
- anchorRange: IRange,
- direction: Direction,
- worksheet: Worksheet
-): IRange {
- // use `moveToNextCell` reversely to get the next going to cell
+export function shrinkToNextCell(startRange: IRange, direction: Direction, worksheet: Worksheet): IRange {
+ // use `moveToNextCell` reversely to get the next going to range
const reversedDirection = getReverseDirection(direction);
- const next = findNextRange(getLastArrayOfRange(startRange, reversedDirection, worksheet), direction, worksheet);
-
- const destRange: IRange = {
- startRow: Math.min(anchorRange.startRow, next.startRow),
- startColumn: Math.min(anchorRange.startColumn, next.startColumn),
- endRow: Math.max(anchorRange.endRow, next.endRow),
- endColumn: Math.max(anchorRange.endColumn, next.endColumn),
- rangeType: RANGE_TYPE.NORMAL,
- };
-
- return destRange;
+ const shrinkFromEdge = getEdgeOfRange(startRange, reversedDirection, worksheet);
+ const otherEdge = getEdgeOfRange(startRange, direction, worksheet);
+ const next = findNextRange(shrinkFromEdge, direction, worksheet);
+ return alignToMergedCellsBorders(Rectangle.union(otherEdge, next), worksheet, false);
}
export function expandToContinuousRange(startRange: IRange, directions: IExpandParams, worksheet: Worksheet): IRange {
@@ -652,7 +488,7 @@ export function expandToWholeSheet(worksheet: Worksheet): IRange {
};
}
-function getLastArrayOfRange(startRange: IRange, direction: Direction, worksheet: Worksheet): IRange {
+function getEdgeOfRange(startRange: IRange, direction: Direction, worksheet: Worksheet): IRange {
let destRange: IRange;
switch (direction) {
case Direction.UP:
@@ -723,3 +559,50 @@ function rangeHasValue(
matrix,
};
}
+
+export function getStartRange(range: IRange, primary: ISelectionCell, direction: Direction): IRange {
+ const ret = Rectangle.clone(range);
+
+ switch (direction) {
+ case Direction.UP:
+ case Direction.DOWN:
+ ret.startColumn = ret.endColumn = primary.actualColumn;
+ break;
+ case Direction.LEFT:
+ case Direction.RIGHT:
+ ret.startRow = ret.endRow = primary.actualRow;
+ break;
+ }
+
+ return ret;
+}
+
+export function checkIfShrink(selection: ISelection, direction: Direction, worksheet: Worksheet): boolean {
+ const { primary, range } = selection;
+
+ const startRange: IRange = Rectangle.clone(range);
+ switch (direction) {
+ case Direction.UP:
+ case Direction.DOWN:
+ startRange.startRow = primary!.startRow;
+ startRange.endRow = primary!.endRow;
+ break;
+ case Direction.LEFT:
+ case Direction.RIGHT:
+ startRange.startColumn = primary!.startColumn;
+ startRange.endColumn = primary!.endColumn;
+ break;
+ }
+
+ const anchorRange = getEdgeOfRange(startRange, direction, worksheet);
+ switch (direction) {
+ case Direction.DOWN:
+ return range.startRow < anchorRange.startRow;
+ case Direction.UP:
+ return range.endRow > anchorRange.endRow;
+ case Direction.LEFT:
+ return anchorRange.endColumn < range.endColumn;
+ case Direction.RIGHT:
+ return anchorRange.startColumn > range.startColumn;
+ }
+}
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 99cf7cc956..e457e3e336 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -50,13 +50,13 @@ export {
export type { ISetRangeValuesCommandParams } from './commands/commands/set-range-values.command';
export { SetRangeValuesCommand } from './commands/commands/set-range-values.command';
export type {
- IChangeSelectionCommandParams,
IExpandSelectionCommandParams,
+ IMoveSelectionCommandParams,
ISelectAllCommandParams,
} from './commands/commands/set-selections.command';
export {
- ChangeSelectionCommand,
ExpandSelectionCommand,
+ MoveSelectionCommand,
SelectAllCommand,
} from './commands/commands/set-selections.command';
export {
diff --git a/packages/core/src/Types/Interfaces/IRange.ts b/packages/core/src/Types/Interfaces/IRange.ts
index 60a4d0b189..374105b1ac 100644
--- a/packages/core/src/Types/Interfaces/IRange.ts
+++ b/packages/core/src/Types/Interfaces/IRange.ts
@@ -20,23 +20,25 @@ export enum RANGE_TYPE {
*
* means "A1:B2"
*/
-// TODO@wzhudev: Rename to IRange
export interface IRange {
/**
* The start row (inclusive) of the range
* startRow
*/
startRow: number;
+
/**
* The start column (inclusive) of the range
* startColumn
*/
startColumn: number;
+
/**
* The end row (exclusive) of the range
* endRow
*/
endRow: number;
+
/**
* The end column (exclusive) of the range
* endColumn
diff --git a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
index 557a11905b..1d11613665 100644
--- a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
@@ -1,6 +1,6 @@
import {
- ChangeSelectionCommand,
ExpandSelectionCommand,
+ MoveSelectionCommand,
SelectAllCommand,
SetBoldCommand,
SetFontFamilyCommand,
@@ -132,7 +132,7 @@ export class SheetUIController extends Disposable {
// init commands
[
- ChangeSelectionCommand,
+ MoveSelectionCommand,
ExpandSelectionCommand,
SelectAllCommand,
SetBoldCommand,
diff --git a/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts b/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
index 3c6504fb61..22e39c5ea0 100644
--- a/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
+++ b/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
@@ -1,9 +1,9 @@
import {
- ChangeSelectionCommand,
ExpandSelectionCommand,
- IChangeSelectionCommandParams,
IExpandSelectionCommandParams,
+ IMoveSelectionCommandParams,
ISelectAllCommandParams,
+ MoveSelectionCommand,
SelectAllCommand,
} from '@univerjs/base-sheets';
import { IShortcutItem, KeyCode, MetaKeys } from '@univerjs/base-ui';
@@ -11,8 +11,8 @@ import { Direction, FOCUSING_SHEET } from '@univerjs/core';
import { SHEET_EDITOR_ACTIVATED } from '../../services/context/context';
-export const MoveSelectionDownShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionDownShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_DOWN,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -20,8 +20,8 @@ export const MoveSelectionDownShortcutItem: IShortcutItem<IChangeSelectionComman
},
};
-export const MoveSelectionUpShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionUpShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_UP,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -29,8 +29,8 @@ export const MoveSelectionUpShortcutItem: IShortcutItem<IChangeSelectionCommandP
},
};
-export const MoveSelectionLeftShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionLeftShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_LEFT,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -38,8 +38,8 @@ export const MoveSelectionLeftShortcutItem: IShortcutItem<IChangeSelectionComman
},
};
-export const MoveSelectionRightShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionRightShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_RIGHT,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -47,8 +47,8 @@ export const MoveSelectionRightShortcutItem: IShortcutItem<IChangeSelectionComma
},
};
-export const MoveSelectionTabShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionTabShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.TAB,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -58,8 +58,8 @@ export const MoveSelectionTabShortcutItem: IShortcutItem<IChangeSelectionCommand
// move selection to continuous end
-export const MoveSelectionEndDownShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionEndDownShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_DOWN | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -68,8 +68,8 @@ export const MoveSelectionEndDownShortcutItem: IShortcutItem<IChangeSelectionCom
},
};
-export const MoveSelectionEndUpShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionEndUpShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_UP | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -78,8 +78,8 @@ export const MoveSelectionEndUpShortcutItem: IShortcutItem<IChangeSelectionComma
},
};
-export const MoveSelectionEndLeftShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionEndLeftShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_LEFT | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
@@ -88,8 +88,8 @@ export const MoveSelectionEndLeftShortcutItem: IShortcutItem<IChangeSelectionCom
},
};
-export const MoveSelectionEndRightShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
- id: ChangeSelectionCommand.id,
+export const MoveSelectionEndRightShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
+ id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_RIGHT | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {