Skip to content

Commit 97ef352

Browse files
fix(painter): rebuild drawing page fields on context changes
1 parent 0802f9e commit 97ef352

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
@@ -6416,6 +6416,62 @@ describe('DomPainter', () => {
64166416
expect(svgEl?.style.transform).toBe('');
64176417
});
64186418

6419+
it('rebuilds drawing text with PAGE fields when page context changes during patch rendering', () => {
6420+
const vectorShapeBlock: FlowBlock = {
6421+
kind: 'drawing',
6422+
id: 'drawing-page-field',
6423+
drawingKind: 'vectorShape',
6424+
geometry: { width: 100, height: 50, rotation: 0, flipH: false, flipV: false },
6425+
shapeKind: 'rect',
6426+
textContent: {
6427+
parts: [
6428+
{ text: 'Page ', formatting: { fontFamily: 'Arial', fontSize: 18 } },
6429+
{ text: '', fieldType: 'PAGE', formatting: { fontFamily: 'Arial', fontSize: 18 } },
6430+
],
6431+
},
6432+
textAlign: 'center',
6433+
};
6434+
6435+
const vectorShapeMeasure: Measure = {
6436+
kind: 'drawing',
6437+
drawingKind: 'vectorShape',
6438+
width: 100,
6439+
height: 50,
6440+
scale: 1,
6441+
naturalWidth: 100,
6442+
naturalHeight: 50,
6443+
geometry: { width: 100, height: 50, rotation: 0, flipH: false, flipV: false },
6444+
};
6445+
6446+
const drawingFragment = {
6447+
kind: 'drawing' as const,
6448+
drawingKind: 'vectorShape' as const,
6449+
blockId: 'drawing-page-field',
6450+
x: 30,
6451+
y: 40,
6452+
width: 100,
6453+
height: 50,
6454+
geometry: { width: 100, height: 50, rotation: 0, flipH: false, flipV: false },
6455+
scale: 1,
6456+
};
6457+
6458+
const painter = createTestPainter({ blocks: [vectorShapeBlock], measures: [vectorShapeMeasure] });
6459+
const firstLayout: Layout = {
6460+
pageSize: layout.pageSize,
6461+
pages: [{ number: 1, numberText: '1', fragments: [drawingFragment] }],
6462+
};
6463+
const secondLayout: Layout = {
6464+
pageSize: layout.pageSize,
6465+
pages: [{ number: 2, numberText: '2', fragments: [drawingFragment] }],
6466+
};
6467+
6468+
painter.paint(firstLayout, mount);
6469+
expect(mount.querySelector('.superdoc-vector-shape')?.textContent).toContain('Page 1');
6470+
6471+
painter.paint(secondLayout, mount);
6472+
expect(mount.querySelector('.superdoc-vector-shape')?.textContent).toContain('Page 2');
6473+
});
6474+
64196475
describe('resolved paragraph rendering', () => {
64206476
it('renders resolved paragraph lines with precomputed indent styles', () => {
64216477
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)