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(engine-render): align multiline cell text (#7427)

wpxp123456153baa92394d
diff --git a/packages/engine-render/src/components/sheets/extensions/__tests__/font.spec.ts b/packages/engine-render/src/components/sheets/extensions/__tests__/font.spec.ts
index fb3327c1e2..9ee21488f2 100644
--- a/packages/engine-render/src/components/sheets/extensions/__tests__/font.spec.ts
+++ b/packages/engine-render/src/components/sheets/extensions/__tests__/font.spec.ts
@@ -296,6 +296,7 @@ describe('font extension', () => {
const documentDataModel = {
updateDocumentDataPageSize: vi.fn(),
+ getBody: vi.fn(() => ({ paragraphs: [{}] })),
getSnapshot: vi.fn(() => ({
documentStyle: {
marginLeft: 1,
@@ -357,6 +358,109 @@ describe('font extension', () => {
expect(() => font._renderDocuments(ctx, 0, 0, renderFontCtx, overflow)).toThrow('documents is null');
});
+ it('uses the natural content width for a non-wrapped document without cell overflow', () => {
+ const font = new Font() as any;
+ const ctx = createCtx();
+ const overflow = new ObjectMatrix<any>();
+ const spreadsheetSkeleton = createSpreadsheetSkeleton();
+ const documentDataModel = {
+ updateDocumentDataPageSize: vi.fn(),
+ getBody: vi.fn(() => ({ paragraphs: [{}, {}] })),
+ getSnapshot: vi.fn(() => ({
+ documentStyle: {
+ marginLeft: 1,
+ marginRight: 2,
+ },
+ })),
+ };
+ const documentSkeleton = {
+ getViewModel: vi.fn(() => ({
+ getDataModel: vi.fn(() => documentDataModel),
+ })),
+ calculate: vi.fn(),
+ makeDirty: vi.fn(),
+ getSkeletonData: vi.fn(() => ({
+ pages: [{
+ width: 30,
+ height: 12,
+ sections: [{
+ columns: [{
+ width: 30,
+ spaceWidth: 0,
+ lines: [{ lineHeight: 10 }],
+ }],
+ }],
+ }],
+ })),
+ };
+ const documents = {
+ resize: vi.fn(),
+ changeSkeleton: vi.fn(() => documents),
+ render: vi.fn(),
+ };
+ font.parent = {
+ getDocuments: vi.fn(() => documents),
+ };
+
+ font._renderDocuments(ctx, 0, 0, {
+ fontCache: createFontCache({
+ documentSkeleton,
+ wrapStrategy: WrapStrategy.OVERFLOW,
+ vertexAngle: 0,
+ }),
+ startX: 0,
+ startY: 0,
+ endX: 40,
+ endY: 20,
+ spreadsheetSkeleton,
+ }, overflow);
+
+ expect(documentDataModel.updateDocumentDataPageSize).toHaveBeenNthCalledWith(1, Number.POSITIVE_INFINITY);
+ expect(documentDataModel.updateDocumentDataPageSize).toHaveBeenNthCalledWith(2, 33);
+ expect(documentSkeleton.calculate).toHaveBeenCalledOnce();
+
+ documentDataModel.updateDocumentDataPageSize.mockClear();
+ documentDataModel.getBody.mockReturnValue({ paragraphs: [{}] });
+ documentSkeleton.calculate.mockClear();
+
+ font._renderDocuments(ctx, 0, 0, {
+ fontCache: createFontCache({
+ documentSkeleton,
+ wrapStrategy: WrapStrategy.OVERFLOW,
+ vertexAngle: 0,
+ }),
+ startX: 0,
+ startY: 0,
+ endX: 40,
+ endY: 20,
+ spreadsheetSkeleton,
+ }, overflow);
+
+ expect(documentDataModel.updateDocumentDataPageSize).toHaveBeenCalledOnce();
+ expect(documentDataModel.updateDocumentDataPageSize).toHaveBeenCalledWith(Number.POSITIVE_INFINITY);
+ expect(documentSkeleton.calculate).not.toHaveBeenCalled();
+
+ documentDataModel.updateDocumentDataPageSize.mockClear();
+ documentDataModel.getBody.mockReturnValue({ paragraphs: [{}, {}] });
+
+ font._renderDocuments(ctx, 0, 0, {
+ fontCache: createFontCache({
+ documentSkeleton,
+ wrapStrategy: WrapStrategy.WRAP,
+ vertexAngle: 45,
+ }),
+ startX: 0,
+ startY: 0,
+ endX: 40,
+ endY: 20,
+ spreadsheetSkeleton,
+ }, overflow);
+
+ expect(documentDataModel.updateDocumentDataPageSize).toHaveBeenCalledOnce();
+ expect(documentDataModel.updateDocumentDataPageSize).toHaveBeenCalledWith(Number.POSITIVE_INFINITY);
+ expect(documentSkeleton.calculate).not.toHaveBeenCalled();
+ });
+
it('covers image rendering fallback branches', () => {
const font = new Font() as any;
const ctx = createCtx();
diff --git a/packages/engine-render/src/components/sheets/extensions/font.ts b/packages/engine-render/src/components/sheets/extensions/font.ts
index e113a58c00..f1878c2979 100644
--- a/packages/engine-render/src/components/sheets/extensions/font.ts
+++ b/packages/engine-render/src/components/sheets/extensions/font.ts
@@ -696,7 +696,11 @@ export class Font extends SheetExtension {
// so that the divide will be aligned when the skeleton is calculated.
const overflowRectangle = overflowCache.getValue(row, col);
const isOverflow = !(wrapStrategy === WrapStrategy.WRAP && vertexAngle === 0);
- if (isOverflow && overflowRectangle) {
+ const hasMultipleParagraphs = (documentDataModel.getBody()?.paragraphs?.length ?? 0) > 1;
+ if (
+ (isOverflow && overflowRectangle) ||
+ (wrapStrategy !== WrapStrategy.WRAP && hasMultipleParagraphs)
+ ) {
const contentSize = getDocsSkeletonPageSize(documentSkeleton);
const documentStyle = documentDataModel.getSnapshot().documentStyle;
@@ -710,6 +714,9 @@ export class Font extends SheetExtension {
.updateDocumentDataPageSize(width + marginLeft + marginRight);
documentSkeleton.calculate();
}
+ }
+
+ if (isOverflow && overflowRectangle) {
const endColumn = overflowRectangle.endColumn;
const startColumn = overflowRectangle.startColumn;
const startRow = overflowRectangle.startRow;