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(sheets-ui): ignore zero-width formula bar geometry (#7561)

naktamellod5db16927542
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.ts
index 6b21f10c3a..41b1f4170f 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
@@ -67,7 +67,11 @@ describe('EditorDataSyncController', () => {
controllers.splice(0).forEach((controller) => controller.dispose());
});
- function createController(options: { isFocusFxBar?: boolean; themeTextColor?: string } = {}) {
+ function createController(options: {
+ isFocusFxBar?: boolean;
+ themeTextColor?: string;
+ formulaBarPosition?: { width: number; height: number } | null;
+ } = {}) {
const commandService = new TestCommandService();
const editorBridgeService = {
currentEditCellState$: new BehaviorSubject(null),
@@ -93,7 +97,9 @@ describe('EditorDataSyncController', () => {
injector.add([RangeProtectionRuleModel, { useValue: { getRangeRuleInitState: () => true } as never }]);
injector.add([WorksheetProtectionRuleModel, { useValue: { getSheetRuleInitState: () => true } as never }]);
injector.add([FormulaEditorController, { useValue: { autoScroll: vi.fn() } as never }]);
- injector.add([IFormulaEditorManagerService, { useValue: { getPosition: () => null } as never }]);
+ injector.add([IFormulaEditorManagerService, {
+ useValue: { getPosition: () => options.formulaBarPosition ?? null } as never,
+ }]);
injector.add([IContextService, { useValue: contextService as never }]);
injector.add([ThemeService, {
useValue: {
@@ -122,6 +128,54 @@ describe('EditorDataSyncController', () => {
});
}
+ it('ignores a zero-width formula bar position', () => {
+ const { controller } = createController({ formulaBarPosition: { width: 0, height: 28 } });
+ const formulaBarSnapshot: IDocumentData = {
+ id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ documentStyle: {},
+ body: {
+ dataStream: 'text\r\n',
+ },
+ };
+
+ checkAndSetRenderStyleConfig(controller, formulaBarSnapshot);
+
+ expect(formulaBarSnapshot.documentStyle?.pageSize?.width).toBe(Number.POSITIVE_INFINITY);
+ });
+
+ it('applies a measured formula bar width', () => {
+ const { controller } = createController({ formulaBarPosition: { width: 320, height: 28 } });
+ const formulaBarSnapshot: IDocumentData = {
+ id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ documentStyle: {},
+ body: {
+ dataStream: 'text\r\n',
+ },
+ };
+
+ checkAndSetRenderStyleConfig(controller, formulaBarSnapshot);
+
+ expect(formulaBarSnapshot.documentStyle?.pageSize?.width).toBe(320);
+ });
+
+ it('keeps the previous formula bar width when the latest position has zero width', () => {
+ const formulaBarPosition = { width: 320, height: 28 };
+ const { controller } = createController({ formulaBarPosition });
+ const formulaBarSnapshot: IDocumentData = {
+ id: DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ documentStyle: {},
+ body: {
+ dataStream: 'text\r\n',
+ },
+ };
+
+ checkAndSetRenderStyleConfig(controller, formulaBarSnapshot);
+ formulaBarPosition.width = 0;
+ checkAndSetRenderStyleConfig(controller, formulaBarSnapshot);
+
+ expect(formulaBarSnapshot.documentStyle.pageSize?.width).toBe(320);
+ });
+
it('refreshes the current edit cell when set-values updates the edited cell', () => {
const { commandService, editorBridgeService } = createController();
diff --git a/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts b/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts
index 25bafa462e..1cccb72985 100644
--- a/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/data-sync.controller.ts
@@ -341,6 +341,7 @@ export class EditorDataSyncController extends Disposable {
}
const isFormulaBar = snapshot.id === DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY;
+ const previousPageWidth = snapshot.documentStyle?.pageSize?.width;
if (isFormulaBar) {
snapshot.documentStyle = {
...formulaEditorStyle,
@@ -361,11 +362,14 @@ export class EditorDataSyncController extends Disposable {
}
const position = isFormulaBar ? this._formulaEditorManagerService.getPosition() : null;
if (isFormulaBar && position) {
- const width = position.width;
- snapshot.documentStyle.pageSize = {
- width,
- height: Infinity,
- };
+ // A non-positive width means the formula bar is not laid out, so keep the last valid width.
+ const width = position.width > 0 ? position.width : previousPageWidth;
+ if (width != null && width > 0) {
+ snapshot.documentStyle.pageSize = {
+ width,
+ height: Infinity,
+ };
+ }
}
const isFormula = (snapshot.body?.dataStream ?? '').startsWith('=');