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(sheets-formula-ui): fix formula editor ref selections (#7167)
wpxp1234561eb9b429782d
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/__tests__/formula-selection-business.spec.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/__tests__/formula-selection-business.spec.tsindex 6eecc045d3..6e74f88516 100644--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/__tests__/formula-selection-business.spec.ts+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/__tests__/formula-selection-business.spec.ts@@ -113,7 +113,10 @@ describe('formula highlight helpers', () => {});it('calculates visible formula reference selections and activates the reference under the editor cursor', () => {- const currentSelections = [{ primary: { actualRow: 9, actualColumn: 9 } }];+ const currentSelections = [{+ range: range(0, 0),+ primary: { actualRow: 9, actualColumn: 9 },+ }];const refSelectionsService = {getCurrentSelections: vi.fn(() => currentSelections),setSelections: vi.fn(),@@ -165,6 +168,84 @@ describe('formula highlight helpers', () => {expect(refSelectionsRenderService.setActiveSelectionIndex).toHaveBeenCalledWith(0);});+ it('does not assign the active keyboard selection primary to an earlier formula reference', () => {+ const activeKeyboardSelection = {+ range: range(7, 2),+ primary: { actualRow: 7, actualColumn: 2 },+ };+ const workbook = {+ getUnitId: vi.fn(() => 'unit1'),+ getActiveSheet: vi.fn(() => ({+ getSheetId: () => 'sheet1',+ getName: () => 'Sheet1',+ getRowCount: () => 100,+ getColumnCount: () => 50,+ })),+ getSheetBySheetName: vi.fn(() => ({ getSheetId: () => 'sheet1' })),+ };++ const result = calcHighlightRanges({+ unitId: 'unit1',+ subUnitId: 'sheet1',+ currentWorkbook: workbook as any,+ refSelections: [+ { token: 'C10', themeColor: '#ff0000', refIndex: 0, startIndex: 0, endIndex: 2, index: 0 },+ { token: 'C8', themeColor: '#00ff00', refIndex: 1, startIndex: 4, endIndex: 5, index: 1 },+ ],+ editor: undefined,+ refSelectionsService: {+ getCurrentSelections: vi.fn(() => [activeKeyboardSelection]),+ setSelections: vi.fn(),+ } as any,+ refSelectionsRenderService: undefined,+ sheetSkeletonManagerService: {+ getSkeleton: vi.fn(() => ({})),+ } as any,+ themeService: { getColorFromTheme: vi.fn((key: string) => key === 'white' ? '#fff' : key) } as any,+ univerInstanceService: { getUnit: vi.fn(() => workbook) } as any,+ });++ expect(result?.[0].range).toMatchObject(range(9, 2));+ expect(result?.[0].primary).toBeUndefined();+ expect(result?.[1].range).toMatchObject(range(7, 2));+ expect(result?.[1].primary).toBe(activeKeyboardSelection.primary);+ });++ it('ignores primary-only selections when calculating formula reference highlight primary cells', () => {+ const workbook = {+ getUnitId: vi.fn(() => 'unit1'),+ getActiveSheet: vi.fn(() => ({+ getSheetId: () => 'sheet1',+ getName: () => 'Sheet1',+ getRowCount: () => 100,+ getColumnCount: () => 50,+ })),+ getSheetBySheetName: vi.fn(() => ({ getSheetId: () => 'sheet1' })),+ };++ const result = calcHighlightRanges({+ unitId: 'unit1',+ subUnitId: 'sheet1',+ currentWorkbook: workbook as any,+ refSelections: [+ { token: 'A1', themeColor: '#ff0000', refIndex: 0, startIndex: 0, endIndex: 1, index: 0 },+ ],+ editor: undefined,+ refSelectionsService: {+ getCurrentSelections: vi.fn(() => [{ primary: { actualRow: 9, actualColumn: 9 } }]),+ setSelections: vi.fn(),+ } as any,+ refSelectionsRenderService: undefined,+ sheetSkeletonManagerService: {+ getSkeleton: vi.fn(() => ({})),+ } as any,+ themeService: { getColorFromTheme: vi.fn((key: string) => key === 'white' ? '#fff' : key) } as any,+ univerInstanceService: { getUnit: vi.fn(() => workbook) } as any,+ });++ expect(result?.[0].primary).toBeUndefined();+ });+it('clears highlight selections when the workbook or active sheet is unavailable', () => {const refSelectionsService = {getCurrentSelections: vi.fn(() => []),diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.tsindex 8d2032f16c..4b41c58f17 100644--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { ITextRange, ITextRun, Workbook } from '@univerjs/core';+import type { IRange, ITextRange, ITextRun, Workbook } from '@univerjs/core';import type { Editor } from '@univerjs/docs-ui';import type { ISequenceNode } from '@univerjs/engine-formula';import type { ISelectionWithStyle, SheetsSelectionsService } from '@univerjs/sheets';@@ -40,6 +40,16 @@ export interface IRefSelection {index: number;}+function isSameSelectionRange(left: IRange | undefined, right: IRange): boolean {+ return !!left &&+ left.startRow === right.startRow &&+ left.startColumn === right.startColumn &&+ left.endRow === right.endRow &&+ left.endColumn === right.endColumn &&+ (!left.sheetId || !right.sheetId || left.sheetId === right.sheetId) &&+ (!left.unitId || !right.unitId || left.unitId === right.unitId);+}+// eslint-disable-next-line complexity, max-lines-per-functionexport function calcHighlightRanges(opts: {unitId: string;@@ -80,6 +90,24 @@ export function calcHighlightRanges(opts: {if (!skeleton) return;const endIndexes: number[] = [];const currentSelections = refSelectionsService.getCurrentSelections();+ const matchedCurrentSelectionIndexes = new Set<number>();+ const getPrimaryForRange = (range: IRange, fallbackIndex: number) => {+ const currentSelection = currentSelections[fallbackIndex];+ if (isSameSelectionRange(currentSelection?.range, range)) {+ matchedCurrentSelectionIndexes.add(fallbackIndex);+ return currentSelection.primary;+ }++ const matchedIndex = currentSelections.findIndex((selection, index) =>+ !matchedCurrentSelectionIndexes.has(index) && isSameSelectionRange(selection.range, range)+ );+ if (matchedIndex !== -1) {+ matchedCurrentSelectionIndexes.add(matchedIndex);+ return currentSelections[matchedIndex].primary;+ }++ return undefined;+ };for (let i = 0, len = refSelections.length; i < len; i++) {const refSelection = refSelections[i];const { themeColor, token, refIndex, endIndex } = refSelection;@@ -107,10 +135,9 @@ export function calcHighlightRanges(opts: {const range = setEndForRange(rawRange, worksheet.getRowCount(), worksheet.getColumnCount());range.unitId = unitId;range.sheetId = currentSheetId;- const currentSelection = currentSelections[selectionWithStyle.length];selectionWithStyle.push({range,- primary: currentSelection ? currentSelection.primary : undefined,+ primary: getPrimaryForRange(range, selectionWithStyle.length),style: genFormulaRefSelectionStyle(themeService, themeColor, refIndex.toString()),});endIndexes.push(endIndex);