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
fix(core):Selection Action
Dushusir93d7493795fe
diff --git a/packages/base-sheets/package.json b/packages/base-sheets/package.jsonindex 4e09313945..ded3f43b6e 100644--- a/packages/base-sheets/package.json+++ b/packages/base-sheets/package.json@@ -24,7 +24,7 @@"test": "jest","test:play": "npx playwright test","test:debug": "PWDEBUG=1 npx playwright test",- "dev": "tsc && vite",+ "dev": "vite","build": "tsc && vite build","serve": "vite preview"},diff --git a/packages/base-sheets/src/Controller/Selection/SelectionController.ts b/packages/base-sheets/src/Controller/Selection/SelectionController.tsindex 70039c0f97..81142a2692 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionController.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionController.ts@@ -41,6 +41,9 @@ enum SELECTION_MANAGER_KEY {fill = '__SpreadsheetSelectionFillControl__',}+/**+ * The main selection canvas component+ */export class SelectionControl {private _mainComponent: Spreadsheet;@@ -159,6 +162,8 @@ export class SelectionControl {/*** just handle the view+ *+ * inner update*/_updateControl() {const { startX, startY, endX, endY } = this._selectionModel;@@ -280,46 +285,46 @@ export class SelectionControl {this._updateControl();}- /**- *When switching to the current sheet- *- * 1. Reinitialize the rendering component- * 2. Calculate the position based on the current skeleton- * 3. Update data- * 4. Trigger rendering- */- render() {- this._initialize();-- let cellInfo = null;- const main = this._manager.getMainComponent();- const curCellRange = this._selectionModel.currentCell;-- if (curCellRange) {- cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);- }-- const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = this._selectionModel;- const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);- const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);-- this._manager.updateSelectionValue(- this,- {- startColumn: finalStartColumn,- startRow: finalStartRow,- endColumn: finalEndColumn,- endRow: finalEndRow,- startY: startCell?.startY || 0,- endY: endCell?.endY || 0,- startX: startCell?.startX || 0,- endX: endCell?.endX || 0,- },- cellInfo- );-- this._updateControl();- }+ // /**+ // *When switching to the current sheet+ // *+ // * 1. Reinitialize the rendering component+ // * 2. Calculate the position based on the current skeleton+ // * 3. Update data+ // * 4. Trigger rendering+ // */+ // render() {+ // this._initialize();++ // let cellInfo = null;+ // const main = this._manager.getMainComponent();+ // const curCellRange = this._selectionModel.currentCell;++ // if (curCellRange) {+ // cellInfo = main.getCellByIndex(curCellRange.row, curCellRange.column);+ // }++ // const { startRow: finalStartRow, startColumn: finalStartColumn, endRow: finalEndRow, endColumn: finalEndColumn } = this._selectionModel;+ // const startCell = main.getNoMergeCellPositionByIndex(finalStartRow, finalStartColumn);+ // const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);++ // this._manager.updateSelectionValue(+ // this,+ // {+ // startColumn: finalStartColumn,+ // startRow: finalStartRow,+ // endColumn: finalEndColumn,+ // endRow: finalEndRow,+ // startY: startCell?.startY || 0,+ // endY: endCell?.endY || 0,+ // startX: startCell?.startX || 0,+ // endX: endCell?.endX || 0,+ // },+ // cellInfo+ // );++ // this._updateControl();+ // }clearHighlight() {this._selectionModel.clearCurrentCell();@@ -437,8 +442,7 @@ export class SelectionControl {static fromJson(manager: SelectionManager, zIndex: number, newSelectionRange: ISelection) {const control = SelectionControl.create(manager, zIndex);- manager.updateSelectionValue(control, newSelectionRange);- control._updateControl();+ control.update(newSelectionRange);return control;}}diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.tsindex e94f65cb76..7852dba7fe 100644--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts@@ -28,7 +28,7 @@ export class SelectionManager {// private _selectionControls = new Map<string, SelectionControl[]>(); // sheetID:Controls- private _selectionControls: SelectionControl[]; // sheetID:Controls+ private _selectionControls: SelectionControl[] = []; // sheetID:Controlsprivate _selectionModels = new Map<string, SelectionModel[]>(); // sheetID:Models@@ -122,12 +122,12 @@ export class SelectionManager {}}- resetCurrentModels() {- const worksheetId = this.getWorksheetId();- if (worksheetId) {- this._selectionModels.set(worksheetId, []);- }- }+ // resetCurrentModels() {+ // const worksheetId = this.getWorksheetId();+ // if (worksheetId) {+ // this._selectionModels.set(worksheetId, []);+ // }+ // }getPlugin() {return this._plugin;@@ -162,10 +162,7 @@ export class SelectionManager {const endCell = main.getNoMergeCellPositionByIndex(finalEndRow, finalEndColumn);// update control- currentControls.push(control);-- // update model- this.addSelectionValue(+ control.update({startColumn: finalStartColumn,startRow: finalStartRow,@@ -178,18 +175,20 @@ export class SelectionManager {},cellInfo);++ currentControls.push(control);}/*** update all current controls data in model*/- addSelectionValue(selection: ISelection, cell: Nullable<ICellInfo>) {+ setSelectionModel(models?: ISelectionModelValue[]) {if (!this._worksheet) return;- const currentModelsValue = this.getSelectionModelsValue();- currentModelsValue?.push({- selection,- cell,- });++ // get models from current control+ if (!models) {+ models = this._selectionControls.map((control) => control.model.getValue());+ }const workbook = this._worksheet.getContext().getWorkBook();const commandManager = workbook.getCommandManager();@@ -197,32 +196,64 @@ export class SelectionManager {const value: ISetSelectionValueActionData = {sheetId: this._worksheet.getSheetId(),actionName: ACTION_NAMES.SET_SELECTION_VALUE_ACTION,- selections: currentModelsValue,+ selections: models,};const command = new Command(workbook, value);commandManager.invoke(command);}+ // /**+ // * update current control in model+ // */+ // updateSelectionValue(selectionControl: SelectionControl, selection: ISelection, cell: Nullable<ICellInfo>) {+ // if (!this._worksheet) return;++ // const controls = this.getCurrentControls();+ // if (!controls) {+ // return;+ // }+ // const selectionModelsValue: ISelectionModelValue[] = [];+ // for (let control of controls) {+ // // update current control+ // if (control === selectionControl) {+ // selectionModelsValue.push({ selection, cell });+ // } else {+ // selectionModelsValue.push(control.model.getValue());+ // }+ // }++ // const workbook = this._worksheet.getContext().getWorkBook();+ // const commandManager = workbook.getCommandManager();++ // const value: ISetSelectionValueActionData = {+ // sheetId: this._worksheet.getSheetId(),+ // actionName: ACTION_NAMES.SET_SELECTION_VALUE_ACTION,+ // selections: selectionModelsValue,+ // };++ // const command = new Command(workbook, value);+ // commandManager.invoke(command);+ // }+/*** update current control in model*/- updateSelectionValue(selectionControl: SelectionControl, selection: ISelection, cell: Nullable<ICellInfo>) {- if (!this._worksheet) return;+ updateSelectionModel(selection: ISelection, cell: Nullable<ICellInfo>, index: number) {+ const worksheetId = this.getWorksheetId();- const controls = this.getCurrentControls();- if (!controls) {- return;- }+ if (!this._worksheet || !worksheetId) return;++ // Collect constituency data for the entire current sheet+ const selectionModels = this._selectionModels.get(worksheetId);const selectionModelsValue: ISelectionModelValue[] = [];- for (let control of controls) {- // update current control- if (control === selectionControl) {+ selectionModels?.forEach((model, i) => {+ if (i === index) {selectionModelsValue.push({ selection, cell });} else {- selectionModelsValue.push(control.model.getValue());+ selectionModelsValue.push(model.getValue());}- }+ });const workbook = this._worksheet.getContext().getWorkBook();const commandManager = workbook.getCommandManager();@@ -238,25 +269,27 @@ export class SelectionManager {}getSelectionModels() {- const controls = this.getCurrentControls();- if (!controls) {+ const worksheetId = this.getWorksheetId();+ if (!worksheetId) {return;}- const selectionModels: SelectionModel[] = [];- for (let control of controls) {- selectionModels.push(control.model);++ const models = this._selectionModels.get(worksheetId);++ if (!models) {+ this._selectionModels.set(worksheetId, []);}- return selectionModels;+ return this._selectionModels.get(worksheetId);}getSelectionModelsValue(): ISelectionModelValue[] {- const controls = this.getCurrentControls();- if (!controls) {+ const models = this.getSelectionModels();+ if (!models) {return [];}const selectionModelsValue = [];- for (let control of controls) {- selectionModelsValue.push(control.model.getValue());+ for (let model of models) {+ selectionModelsValue.push(model.getValue());}return selectionModelsValue;}@@ -351,16 +384,14 @@ export class SelectionManager {for (let control of curControls) {control.dispose();}- this.resetCurrentModels();- curControls = this.getCurrentControls()!;+ // this.resetCurrentModels();+ curControls = [];}selectionControl = SelectionControl.create(this, curControls.length);curControls.push(selectionControl);}- this.addSelectionValue(startSelectionRange, cellInfo);-selectionControl.update(startSelectionRange, cellInfo);this.hasSelection = true;@@ -384,6 +415,7 @@ export class SelectionManager {});this._upObserver = scene.onPointerUpObserver.add((upEvt: IPointerEvent | IMouseEvent) => {+ this.up();scene.onPointerMoveObserver.remove(this._moveObserver);scene.onPointerUpObserver.remove(this._upObserver);scene.enableEvent();@@ -446,12 +478,22 @@ export class SelectionManager {} = selectionControl?.model || { startRow: -1, endRow: -1, startColumn: -1, endColumn: -1 };if (oldStartColumn !== finalStartColumn || oldStartRow !== finalStartRow || oldEndColumn !== finalEndColumn || oldEndRow !== finalEndRow) {- selectionControl && this.updateSelectionValue(selectionControl, newSelectionRange);- selectionControl && selectionControl._updateControl();+ selectionControl && selectionControl.update(newSelectionRange);selectionControl && this._plugin.getObserver('onChangeSelectionObserver')?.notifyObservers(selectionControl);}}+ /**+ * pointer up event+ * @param moveEvt+ * @param selectionControl+ * @returns+ */+ up() {+ // update model+ this.setSelectionModel();+ }+private _rowEventInitial() {const row = this._rowComponent;row.onPointerDownObserver.add((evt: IPointerEvent | IMouseEvent) => {@@ -479,6 +521,7 @@ export class SelectionManager {*/private _initModels() {const selections: ISelectionsConfig = this._plugin.config.selections;+Object.keys(selections).forEach((worksheetId) => {const selectionsList = selections[worksheetId];const currentModels: SelectionModel[] = [];@@ -602,6 +645,8 @@ export class SelectionManager {* 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> {diff --git a/packages/base-sheets/src/Model/Apply/SetSelectionValue.ts b/packages/base-sheets/src/Model/Apply/SetSelectionValue.tsindex b223edb6b6..d88fc0fd35 100644--- a/packages/base-sheets/src/Model/Apply/SetSelectionValue.ts+++ b/packages/base-sheets/src/Model/Apply/SetSelectionValue.ts@@ -1,4 +1,4 @@-import { PLUGIN_NAMES, Tools, Worksheet } from '@univer/core';+import { PLUGIN_NAMES, Worksheet } from '@univer/core';import { SpreadsheetPlugin } from '../..';import { ISelectionModelValue } from '../Action/SetSelectionValueAction';@@ -13,19 +13,16 @@ import { ISelectionModelValue } from '../Action/SetSelectionValueAction';* @internal*/export function SetSelectionValue(worksheet: Worksheet, selections: ISelectionModelValue[]): ISelectionModelValue[] {- const currentControls = worksheet.getContext().getPluginManager().getPluginByName<SpreadsheetPlugin>(PLUGIN_NAMES.SPREADSHEET)?.getSelectionManager().getCurrentControls();+ const selectionManager = worksheet.getContext().getPluginManager().getPluginByName<SpreadsheetPlugin>(PLUGIN_NAMES.SPREADSHEET)?.getSelectionManager();- const result: ISelectionModelValue[] = [];+ if (!selectionManager) return [];- if (!currentControls) return [];+ const result = selectionManager?.getSelectionModelsValue();+ const models = selectionManager?.getSelectionModels();- currentControls.forEach((control, i) => {+ models?.forEach((model, i) => {const { selection, cell } = selections[i];- result.push({- selection: Tools.deepClone(selection),- cell: Tools.deepClone(cell),- });- control.updateModel(selection, cell);+ model.setValue(selection, cell);});return result;