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.

fix(formula): optimize the behavior when selecting merged cells by keyboard during formula editing (#6377)

wpxp1234561fe5701ff27f
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-sheet-selection-change.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-sheet-selection-change.ts
index 8157cfb702..689d609d0b 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-sheet-selection-change.ts
+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-sheet-selection-change.ts
@@ -21,7 +21,7 @@ import type { Editor } from '@univerjs/docs-ui';
import type { ISelectionWithCoord, ISetSelectionsOperationParams } from '@univerjs/sheets';
import type { RefObject } from 'react';
import type { IRefSelection } from './use-highlight';
-import { DisposableCollection, ICommandService, IUniverInstanceService, ThemeService, UniverInstanceType } from '@univerjs/core';
+import { DisposableCollection, ICommandService, IUniverInstanceService, Rectangle, ThemeService, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService } from '@univerjs/docs';
import { deserializeRangeWithSheet, generateStringWithSequence, LexerTreeBuilder, sequenceNodeType, serializeRange, serializeRangeWithSheet, serializeRangeWithSpreadsheet } from '@univerjs/engine-formula';
import { IRenderManagerService } from '@univerjs/engine-render';
@@ -324,12 +324,21 @@ export const useSheetSelectionChange = (
if (params.selections.length) {
const last = params.selections[params.selections.length - 1];
if (last) {
+ const { range, primary } = last;
+ // When in formula editor, if the current cell is a merged cell, only set the main cell of the merged cell as the selection.
+ if ((primary?.isMergedMainCell || primary?.isMerged) && Rectangle.contains(primary, range)) {
+ range.startRow = primary.startRow;
+ range.endRow = primary.startRow;
+ range.startColumn = primary.startColumn;
+ range.endColumn = primary.startColumn;
+ }
+
const isAdd = isSelectingRef.current === FormulaSelectingType.NEED_ADD;
const selections: IRange[] = (refSelectionsRenderService?.getSelectionDataWithStyle() ?? []).map((i) => i.rangeWithCoord);
if (isAdd) {
- selections.push(last.range);
+ selections.push(range);
} else {
- selections[selections.length - 1] = last.range;
+ selections[selections.length - 1] = range;
}
onSelectionsChange(selections, true);
}
diff --git a/packages/sheets-ui/src/commands/commands/set-selection.command.ts b/packages/sheets-ui/src/commands/commands/set-selection.command.ts
index c7186984db..3fe8e45c5a 100644
--- a/packages/sheets-ui/src/commands/commands/set-selection.command.ts
+++ b/packages/sheets-ui/src/commands/commands/set-selection.command.ts
@@ -76,6 +76,7 @@ export interface IMoveSelectionEnterAndTabCommandParams {
export const MoveSelectionCommand: ICommand<IMoveSelectionCommandParams> = {
id: 'sheet.command.move-selection',
type: CommandType.COMMAND,
+ // eslint-disable-next-line max-lines-per-function
handler: (accessor, params) => {
if (!params) {
return false;
@@ -91,7 +92,29 @@ export const MoveSelectionCommand: ICommand<IMoveSelectionCommandParams> = {
}
const { direction, jumpOver, extra } = params;
- const { range, primary } = selection;
+ let { range, primary } = selection;
+
+ if (extra === 'formula-editor') {
+ // When in formula editor, if the current cell is a merged cell, expand the selection to the merged cell range for calculation.
+ if (range.startRow === range.endRow && range.startColumn === range.endColumn) {
+ const mergedCell = worksheet.getMergedCell(range.startRow, range.startColumn);
+ if (mergedCell) {
+ range = {
+ ...range,
+ ...mergedCell,
+ };
+ }
+ }
+
+ // When in formula editor, try to use the last primary cell in ref selections as the primary cell for calculation.
+ if (!primary) {
+ const lastSelectionPrimary = getSelectionsService(accessor, params.fromCurrentSelection).getCurrentLastSelectionPrimaryCell();
+ if (lastSelectionPrimary && Rectangle.contains(range, lastSelectionPrimary)) {
+ primary = lastSelectionPrimary;
+ }
+ }
+ }
+
const startRange = getStartRange(range, primary, direction);
// the start range is from the primary selection range
@@ -344,10 +367,31 @@ export const ExpandSelectionCommand: ICommand<IExpandSelectionCommandParams> = {
const selection = getSelectionsService(accessor).getCurrentLastSelection();
if (!selection) return false;
- const { range: startRange, primary } = selection;
const { jumpOver, direction, extra } = params;
+ let { range: startRange, primary } = selection;
+
+ if (extra === 'formula-editor') {
+ // When in formula editor, if the current cell is a merged cell, expand the selection to the merged cell range for calculation.
+ if (startRange.startRow === startRange.endRow && startRange.startColumn === startRange.endColumn) {
+ const mergedCell = worksheet.getMergedCell(startRange.startRow, startRange.startColumn);
+ if (mergedCell) {
+ startRange = {
+ ...startRange,
+ ...mergedCell,
+ };
+ }
+ }
+
+ // When in formula editor, try to use the last primary cell in ref selections as the primary cell for calculation.
+ if (!primary) {
+ const lastSelectionPrimary = getSelectionsService(accessor).getCurrentLastSelectionPrimaryCell();
+ if (lastSelectionPrimary && Rectangle.contains(startRange, lastSelectionPrimary)) {
+ primary = lastSelectionPrimary;
+ }
+ }
+ }
- const isShrink = checkIfShrink(selection, direction, worksheet);
+ const isShrink = checkIfShrink({ range: startRange, primary }, direction, worksheet);
const destRange = !isShrink
? jumpOver === JumpOver.moveGap
? expandToNextGapRange(startRange, direction, worksheet)
diff --git a/packages/sheets/src/services/selections/selection-data-model.ts b/packages/sheets/src/services/selections/selection-data-model.ts
index 36a44f1356..7ccf816dcc 100644
--- a/packages/sheets/src/services/selections/selection-data-model.ts
+++ b/packages/sheets/src/services/selections/selection-data-model.ts
@@ -31,6 +31,7 @@ export class WorkbookSelectionModel extends Disposable {
* Selection data model for each worksheet.
*/
private _worksheetSelections = new Map<string, ISelectionWithStyle[]>();
+ private _worksheetLastSelectionPrimaryCell = new Map<string, Nullable<ISelectionCell>>();
private readonly _selectionMoveStart$ = new Subject<Nullable<ISelectionWithStyle[]>>();
readonly selectionMoveStart$ = this._selectionMoveStart$.asObservable();
@@ -67,6 +68,7 @@ export class WorkbookSelectionModel extends Disposable {
//@ts-ignore
this._workbook = null!;
+ this.clear();
}
addSelections(sheetId: string, selectionDatas: ISelectionWithStyle[]): void {
@@ -130,17 +132,26 @@ export class WorkbookSelectionModel extends Disposable {
return this._worksheetSelections.get(sheetId)!;
}
+ getLastSelectionPrimaryCellOfWorksheet(sheetId: string): Nullable<ISelectionCell> {
+ return this._worksheetLastSelectionPrimaryCell.get(sheetId) ?? null;
+ }
+
setSelectionsOfWorksheet(sheetId: string, selections: ISelectionWithStyle[]): void {
this._worksheetSelections.set(sheetId, [...selections]);
+ if (selections.length > 0 && selections[selections.length - 1].primary) {
+ this._worksheetLastSelectionPrimaryCell.set(sheetId, selections[selections.length - 1].primary!);
+ }
}
deleteSheetSelection(sheetId: string) {
this._worksheetSelections.set(sheetId, []);
+ this._worksheetLastSelectionPrimaryCell.delete(sheetId);
}
/** Clear all selections in this workbook. */
clear(): void {
this._worksheetSelections.clear();
+ this._worksheetLastSelectionPrimaryCell.clear();
this._selectionSet$.next([]);
}
diff --git a/packages/sheets/src/services/selections/selection.service.ts b/packages/sheets/src/services/selections/selection.service.ts
index 334527839b..4a5b0d3159 100644
--- a/packages/sheets/src/services/selections/selection.service.ts
+++ b/packages/sheets/src/services/selections/selection.service.ts
@@ -140,6 +140,16 @@ export class SheetsSelectionsService extends RxDisposable {
return selectionData?.[selectionData.length - 1] as Readonly<Nullable<ISelectionWithStyle & { primary: ISelectionCell }>>;
}
+ getCurrentLastSelectionPrimaryCell(): DeepReadonly<Nullable<ISelectionCell>> {
+ const current = this._currentSelectionPos;
+ if (!current) {
+ return null;
+ }
+
+ const { unitId, sheetId } = current;
+ return this._ensureWorkbookSelection(unitId).getLastSelectionPrimaryCellOfWorksheet(sheetId);
+ }
+
addSelections(selectionsData: ISelectionWithStyle[]): void;
addSelections(unitId: string, worksheetId: string, selectionDatas: ISelectionWithStyle[]): void;
addSelections(unitIdOrSelections: string | ISelectionWithStyle[], worksheetId?: string, selectionDatas?: ISelectionWithStyle[]): void {