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): honor traditional table margin overflow policy (#7934)

Univer0ce9403dd33c
diff --git a/packages/docs-ui/src/views/__tests__/ParagraphMenu.spec.ts b/packages/docs-ui/src/views/__tests__/ParagraphMenu.spec.ts
index 1ef3be3a22..4586eb0874 100644
--- a/packages/docs-ui/src/views/__tests__/ParagraphMenu.spec.ts
+++ b/packages/docs-ui/src/views/__tests__/ParagraphMenu.spec.ts
@@ -17,12 +17,12 @@
import type { IDocumentBlockRange, IDocumentBody, IParagraphStyle } from '@univerjs/core';
import type { IDocBlockMoveValidationContext } from '@univerjs/docs';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
-import type { IMutiPageParagraphBound } from '../../services/doc-event-manager.service';
-import type { IDocBlockMenuTarget } from '../../services/doc-paragraph-menu.service';
import { DataStreamTreeTokenType, DocumentBlockRangeType, DocumentBlockType, JSONX, NamedStyleType } from '@univerjs/core';
import { DocBlockMoveValidatorService } from '@univerjs/docs';
import { ContextMenuPosition } from '@univerjs/ui';
import { describe, expect, it } from 'vitest';
+import type { IMutiPageParagraphBound } from '../../services/doc-event-manager.service';
+import type { IDocBlockMenuTarget } from '../../services/doc-paragraph-menu.service';
import {
DocCopyCurrentParagraphCommand,
DocCutCurrentParagraphCommand,
diff --git a/packages/engine-render/src/components/docs/layout/block/__tests__/table.spec.ts b/packages/engine-render/src/components/docs/layout/block/__tests__/table.spec.ts
index 1b31667d1b..4b142d545c 100644
--- a/packages/engine-render/src/components/docs/layout/block/__tests__/table.spec.ts
+++ b/packages/engine-render/src/components/docs/layout/block/__tests__/table.spec.ts
@@ -15,7 +15,6 @@
*/
import type { ITable } from '@univerjs/core';
-import type { ILayoutContext } from '../../tools';
import {
BooleanNumber,
DocumentFlavor,
@@ -28,6 +27,7 @@ import {
VerticalAlignmentType,
} from '@univerjs/core';
import { beforeEach, describe, expect, it, vi } from 'vitest';
+import type { ILayoutContext } from '../../tools';
import { BreakType, DocumentSkeletonPageType } from '../../../../../basics/i-document-skeleton-cached';
import { getDocumentCompatibilityPolicy } from '../../../document-compatibility';
import {
@@ -224,6 +224,18 @@ describe('table utilities', () => {
});
describe('getTableLeft', () => {
+ it.each([TableAlignmentType.CENTER, TableAlignmentType.END])('allows margin overflow only when requested (alignment: %s)', (align) => {
+ const expected = align === TableAlignmentType.CENTER ? -29 : -58;
+ expect(getTableLeft(554, 612, align, { v: 0 }, true)).toBe(expected);
+ expect(getTableLeft(554, 612, align)).toBe(0);
+ expect(getTableLeft(554, 612, align, { v: 0 }, false)).toBe(0);
+ });
+
+ it.each([TableAlignmentType.START, TableAlignmentType.CENTER, TableAlignmentType.END])('preserves tables that fit the column (alignment: %s)', (align) => {
+ expect(getTableLeft(554, 120, align, { v: 8 }, true))
+ .toBe(getTableLeft(554, 120, align, { v: 8 }));
+ });
+
it('centers a table within the supplied column width', () => {
expect(getTableLeft(243, 243, TableAlignmentType.CENTER)).toBe(0);
expect(261 + getTableLeft(243, 120, TableAlignmentType.CENTER)).toBe(322.5);
@@ -314,6 +326,51 @@ describe('docs table layout', () => {
expect(JSON.stringify(tableSource)).toBe(source);
});
+ it.each(
+ [DocumentFlavor.TRADITIONAL, DocumentFlavor.MODERN, DocumentFlavor.UNSPECIFIED, undefined].flatMap((flavor) =>
+ [TableAlignmentType.CENTER, TableAlignmentType.END].flatMap((align) =>
+ [false, true].flatMap((sliced) => [false, true].map((incremental) => ({ flavor, align, sliced, incremental })))
+ )
+ )
+ )('positions oversized tables with the compatibility policy ($flavor, $align, sliced: $sliced, incremental: $incremental)', ({ flavor, align, sliced, incremental }) => {
+ const { ctx, curPage, viewModel, tableNode, sectionBreakConfig, tableSource } = createContextAndTable();
+ curPage.pageWidth = 100;
+ tableSource.align = align;
+ tableSource.size = { type: TableSizeType.SPECIFIED, width: { v: 120 } };
+ if (flavor != null) {
+ sectionBreakConfig.documentCompatibilityPolicy = getDocumentCompatibilityPolicy(flavor);
+ }
+ const source = JSON.stringify(tableSource);
+ let tables;
+ if (sliced) {
+ if (incremental) {
+ const state = startTableSkeletonsBuild(ctx, curPage, viewModel, tableNode, sectionBreakConfig, 35)!;
+ while (!stepTableSkeletonsBuild(state)) {
+ // Complete the resumable pagination path.
+ }
+ tables = state.result!.skeTables;
+ } else {
+ tables = createTableSkeletons(ctx, curPage, viewModel, tableNode, sectionBreakConfig, 35).skeTables;
+ }
+ } else if (incremental) {
+ const state = startTableSkeletonBuild(ctx, curPage, viewModel, tableNode, sectionBreakConfig)!;
+ while (!stepTableSkeletonBuild(state)) {
+ // Complete the resumable single-table path.
+ }
+ tables = [state.tableSkeleton];
+ } else {
+ tables = [createTableSkeleton(ctx, curPage, viewModel, tableNode, sectionBreakConfig)!];
+ }
+ expect(tables.length).toBeGreaterThanOrEqual(sliced ? 2 : 1);
+ const alignmentLeft = align === TableAlignmentType.CENTER ? -20 : -40;
+ const expectedLeft = flavor === DocumentFlavor.TRADITIONAL ? alignmentLeft : 0;
+ for (const table of tables) {
+ expect(table.width).toBe(120);
+ expect(table.left).toBe(expectedLeft);
+ }
+ expect(JSON.stringify(tableSource)).toBe(source);
+ });
+
it('creates table skeleton and applies row/cell alignment data', () => {
const { ctx, curPage, viewModel, tableNode, sectionBreakConfig } = createContextAndTable();
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts
index 5e444f3413..8b6942940f 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/__tests__/layout-ruler.spec.ts
@@ -14,11 +14,6 @@
* limitations under the License.
*/
-import type {
- IDocumentSkeletonDivide,
- IDocumentSkeletonGlyph,
-} from '../../../../../../basics/i-document-skeleton-cached';
-import type { IParagraphConfig } from '../../../../../../basics/interfaces';
import {
AlignTypeH,
BooleanNumber,
@@ -44,6 +39,11 @@ import {
WrapTextType,
} from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import type {
+ IDocumentSkeletonDivide,
+ IDocumentSkeletonGlyph,
+} from '../../../../../../basics/i-document-skeleton-cached';
+import type { IParagraphConfig } from '../../../../../../basics/interfaces';
import { DocumentSkeletonPageType, GlyphType, LineType } from '../../../../../../basics/i-document-skeleton-cached';
import { setDocsCustomBlockRenderViewportProvider } from '../../../../custom-block-render-viewport';
import { getDocumentCompatibilityPolicy } from '../../../../document-compatibility';
@@ -2904,6 +2904,39 @@ describe('layout-ruler', () => {
expect(table.top).toBe(1029);
});
+ it.each(
+ [DocumentFlavor.TRADITIONAL, DocumentFlavor.MODERN, DocumentFlavor.UNSPECIFIED, undefined].flatMap((flavor) =>
+ [TableAlignmentType.CENTER, TableAlignmentType.END].flatMap((align) =>
+ [false, true].map((hasWidth) => ({ flavor, align, hasWidth }))
+ )
+ )
+ )('retains the table overflow policy during final paragraph placement ($flavor, $align, width: $hasWidth)', ({ flavor, align, hasWidth }) => {
+ const table = {
+ tableId: 'imported-table',
+ tableSource: {
+ align,
+ ...(hasWidth ? { size: { type: TableSizeType.SPECIFIED, width: { v: 612 } } } : {}),
+ textWrap: TableTextWrapType.NONE,
+ },
+ top: 0,
+ left: 0,
+ width: 612,
+ height: 100,
+ } as any;
+ const page = { skeDrawings: new Map(), skeTables: new Map() } as any;
+ const column = { left: 17, width: 554 } as any;
+ const section = { top: 0, height: 900 } as any;
+ const cache = [{ table, tableId: table.tableId, hasPositioned: false, isSlideTable: false, tableNode: {} }] as any;
+ const source = JSON.stringify(table.tableSource);
+ const sectionBreakConfig = flavor != null ? { documentCompatibilityPolicy: getDocumentCompatibilityPolicy(flavor) } : {};
+ __testing.updateAndPositionTable({} as any, 0, 14, page, [page], column, section, cache, 0, sectionBreakConfig as any);
+ const alignmentLeft = align === TableAlignmentType.CENTER ? -29 : -58;
+ const offset = flavor === DocumentFlavor.TRADITIONAL && hasWidth ? alignmentLeft : 0;
+ expect(table.left).toBe(17 + offset);
+ expect(page.skeTables.get(table.tableId)).toBe(table);
+ expect(JSON.stringify(table.tableSource)).toBe(source);
+ });
+
it('positions a sliced table continuation in its current column', () => {
const table = {
tableId: 'table#-#1',
diff --git a/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts b/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
index b207e43f83..eae9ed9eaf 100644
--- a/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
+++ b/packages/engine-render/src/components/docs/layout/block/paragraph/layout-ruler.ts
@@ -55,7 +55,12 @@ import {
} from '../../../../../basics/i-document-skeleton-cached';
import { isCjkLeftAlignedPunctuation } from '../../../../../basics/tools';
import { getDocsCustomBlockRenderViewport } from '../../../custom-block-render-viewport';
-import { getNominalFontLineHeight, isTraditionalDocumentCompatibility } from '../../../document-compatibility';
+import {
+ getDocumentCompatibilityPolicy,
+ getNominalFontLineHeight,
+ isTraditionalDocumentCompatibility,
+ shouldAllowImportedTableMarginOverflow,
+} from '../../../document-compatibility';
import { BreakPointType } from '../../line-breaker/break';
import {
addGlyphToDivide,
@@ -2224,7 +2229,13 @@ function _updateAndPositionTable(
switch (tableSource.textWrap) {
case TableTextWrapType.NONE: {
table.top = section.top + lineTop;
- table.left = column.left + getTableLeft(column.width, table.width, tableSource.align, tableSource.indent);
+ table.left = column.left + getTableLeft(
+ column.width,
+ table.width,
+ tableSource.align,
+ tableSource.indent,
+ shouldAllowImportedTableMarginOverflow(sectionBreakConfig.documentCompatibilityPolicy ?? getDocumentCompatibilityPolicy(), tableSource)
+ );
__avoidFlowAffectingDrawingsForTable(table, page, column);
__avoidNonOverlappingFloatingTables(table, page);
break;
diff --git a/packages/engine-render/src/components/docs/layout/block/table.ts b/packages/engine-render/src/components/docs/layout/block/table.ts
index 279e96df3e..dc57ea6674 100644
--- a/packages/engine-render/src/components/docs/layout/block/table.ts
+++ b/packages/engine-render/src/components/docs/layout/block/table.ts
@@ -15,18 +15,6 @@
*/
import type { INumberUnit, ITable, ITableCell, ITableRow, Nullable } from '@univerjs/core';
-import type {
- IDocumentSkeletonPage,
- IDocumentSkeletonRow,
- IDocumentSkeletonTable,
- IParagraphList,
-} from '../../../../basics/i-document-skeleton-cached';
-import type { ISectionBreakConfig } from '../../../../basics/interfaces';
-import type { DataStreamTreeNode } from '../../view-model/data-stream-tree-node';
-import type { DocumentViewModel } from '../../view-model/document-view-model';
-import type { DocumentFootnoteLayout } from '../footnote-layout';
-import type { ICellSkeletonBuildState } from '../model/page';
-import type { ILayoutContext } from '../tools';
import {
BooleanNumber,
DataStreamTreeTokenType,
@@ -39,8 +27,24 @@ import {
TableSizeType,
VerticalAlignmentType,
} from '@univerjs/core';
+import type {
+ IDocumentSkeletonPage,
+ IDocumentSkeletonRow,
+ IDocumentSkeletonTable,
+ IParagraphList,
+} from '../../../../basics/i-document-skeleton-cached';
+import type { ISectionBreakConfig } from '../../../../basics/interfaces';
+import type { DataStreamTreeNode } from '../../view-model/data-stream-tree-node';
+import type { DocumentViewModel } from '../../view-model/document-view-model';
+import type { DocumentFootnoteLayout } from '../footnote-layout';
+import type { ICellSkeletonBuildState } from '../model/page';
+import type { ILayoutContext } from '../tools';
import { DocumentSkeletonPageType } from '../../../../basics/i-document-skeleton-cached';
-import { getDocumentCompatibilityPolicy, isTraditionalDocumentCompatibility } from '../../document-compatibility';
+import {
+ getDocumentCompatibilityPolicy,
+ isTraditionalDocumentCompatibility,
+ shouldAllowImportedTableMarginOverflow,
+} from '../../document-compatibility';
import {
createNullCellPage,
createSkeletonCellPages,
@@ -342,7 +346,8 @@ function _finishTableSkeletonBuild(state: ITableSkeletonBuildState): void {
pageWidth - marginLeft - marginRight,
state.tableWidth,
table.align,
- table.indent
+ table.indent,
+ shouldAllowImportedTableMarginOverflow(state.sectionBreakConfig.documentCompatibilityPolicy ?? getDocumentCompatibilityPolicy(), table)
);
state.complete = true;
}
@@ -679,7 +684,7 @@ export function cachePrecomputedSlicedTableSkeletons(
}
function _finishTableSkeletonsBuild(state: ISlicedTableSkeletonBuildState): void {
- updateTableSkeletonsPosition(state.createCache, state.curPage, state.skeTables, state.table);
+ updateTableSkeletonsPosition(state.createCache, state.curPage, state.skeTables, state.table, state.sectionBreakConfig);
state.skeTables.forEach((table) => applyTableOuterBorderSpacing(table, state.sectionBreakConfig));
const policy =
state.sectionBreakConfig.documentCompatibilityPolicy ?? getDocumentCompatibilityPolicy();
@@ -783,7 +788,7 @@ export function createTableSkeletons(
} while (cellPageHeights);
}
- updateTableSkeletonsPosition(createCache, curPage, skeTables, table);
+ updateTableSkeletonsPosition(createCache, curPage, skeTables, table, sectionBreakConfig);
skeTables.forEach((table) => applyTableOuterBorderSpacing(table, sectionBreakConfig));
const documentCompatibilityPolicy = sectionBreakConfig.documentCompatibilityPolicy ?? getDocumentCompatibilityPolicy();
@@ -801,11 +806,18 @@ function updateTableSkeletonsPosition(
cache: ICreateTableCache,
curPage: IDocumentSkeletonPage,
skeTables: IDocumentSkeletonTable[],
- table: ITable
+ table: ITable,
+ sectionBreakConfig: ISectionBreakConfig
) {
const { pageWidth, marginLeft = 0, marginRight = 0 } = curPage;
const { tableWidth } = cache;
- const tableLeft = getTableLeft(pageWidth - marginLeft - marginRight, tableWidth, table.align, table.indent);
+ const tableLeft = getTableLeft(
+ pageWidth - marginLeft - marginRight,
+ tableWidth,
+ table.align,
+ table.indent,
+ shouldAllowImportedTableMarginOverflow(sectionBreakConfig.documentCompatibilityPolicy ?? getDocumentCompatibilityPolicy(), table)
+ );
const rowFragments = new Map<number, number>();
for (const part of skeTables) {
for (const row of part.rows) {
@@ -1455,16 +1467,22 @@ export function resolveTableLayoutColumns(table: ITable, availableWidth: number,
};
}
-export function getTableLeft(pageWidth: number, tableWidth: number, align: TableAlignmentType, indent: INumberUnit = { v: 0 }) {
+export function getTableLeft(
+ pageWidth: number,
+ tableWidth: number,
+ align: TableAlignmentType,
+ indent: INumberUnit = { v: 0 },
+ allowMarginOverflow = false
+) {
switch (align) {
case TableAlignmentType.START: {
return indent.v;
}
case TableAlignmentType.END: {
- return Math.max(0, pageWidth - tableWidth);
+ return allowMarginOverflow ? pageWidth - tableWidth : Math.max(0, pageWidth - tableWidth);
}
case TableAlignmentType.CENTER: {
- return Math.max(0, (pageWidth - tableWidth) / 2);
+ return allowMarginOverflow ? (pageWidth - tableWidth) / 2 : Math.max(0, (pageWidth - tableWidth) / 2);
}
default: {
throw new Error('Unknown table alignment type');