Skip to content

Commit edbe218

Browse files
fix(painter): rebuild drawing page fields on context changes
1 parent 56ee5cd commit edbe218

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

packages/layout-engine/painters/dom/src/index.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6472,6 +6472,62 @@ describe('DomPainter', () => {
64726472
expect(svgEl?.style.transform).toBe('');
64736473
});
64746474

6475+
it('rebuilds drawing text with PAGE fields when page context changes during patch rendering', () => {
6476+
const vectorShapeBlock: FlowBlock = {
6477+
kind: 'drawing',
6478+
id: 'drawing-page-field',
6479+
drawingKind: 'vectorShape',
6480+
geometry: { width: 100, height: 50, rotation: 0, flipH: false, flipV: false },
6481+
shapeKind: 'rect',
6482+
textContent: {
6483+
parts: [
6484+
{ text: 'Page ', formatting: { fontFamily: 'Arial', fontSize: 18 } },
6485+
{ text: '', fieldType: 'PAGE', formatting: { fontFamily: 'Arial', fontSize: 18 } },
6486+
],
6487+
},
6488+
textAlign: 'center',
6489+
};
6490+
6491+
const vectorShapeMeasure: Measure = {
6492+
kind: 'drawing',
6493+
drawingKind: 'vectorShape',
6494+
width: 100,
6495+
height: 50,
6496+
scale: 1,
6497+
naturalWidth: 100,
6498+
naturalHeight: 50,
6499+
geometry: { width: 100, height: 50, rotation: 0, flipH: false, flipV: false },
6500+
};
6501+
6502+
const drawingFragment = {
6503+
kind: 'drawing' as const,
6504+
drawingKind: 'vectorShape' as const,
6505+
blockId: 'drawing-page-field',
6506+
x: 30,
6507+
y: 40,
6508+
width: 100,
6509+
height: 50,
6510+
geometry: { width: 100, height: 50, rotation: 0, flipH: false, flipV: false },
6511+
scale: 1,
6512+
};
6513+
6514+
const painter = createTestPainter({ blocks: [vectorShapeBlock], measures: [vectorShapeMeasure] });
6515+
const firstLayout: Layout = {
6516+
pageSize: layout.pageSize,
6517+
pages: [{ number: 1, numberText: '1', fragments: [drawingFragment] }],
6518+
};
6519+
const secondLayout: Layout = {
6520+
pageSize: layout.pageSize,
6521+
pages: [{ number: 2, numberText: '2', fragments: [drawingFragment] }],
6522+
};
6523+
6524+
painter.paint(firstLayout, mount);
6525+
expect(mount.querySelector('.superdoc-vector-shape')?.textContent).toContain('Page 1');
6526+
6527+
painter.paint(secondLayout, mount);
6528+
expect(mount.querySelector('.superdoc-vector-shape')?.textContent).toContain('Page 2');
6529+
});
6530+
64756531
describe('resolved paragraph rendering', () => {
64766532
it('renders resolved paragraph lines with precomputed indent styles', () => {
64776533
const paragraphBlock: FlowBlock = {

packages/layout-engine/painters/dom/src/renderer.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ function pageContextSignature(context: FragmentRenderContext): string {
268268
].join('|');
269269
}
270270

271+
function hasPageContextTokenInShapeText(textContent: ShapeTextContent | undefined): boolean {
272+
return (
273+
Array.isArray(textContent?.parts) &&
274+
textContent.parts.some((part) => part.fieldType === 'PAGE' || part.fieldType === 'NUMPAGES')
275+
);
276+
}
277+
278+
function hasPageContextTokenInShapeGroup(shapes: readonly ShapeGroupChild[] | undefined): boolean {
279+
return (
280+
Array.isArray(shapes) &&
281+
shapes.some((shape) => {
282+
if (shape.shapeType !== 'vectorShape') {
283+
return false;
284+
}
285+
return hasPageContextTokenInShapeText(shape.attrs.textContent);
286+
})
287+
);
288+
}
289+
271290
function hasPageContextTokenInBlock(block: FlowBlock | undefined): boolean {
272291
if (!block) return false;
273292
if (block.kind === 'paragraph') {
@@ -297,6 +316,14 @@ function hasPageContextTokenInBlock(block: FlowBlock | undefined): boolean {
297316
}
298317
}
299318
}
319+
} else if (block.kind === 'drawing') {
320+
const drawing = block as DrawingBlock;
321+
if (drawing.drawingKind === 'vectorShape') {
322+
return hasPageContextTokenInShapeText(drawing.textContent);
323+
}
324+
if (drawing.drawingKind === 'shapeGroup') {
325+
return hasPageContextTokenInShapeGroup(drawing.shapes);
326+
}
300327
}
301328
return false;
302329
}

0 commit comments

Comments
 (0)