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(sheets): add set selection type by data
Dushusir19ae9a5fb3d4
diff --git a/packages/base-sheets/src/Controller/CellEditorController.ts b/packages/base-sheets/src/Controller/CellEditorController.tsindex 5a866c5f94..377f7e2a00 100644--- a/packages/base-sheets/src/Controller/CellEditorController.ts+++ b/packages/base-sheets/src/Controller/CellEditorController.ts@@ -313,7 +313,7 @@ export class CellEditorController {this.isEditMode = true;- const currentCell = this._plugin.getSelectionManager().getCurrentModel();+ const currentCell = this._plugin.getSelectionManager().getCurrentCellModel();if (!currentCell) {return false;@@ -424,7 +424,7 @@ export class CellEditorController {}setCurrentEditRangeData() {- const model = this._plugin.getSelectionManager().getCurrentModel();+ const model = this._plugin.getSelectionManager().getCurrentCellModel();if (!model) return;this.currentEditRangeData = {diff --git a/packages/base-sheets/src/Controller/RightMenuController.ts b/packages/base-sheets/src/Controller/RightMenuController.tsindex bcecc8f1f1..4a8ab7a670 100644--- a/packages/base-sheets/src/Controller/RightMenuController.ts+++ b/packages/base-sheets/src/Controller/RightMenuController.ts@@ -340,6 +340,10 @@ export class RightMenuController {const { offsetX: evtOffsetX, offsetY: evtOffsetY } = evt;if (evt.button === 2) {spreadSheet.oncontextmenu = () => false;++ const selecionModel = this._plugin.getSelectionManager().getCurrentModel();+ console.log('selecionModel', selecionModel?.type);+this._RightMenu.handleContextMenu(evt);}});diff --git a/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts b/packages/base-sheets/src/Controller/Selection/ColumnTitleController.tsindex 6a3f73c9b5..d95342e048 100644--- a/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts+++ b/packages/base-sheets/src/Controller/Selection/ColumnTitleController.ts@@ -90,12 +90,18 @@ export class ColumnTitleController {highlightColumn() {this._manager.clearSelectionControls();const sheet = this._manager.getPlugin().getWorkbook().getActiveSheet();- this._manager.addControlToCurrentByRangeData({- startRow: 0,- startColumn: this._index,- endColumn: this._index,- endRow: sheet.getRowCount() - 1,- });+ this._manager.addControlToCurrentByRangeData(+ {+ startRow: 0,+ startColumn: this._index,+ endColumn: this._index,+ endRow: sheet.getRowCount() - 1,+ },+ {+ row: 0,+ column: this._index,+ }+ );}highlightColumnTitle(e: IPointerEvent | IMouseEvent) {diff --git a/packages/base-sheets/src/Controller/Selection/RowTitleController.ts b/packages/base-sheets/src/Controller/Selection/RowTitleController.tsindex cbcdc730d4..24c33cb00e 100644--- a/packages/base-sheets/src/Controller/Selection/RowTitleController.ts+++ b/packages/base-sheets/src/Controller/Selection/RowTitleController.ts@@ -92,12 +92,18 @@ export class RowTitleController {highlightRow() {this._manager.clearSelectionControls();const sheet = this._manager.getPlugin().getWorkbook().getActiveSheet();- this._manager.addControlToCurrentByRangeData({- startRow: this._index,- startColumn: 0,- endColumn: sheet.getColumnCount() - 1,- endRow: this._index,- });+ this._manager.addControlToCurrentByRangeData(+ {+ startRow: this._index,+ startColumn: 0,+ endColumn: sheet.getColumnCount() - 1,+ endRow: this._index,+ },+ {+ row: this._index,+ column: 0,+ }+ );}highlightRowTitle(e: IPointerEvent | IMouseEvent) {diff --git a/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts b/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.tsindex 24f47095cc..e760db886f 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionControlDragDrop.ts@@ -148,7 +148,7 @@ export class SelectionControlDragAndDrop {// reset modelconst selection = this._model.getValue().selection;- this._model = new SelectionModel(SELECTION_TYPE.NORMAL);+ this._model = new SelectionModel(SELECTION_TYPE.NORMAL, this._control.getPlugin());this._model.setValue(selection);// update controldiff --git a/packages/base-sheets/src/Controller/Selection/SelectionController.ts b/packages/base-sheets/src/Controller/Selection/SelectionController.tsindex 43c4c0e240..5802fe709b 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionController.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionController.ts@@ -70,7 +70,7 @@ export class SelectionControl {private _selectionShape: Group;- private _selectionModel: SelectionModel = new SelectionModel(SELECTION_TYPE.NORMAL);+ private _selectionModel: SelectionModel;private _selectionDragAndDrop: SelectionControlDragAndDrop;@@ -81,6 +81,7 @@ export class SelectionControl {}private _initialize() {+ this._selectionModel = new SelectionModel(SELECTION_TYPE.NORMAL, this._manager.getPlugin());const zIndex = this._zIndex;this._leftControl = new Rect(SELECTION_MANAGER_KEY.left + zIndex, {top: 0,diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.tsindex 2e48cefb55..ff7b975b46 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts@@ -107,7 +107,7 @@ export class SelectionManager {return this._selectionModels.get(worksheetId);}- getCurrentModel(): Nullable<ICellInfo> {+ getCurrentCellModel(): Nullable<ICellInfo> {const models = this.getCurrentModels();if (models && models.length > 0) {for (const model of models) {@@ -119,6 +119,18 @@ export class SelectionManager {}}+ getCurrentModel(): Nullable<SelectionModel> {+ const models = this.getCurrentModels();+ if (models && models.length > 0) {+ for (const model of models) {+ const currentCell = model.currentCell;+ if (currentCell) {+ return model;+ }+ }+ }+ }+getCurrentModelsValue(): ISelectionModelValue[] {const models = this.getCurrentModels();if (!models) {@@ -321,7 +333,7 @@ export class SelectionManager {}const models = selections.map(({ selection, cell }) => {- const model = new SelectionModel(SELECTION_TYPE.NORMAL);+ const model = new SelectionModel(SELECTION_TYPE.NORMAL, this._plugin);model.setValue(selection, cell);return model;});@@ -337,7 +349,7 @@ export class SelectionManager {* @returns*/move(direction: Direction): void {- const currentCell = this.getCurrentModel();+ const currentCell = this.getCurrentCellModel();if (!currentCell) return;@@ -459,12 +471,12 @@ export class SelectionManager {for (let control of curControls) {// right click- if (evt.button === 2 && control.model.isInclude(startSelectionRange, SELECTION_TYPE.NORMAL)) {+ if (evt.button === 2 && control.model.isInclude(startSelectionRange)) {selectionControl = control;return;}// Click to an existing selection- if (control.model.isEqual(startSelectionRange, SELECTION_TYPE.NORMAL)) {+ if (control.model.isEqual(startSelectionRange)) {selectionControl = control;break;}@@ -680,7 +692,7 @@ export class SelectionManager {const { startColumn, startRow, endColumn, endRow } = selectionConfig.selection;const cell = selectionConfig.cell;- const model = new SelectionModel(SELECTION_TYPE.NORMAL);+ const model = new SelectionModel(SELECTION_TYPE.NORMAL, this._plugin);const cellInfo = cell? {diff --git a/packages/base-sheets/src/Model/SelectionModel.ts b/packages/base-sheets/src/Model/SelectionModel.tsindex 58dd7ebf4d..8e6719caa7 100644--- a/packages/base-sheets/src/Model/SelectionModel.ts+++ b/packages/base-sheets/src/Model/SelectionModel.ts@@ -1,5 +1,6 @@import { ICellInfo, ISelection, Nullable, makeCellToSelection } from '@univer/core';import { SELECTION_TYPE } from '../Controller/Selection/SelectionController';+import { SheetPlugin } from '../SheetPlugin';export class SelectionModel implements ISelection {private _startColumn: number;@@ -22,29 +23,32 @@ export class SelectionModel implements ISelection {private _currentCell: Nullable<ICellInfo>;- constructor(type: SELECTION_TYPE) {+ private _plugin: SheetPlugin;++ constructor(type: SELECTION_TYPE = SELECTION_TYPE.NORMAL, plugin: SheetPlugin) {this._type = type;+ this._plugin = plugin;}- isEqual(selectionRange: ISelection, newType: SELECTION_TYPE) {+ isEqual(selectionRange: ISelection) {const { startColumn, startRow, endColumn, endRow, type } = this;const { startColumn: newStartColumn, startRow: newStartRow, endColumn: newEndColumn, endRow: newEndRow } = selectionRange;- if (type !== newType) {- return false;- }+ // if (type !== newType) {+ // return false;+ // }if (startColumn === newStartColumn && startRow === newStartRow && endColumn === newEndColumn && endRow === newEndRow) {return true;}return false;}- isInclude(selectionRange: ISelection, newType: SELECTION_TYPE) {+ isInclude(selectionRange: ISelection) {const { startColumn, startRow, endColumn, endRow, type } = this;const { startColumn: newStartColumn, startRow: newStartRow, endColumn: newEndColumn, endRow: newEndRow } = selectionRange;- if (type !== newType) {- return false;- }+ // if (type !== newType) {+ // return false;+ // }if (!(newEndColumn < startColumn || newStartColumn > endColumn || newStartRow > endRow || newEndRow < startRow)) {return true;@@ -114,7 +118,6 @@ export class SelectionModel implements ISelection {}setValue(newSelectionRange: ISelection, currentCell: Nullable<ICellInfo>) {- // TODO action触发const {startColumn,startRow,@@ -143,6 +146,8 @@ export class SelectionModel implements ISelection {this._endY = endY;+ this.setTypeByData();+this.setCurrentCell(currentCell);}@@ -155,4 +160,21 @@ export class SelectionModel implements ISelection {clearCurrentCell() {this._currentCell = null;}++ /**+ * Determine the type type based on the data+ */+ setTypeByData() {+ const rowCount = this._plugin.getWorkbook().getActiveSheet().getConfig().rowCount;+ const columnCount = this._plugin.getWorkbook().getActiveSheet().getConfig().columnCount;++ if (this._startRow === 0 && this._endRow === rowCount - 1) {+ return (this._type = SELECTION_TYPE.COLUMN);+ }+ if (this._startColumn === 0 && this._endColumn === columnCount - 1) {+ return (this._type = SELECTION_TYPE.ROW);+ }++ return (this._type = SELECTION_TYPE.NORMAL);+ }}