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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(base-sheets): selection shift select
Dushusir8e8670d39c0e
diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.tsindex 25e547ebd7..498f22498e 100644--- a/packages/base-sheets/src/Controller/CellEditorController.ts+++ b/packages/base-sheets/src/Controller/CellEditorController.ts@@ -110,7 +110,7 @@ export class CellEditorController {this.exitEditMode();// move to cell below- // this._plugin.getSelectionManager().moveDown()+ this._plugin.getSelectionManager().move(Direction.BOTTOM);} else {this.enterEditMode();}@@ -122,17 +122,36 @@ export class CellEditorController {}break;- // case 'ArrowUp':- // break;+ case 'ArrowUp':+ if (!this.isEditMode) {+ this._plugin.getSelectionManager().move(Direction.TOP);+ }+ break;- // case 'ArrowDown':- // break;+ case 'ArrowDown':+ if (!this.isEditMode) {+ this._plugin.getSelectionManager().move(Direction.BOTTOM);+ }+ break;- // case 'ArrowLeft':- // break;+ case 'ArrowLeft':+ if (!this.isEditMode) {+ this._plugin.getSelectionManager().move(Direction.LEFT);+ }+ break;- // case 'ArrowRight':- // break;+ case 'ArrowRight':+ if (!this.isEditMode) {+ this._plugin.getSelectionManager().move(Direction.RIGHT);+ }+ break;++ case 'Tab':+ if (!this.isEditMode) {+ this._plugin.getSelectionManager().move(Direction.RIGHT);+ }+ evt.preventDefault();+ break;default:break;@@ -288,14 +307,12 @@ export class CellEditorController {this.isEditMode = true;- const selection = this._plugin.getSelectionManager().getActiveSelection();+ const currentCell = this._plugin.getSelectionManager().getCurrentModel();- if (!selection || !selection.model || !selection.model.currentCell) {+ if (!currentCell) {return false;}- const currentCell = selection.model.currentCell;-let startX;let endX;let startY;@@ -386,14 +403,14 @@ export class CellEditorController {}setCurrentEditRangeData() {- const model = this._plugin.getSelectionManager().getActiveSelection()?.model;+ const model = this._plugin.getSelectionManager().getCurrentModel();if (!model) return;this.currentEditRangeData = {- startRow: model.startRow,- startColumn: model.startColumn,- endRow: model.endRow,- endColumn: model.endColumn,+ startRow: model.row,+ startColumn: model.column,+ endRow: model.row,+ endColumn: model.column,};}diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.tsindex 4d97d6526b..2f0ad59032 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts@@ -69,6 +69,18 @@ export class SelectionManager {return this._selectionControls;}+ getCurrentControl() {+ const controls = this.getCurrentControls();+ if (controls && controls.length > 0) {+ for (const control of controls) {+ const currentCell = control.model.currentCell;+ if (currentCell) {+ return control;+ }+ }+ }+ }+getCurrentModels() {const worksheetId = this.getWorksheetId();if (!worksheetId) {@@ -83,6 +95,18 @@ export class SelectionManager {return this._selectionModels.get(worksheetId);}+ getCurrentModel(): Nullable<ICellInfo> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ for (const model of models) {+ const currentCell = model.currentCell;+ if (currentCell) {+ return currentCell;+ }+ }+ }+ }+getCurrentModelsValue(): ISelectionModelValue[] {const models = this.getCurrentModels();if (!models) {@@ -99,7 +123,7 @@ export class SelectionManager {* Renders all controls of the currently active sheet* @returns*/- renderCurrentControls(newSelectionRange?: ISelection, highlight?: Nullable<ICellInfo>) {+ renderCurrentControls(models?: SelectionModel[]) {const worksheetId = this.getWorksheetId();if (worksheetId) {if (this._selectionControls) {@@ -110,10 +134,10 @@ export class SelectionManager {}// render current control- const selectionModels = this._selectionModels.get(worksheetId);+ const selectionModels = models || this._selectionModels.get(worksheetId);this._selectionControls = [];selectionModels?.forEach((model) => {- const curCellRange = highlight || model.currentCell;+ const curCellRange = model.currentCell;const main = this._mainComponent;const control = SelectionControl.create(this, this._selectionControls.length);@@ -123,7 +147,7 @@ export class SelectionManager {cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);}- const { startRow, startColumn, endRow, endColumn } = newSelectionRange || model;+ const { startRow, startColumn, endRow, endColumn } = model;const startCell = main.getNoMergeCellPositionByIndex(startRow, startColumn);const endCell = main.getNoMergeCellPositionByIndex(endRow, endColumn);@@ -176,17 +200,17 @@ export class SelectionManager {cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);}- const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = selectionRange;- const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);- const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);+ const { startRow, startColumn, endRow, endColumn } = selectionRange;+ const startCell = main.getNoMergeCellPositionByIndex(startRow, startColumn);+ const endCell = main.getNoMergeCellPositionByIndex(endRow, endColumn);// update controlcontrol.update({- startColumn: finalStartColumn,- startRow: finalStartRow,- endColumn: finalEndColumn,- endRow: finalEndRow,+ startColumn,+ startRow,+ endColumn,+ endRow,startY: startCell?.startY || 0,endY: endCell?.endY || 0,startX: startCell?.startX || 0,@@ -196,6 +220,20 @@ export class SelectionManager {);currentControls.push(control);++ this.setSelectionModel();+ }++ clearSelectionControls() {+ let curControls = this.getCurrentControls();++ if (curControls.length > 0) {+ for (let control of curControls) {+ control.dispose();+ }++ curControls.length = 0; // clear currentSelectionControls+ }}/**@@ -271,8 +309,63 @@ export class SelectionManager {recreateControlsByRangeData() {}- move(direction: Direction) {- // const { startRow, startColumn, endColumn, endRow } = this.getCurrentModels();+ /**+ * Move the selection according to different directions, usually used for the shortcut key operation of ↑ ↓ ← →+ * @param direction+ * @returns+ */+ move(direction: Direction): void {+ const currentCell = this.getCurrentModel();++ if (!currentCell) return;++ let { row, column } = currentCell;+ const rowCount = this._worksheet?.getRowCount() || 1000;+ const columnCount = this._worksheet?.getColumnCount() || 50;+ switch (direction) {+ case Direction.TOP:+ row--;+ if (row < 0) {+ row = 0;+ }+ break;+ case Direction.BOTTOM:+ row++;++ if (row > rowCount) {+ row = rowCount;+ }+ break;+ case Direction.LEFT:+ column--;+ if (column < 0) {+ column = 0;+ }+ break;+ case Direction.RIGHT:+ column++;+ if (column > columnCount) {+ column = columnCount;+ }+ break;++ default:+ break;+ }++ let rangeData: IRangeData = {+ startRow: row,+ endRow: row,+ startColumn: column,+ endColumn: column,+ };+ let currentCellData: IRangeCellData = {+ row,+ column,+ };++ this.clearSelectionControls();+ this.addControlToCurrentByRangeData(rangeData, currentCellData);}constructor(private _sheetView: SheetView) {@@ -328,7 +421,7 @@ export class SelectionManager {this._startSelectionRange = startSelectionRange;- let selectionControl: Nullable<SelectionControl> = null;+ let selectionControl: Nullable<SelectionControl> = this.getCurrentControl();let curControls = this.getCurrentControls();@@ -349,24 +442,45 @@ export class SelectionManager {}// There can only be one highlighted cell, so clear the highlighted cell of the existing selection- control.clearHighlight();+ if (!evt.shiftKey) {+ control.clearHighlight();+ }}- if (!selectionControl) {- if (curControls.length > 0 && !evt.ctrlKey) {- for (let control of curControls) {- control.dispose();- }-- curControls.length = 0; // clear currentSelectionControls+ // In addition to pressing the ctrl or shift key, we must clear the previous selection+ if (curControls.length > 0 && !evt.ctrlKey && !evt.shiftKey) {+ for (let control of curControls) {+ control.dispose();}+ curControls.length = 0; // clear currentSelectionControls+ }++ const currentCell = selectionControl && selectionControl.model.currentCell;++ if (selectionControl && evt.shiftKey && currentCell) {+ const { row, column } = currentCell;++ const startCell = main.getNoMergeCellPositionByIndex(row, column);+ const endCell = main.getNoMergeCellPositionByIndex(endRow, endColumn);++ const newSelectionRange = {+ startColumn: column,+ startRow: row,+ endColumn: startSelectionRange.startColumn,+ endRow: startSelectionRange.startRow,+ startY: startCell?.startY || 0,+ endY: endCell?.endY || 0,+ startX: startCell?.startX || 0,+ endX: endCell?.endX || 0,+ };+ selectionControl.update(newSelectionRange, currentCell);+ } else {selectionControl = SelectionControl.create(this, curControls.length);+ selectionControl.update(startSelectionRange, cellInfo);curControls.push(selectionControl);}- selectionControl.update(startSelectionRange, cellInfo);-this.hasSelection = true;const scene = this.getScene();@@ -613,95 +727,91 @@ export class SelectionManager {});}+ /**+ * Get all range data of the current selection+ * @returns+ */+ getActiveRangeListData(): Nullable<IRangeData[]> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ const selections = models?.map((model: SelectionModel) => ({+ startRow: model.startRow,+ startColumn: model.startColumn,+ endRow: model.endRow,+ endColumn: model.endColumn,+ }));++ return selections;+ }+ }+/*** Returns the list of active ranges in the active sheet or null if there are no active ranges.* If there is a single range selected, this behaves as a getActiveRange() call.*- * TODO- ** @returns*/getActiveRangeList(): Nullable<RangeList> {- const controls = this.getCurrentControls();- if (controls && controls.length > 0) {- const selections = controls?.map((control: SelectionControl) => {- const model: SelectionModel = control.model;- return {- startRow: model.startRow,- startColumn: model.startColumn,- endRow: model.endRow,- endColumn: model.endColumn,- };- });- return this._worksheet?.getRangeList(selections);- }+ const rangeListData = this.getActiveRangeListData();+ return rangeListData && this._worksheet?.getRangeList(rangeListData);}/**- * Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.+ * Get the range of the currently active cell* @returns*/- getActiveRange(): Nullable<Range> {- const controls = this.getCurrentControls();- if (controls && controls.length > 0) {- const control = controls?.find((control: SelectionControl) => {- if (control.getCurrentCellInfo()) {- return control;+ getActiveRangeData(): Nullable<IRangeData> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ for (const model of models) {+ if (model.currentCell) {+ return {+ startRow: model.startRow,+ startColumn: model.startColumn,+ endRow: model.endRow,+ endColumn: model.endColumn,+ };}- return null;- });- if (control) {- const model = control.model;- const rangeData = {- startRow: model.startRow,- startColumn: model.startColumn,- endRow: model.endRow,- endColumn: model.endColumn,- };- return this._worksheet?.getRange(rangeData);}}- // const model = controls && controls[controls.length - 1].model;- // return (- // model &&- // this._worksheet?.getRange({- // startRow: model.startRow,- // startColumn: model.startColumn,- // endRow: model.endRow,- // endColumn: model.endColumn,- // })- // );-- // if (controls && controls.length > 0) {- // const selections = controls?.map((control: SelectionControl) => {- // const model: SelectionModel = control.model;- // return {- // startRow: model.startRow,- // startColumn: model.startColumn,- // endRow: model.endRow,- // endColumn: model.endColumn,- // };- // });- // return this._worksheet?.getRangeList(selections);- // }+ }++ /**+ * Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.+ * @returns+ */+ getActiveRange(): Nullable<Range> {+ const rangeData = this.getActiveRangeData();+ return rangeData && this._worksheet?.getRange(rangeData);}/*** get active selection control* @returns*/- getActiveSelection(): Nullable<SelectionControl> {- const controls = this.getCurrentControls();- if (controls && controls.length > 0) {- const control = controls?.find((control: SelectionControl) => {- if (control.getCurrentCellInfo()) {- return control;+ getCurrentCellData(): Nullable<IRangeData> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ for (const model of models) {+ const currentCell = model.currentCell;+ if (currentCell) {+ return {+ startRow: currentCell.row,+ startColumn: currentCell.column,+ endRow: currentCell.row,+ endColumn: currentCell.column,+ };}- return null;- });- if (control) {- return control;}}}++ /**+ * get active selection control+ * @returns+ */+ getCurrentCell(): Nullable<Range> {+ const rangeData = this.getCurrentCellData();+ return rangeData && this._worksheet?.getRange(rangeData);+ }}