-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathindex.ts
More file actions
415 lines (377 loc) · 13.3 KB
/
index.ts
File metadata and controls
415 lines (377 loc) · 13.3 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
/**
* @superdoc/style-engine
*
* Resolves OOXML styles to normalized ComputedStyle objects that engines can consume.
* This module owns the cascade rules (defaults -> styles -> numbering -> direct formatting).
*
* Tab Stops:
* - Passes through OOXML TabStop values unchanged (positions in twips, val: start/end/etc.)
* - No unit conversion happens here - preserves exact OOXML values for round-trip fidelity
* - Conversion to pixels happens at measurement boundary only
*/
// Re-export cascade utilities - these are the SINGLE SOURCE OF TRUTH for property merging
export { combineProperties, combineRunProperties, combineIndentProperties, type PropertyObject } from './cascade.js';
import type {
TabStop,
FieldAnnotationMetadata,
StructuredContentMetadata,
DocumentSectionMetadata,
DocPartMetadata,
SdtMetadata,
} from '@superdoc/contracts';
export type {
FieldAnnotationMetadata,
StructuredContentMetadata,
DocumentSectionMetadata,
DocPartMetadata,
SdtMetadata,
};
export type SdtNodeType =
| 'fieldAnnotation'
| 'structuredContent'
| 'structuredContentBlock'
| 'documentSection'
| 'docPartObject';
export interface ResolveSdtMetadataInput {
nodeType?: SdtNodeType | string | null;
attrs?: Record<string, unknown> | null;
/**
* Optional cache key for reusing normalized metadata between identical SDT nodes.
* When omitted, the helper derives a key from attrs.hash/id when available.
*/
cacheKey?: string | null;
}
export interface ResolveStyleOptions {
sdt?: ResolveSdtMetadataInput | null;
}
// ---------------------------------------------------------------------------
// Public interfaces
// ---------------------------------------------------------------------------
export interface BorderStyle {
style?: 'none' | 'solid' | 'dashed' | 'dotted' | 'double';
width?: number;
color?: string;
}
export interface ComputedParagraphStyle {
alignment?: 'left' | 'center' | 'right' | 'justify';
spacing?: {
before?: number;
after?: number;
line?: number;
lineRule?: 'auto' | 'exact' | 'atLeast';
};
indent?: {
left?: number;
right?: number;
firstLine?: number;
hanging?: number;
};
borders?: {
top?: BorderStyle;
right?: BorderStyle;
bottom?: BorderStyle;
left?: BorderStyle;
};
shading?: {
fill?: string;
pattern?: string;
};
tabs?: TabStop[];
}
export interface StyleContext {
styles?: Record<string, unknown>;
numbering?: Record<string, unknown>;
theme?: Record<string, unknown>;
defaults?: {
paragraphFont?: string;
fontSize?: number;
paragraphFontFallback?: string;
paragraphFontFamily?: string;
decimalSeparator?: string;
defaultTabIntervalTwips?: number;
};
}
// ---------------------------------------------------------------------------
// Style resolution
// ---------------------------------------------------------------------------
const sdtMetadataCache = new Map<string, SdtMetadata>();
/**
* Clears the internal SDT metadata cache.
*
* This is primarily useful for testing to ensure a clean state between test runs.
* In production, the cache persists for the lifetime of the module to maximize performance.
*
* @example
* ```typescript
* import { clearSdtMetadataCache } from '@superdoc/style-engine';
*
* // Before each test
* beforeEach(() => {
* clearSdtMetadataCache();
* });
* ```
*/
export function clearSdtMetadataCache(): void {
sdtMetadataCache.clear();
}
/**
* Normalizes Structured Document Tag (SDT) metadata into a stable contract shape.
*
* Supports the following SDT node types:
* - `fieldAnnotation`: Inline field annotations with display labels, colors, and visibility
* - `structuredContent` / `structuredContentBlock`: Inline or block-level structured content containers
* - `documentSection`: Document section metadata with locks and descriptions
* - `docPartObject`: Document part objects (e.g., TOC, bibliography)
*
* Results are cached by hash/id to avoid recomputing metadata for identical SDT instances.
*
* @param input - SDT node information including nodeType, attrs, and optional cacheKey
* @returns Normalized SdtMetadata or undefined if nodeType is unsupported/missing
*
* @example
* ```typescript
* import { resolveSdtMetadata } from '@superdoc/style-engine';
*
* const metadata = resolveSdtMetadata({
* nodeType: 'fieldAnnotation',
* attrs: {
* fieldId: 'CLIENT_NAME',
* displayLabel: 'Client Name',
* fieldColor: '#980043',
* visibility: 'visible'
* }
* });
*
* console.log(metadata?.type); // 'fieldAnnotation'
* console.log(metadata?.fieldColor); // '#980043'
* ```
*/
export function resolveSdtMetadata(input?: ResolveSdtMetadataInput | null): SdtMetadata | undefined {
if (!input) return undefined;
const { nodeType, attrs, cacheKey: explicitKey } = input;
if (!nodeType) return undefined;
const normalizedAttrs = isPlainObject(attrs) ? (attrs as Record<string, unknown>) : {};
const cacheKey = buildSdtCacheKey(nodeType, normalizedAttrs, explicitKey);
if (cacheKey && sdtMetadataCache.has(cacheKey)) {
return sdtMetadataCache.get(cacheKey);
}
let metadata: SdtMetadata | undefined;
switch (nodeType) {
case 'fieldAnnotation':
metadata = normalizeFieldAnnotationMetadata(normalizedAttrs);
break;
case 'structuredContent':
case 'structuredContentBlock':
metadata = normalizeStructuredContentMetadata(nodeType, normalizedAttrs);
break;
case 'documentSection':
metadata = normalizeDocumentSectionMetadata(normalizedAttrs);
break;
case 'docPartObject':
metadata = normalizeDocPartMetadata(normalizedAttrs);
break;
}
if (metadata && cacheKey) {
sdtMetadataCache.set(cacheKey, metadata);
}
return metadata;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function normalizeFieldAnnotationMetadata(attrs: Record<string, unknown>): FieldAnnotationMetadata {
const fieldId = toOptionalString(attrs.fieldId) ?? '';
const formatting = extractFormatting(attrs);
const size = normalizeSize(attrs.size);
const extras = isPlainObject(attrs.extras) ? (attrs.extras as Record<string, unknown>) : null;
const marks = isPlainObject(attrs.marks) ? (attrs.marks as Record<string, unknown>) : undefined;
return {
type: 'fieldAnnotation',
fieldId,
variant: normalizeFieldAnnotationVariant(attrs.type),
fieldType: toOptionalString(attrs.fieldType),
displayLabel: toOptionalString(attrs.displayLabel),
defaultDisplayLabel: toOptionalString(attrs.defaultDisplayLabel),
alias: toOptionalString(attrs.alias),
fieldColor: normalizeColorValue(attrs.fieldColor),
borderColor: normalizeColorValue(attrs.borderColor),
highlighted: toBoolean(attrs.highlighted, true),
fontFamily: toNullableString(attrs.fontFamily),
fontSize: normalizeFontSize(attrs.fontSize),
textColor: normalizeColorValue(attrs.textColor) ?? null,
textHighlight: normalizeColorValue(attrs.textHighlight) ?? null,
linkUrl: toNullableString(attrs.linkUrl),
imageSrc: toNullableString(attrs.imageSrc),
rawHtml: attrs.rawHtml ?? undefined,
size: size ?? null,
extras,
multipleImage: toBoolean(attrs.multipleImage, false),
hash: toOptionalString(attrs.hash) ?? null,
generatorIndex: toNumber(attrs.generatorIndex),
sdtId: toOptionalString(attrs.sdtId) ?? null,
hidden: toBoolean(attrs.hidden, false),
visibility: normalizeVisibility(attrs.visibility),
isLocked: toBoolean(attrs.isLocked, false),
formatting,
marks,
};
}
function normalizeStructuredContentMetadata(
nodeType: 'structuredContent' | 'structuredContentBlock',
attrs: Record<string, unknown>,
): StructuredContentMetadata {
const metadata: StructuredContentMetadata = {
type: 'structuredContent',
scope: nodeType === 'structuredContentBlock' ? 'block' : 'inline',
id: toNullableString(attrs.id),
tag: toOptionalString(attrs.tag),
alias: toOptionalString(attrs.alias),
lockMode: attrs.lockMode as StructuredContentMetadata['lockMode'],
sdtPr: attrs.sdtPr,
};
// `appearance` comes from the SDT's <w15:appearance> element on import.
// Only the three spec-defined values flow through; anything else is
// discarded so a bad value doesn't poison rendering decisions.
const rawAppearance = toOptionalString(attrs.appearance);
if (rawAppearance === 'boundingBox' || rawAppearance === 'tags' || rawAppearance === 'hidden') {
metadata.appearance = rawAppearance;
}
return metadata;
}
function normalizeDocumentSectionMetadata(attrs: Record<string, unknown>): DocumentSectionMetadata {
return {
type: 'documentSection',
id: toNullableString(attrs.id),
title: toOptionalString(attrs.title) ?? null,
description: toOptionalString(attrs.description) ?? null,
sectionType: toOptionalString(attrs.sectionType) ?? null,
isLocked: toBoolean(attrs.isLocked, false),
sdBlockId: toNullableString(attrs.sdBlockId),
};
}
function normalizeDocPartMetadata(attrs: Record<string, unknown>): DocPartMetadata {
return {
type: 'docPartObject',
gallery: toOptionalString(attrs.docPartGallery ?? attrs.gallery) ?? null,
// Source uniqueId from attrs.id (PM adapter uses getDocPartObjectId which extracts attrs.id)
// Fall back to attrs.uniqueId for compatibility
uniqueId: toOptionalString(attrs.id ?? attrs.uniqueId) ?? null,
alias: toOptionalString(attrs.alias) ?? null,
instruction: toOptionalString(attrs.instruction) ?? null,
};
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === 'object' && !Array.isArray(value);
}
function toOptionalString(value: unknown): string | undefined {
if (value == null) return undefined;
if (typeof value === 'string') {
const trimmed = value.trim();
return trimmed.length ? trimmed : undefined;
}
return String(value);
}
function toNullableString(value: unknown): string | null {
const str = toOptionalString(value);
return str ?? null;
}
function toBoolean(value: unknown, fallback: boolean): boolean {
if (typeof value === 'boolean') return value;
if (typeof value === 'string') {
const lower = value.toLowerCase();
if (lower === 'true') return true;
if (lower === 'false') return false;
}
if (value == null) return fallback;
return Boolean(value);
}
function normalizeVisibility(value: unknown): 'visible' | 'hidden' | undefined {
if (typeof value !== 'string') return undefined;
const normalized = value.toLowerCase();
if (normalized === 'visible' || normalized === 'hidden') {
return normalized as 'visible' | 'hidden';
}
return undefined;
}
function normalizeColorValue(value: unknown): string | undefined {
if (typeof value !== 'string') return undefined;
const trimmed = value.trim();
if (!trimmed || trimmed.toLowerCase() === 'none') return undefined;
return trimmed;
}
function normalizeFontSize(value: unknown): string | number | null {
if (value == null) return null;
if (typeof value === 'number') {
return Number.isFinite(value) ? value : null;
}
if (typeof value === 'string') {
const trimmed = value.trim();
return trimmed.length ? trimmed : null;
}
return null;
}
function toNumber(value: unknown): number | null {
if (typeof value === 'number') {
return Number.isFinite(value) ? value : null;
}
if (typeof value === 'string') {
const parsed = parseFloat(value);
return Number.isFinite(parsed) ? parsed : null;
}
return null;
}
function normalizeSize(value: unknown): { width?: number; height?: number } | null {
if (!isPlainObject(value)) return null;
const obj = value as Record<string, unknown>;
const width = toNumber(obj.width);
const height = toNumber(obj.height);
if (width == null && height == null) return null;
const result: { width?: number; height?: number } = {};
if (width != null) result.width = width;
if (height != null) result.height = height;
return result;
}
function normalizeFieldAnnotationVariant(value: unknown): FieldAnnotationMetadata['variant'] | undefined {
if (typeof value !== 'string') return undefined;
const normalized = value.toLowerCase();
if (
normalized === 'text' ||
normalized === 'image' ||
normalized === 'signature' ||
normalized === 'checkbox' ||
normalized === 'html' ||
normalized === 'link'
) {
return normalized as FieldAnnotationMetadata['variant'];
}
return undefined;
}
function extractFormatting(attrs: Record<string, unknown>): FieldAnnotationMetadata['formatting'] | undefined {
const bold = toBoolean(attrs.bold, false);
const italic = toBoolean(attrs.italic, false);
const underline = toBoolean(attrs.underline, false);
const formatting: FieldAnnotationMetadata['formatting'] = {};
if (bold) formatting.bold = true;
if (italic) formatting.italic = true;
if (underline) formatting.underline = true;
return Object.keys(formatting).length ? formatting : undefined;
}
function buildSdtCacheKey(
nodeType: string,
attrs: Record<string, unknown>,
explicitKey?: string | null,
): string | undefined {
const provided = toOptionalString(explicitKey);
if (provided) {
return `${nodeType}:${provided}`;
}
const hash = toOptionalString(attrs.hash);
if (hash) {
return `${nodeType}:${hash}`;
}
const id = toOptionalString(attrs.id);
if (id) {
return `${nodeType}:${id}`;
}
return undefined;
}