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(sheet): implement sheet selection features basics (#174)

Wenzhao Hu7df3290441a4
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 400d2bf5ef..f1a24b911a 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -1,2 +1,4 @@
+packages/base-sheets/src/commands/commands/set-selections.command.ts @wzhudev
+packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts @wzhudev
docs/graphs.tldr @wzhudev
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 684ec2822c..9ad1c81b41 100644
--- a/packages/base-sheets/src/commands/commands/set-selections.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-selections.command.ts
@@ -4,15 +4,24 @@ import {
ICommand,
ICommandService,
ICurrentUniverService,
+ ILogService,
ISelectionRange,
+ Rectangle,
} from '@univerjs/core';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../services/selection-manager.service';
import { SetSelectionsOperation } from '../operations/selection.operation';
+import {
+ expandToContinuousRange,
+ expandToNextGapCell,
+ expandToWholeSheet,
+ findNextGapCell,
+} from './utils/selection-util';
export interface IChangeSelectionCommandParams {
direction: Direction;
- toEnd?: boolean;
+ toNextGap?: boolean;
+ expand?: boolean;
}
export const ChangeSelectionCommand: ICommand<IChangeSelectionCommandParams> = {
@@ -30,35 +39,56 @@ export const ChangeSelectionCommand: ICommand<IChangeSelectionCommandParams> = {
return false;
}
- const { direction } = params;
- const originSelection = selections[selections.length - 1];
- const destRange: ISelectionRange = { ...originSelection };
-
- // FIXME: some error here. The selection does not know if there is a span cell.
- switch (direction) {
- case Direction.UP:
- destRange.startRow = Math.max(0, originSelection.startRow - 1);
- destRange.endRow = destRange.startRow;
- break;
- case Direction.DOWN:
- destRange.startRow = Math.min(originSelection.endRow + 1, currentWorksheet.getRowCount() - 1);
- destRange.endRow = destRange.startRow;
- break;
- case Direction.LEFT:
- destRange.startColumn = Math.max(0, originSelection.startColumn - 1);
- destRange.endColumn = destRange.startColumn;
- break;
- case Direction.RIGHT:
- destRange.startColumn = Math.min(originSelection.endColumn + 1, currentWorksheet.getColumnCount() - 1);
- destRange.endColumn = destRange.startColumn;
- break;
- default:
- break;
- }
+ const { direction, toNextGap, expand } = params;
+ const startRange = selections[selections.length - 1];
- // TODO: deal with `toEnd` parameter here
+ let destRange: ISelectionRange = { ...startRange };
- commandService.executeCommand(SetSelectionsOperation.id, {
+ if (!toNextGap) {
+ // navigation must be within the worksheet's range
+ switch (direction) {
+ case Direction.UP:
+ destRange.startRow = Math.max(0, startRange.startRow - 1);
+ destRange.endRow = destRange.startRow;
+ break;
+ case Direction.DOWN:
+ destRange.startRow = Math.min(startRange.endRow + 1, currentWorksheet.getRowCount() - 1);
+ destRange.endRow = destRange.startRow;
+ break;
+ case Direction.LEFT:
+ destRange.startColumn = Math.max(0, startRange.startColumn - 1);
+ destRange.endColumn = destRange.startColumn;
+ break;
+ case Direction.RIGHT:
+ destRange.startColumn = Math.min(startRange.endColumn + 1, currentWorksheet.getColumnCount() - 1);
+ destRange.endColumn = destRange.startColumn;
+ break;
+ default:
+ break;
+ }
+
+ // deal with merged cells
+ currentWorksheet
+ .getMatrixWithMergedCells(
+ destRange.startRow,
+ destRange.startColumn,
+ destRange.startRow,
+ destRange.startColumn
+ )
+ .forValue((row, col, value) => {
+ destRange.startRow = row;
+ destRange.startColumn = col;
+ destRange.endRow = row + (value.rowSpan !== undefined ? value.rowSpan - 1 : 0);
+ destRange.endColumn = col + (value.colSpan !== undefined ? value.colSpan - 1 : 0);
+ });
+ } else if (!expand) {
+ destRange = findNextGapCell(destRange, direction, currentWorksheet);
+ } else {
+ destRange = expandToNextGapCell(destRange, direction, currentWorksheet);
+ console.log('debug expand to:', destRange);
+ }
+
+ return commandService.executeCommand(SetSelectionsOperation.id, {
unitId: currentWorkbook.getUnitId(),
sheetId: currentWorksheet.getSheetId(),
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
@@ -78,14 +108,13 @@ export const ChangeSelectionCommand: ICommand<IChangeSelectionCommandParams> = {
},
],
});
-
- return true;
},
};
export interface IExpandSelectionCommandParams {
direction: Direction;
- toEnd?: boolean;
+ toNextGap?: boolean;
+ expand?: boolean;
}
export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
@@ -93,3 +122,111 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
type: CommandType.COMMAND,
handler: async (accessor, params) => true,
};
+
+export interface ISelectAllCommandParams {
+ expandToGapFirst?: boolean;
+ loop?: boolean;
+}
+
+let RANGES_STACK: ISelectionRange[] = [];
+
+let SELECTED_RANGE_WORKSHEET = '';
+
+/**
+ * This command expand selection to all neighbor ranges. If there are no neighbor ranges. Select the whole sheet.
+ */
+export const SelectAllCommand: ICommand<ISelectAllCommandParams> = {
+ id: 'sheet.command.select-all',
+ type: CommandType.COMMAND,
+ onDispose() {
+ RANGES_STACK = [];
+ SELECTED_RANGE_WORKSHEET = '';
+ },
+ handler: async (accessor, params = { expandToGapFirst: true, loop: false }) => {
+ const selectionManager = accessor.get(SelectionManagerService);
+ const currentUniverService = accessor.get(ICurrentUniverService);
+ const logService = accessor.get(ILogService);
+
+ const currentSelection = selectionManager.getLast();
+ const currentWorkbook = currentUniverService.getCurrentUniverSheetInstance()?.getWorkBook();
+ const currentWorksheet = currentWorkbook.getActiveSheet();
+
+ if (!currentSelection || !currentWorksheet) {
+ return false;
+ }
+
+ const id = `${currentWorkbook.getUnitId()}|${currentWorksheet.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 { expandToGapFirst, loop } = params;
+ const currentRange = currentSelection.rangeData;
+ const isWholeSheetSelected =
+ currentRange.endColumn === maxCol - 1 &&
+ currentRange.endRow === maxRow - 1 &&
+ currentRange.startRow === 0 &&
+ currentRange.startColumn === 0;
+
+ if (!RANGES_STACK.some((s) => Rectangle.equals(s, currentRange))) {
+ RANGES_STACK = [];
+ RANGES_STACK.push(currentRange);
+ }
+
+ let destRange: ISelectionRange;
+
+ if (isWholeSheetSelected) {
+ if (loop) {
+ const currentSelectionIndex = RANGES_STACK.findIndex((s) => Rectangle.equals(s, currentRange));
+ if (currentSelectionIndex !== RANGES_STACK.length - 1) {
+ return false;
+ }
+
+ destRange = RANGES_STACK[0];
+ } else {
+ 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);
+ }
+ } else {
+ destRange = expandToWholeSheet(currentWorksheet);
+ }
+
+ if (!RANGES_STACK.some((s) => Rectangle.equals(s, destRange))) {
+ RANGES_STACK.push(destRange);
+ }
+
+ return commandService.executeCommand(SetSelectionsOperation.id, {
+ unitId: currentWorkbook.getUnitId(),
+ sheetId: currentWorksheet.getSheetId(),
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: [
+ {
+ rangeData: destRange,
+ cellRange: {
+ row: destRange.startRow,
+ column: destRange.startColumn,
+ isMerged: false,
+ isMergedMainCell: false,
+ startRow: destRange.startRow,
+ startColumn: destRange.startColumn,
+ endRow: destRange.endRow,
+ endColumn: destRange.endColumn,
+ },
+ },
+ ],
+ });
+ },
+};
diff --git a/packages/base-sheets/src/commands/commands/utils/selection-util.ts b/packages/base-sheets/src/commands/commands/utils/selection-util.ts
new file mode 100644
index 0000000000..15efd5fb09
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/utils/selection-util.ts
@@ -0,0 +1,410 @@
+import { Direction, ICellData, ISelectionRange, ObjectMatrix, Worksheet } from '@univerjs/core';
+
+export interface IExpandParams {
+ left?: boolean;
+ right?: boolean;
+ up?: boolean;
+ down?: boolean;
+}
+
+/**
+ * If the current cell has value
+ */
+export function findNextGapCell(
+ startRange: ISelectionRange,
+ direction: Direction,
+ worksheet: Worksheet
+): ISelectionRange {
+ const params: IExpandParams = {
+ left: Direction.LEFT === direction,
+ right: Direction.RIGHT === direction,
+ up: Direction.UP === direction,
+ down: Direction.DOWN === direction,
+ };
+
+ const expandedRange = expandToNextGapCell(startRange, direction, worksheet);
+ const row =
+ Direction.UP === direction
+ ? expandedRange.startRow
+ : Direction.DOWN === direction
+ ? expandedRange.endRow
+ : startRange.startRow;
+ const column =
+ Direction.LEFT === direction
+ ? expandedRange.startColumn
+ : Direction.RIGHT === direction
+ ? expandedRange.endColumn
+ : startRange.startColumn;
+ const matrix = worksheet.getMatrixWithMergedCells(row, column, row, column);
+
+ let destRange: ISelectionRange | null = null;
+ matrix.forValue((row, col, value) => {
+ destRange = {
+ startRow: row,
+ startColumn: col,
+ endRow: row + (value.rowSpan !== undefined ? value.rowSpan - 1 : 0),
+ endColumn: col + (value.colSpan !== undefined ? value.colSpan - 1 : 0),
+ };
+ });
+
+ if (!destRange) {
+ throw new Error();
+ }
+
+ return destRange;
+}
+
+export function expandToNextGapCell(
+ startRange: ISelectionRange,
+ direction: Direction,
+ worksheet: Worksheet
+): ISelectionRange {
+ const params: IExpandParams = {
+ left: Direction.LEFT === direction,
+ right: Direction.RIGHT === direction,
+ up: Direction.UP === direction,
+ down: Direction.DOWN === direction,
+ };
+
+ const destRange = { ...startRange };
+ const maxRow = worksheet.getMaxRows();
+
+ function rangeHasValue(
+ row: number,
+ col: number,
+ rowEnd: number,
+ colEnd: number
+ ): {
+ hasValue: boolean;
+ matrix: ObjectMatrix<ICellData & { rowSpan?: number; colSpan?: number }>;
+ } {
+ let hasValue = false;
+
+ const matrix = worksheet.getMatrixWithMergedCells(row, col, rowEnd, colEnd).forValue((_, __, value) => {
+ if (value.v) {
+ hasValue = true;
+ return false; // stop looping
+ }
+ });
+
+ return {
+ hasValue,
+ matrix,
+ };
+ }
+
+ const { startRow, startColumn, endRow, endColumn } = getLastArrayOfRange(startRange, direction);
+ let currentPositionHasValue = rangeHasValue(startRow, startColumn, endRow, endColumn).hasValue;
+ let firstMove = true;
+ let shouldContinue = true;
+
+ while (shouldContinue) {
+ if (params.up) {
+ 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(
+ destRow,
+ destRange.startColumn,
+ destRow,
+ destRange.endColumn
+ );
+
+ 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
+ );
+ });
+
+ if (!currentPositionHasValue && nextRangeHasValue) {
+ shouldContinue = false;
+ break;
+ }
+
+ currentPositionHasValue = nextRangeHasValue;
+ firstMove = false;
+ }
+ }
+
+ if (params.down) {
+ if (destRange.endRow === maxRow - 1) {
+ shouldContinue = false;
+ break;
+ }
+
+ const nextRow = destRange.endRow + 1;
+ const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
+ nextRow,
+ destRange.startColumn,
+ nextRow,
+ destRange.endColumn
+ );
+
+ if (currentPositionHasValue && !nextRangeHasValue && !firstMove) {
+ shouldContinue = false;
+ break;
+ } else {
+ matrix.forValue((row, col, value) => {
+ destRange.startColumn = Math.min(col, destRange.startColumn);
+ destRange.endRow = Math.max(
+ row + (value.rowSpan !== undefined ? value.rowSpan - 1 : 0),
+ destRange.endRow
+ );
+ destRange.endColumn = Math.max(
+ col + (value.rowSpan === undefined ? 0 : value.rowSpan - 1),
+ destRange.endColumn
+ );
+ });
+
+ if (!currentPositionHasValue && nextRangeHasValue) {
+ shouldContinue = false;
+ break;
+ }
+
+ currentPositionHasValue = nextRangeHasValue;
+ firstMove = false;
+ }
+ }
+
+ if (params.left) {
+ if (destRange.startColumn === 0) {
+ shouldContinue = false;
+ break;
+ }
+
+ const destCol = destRange.startColumn - 1;
+ const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
+ destRange.startRow,
+ destCol,
+ destRange.endRow,
+ destCol
+ );
+
+ 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 (!currentPositionHasValue && nextRangeHasValue) {
+ shouldContinue = false;
+ break;
+ }
+
+ currentPositionHasValue = nextRangeHasValue;
+ firstMove = false;
+ }
+ }
+
+ if (params.right) {
+ if (destRange.endColumn === worksheet.getMaxColumns() - 1) {
+ shouldContinue = false;
+ break;
+ }
+
+ const destCol = destRange.endColumn + 1;
+ const { hasValue: nextRangeHasValue, matrix } = rangeHasValue(
+ destRange.startRow,
+ destCol,
+ destRange.endRow,
+ destCol
+ );
+
+ 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 (!currentPositionHasValue && nextRangeHasValue) {
+ shouldContinue = false;
+ break;
+ }
+
+ currentPositionHasValue = nextRangeHasValue;
+ firstMove = false;
+ }
+ }
+ }
+
+ return destRange;
+}
+
+// NOTE@wzhudev: Worksheet or Workbook used in commands layer / service layer / controller layer me should be
+// changed to SheerViewModel or BookViewModel in the future.
+
+export function expandToContinuousRange(
+ startRange: ISelectionRange,
+ directions: IExpandParams,
+ worksheet: Worksheet
+): ISelectionRange {
+ const { left, right, up, down } = directions;
+ const maxRow = worksheet.getMaxRows();
+ const maxColumn = worksheet.getMaxColumns();
+
+ let changed = true;
+ const destRange: ISelectionRange = { ...startRange }; // startRange should not be used below
+
+ while (changed) {
+ changed = false;
+
+ if (up && destRange.startRow !== 0) {
+ // see if there are value in the upper row of contents
+ // set `changed` to true if `startRow` really changes
+ const destRow = destRange.startRow - 1; // it may decrease if there are merged cell
+ const matrixFromLastRow = worksheet.getMatrixWithMergedCells(
+ destRow,
+ destRange.startColumn,
+ destRow,
+ destRange.endColumn
+ );
+
+ // we should check if there are value in the upper row of contents, if it does
+ // we should update the `destRange` and set `changed` to true
+ matrixFromLastRow.forValue((row, col, value) => {
+ if (value.v) {
+ destRange.startRow = Math.min(row, destRange.startRow);
+ destRange.startColumn = Math.min(col, destRange.startColumn);
+ destRange.endColumn = Math.max(col, destRange.endColumn);
+ changed = true;
+ }
+ });
+ }
+
+ if (down && destRange.endRow !== maxRow - 1) {
+ const destRow = destRange.endRow + 1;
+ const matrixFromLastRow = worksheet.getMatrixWithMergedCells(
+ destRow,
+ destRange.startColumn,
+ destRow,
+ destRange.endColumn
+ );
+
+ matrixFromLastRow.forValue((row, col, value) => {
+ if (value.v) {
+ destRange.endRow = Math.max(
+ row + (value.rowSpan !== undefined ? value.rowSpan - 1 : 0),
+ destRange.endRow
+ );
+ destRange.startColumn = Math.min(col, destRange.startColumn);
+ destRange.endColumn = Math.max(col, destRange.endColumn);
+ changed = true;
+ }
+ });
+ }
+
+ if (left && destRange.startRow !== 0) {
+ const destCol = destRange.startColumn - 1;
+ const matrixFromLastCol = worksheet.getMatrixWithMergedCells(
+ destRange.startRow,
+ destCol,
+ destRange.endRow,
+ destCol
+ );
+
+ matrixFromLastCol.forValue((row, col, value) => {
+ if (value.v) {
+ destRange.startColumn = Math.min(col, destRange.startColumn);
+ destRange.startRow = Math.min(row, destRange.startRow);
+ destRange.endRow = Math.max(row, destRange.endRow);
+ changed = true;
+ }
+ });
+ }
+
+ if (right && destRange.endColumn !== maxColumn - 1) {
+ const destCol = destRange.endColumn + 1;
+ const matrixFromLastCol = worksheet.getMatrixWithMergedCells(
+ destRange.startRow,
+ destCol,
+ destRange.endRow,
+ destCol
+ );
+
+ matrixFromLastCol.forValue((row, col, value) => {
+ if (value.v) {
+ destRange.endColumn = Math.max(
+ col + (value.colSpan !== undefined ? value.colSpan - 1 : 0),
+ destRange.endColumn
+ );
+ destRange.startRow = Math.min(row, destRange.startRow);
+ destRange.endRow = Math.max(row, destRange.endRow);
+ changed = true;
+ }
+ });
+ }
+ }
+
+ return destRange;
+}
+
+export function expandToWholeSheet(worksheet: Worksheet): ISelectionRange {
+ return {
+ startRow: 0,
+ startColumn: 0,
+ endRow: worksheet.getMaxRows() - 1,
+ endColumn: worksheet.getMaxColumns() - 1,
+ };
+}
+
+function getLastArrayOfRange(startRange: ISelectionRange, direction: Direction): ISelectionRange {
+ switch (direction) {
+ case Direction.UP:
+ return {
+ startRow: startRange.startRow,
+ startColumn: startRange.startColumn,
+ endRow: startRange.startRow,
+ endColumn: startRange.endColumn,
+ };
+ case Direction.DOWN:
+ return {
+ startRow: startRange.endRow,
+ startColumn: startRange.startColumn,
+ endRow: startRange.endRow,
+ endColumn: startRange.endColumn,
+ };
+ case Direction.LEFT:
+ return {
+ startRow: startRange.startRow,
+ startColumn: startRange.startColumn,
+ endRow: startRange.endRow,
+ endColumn: startRange.startColumn,
+ };
+ case Direction.RIGHT:
+ return {
+ startRow: startRange.startRow,
+ startColumn: startRange.endColumn,
+ endRow: startRange.endRow,
+ endColumn: startRange.endColumn,
+ };
+ default:
+ throw new Error('Invalid direction');
+ }
+}
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 0efeb85e7a..1f7fa1b818 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -67,6 +67,8 @@ export {
ExpandSelectionCommand,
IChangeSelectionCommandParams,
IExpandSelectionCommandParams,
+ ISelectAllCommandParams,
+ SelectAllCommand,
} from './commands/commands/set-selections.command';
export {
ResetBackgroundColorCommand,
diff --git a/packages/core/src/Sheets/Domain/Worksheet.ts b/packages/core/src/Sheets/Domain/Worksheet.ts
index 4e4fabc5f8..0c26934ed0 100644
--- a/packages/core/src/Sheets/Domain/Worksheet.ts
+++ b/packages/core/src/Sheets/Domain/Worksheet.ts
@@ -153,6 +153,8 @@ export class Worksheet {
endCol: number
): ObjectMatrix<ICellData & { rowSpan?: number; colSpan?: number }> {
const matrix = this.getCellMatrix();
+
+ // get all merged cells
const mergedCellsInRange = this._snapshot.mergeData.filter((rect) => {
const rectRange = new Rectangle(rect);
const targetRange = new Rectangle(row, col, endRow, endCol);
@@ -161,6 +163,7 @@ export class Worksheet {
const ret = new ObjectMatrix<ICellData & { rowSpan?: number; colSpan?: number }>();
+ // iterate all cells in the range
createRowColIter(row, endRow, col, endCol).forEach((row, col) => {
const v = matrix.getValue(row, col);
if (v) {
@@ -174,7 +177,7 @@ export class Worksheet {
createRowColIter(startRow, endRow, startColumn, endColumn).forEach((row, col) => {
if (row === startRow && col === startColumn) {
ret.setValue(row, col, {
- ...ret.getValue(row, col),
+ ...matrix.getValue(row, col),
rowSpan: endRow - startRow + 1,
colSpan: endColumn - startColumn + 1,
});
diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.ts
index d3840566e6..0116aee6e8 100644
--- a/packages/core/src/services/command/command.service.ts
+++ b/packages/core/src/services/command/command.service.ts
@@ -22,6 +22,9 @@ export interface ICommand<P extends object = object, R = boolean> {
readonly type: CommandType;
handler(accessor: IAccessor, params?: P): Promise<R>;
+
+ /** When this command is unregistered, this function would be called. */
+ onDispose?: () => void;
}
export interface IMultiCommand<P extends object = object, R = boolean> extends ICommand<P, R> {
@@ -128,6 +131,8 @@ class CommandRegistry {
return toDisposable(() => {
this.commands.delete(command.id);
this.commandInjector.delete(command.id);
+
+ command.onDispose?.();
});
}
@@ -338,6 +343,8 @@ class MultiCommand implements IMultiCommand {
return toDisposable(() => {
const index = this._implementations.indexOf(registry);
this._implementations.splice(index, 1);
+
+ implementation.onDispose?.();
});
}
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 3142ceed96..0b9fe30bd6 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,7 @@
import {
ChangeSelectionCommand,
ExpandSelectionCommand,
+ SelectAllCommand,
SetBoldCommand,
SetFontFamilyCommand,
SetFontSizeCommand,
@@ -21,7 +22,6 @@ import { Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
import { QuitCellEditorCommand } from '../services/cell-editor/cell-editor.command';
-import { QuitCellEditorShortcutItem } from '../services/shortcuts/shortcuts';
import { RightMenuInput } from '../View/RightMenu/RightMenuInput';
import { RightMenuItem } from '../View/RightMenu/RightMenuItem';
import { RenderSheetFooter } from '../View/SheetContainer/SheetContainer';
@@ -67,6 +67,7 @@ import {
CellMergeVerticalMenuItemFactory,
} from './menu/merge.menu';
import { SheetBarUIController } from './SheetBarUIController';
+import { QuitCellEditorShortcutItem } from './shortcuts/editor.shortcut';
import {
ExpandSelectionDownShortcutItem,
ExpandSelectionEndDownShortcutItem,
@@ -85,6 +86,7 @@ import {
MoveSelectionRightShortcutItem,
MoveSelectionTabShortcutItem,
MoveSelectionUpShortcutItem,
+ SelectAllShortcutItem,
} from './shortcuts/selection.shortcut';
import {
SetBoldShortcutItem,
@@ -118,6 +120,7 @@ export class SheetUIController extends Disposable {
private _initialize(): void {
// init custom component
const componentManager = this._componentManager;
+
// FIXME: no dispose logic
componentManager.register(CONTEXT_MENU_INPUT_LABEL, RightMenuInput);
componentManager.register(RightMenuItem.name, RightMenuItem);
@@ -126,6 +129,7 @@ export class SheetUIController extends Disposable {
[
ChangeSelectionCommand,
ExpandSelectionCommand,
+ SelectAllCommand,
SetBoldCommand,
SetItalicCommand,
SetStrikeThroughCommand,
@@ -188,6 +192,7 @@ export class SheetUIController extends Disposable {
// init shortcuts
[
+ // selection shortcuts
MoveSelectionDownShortcutItem,
MoveSelectionUpShortcutItem,
MoveSelectionLeftShortcutItem,
@@ -205,12 +210,15 @@ export class SheetUIController extends Disposable {
ExpandSelectionEndUpShortcutItem,
ExpandSelectionEndLeftShortcutItem,
ExpandSelectionEndRightShortcutItem,
+ SelectAllShortcutItem,
+ // toggle cell style shortcuts
SetBoldShortcutItem,
SetItalicShortcutItem,
SetUnderlineShortcutItem,
SetStrikeThroughShortcutItem,
+ // cell content editing shortcuts
ClearSelectionValueShortcutItem,
QuitCellEditorShortcutItem,
].forEach((item) => {
diff --git a/packages/ui-plugin-sheets/src/services/shortcuts/shortcuts.ts b/packages/ui-plugin-sheets/src/controller/shortcuts/editor.shortcut.ts
similarity index 63%
rename from packages/ui-plugin-sheets/src/services/shortcuts/shortcuts.ts
rename to packages/ui-plugin-sheets/src/controller/shortcuts/editor.shortcut.ts
index 723aebe142..08d08eb2e3 100644
--- a/packages/ui-plugin-sheets/src/services/shortcuts/shortcuts.ts
+++ b/packages/ui-plugin-sheets/src/controller/shortcuts/editor.shortcut.ts
@@ -1,7 +1,7 @@
import { IShortcutItem, KeyCode } from '@univerjs/base-ui';
-import { QuitCellEditorCommand } from '../cell-editor/cell-editor.command';
-import { SHEET_EDITOR_ACTIVATED } from '../context/context';
+import { QuitCellEditorCommand } from '../../services/cell-editor/cell-editor.command';
+import { SHEET_EDITOR_ACTIVATED } from '../../services/context/context';
export const QuitCellEditorShortcutItem: IShortcutItem = {
id: QuitCellEditorCommand.id,
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 3c8e06651e..c484ed1186 100644
--- a/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
+++ b/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
@@ -3,10 +3,14 @@ import {
ExpandSelectionCommand,
IChangeSelectionCommandParams,
IExpandSelectionCommandParams,
+ ISelectAllCommandParams,
+ SelectAllCommand,
} from '@univerjs/base-sheets';
import { IShortcutItem, KeyCode, MetaKeys } from '@univerjs/base-ui';
import { Direction, FOCUSING_SHEET } from '@univerjs/core';
+import { SHEET_EDITOR_ACTIVATED } from '../../services/context/context';
+
export const MoveSelectionDownShortcutItem: IShortcutItem<IChangeSelectionCommandParams> = {
id: ChangeSelectionCommand.id,
binding: KeyCode.ARROW_DOWN,
@@ -59,8 +63,8 @@ export const MoveSelectionEndDownShortcutItem: IShortcutItem<IChangeSelectionCom
binding: KeyCode.ARROW_DOWN | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
- direction: Direction.LEFT,
- toEnd: true,
+ direction: Direction.DOWN,
+ toNextGap: true,
},
};
@@ -70,7 +74,7 @@ export const MoveSelectionEndUpShortcutItem: IShortcutItem<IChangeSelectionComma
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.UP,
- toEnd: true,
+ toNextGap: true,
},
};
@@ -80,7 +84,7 @@ export const MoveSelectionEndLeftShortcutItem: IShortcutItem<IChangeSelectionCom
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.LEFT,
- toEnd: true,
+ toNextGap: true,
},
};
@@ -90,7 +94,7 @@ export const MoveSelectionEndRightShortcutItem: IShortcutItem<IChangeSelectionCo
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.RIGHT,
- toEnd: true,
+ toNextGap: true,
},
};
@@ -135,41 +139,56 @@ export const ExpandSelectionRightShortcutItem: IShortcutItem<IExpandSelectionCom
// continuous selection to continuous end
export const ExpandSelectionEndDownShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
- id: ExpandSelectionCommand.id,
+ id: ChangeSelectionCommand.id,
binding: KeyCode.ARROW_DOWN | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.DOWN,
- toEnd: true,
+ toNextGap: true,
+ expand: true,
},
};
export const ExpandSelectionEndUpShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
- id: ExpandSelectionCommand.id,
+ id: ChangeSelectionCommand.id,
binding: KeyCode.ARROW_UP | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.UP,
- toEnd: true,
+ toNextGap: true,
+ expand: true,
},
};
export const ExpandSelectionEndLeftShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
- id: ExpandSelectionCommand.id,
+ id: ChangeSelectionCommand.id,
binding: KeyCode.ARROW_LEFT | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.LEFT,
- toEnd: true,
+ toNextGap: true,
+ expand: true,
},
};
export const ExpandSelectionEndRightShortcutItem: IShortcutItem<IExpandSelectionCommandParams> = {
- id: ExpandSelectionCommand.id,
+ id: ChangeSelectionCommand.id,
binding: KeyCode.ARROW_RIGHT | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.RIGHT,
- toEnd: true,
+ toNextGap: true,
+ expand: true,
+ },
+};
+
+export const SelectAllShortcutItem: IShortcutItem<ISelectAllCommandParams> = {
+ id: SelectAllCommand.id,
+ binding: KeyCode.A | MetaKeys.CTRL_COMMAND,
+ preconditions: (contextService) =>
+ contextService.getContextValue(FOCUSING_SHEET) && !contextService.getContextValue(SHEET_EDITOR_ACTIVATED),
+ staticParameters: {
+ expandToGapFirst: true,
+ loop: true,
},
};