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(fx): fix fx bar ref range highlight (#7168)
wpxp123456f422574a30b7
diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/data-sync.controller.spec.ts b/packages/sheets-ui/src/controllers/editor/__tests__/data-sync.controller.spec.tsindex 1c3f6b6884..43fbeb538b 100644--- a/packages/sheets-ui/src/controllers/editor/__tests__/data-sync.controller.spec.ts+++ b/packages/sheets-ui/src/controllers/editor/__tests__/data-sync.controller.spec.ts@@ -14,12 +14,22 @@* limitations under the License.*/-import type { ICommandInfo } from '@univerjs/core';+import type { DocumentDataModel, ICommandInfo, IDocumentData } from '@univerjs/core';+import {+ BooleanNumber,+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,+ FOCUSING_FX_BAR_EDITOR,+} from '@univerjs/core';import { MoveRangeMutation, SetRangeValuesMutation } from '@univerjs/sheets';-import { BehaviorSubject } from 'rxjs';+import { BehaviorSubject, EMPTY } from 'rxjs';import { afterEach, describe, expect, it, vi } from 'vitest';import { EditorDataSyncController } from '../data-sync.controller';+interface ITestableEditorDataSyncController {+ _checkAndSetRenderStyleConfig: (documentDataModel: Pick<DocumentDataModel, 'getSnapshot'>) => void;+}+class TestCommandService {private readonly _listeners = new Set<(command: ICommandInfo) => void>();@@ -43,7 +53,7 @@ describe('EditorDataSyncController', () => {controllers.splice(0).forEach((controller) => controller.dispose());});- function createController() {+ function createController(options: { isFocusFxBar?: boolean } = {}) {const commandService = new TestCommandService();const editorBridgeService = {currentEditCellState$: new BehaviorSubject(null),@@ -54,6 +64,11 @@ describe('EditorDataSyncController', () => {refreshEditCellState: vi.fn(),isForceKeepVisible: vi.fn(() => false),};+ const contextService = {+ getContextValue: vi.fn((key: string) =>+ key === FOCUSING_FX_BAR_EDITOR ? options.isFocusFxBar ?? false : false),+ subscribeContextValue$: vi.fn(() => EMPTY),+ };const controller = new EditorDataSyncController({} as never,@@ -63,17 +78,25 @@ describe('EditorDataSyncController', () => {{ getRangeRuleInitState: () => true } as never,{ getSheetRuleInitState: () => true } as never,{ autoScroll: vi.fn() } as never,- { getPosition: () => null } as never+ { getPosition: () => null } as never,+ contextService as never);controllers.push(controller);return {commandService,+ controller,editorBridgeService,};}+ function checkAndSetRenderStyleConfig(controller: EditorDataSyncController, snapshot: IDocumentData) {+ (controller as unknown as ITestableEditorDataSyncController)._checkAndSetRenderStyleConfig({+ getSnapshot: vi.fn(() => snapshot),+ });+ }+it('refreshes the current edit cell when set-values updates the edited cell', () => {const { commandService, editorBridgeService } = createController();@@ -131,4 +154,64 @@ describe('EditorDataSyncController', () => {expect(editorBridgeService.refreshEditCellState).toHaveBeenCalledTimes(1);});++ it('renders formula reference styles in the cell editor when the cell editor is focused', () => {+ const { controller } = createController();+ const cellEditorSnapshot: IDocumentData = {+ id: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,+ body: {+ dataStream: '=G10+G12+G13\r\n',+ textRuns: [+ { st: 1, ed: 4, ts: { cl: { rgb: '#8f7cf6' }, fs: 11 } },+ ],+ },+ documentStyle: {},+ };+ const formulaBarSnapshot: IDocumentData = {+ id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,+ body: {+ dataStream: '=G10+G12+G13\r\n',+ textRuns: [+ { st: 1, ed: 4, ts: { cl: { rgb: '#8f7cf6' }, fs: 11 } },+ ],+ },+ documentStyle: {},+ };++ checkAndSetRenderStyleConfig(controller, cellEditorSnapshot);+ checkAndSetRenderStyleConfig(controller, formulaBarSnapshot);++ expect(cellEditorSnapshot.documentStyle.renderConfig?.isRenderStyle).toBe(BooleanNumber.TRUE);+ expect(formulaBarSnapshot.documentStyle.renderConfig?.isRenderStyle).toBe(BooleanNumber.FALSE);+ });++ it('renders formula reference styles in the formula bar when the formula bar is focused', () => {+ const { controller } = createController({ isFocusFxBar: true });+ const cellEditorSnapshot: IDocumentData = {+ id: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,+ body: {+ dataStream: '=G10+G12+G13\r\n',+ textRuns: [+ { st: 1, ed: 4, ts: { cl: { rgb: '#8f7cf6' }, fs: 11 } },+ ],+ },+ documentStyle: {},+ };+ const formulaBarSnapshot: IDocumentData = {+ id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,+ body: {+ dataStream: '=G10+G12+G13\r\n',+ textRuns: [+ { st: 1, ed: 4, ts: { cl: { rgb: '#8f7cf6' }, fs: 11 } },+ ],+ },+ documentStyle: {},+ };++ checkAndSetRenderStyleConfig(controller, cellEditorSnapshot);+ checkAndSetRenderStyleConfig(controller, formulaBarSnapshot);++ expect(cellEditorSnapshot.documentStyle.renderConfig?.isRenderStyle).toBe(BooleanNumber.FALSE);+ expect(formulaBarSnapshot.documentStyle.renderConfig?.isRenderStyle).toBe(BooleanNumber.TRUE);+ });});diff --git a/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts b/packages/sheets-ui/src/controllers/editor/data-sync.controller.tsindex 5a65085faa..46e85efde9 100644--- a/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts+++ b/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts@@ -26,8 +26,10 @@ import {DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,DOCS_NORMAL_EDITOR_UNIT_ID_KEY,DocumentFlavor,+ FOCUSING_FX_BAR_EDITOR,HorizontalAlign,ICommandService,+ IContextService,Inject,IUniverInstanceService,Tools,@@ -76,7 +78,8 @@ export class EditorDataSyncController extends Disposable {@Inject(RangeProtectionRuleModel) private readonly _rangeProtectionRuleModel: RangeProtectionRuleModel,@Inject(WorksheetProtectionRuleModel) private readonly _worksheetProtectionRuleModel: WorksheetProtectionRuleModel,@Inject(FormulaEditorController) private readonly _formulaEditorController: FormulaEditorController,- @IFormulaEditorManagerService private readonly _formulaEditorManagerService: IFormulaEditorManagerService+ @IFormulaEditorManagerService private readonly _formulaEditorManagerService: IFormulaEditorManagerService,+ @IContextService private readonly _contextService: IContextService) {super();@@ -86,6 +89,7 @@ export class EditorDataSyncController extends Disposable {private _initialize() {this._syncFormulaEditorContent();this._commandExecutedListener();+ this._syncFormulaRefRenderStyleOnFocusChange();}private _getEditorViewModel(unitId: string): Nullable<DocumentViewModel> {@@ -239,6 +243,7 @@ export class EditorDataSyncController extends Disposable {syncer: parmas.unitId,});+ this._checkAndSetRenderStyleConfig(docDataModel);docViewModel.reset(docDataModel);skeleton.calculate();@@ -291,34 +296,69 @@ export class EditorDataSyncController extends Disposable {}}+ private _syncFormulaRefRenderStyleOnFocusChange() {+ this.disposeWithMe(this._contextService.subscribeContextValue$(FOCUSING_FX_BAR_EDITOR).subscribe(() => {+ this._refreshRenderStyleConfig(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);+ this._refreshRenderStyleConfig(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);+ }));+ }++ private _refreshRenderStyleConfig(unitId: string) {+ const currentRender = this._renderManagerService.getRenderById(unitId);+ const skeleton = currentRender?.with(DocSkeletonManagerService).getSkeleton();+ const docDataModel = this._univerInstanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);+ const docViewModel = this._getEditorViewModel(unitId);++ if (currentRender == null || skeleton == null || docDataModel == null || docViewModel == null) {+ return;+ }++ this._checkAndSetRenderStyleConfig(docDataModel);+ docViewModel.reset(docDataModel);+ skeleton.calculate();+ currentRender.mainComponent?.makeDirty();++ if (unitId === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {+ this._formulaEditorController.autoScroll();+ }+ }+private _checkAndSetRenderStyleConfig(documentDataModel: DocumentDataModel) {const snapshot = documentDataModel.getSnapshot();- const { body } = snapshot;- if (snapshot.id !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY) {+ if (snapshot.id !== DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY && snapshot.id !== DOCS_NORMAL_EDITOR_UNIT_ID_KEY) {return;}- snapshot.documentStyle = formulaEditorStyle;+ const isFormulaBar = snapshot.id === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;+ if (isFormulaBar) {+ snapshot.documentStyle = formulaEditorStyle;+ } else {+ snapshot.documentStyle ??= {};+ }let renderConfig = snapshot.documentStyle.renderConfig;if (renderConfig == null) {renderConfig = {};snapshot.documentStyle.renderConfig = renderConfig;}- const position = this._formulaEditorManagerService.getPosition();- if (position) {+ const position = isFormulaBar ? this._formulaEditorManagerService.getPosition() : null;+ if (isFormulaBar && position) {const width = position.width;snapshot.documentStyle.pageSize = {width,height: Infinity,};}- if ((body?.dataStream ?? '').startsWith('=')) {- renderConfig.isRenderStyle = BooleanNumber.TRUE;- } else {- renderConfig.isRenderStyle = BooleanNumber.FALSE;++ const isFormula = (snapshot.body?.dataStream ?? '').startsWith('=');+ if (!isFormula) {+ renderConfig.isRenderStyle = isFormulaBar ? BooleanNumber.FALSE : BooleanNumber.TRUE;+ return;}++ const isFocusFxBar = this._contextService.getContextValue(FOCUSING_FX_BAR_EDITOR);+ renderConfig.isRenderStyle = isFormulaBar === isFocusFxBar ? BooleanNumber.TRUE : BooleanNumber.FALSE;}private _clearParagraph(paragraphs: IParagraph[]) {