-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathversionSignature.ts
More file actions
682 lines (622 loc) · 25 KB
/
versionSignature.ts
File metadata and controls
682 lines (622 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
import {
buildLayoutSourceIdentityForFragment,
getParagraphInlineDirection,
type DrawingBlock,
type FieldAnnotationRun,
type FlowBlock,
type Fragment,
type ImageBlock,
type ImageDrawing,
type ImageRun,
type LayoutSourceIdentity,
type LayoutStoryLocator,
type ParagraphAttrs,
type ParagraphBlock,
type SdtMetadata,
type ShapeGroupDrawing,
type SourceAnchor,
type TableAttrs,
type TableBlock,
type TableCellAttrs,
type TrackedChangeMeta,
type TextRun,
type VectorShapeDrawing,
} from '@superdoc/contracts';
import { getFontConfigVersion } from '@superdoc/font-system';
import { hashParagraphBorders } from './paragraphBorderHash.js';
import {
hashCellBorders,
hashTableBorders,
getRunBooleanProp,
getRunNumberProp,
getRunStringProp,
getRunUnderlineColor,
getRunUnderlineStyle,
} from './hashUtils.js';
// ---------------------------------------------------------------------------
// SDT metadata helpers
// ---------------------------------------------------------------------------
const getSdtMetadataId = (metadata: SdtMetadata | null | undefined): string => {
if (!metadata) return '';
if ('id' in metadata && metadata.id != null) {
return String(metadata.id);
}
return '';
};
const getSdtMetadataLockMode = (metadata: SdtMetadata | null | undefined): string => {
if (!metadata) return '';
return metadata.type === 'structuredContent' ? (metadata.lockMode ?? '') : '';
};
const getSdtMetadataVersion = (metadata: SdtMetadata | null | undefined): string => {
if (!metadata) return '';
return [metadata.type, getSdtMetadataLockMode(metadata), getSdtMetadataId(metadata)].join(':');
};
const getTrackedChangeLayers = (run: TextRun): TrackedChangeMeta[] => {
if (Array.isArray(run.trackedChanges) && run.trackedChanges.length > 0) {
return run.trackedChanges;
}
return run.trackedChange ? [run.trackedChange] : [];
};
const trackedChangeVersion = (run: TextRun): string =>
getTrackedChangeLayers(run)
.map((trackedChange) =>
[
trackedChange.kind ?? '',
trackedChange.id ?? '',
trackedChange.storyKey ?? '',
trackedChange.overlapParentId ?? '',
trackedChange.relationship ?? '',
trackedChange.author ?? '',
trackedChange.authorEmail ?? '',
trackedChange.authorImage ?? '',
trackedChange.date ?? '',
trackedChange.before ? JSON.stringify(trackedChange.before) : '',
trackedChange.after ? JSON.stringify(trackedChange.after) : '',
].join(':'),
)
.join('|');
// ---------------------------------------------------------------------------
// Clip path helpers
// ---------------------------------------------------------------------------
const CLIP_PATH_PREFIXES = ['inset(', 'polygon(', 'circle(', 'ellipse(', 'path(', 'rect('];
const readClipPathValue = (value: unknown): string => {
if (typeof value !== 'string') return '';
const normalized = value.trim();
if (normalized.length === 0) return '';
const lower = normalized.toLowerCase();
if (!CLIP_PATH_PREFIXES.some((prefix) => lower.startsWith(prefix))) return '';
return normalized;
};
const resolveClipPathFromAttrs = (attrs: unknown): string => {
if (!attrs || typeof attrs !== 'object') return '';
const record = attrs as Record<string, unknown>;
return readClipPathValue(record.clipPath);
};
const resolveBlockClipPath = (block: unknown): string => {
if (!block || typeof block !== 'object') return '';
const record = block as Record<string, unknown>;
return readClipPathValue(record.clipPath) || resolveClipPathFromAttrs(record.attrs);
};
const imageHyperlinkVersion = (hyperlink: ImageBlock['hyperlink'] | undefined): string => {
if (!hyperlink) return '';
return JSON.stringify([hyperlink.url ?? '', hyperlink.tooltip ?? '']);
};
const imageLuminanceVersion = (lum: ImageBlock['lum'] | undefined): string => {
if (!lum) return '';
return [lum.bright ?? '', lum.contrast ?? ''].join(':');
};
const renderedBlockImageVersion = (image: ImageBlock | ImageDrawing): string =>
[
image.src ?? '',
image.width ?? '',
image.height ?? '',
image.alt ?? '',
image.title ?? '',
image.objectFit ?? '',
image.display ?? '',
image.gain ?? '',
image.blacklevel ?? '',
image.grayscale ? 1 : 0,
imageLuminanceVersion(image.lum),
image.rotation ?? '',
image.flipH ? 1 : 0,
image.flipV ? 1 : 0,
imageHyperlinkVersion(image.hyperlink),
resolveBlockClipPath(image),
].join('|');
const renderedInlineImageRunVersion = (image: ImageRun): string =>
[
'img',
image.src ?? '',
image.width ?? '',
image.height ?? '',
image.alt ?? '',
image.title ?? '',
typeof image.clipPath === 'string' ? image.clipPath.trim() : '',
image.distTop ?? '',
image.distBottom ?? '',
image.distLeft ?? '',
image.distRight ?? '',
image.verticalAlign ?? '',
image.gain ?? '',
image.blacklevel ?? '',
image.grayscale ? 1 : 0,
imageLuminanceVersion(image.lum),
image.rotation ?? '',
image.flipH ? 1 : 0,
image.flipV ? 1 : 0,
imageHyperlinkVersion(image.hyperlink),
stableSerializeEvidenceValue(image.sdt),
stableSerializeEvidenceValue(image.dataAttrs),
].join('|');
// ---------------------------------------------------------------------------
// List marker validation
// ---------------------------------------------------------------------------
const hasListMarkerProperties = (
attrs: unknown,
): attrs is {
numberingProperties: { numId?: number | string; ilvl?: number };
wordLayout?: { marker?: { markerText?: string } };
} => {
if (!attrs || typeof attrs !== 'object') return false;
const obj = attrs as Record<string, unknown>;
if (!obj.numberingProperties || typeof obj.numberingProperties !== 'object') return false;
const numProps = obj.numberingProperties as Record<string, unknown>;
if ('numId' in numProps) {
const numId = numProps.numId;
if (typeof numId !== 'number' && typeof numId !== 'string') return false;
}
if ('ilvl' in numProps) {
const ilvl = numProps.ilvl;
if (typeof ilvl !== 'number') return false;
}
if ('wordLayout' in obj && obj.wordLayout !== undefined) {
if (typeof obj.wordLayout !== 'object' || obj.wordLayout === null) return false;
const wordLayout = obj.wordLayout as Record<string, unknown>;
if ('marker' in wordLayout && wordLayout.marker !== undefined) {
if (typeof wordLayout.marker !== 'object' || wordLayout.marker === null) return false;
const marker = wordLayout.marker as Record<string, unknown>;
if ('markerText' in marker && marker.markerText !== undefined) {
if (typeof marker.markerText !== 'string') return false;
}
}
}
return true;
};
// ---------------------------------------------------------------------------
// FNV-1a hash helpers (for table block hashing)
// ---------------------------------------------------------------------------
const hashString = (seed: number, value: string): number => {
let hash = seed >>> 0;
for (let i = 0; i < value.length; i++) {
hash ^= value.charCodeAt(i);
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
};
const hashNumber = (seed: number, value: number | undefined | null): number => {
const n = Number.isFinite(value) ? (value as number) : 0;
let hash = seed ^ n;
hash = Math.imul(hash, 16777619);
hash ^= hash >>> 13;
return hash >>> 0;
};
// ---------------------------------------------------------------------------
// sourceAnchorSignature
// ---------------------------------------------------------------------------
const stableSerializeEvidenceValue = (value: unknown): string => {
if (value === undefined) return '';
if (value === null) return 'null';
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
return JSON.stringify(value);
}
if (Array.isArray(value)) {
return `[${value.map((item) => stableSerializeEvidenceValue(item)).join(',')}]`;
}
if (typeof value === 'object') {
const record = value as Record<string, unknown>;
return `{${Object.keys(record)
.sort()
.filter((key) => record[key] !== undefined)
.map((key) => `${JSON.stringify(key)}:${stableSerializeEvidenceValue(record[key])}`)
.join(',')}}`;
}
return JSON.stringify(String(value));
};
/**
* Stable source/evidence metadata signature for paint cache invalidation.
*
* Source anchors are not visual geometry. Keep them out of deriveBlockVersion()
* and fragmentSignature(), but include this fingerprint in DomPainter's paint
* reuse signature so metadata-only updates refresh data-source-* attributes and
* paint snapshot anchors.
*/
export const sourceAnchorSignature = (sourceAnchor: SourceAnchor | undefined): string =>
sourceAnchor ? stableSerializeEvidenceValue(sourceAnchor) : '';
/**
* Resolve the editor-neutral identity for a fragment (prep-001).
*
* Prefers `fragment.layoutSourceIdentity` when present; otherwise constructs
* one from the producer's existing fields (`blockId`, `kind`, fragment-local
* line/row indices, optional `sourceAnchor`). Pure helper — does not mutate
* the fragment, and remains safe to call for v1 layouts that never populate
* `layoutSourceIdentity` upstream.
*/
export const resolveFragmentLayoutIdentity = (fragment: Fragment, story?: LayoutStoryLocator): LayoutSourceIdentity => {
return buildLayoutSourceIdentityForFragment(fragment, story);
};
// ---------------------------------------------------------------------------
// deriveBlockVersion
// ---------------------------------------------------------------------------
/**
* Derives a version string for a flow block based on its content and styling properties.
*
* This version string is used for cache invalidation. When any visual property of the block
* changes, the version string changes, triggering a DOM rebuild instead of reusing cached elements.
*
* Kept in layout-resolved so the resolved layout stage can pre-compute block
* versions without depending on painter-dom.
*/
export const deriveBlockVersion = (block: FlowBlock): string => {
if (block.kind === 'paragraph') {
const markerVersion = hasListMarkerProperties(block.attrs)
? `marker:${block.attrs.numberingProperties.numId ?? ''}:${block.attrs.numberingProperties.ilvl ?? 0}:${block.attrs.wordLayout?.marker?.markerText ?? ''}`
: '';
const runsVersion = block.runs
.map((run) => {
if (run.kind === 'image') {
return renderedInlineImageRunVersion(run as ImageRun);
}
if (run.kind === 'lineBreak') {
return 'linebreak';
}
if (run.kind === 'tab') {
// Include every input the painter's tab underline depends on so the paint cache is
// not reused after a relevant change (SD-3330): underline style/color choose the
// mark; fontSize sets its thickness; fontFamily/color feed measured line metrics and
// the resolved underline color. The font epoch matters too: a tab's underline offset
// is derived from measured line metrics, so when a font loads/changes (resolved family
// unchanged, only availability) a tab-only underlined line must repaint - a mixed
// text+tab line is already busted by its text run, but a tab-only line has none.
// bold/italic matter for the same reason: a tab-only line's metrics now come from the
// tab's font via getFontInfoFromRun, which feeds bold/italic into the measured ascent/
// descent (buildFontString), so the underline offset and line height depend on them.
// Without these a font/style/availability change can leave a stale tab underline until an
// unrelated edit forces a rebuild.
return [
run.text ?? '',
'tab',
run.underline?.style ?? '',
run.underline?.color ?? '',
run.fontSize ?? '',
run.fontFamily ?? '',
(run as { bold?: boolean }).bold ? 1 : 0,
(run as { italic?: boolean }).italic ? 1 : 0,
getFontConfigVersion(),
(run as { color?: string }).color ?? '',
].join(',');
}
if (run.kind === 'fieldAnnotation') {
const fieldRun = run as FieldAnnotationRun;
const size = fieldRun.size ? `${fieldRun.size.width ?? ''}x${fieldRun.size.height ?? ''}` : '';
const highlighted = fieldRun.highlighted !== false ? 1 : 0;
return [
'field',
fieldRun.variant ?? '',
fieldRun.displayLabel ?? '',
fieldRun.fieldColor ?? '',
fieldRun.borderColor ?? '',
highlighted,
fieldRun.hidden ? 1 : 0,
fieldRun.visibility ?? '',
fieldRun.imageSrc ?? '',
fieldRun.linkUrl ?? '',
fieldRun.rawHtml ?? '',
size,
fieldRun.fontFamily ?? '',
fieldRun.fontSize ?? '',
fieldRun.textColor ?? '',
fieldRun.textHighlight ?? '',
fieldRun.bold ? 1 : 0,
fieldRun.italic ? 1 : 0,
fieldRun.underline ? 1 : 0,
fieldRun.fieldId ?? '',
fieldRun.fieldType ?? '',
].join(',');
}
const textRun = run as TextRun;
const trackedVersion = trackedChangeVersion(textRun);
return [
textRun.text ?? '',
textRun.fontFamily,
// Font epoch: busts paint reuse when a font loads/changes (the resolved physical
// family is the same, only its availability changed - logical family alone can't see it).
getFontConfigVersion(),
textRun.fontSize,
textRun.bold ? 1 : 0,
textRun.italic ? 1 : 0,
textRun.color ?? '',
textRun.underline?.style ?? '',
textRun.underline?.color ?? '',
textRun.strike ? 1 : 0,
textRun.highlight ?? '',
textRun.letterSpacing != null ? textRun.letterSpacing : '',
textRun.vertAlign ?? '',
textRun.baselineShift != null ? textRun.baselineShift : '',
textRun.token ?? '',
trackedVersion,
textRun.comments?.length ?? 0,
// SD-3098: DomPainter reads run.bidi to apply dir + RLM injection; signature must include it.
textRun.bidi ? JSON.stringify(textRun.bidi) : '',
].join(',');
})
.join('|');
const attrs = block.attrs as ParagraphAttrs | undefined;
const paragraphAttrsVersion = attrs
? [
attrs.alignment ?? '',
attrs.spacing?.before ?? '',
attrs.spacing?.after ?? '',
attrs.spacing?.line ?? '',
attrs.spacing?.lineRule ?? '',
attrs.indent?.left ?? '',
attrs.indent?.right ?? '',
attrs.indent?.firstLine ?? '',
attrs.indent?.hanging ?? '',
attrs.borders ? hashParagraphBorders(attrs.borders) : '',
attrs.shading?.fill ?? '',
attrs.shading?.color ?? '',
getParagraphInlineDirection(attrs) ?? '',
attrs.tabs?.length ? JSON.stringify(attrs.tabs) : '',
].join(':')
: '';
const sdtAttrs = (block.attrs as ParagraphAttrs | undefined)?.sdt;
const sdtVersion = getSdtMetadataVersion(sdtAttrs);
const parts = [markerVersion, runsVersion, paragraphAttrsVersion, sdtVersion].filter(Boolean);
return parts.join('|');
}
if (block.kind === 'list') {
return block.items.map((item) => `${item.id}:${item.marker.text}:${deriveBlockVersion(item.paragraph)}`).join('|');
}
if (block.kind === 'image') {
const imgSdt = (block as ImageBlock).attrs?.sdt;
const imgSdtVersion = getSdtMetadataVersion(imgSdt);
return [renderedBlockImageVersion(block), imgSdtVersion].join('|');
}
if (block.kind === 'drawing') {
if (block.drawingKind === 'image') {
const imageLike = block as ImageDrawing;
return ['drawing:image', renderedBlockImageVersion(imageLike)].join('|');
}
if (block.drawingKind === 'vectorShape') {
const vector = block as VectorShapeDrawing;
return [
'drawing:vector',
vector.shapeKind ?? '',
vector.fillColor ?? '',
vector.strokeColor ?? '',
vector.strokeWidth ?? '',
vector.geometry.width,
vector.geometry.height,
vector.geometry.rotation ?? 0,
vector.geometry.flipH ? 1 : 0,
vector.geometry.flipV ? 1 : 0,
].join('|');
}
if (block.drawingKind === 'shapeGroup') {
const group = block as ShapeGroupDrawing;
const childSignature = group.shapes
.map((child) => `${child.shapeType}:${JSON.stringify(child.attrs ?? {})}`)
.join(';');
return [
'drawing:group',
group.geometry.width,
group.geometry.height,
group.groupTransform ? JSON.stringify(group.groupTransform) : '',
childSignature,
].join('|');
}
if (block.drawingKind === 'chart') {
return [
'drawing:chart',
block.chartData?.chartType ?? '',
block.chartData?.series?.length ?? 0,
block.geometry.width,
block.geometry.height,
block.chartRelId ?? '',
].join('|');
}
const _exhaustive: never = block;
return `drawing:unknown:${(block as DrawingBlock).id}`;
}
if (block.kind === 'table') {
const tableBlock = block as TableBlock;
let hash = 2166136261;
hash = hashString(hash, block.id);
hash = hashNumber(hash, tableBlock.rows.length);
hash = (tableBlock.columnWidths ?? []).reduce((acc, width) => hashNumber(acc, Math.round(width * 1000)), hash);
const rows = tableBlock.rows ?? [];
for (const row of rows) {
if (!row || !Array.isArray(row.cells)) continue;
hash = hashNumber(hash, row.cells.length);
for (const cell of row.cells) {
if (!cell) continue;
const cellBlocks = cell.blocks ?? (cell.paragraph ? [cell.paragraph] : []);
hash = hashNumber(hash, cellBlocks.length);
hash = hashNumber(hash, cell.rowSpan ?? 1);
hash = hashNumber(hash, cell.colSpan ?? 1);
if (cell.attrs) {
const cellAttrs = cell.attrs as TableCellAttrs;
if (cellAttrs.borders) {
hash = hashString(hash, hashCellBorders(cellAttrs.borders));
}
if (cellAttrs.padding) {
const p = cellAttrs.padding;
hash = hashNumber(hash, p.top ?? 0);
hash = hashNumber(hash, p.right ?? 0);
hash = hashNumber(hash, p.bottom ?? 0);
hash = hashNumber(hash, p.left ?? 0);
}
if (cellAttrs.verticalAlign) {
hash = hashString(hash, cellAttrs.verticalAlign);
}
if (cellAttrs.background) {
hash = hashString(hash, cellAttrs.background);
}
}
for (const cellBlock of cellBlocks) {
hash = hashString(hash, cellBlock?.kind ?? 'unknown');
if (cellBlock?.kind === 'paragraph') {
const paragraphBlock = cellBlock as ParagraphBlock;
const runs = paragraphBlock.runs ?? [];
hash = hashNumber(hash, runs.length);
const attrs = paragraphBlock.attrs as ParagraphAttrs | undefined;
if (attrs) {
hash = hashString(hash, attrs.alignment ?? '');
hash = hashNumber(hash, attrs.spacing?.before ?? 0);
hash = hashNumber(hash, attrs.spacing?.after ?? 0);
hash = hashNumber(hash, attrs.spacing?.line ?? 0);
hash = hashString(hash, attrs.spacing?.lineRule ?? '');
hash = hashNumber(hash, attrs.indent?.left ?? 0);
hash = hashNumber(hash, attrs.indent?.right ?? 0);
hash = hashNumber(hash, attrs.indent?.firstLine ?? 0);
hash = hashNumber(hash, attrs.indent?.hanging ?? 0);
hash = hashString(hash, attrs.shading?.fill ?? '');
hash = hashString(hash, attrs.shading?.color ?? '');
hash = hashString(hash, getParagraphInlineDirection(attrs) ?? '');
if (attrs.borders) {
hash = hashString(hash, hashParagraphBorders(attrs.borders));
}
}
for (const run of runs) {
if (run.kind === 'image') {
hash = hashString(hash, renderedInlineImageRunVersion(run as ImageRun));
hash = hashNumber(hash, run.pmStart ?? -1);
hash = hashNumber(hash, run.pmEnd ?? -1);
continue;
}
if ('text' in run && typeof run.text === 'string') {
hash = hashString(hash, run.text);
}
hash = hashNumber(hash, run.pmStart ?? -1);
hash = hashNumber(hash, run.pmEnd ?? -1);
hash = hashString(hash, getRunStringProp(run, 'color'));
hash = hashString(hash, getRunStringProp(run, 'highlight'));
hash = hashString(hash, getRunBooleanProp(run, 'bold') ? '1' : '');
hash = hashString(hash, getRunBooleanProp(run, 'italic') ? '1' : '');
hash = hashNumber(hash, getRunNumberProp(run, 'fontSize'));
hash = hashString(hash, getRunStringProp(run, 'fontFamily'));
hash = hashString(hash, getRunUnderlineStyle(run));
hash = hashString(hash, getRunUnderlineColor(run));
hash = hashString(hash, getRunBooleanProp(run, 'strike') ? '1' : '');
hash = hashString(hash, getRunStringProp(run, 'vertAlign'));
hash = hashNumber(hash, getRunNumberProp(run, 'baselineShift'));
// SD-3098: include run.bidi so rtl-only changes invalidate the cached block hash.
const bidi = (run as { bidi?: unknown }).bidi;
hash = hashString(hash, bidi ? JSON.stringify(bidi) : '');
hash = hashString(hash, trackedChangeVersion(run as TextRun));
}
} else if (cellBlock?.kind) {
hash = hashString(hash, deriveBlockVersion(cellBlock as FlowBlock));
}
}
}
}
if (tableBlock.attrs) {
const tblAttrs = tableBlock.attrs as TableAttrs;
if (tblAttrs.borders) {
hash = hashString(hash, hashTableBorders(tblAttrs.borders));
}
if (tblAttrs.borderCollapse) {
hash = hashString(hash, tblAttrs.borderCollapse);
}
if (tblAttrs.cellSpacing !== undefined) {
const cs = tblAttrs.cellSpacing;
if (typeof cs === 'number') {
hash = hashNumber(hash, cs);
} else {
const v = (cs as { value?: number; type?: string }).value ?? 0;
const t = (cs as { value?: number; type?: string }).type ?? 'px';
hash = hashString(hash, `cs:${v}:${t}`);
}
}
if (tblAttrs.sdt) {
hash = hashString(hash, tblAttrs.sdt.type);
hash = hashString(hash, getSdtMetadataLockMode(tblAttrs.sdt));
hash = hashString(hash, getSdtMetadataId(tblAttrs.sdt));
}
}
return [block.id, tableBlock.rows.length, hash.toString(16)].join('|');
}
return block.id;
};
// ---------------------------------------------------------------------------
// fragmentSignature
// ---------------------------------------------------------------------------
/**
* Computes a change-detection signature for a layout fragment.
*
* Combines the block-level version with fragment-specific data (line range,
* continuation flags, marker width, drawing geometry, table row range, etc.)
* so that each fragment has a unique identity for incremental re-rendering.
*
* Adapted from painters/dom/src/renderer.ts fragmentSignature(). The painter
* version accepts a BlockLookup map; this version takes a pre-computed
* blockVersion string directly.
*/
export const fragmentSignature = (fragment: Fragment, blockVersion: string): string => {
if (fragment.kind === 'para') {
return [
blockVersion,
fragment.fromLine,
fragment.toLine,
fragment.continuesFromPrev ? 1 : 0,
fragment.continuesOnNext ? 1 : 0,
fragment.markerWidth ?? '',
].join('|');
}
if (fragment.kind === 'list-item') {
return [
blockVersion,
fragment.itemId,
fragment.fromLine,
fragment.toLine,
fragment.continuesFromPrev ? 1 : 0,
fragment.continuesOnNext ? 1 : 0,
].join('|');
}
if (fragment.kind === 'image') {
return [blockVersion, fragment.width, fragment.height].join('|');
}
if (fragment.kind === 'drawing') {
return [
blockVersion,
fragment.drawingKind,
fragment.drawingContentId ?? '',
fragment.width,
fragment.height,
fragment.geometry.width,
fragment.geometry.height,
fragment.geometry.rotation ?? 0,
fragment.scale ?? 1,
fragment.zIndex ?? '',
].join('|');
}
if (fragment.kind === 'table') {
const partialSig = fragment.partialRow
? `${fragment.partialRow.fromLineByCell.join(',')}-${fragment.partialRow.toLineByCell.join(',')}-${fragment.partialRow.partialHeight}`
: '';
return [
blockVersion,
fragment.fromRow,
fragment.toRow,
fragment.width,
fragment.height,
fragment.continuesFromPrev ? 1 : 0,
fragment.continuesOnNext ? 1 : 0,
fragment.repeatHeaderCount ?? 0,
partialSig,
].join('|');
}
return blockVersion;
};