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): fix sheets editor (#7208)

wpxp12345680f8f50e7da4
diff --git a/packages/core/src/sheets/__tests__/util.spec.ts b/packages/core/src/sheets/__tests__/util.spec.ts
index 103c27792c..02016ebe65 100644
--- a/packages/core/src/sheets/__tests__/util.spec.ts
+++ b/packages/core/src/sheets/__tests__/util.spec.ts
@@ -65,6 +65,19 @@ describe('sheet util helpers', () => {
expect(documentModel.getDrawingsOrder()).toEqual([]);
});
+ it('should fill missing cell padding sides with defaults when creating document models', () => {
+ const documentModel = createDocumentModelWithStyle('March 5', {}, {
+ paddingData: { l: 5 },
+ });
+
+ expect(documentModel.getSnapshot().documentStyle).toMatchObject({
+ marginTop: 0,
+ marginRight: 2,
+ marginBottom: 2,
+ marginLeft: 5,
+ });
+ });
+
it('should extract cell style fragments and enrich document links once', () => {
const style = {
ff: 'Inter',
diff --git a/packages/core/src/sheets/util.ts b/packages/core/src/sheets/util.ts
index 79bc37fb87..8bf1a9456f 100644
--- a/packages/core/src/sheets/util.ts
+++ b/packages/core/src/sheets/util.ts
@@ -20,9 +20,9 @@ import type { IDocumentData, IPaddingData, IStyleBase, IStyleData, ITextRotation
import type { ICellData, IRange, IUnitRange } from './typedef';
import { DEFAULT_EMPTY_DOCUMENT_VALUE } from '../common/const';
import { BuildTextUtils, DocumentDataModel } from '../docs';
-import { createParagraphId } from '../docs/paragraph-id';
import { TextX } from '../docs/data-model/text-x/text-x';
import { convertTextRotation } from '../docs/data-model/utils';
+import { createParagraphId } from '../docs/paragraph-id';
import { Rectangle } from '../shared';
import { HorizontalAlign, VerticalAlign, WrapStrategy } from '../types/enum';
import { CustomRangeType, DocumentFlavor } from '../types/interfaces';
@@ -80,7 +80,10 @@ export function createDocumentModelWithStyle(content: string, textStyle: ITextSt
cellValueType,
} = config;
- const { t: marginTop, r: marginRight, b: marginBottom, l: marginLeft } = paddingData || DEFAULT_PADDING_DATA;
+ const marginTop = paddingData?.t ?? DEFAULT_PADDING_DATA.t;
+ const marginRight = paddingData?.r ?? DEFAULT_PADDING_DATA.r;
+ const marginBottom = paddingData?.b ?? DEFAULT_PADDING_DATA.b;
+ const marginLeft = paddingData?.l ?? DEFAULT_PADDING_DATA.l;
const { vertexAngle, centerAngle } = convertTextRotation(textRotation);
const documentData: IDocumentData = {
id: 'd',
diff --git a/packages/sheets-ui/src/services/editor/__tests__/cell-editor-resize.service.spec.ts b/packages/sheets-ui/src/services/editor/__tests__/cell-editor-resize.service.spec.ts
index d7bef0fb3e..e726ab0396 100644
--- a/packages/sheets-ui/src/services/editor/__tests__/cell-editor-resize.service.spec.ts
+++ b/packages/sheets-ui/src/services/editor/__tests__/cell-editor-resize.service.spec.ts
@@ -253,6 +253,18 @@ class TestCachedWidthRenderManagerService extends TestRenderManagerService {
};
}
+class TestViewportEdgeRenderManagerService extends TestRenderManagerService {
+ override readonly sheetCanvasElement = {
+ style: { width: '1192px' },
+ getBoundingClientRect: () => ({ left: 30, top: 50, width: 1192, height: 773 }),
+ };
+
+ override readonly sheetEngine = {
+ width: 1192,
+ getCanvasElement: () => this.sheetCanvasElement,
+ };
+}
+
describe('SheetCellEditorResizeService', () => {
afterEach(() => {
vi.unstubAllGlobals();
@@ -439,4 +451,41 @@ describe('SheetCellEditorResizeService', () => {
show: true,
}));
});
+
+ it('keeps a centered merged-cell editor at least as wide as the edited merged range', async () => {
+ vi.stubGlobal('window', new EventTarget());
+ vi.stubGlobal('document', { activeElement: { dataset: {} } });
+ const injector = new Injector();
+ injector.add([ILogService, { useClass: DesktopLogService }]);
+ injector.add([IContextService, { useClass: ContextService }]);
+ injector.add([IUniverInstanceService, { useClass: TestUniverInstanceService as never }]);
+ injector.add([ICommandService, { useClass: CommandService }]);
+ injector.add([IUndoRedoService, { useClass: LocalUndoRedoService }]);
+ injector.add([ThemeService]);
+ injector.add([ILayoutService, { useClass: TestLayoutService as never }]);
+ injector.add([DocSelectionManagerService]);
+ injector.add([SheetInterceptorService]);
+ injector.add([SheetSkeletonService]);
+ injector.add([IEditorService, { useClass: EditorService }]);
+ injector.add([ICellEditorManagerService, { useClass: TestCellEditorManagerService as never }]);
+ injector.add([IEditorBridgeService, { useClass: TestEditorBridgeService as never }]);
+ injector.add([IRenderManagerService, { useClass: TestViewportEdgeRenderManagerService as never }]);
+ injector.add([IConfigService, { useClass: ConfigService }]);
+ injector.add([SheetCellEditorResizeService]);
+ const editorBridge = injector.get(IEditorBridgeService) as unknown as TestEditorBridgeService;
+ editorBridge.editCellState.position = {
+ startX: 426,
+ startY: 90,
+ endX: 1206,
+ endY: 122,
+ };
+ editorBridge.editCellState.documentLayoutObject.horizontalAlign = HorizontalAlign.CENTER;
+
+ injector.get(SheetCellEditorResizeService).fitTextSize();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ const editorManager = injector.get(ICellEditorManagerService) as unknown as TestCellEditorManagerService;
+ const state = editorManager.getState();
+ expect(state!.endX as number - (state!.startX as number)).toBe(780);
+ });
});
diff --git a/packages/sheets-ui/src/services/editor/cell-editor-resize.service.ts b/packages/sheets-ui/src/services/editor/cell-editor-resize.service.ts
index 82dfde8269..d3ef1b717d 100644
--- a/packages/sheets-ui/src/services/editor/cell-editor-resize.service.ts
+++ b/packages/sheets-ui/src/services/editor/cell-editor-resize.service.ts
@@ -239,15 +239,17 @@ export class SheetCellEditorResizeService extends Disposable {
const maxHeight = height - startY - EDITOR_BORDER_SIZE * 2;
+ const cellWidth = endX - startX;
let maxWidth = width - startX;
if (horizontalAlign === HorizontalAlign.CENTER) {
const rightGap = enginWidth - endX;
const leftGap = startX;
- maxWidth = (endX - startX) + Math.min(leftGap, rightGap) * 2;
+ maxWidth = cellWidth + Math.min(leftGap, rightGap) * 2;
} else if (horizontalAlign === HorizontalAlign.RIGHT) {
maxWidth = endX;
}
maxWidth = maxWidth - EDITOR_BORDER_SIZE * 2;
+ maxWidth = Math.max(maxWidth, cellWidth);
return {
height: maxHeight,