@formepdf/preact 0.23.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/serialize.js +108 -56
- package/package.json +3 -3
package/dist/serialize.js
CHANGED
|
@@ -484,27 +484,38 @@ function serializeText(element) {
|
|
|
484
484
|
node.bookmark = props.bookmark;
|
|
485
485
|
return node;
|
|
486
486
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
487
|
+
const MAIN_CTX = {
|
|
488
|
+
flatten: (c) => flattenChildren(c),
|
|
489
|
+
style: (s) => mapStyle(s),
|
|
490
|
+
textContent: (c) => flattenTextContent(c),
|
|
491
|
+
isRunChild: (c) => inlineDefaults(c.type) !== null,
|
|
492
|
+
buildRuns: (c) => buildTextRuns(c),
|
|
493
|
+
child: (c, parent) => serializeChild(c, parent),
|
|
494
|
+
sourceLocation: (e) => extractSourceLocation(e),
|
|
495
|
+
};
|
|
496
|
+
const TEMPLATE_CTX = {
|
|
497
|
+
flatten: (c) => flattenTemplateChildren(c),
|
|
498
|
+
style: (s) => mapTemplateStyle(s),
|
|
499
|
+
textContent: (c) => flattenTemplateTextContent(c),
|
|
500
|
+
// The template path's own convention for Text, kept exactly: a nested
|
|
501
|
+
// <Text> becomes a run. (<Strong>/<Em> inside a <Text> still flatten to
|
|
502
|
+
// plain content on this path — a fidelity gap, tracked separately, not
|
|
503
|
+
// the deletion this refactor fixes.)
|
|
504
|
+
isRunChild: (c) => c.type === Text,
|
|
505
|
+
buildRuns: (c) => buildTemplateTextRuns(c),
|
|
506
|
+
child: (c, parent) => serializeTemplateChild(c, parent),
|
|
507
|
+
sourceLocation: () => undefined,
|
|
508
|
+
};
|
|
509
|
+
function buildHeading(element, level, ctx) {
|
|
495
510
|
const props = element.props;
|
|
496
|
-
const childElements =
|
|
497
|
-
const
|
|
498
|
-
const kind = {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
content: '',
|
|
502
|
-
};
|
|
503
|
-
if (hasInlineChild) {
|
|
504
|
-
kind.runs = buildTextRuns(props.children);
|
|
511
|
+
const childElements = ctx.flatten(props.children);
|
|
512
|
+
const hasRunChild = childElements.some((c) => isValidElement(c) && ctx.isRunChild(c));
|
|
513
|
+
const kind = { type: 'Heading', level, content: '' };
|
|
514
|
+
if (hasRunChild) {
|
|
515
|
+
kind.runs = ctx.buildRuns(props.children);
|
|
505
516
|
}
|
|
506
517
|
else {
|
|
507
|
-
kind.content =
|
|
518
|
+
kind.content = ctx.textContent(props.children);
|
|
508
519
|
}
|
|
509
520
|
if (props.href)
|
|
510
521
|
kind.href = props.href;
|
|
@@ -512,15 +523,17 @@ function serializeHeading(element, level) {
|
|
|
512
523
|
const mergedStyle = { ...HEADING_DEFAULTS[level], ...(props.style || {}) };
|
|
513
524
|
const node = {
|
|
514
525
|
kind,
|
|
515
|
-
style:
|
|
526
|
+
style: ctx.style(mergedStyle),
|
|
516
527
|
children: [],
|
|
517
|
-
sourceLocation: extractSourceLocation(element),
|
|
518
528
|
};
|
|
529
|
+
const loc = ctx.sourceLocation(element);
|
|
530
|
+
if (loc !== undefined)
|
|
531
|
+
node.sourceLocation = loc;
|
|
519
532
|
if (props.bookmark)
|
|
520
533
|
node.bookmark = props.bookmark;
|
|
521
534
|
return node;
|
|
522
535
|
}
|
|
523
|
-
function
|
|
536
|
+
function buildList(element, ordered, ctx) {
|
|
524
537
|
const props = element.props;
|
|
525
538
|
const markerType = ordered
|
|
526
539
|
? mapListMarker(props.type, 'decimal')
|
|
@@ -529,48 +542,62 @@ function serializeList(element, ordered) {
|
|
|
529
542
|
// Children must be ListItems — anything else is silently dropped to
|
|
530
543
|
// keep the serializer tolerant. (We don't throw on stray content because
|
|
531
544
|
// a Fragment / null child in JSX is too common to be a real error.)
|
|
532
|
-
const childElements =
|
|
533
|
-
const children = childElements.map((c) =>
|
|
545
|
+
const childElements = ctx.flatten(props.children).filter((c) => isValidElement(c) && c.type === ListItem);
|
|
546
|
+
const children = childElements.map((c) => buildListItem(c, ctx));
|
|
534
547
|
const node = {
|
|
535
548
|
kind: { type: 'List', ordered, marker_type: markerType, start },
|
|
536
|
-
style:
|
|
549
|
+
style: ctx.style(props.style),
|
|
537
550
|
children,
|
|
538
|
-
sourceLocation: extractSourceLocation(element),
|
|
539
551
|
};
|
|
552
|
+
const loc = ctx.sourceLocation(element);
|
|
553
|
+
if (loc !== undefined)
|
|
554
|
+
node.sourceLocation = loc;
|
|
540
555
|
if (props.bookmark)
|
|
541
556
|
node.bookmark = props.bookmark;
|
|
542
557
|
return node;
|
|
543
558
|
}
|
|
544
|
-
function
|
|
559
|
+
function buildListItem(element, ctx) {
|
|
545
560
|
const props = element.props;
|
|
546
561
|
// ListItem content is layed out by the engine using layout_children, so
|
|
547
562
|
// we serialize whatever the user put inside as the node's children. This
|
|
548
563
|
// covers plain strings, JSX text, nested lists, formatted runs via
|
|
549
564
|
// <Text>, etc.
|
|
550
|
-
const rawChildren =
|
|
565
|
+
const rawChildren = ctx.flatten(props.children);
|
|
551
566
|
const children = [];
|
|
552
567
|
for (const c of rawChildren) {
|
|
553
568
|
if (typeof c === 'string' || typeof c === 'number') {
|
|
554
569
|
// Auto-wrap raw strings in a Text node so the engine has something
|
|
555
570
|
// concrete to render. Matches the convention everywhere else.
|
|
556
571
|
children.push({
|
|
557
|
-
kind: { type: 'Text', content:
|
|
572
|
+
kind: { type: 'Text', content: ctx.textContent(c) },
|
|
558
573
|
style: {},
|
|
559
574
|
children: [],
|
|
560
575
|
});
|
|
561
576
|
}
|
|
562
577
|
else if (isValidElement(c)) {
|
|
563
|
-
const node =
|
|
578
|
+
const node = ctx.child(c, null);
|
|
564
579
|
if (node)
|
|
565
580
|
children.push(node);
|
|
566
581
|
}
|
|
567
582
|
}
|
|
568
|
-
|
|
583
|
+
const node = {
|
|
569
584
|
kind: { type: 'ListItem' },
|
|
570
|
-
style:
|
|
585
|
+
style: ctx.style(props.style),
|
|
571
586
|
children,
|
|
572
|
-
sourceLocation: extractSourceLocation(element),
|
|
573
587
|
};
|
|
588
|
+
const loc = ctx.sourceLocation(element);
|
|
589
|
+
if (loc !== undefined)
|
|
590
|
+
node.sourceLocation = loc;
|
|
591
|
+
return node;
|
|
592
|
+
}
|
|
593
|
+
function serializeHeading(element, level) {
|
|
594
|
+
return buildHeading(element, level, MAIN_CTX);
|
|
595
|
+
}
|
|
596
|
+
function serializeList(element, ordered) {
|
|
597
|
+
return buildList(element, ordered, MAIN_CTX);
|
|
598
|
+
}
|
|
599
|
+
function serializeListItem(element) {
|
|
600
|
+
return buildListItem(element, MAIN_CTX);
|
|
574
601
|
}
|
|
575
602
|
function serializeImage(element) {
|
|
576
603
|
const props = element.props;
|
|
@@ -1120,6 +1147,28 @@ function serializeTemplateChild(child, parent = null) {
|
|
|
1120
1147
|
return serializeTemplateView(element, parent);
|
|
1121
1148
|
if (element.type === Text)
|
|
1122
1149
|
return serializeTemplateText(element);
|
|
1150
|
+
// Headings and lists reached this dispatch with no arm at all: they are
|
|
1151
|
+
// function components returning null, so they fell through to the
|
|
1152
|
+
// "unknown function component" fallback below and were deleted along with
|
|
1153
|
+
// their subtrees. Same builders as `serialize()`, different context.
|
|
1154
|
+
if (element.type === H1)
|
|
1155
|
+
return buildHeading(element, 1, TEMPLATE_CTX);
|
|
1156
|
+
if (element.type === H2)
|
|
1157
|
+
return buildHeading(element, 2, TEMPLATE_CTX);
|
|
1158
|
+
if (element.type === H3)
|
|
1159
|
+
return buildHeading(element, 3, TEMPLATE_CTX);
|
|
1160
|
+
if (element.type === H4)
|
|
1161
|
+
return buildHeading(element, 4, TEMPLATE_CTX);
|
|
1162
|
+
if (element.type === H5)
|
|
1163
|
+
return buildHeading(element, 5, TEMPLATE_CTX);
|
|
1164
|
+
if (element.type === H6)
|
|
1165
|
+
return buildHeading(element, 6, TEMPLATE_CTX);
|
|
1166
|
+
if (element.type === OrderedList)
|
|
1167
|
+
return buildList(element, true, TEMPLATE_CTX);
|
|
1168
|
+
if (element.type === UnorderedList)
|
|
1169
|
+
return buildList(element, false, TEMPLATE_CTX);
|
|
1170
|
+
if (element.type === ListItem)
|
|
1171
|
+
return buildListItem(element, TEMPLATE_CTX);
|
|
1123
1172
|
if (element.type === Image)
|
|
1124
1173
|
return serializeTemplateImage(element);
|
|
1125
1174
|
if (element.type === Table)
|
|
@@ -1193,36 +1242,39 @@ function serializeTemplateView(element, _parent = null) {
|
|
|
1193
1242
|
node.href = props.href;
|
|
1194
1243
|
return node;
|
|
1195
1244
|
}
|
|
1245
|
+
/**
|
|
1246
|
+
* Formatted runs on the template path. Extracted from `serializeTemplateText`
|
|
1247
|
+
* so headings can build runs the same way text does — the two used to be one
|
|
1248
|
+
* inline block, which is why headings had no way to produce runs at all.
|
|
1249
|
+
*/
|
|
1250
|
+
function buildTemplateTextRuns(children) {
|
|
1251
|
+
const runs = [];
|
|
1252
|
+
for (const child of flattenTemplateChildren(children)) {
|
|
1253
|
+
if (typeof child === 'string' || typeof child === 'number') {
|
|
1254
|
+
const processed = typeof child === 'string' ? processTemplateString(child) : null;
|
|
1255
|
+
runs.push({ content: processed !== null ? processed : String(child) });
|
|
1256
|
+
}
|
|
1257
|
+
else if (isValidElement(child) && child.type === Text) {
|
|
1258
|
+
const childProps = child.props;
|
|
1259
|
+
const run = {
|
|
1260
|
+
content: flattenTemplateTextContent(childProps.children),
|
|
1261
|
+
};
|
|
1262
|
+
if (childProps.style)
|
|
1263
|
+
run.style = mapTemplateStyle(childProps.style);
|
|
1264
|
+
if (childProps.href)
|
|
1265
|
+
run.href = childProps.href;
|
|
1266
|
+
runs.push(run);
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return runs;
|
|
1270
|
+
}
|
|
1196
1271
|
function serializeTemplateText(element) {
|
|
1197
1272
|
const props = element.props;
|
|
1198
1273
|
const childElements = flattenTemplateChildren(props.children);
|
|
1199
1274
|
const hasTextChild = childElements.some(c => isValidElement(c) && c.type === Text);
|
|
1200
1275
|
const kind = { type: 'Text', content: '' };
|
|
1201
1276
|
if (hasTextChild) {
|
|
1202
|
-
|
|
1203
|
-
for (const child of childElements) {
|
|
1204
|
-
if (typeof child === 'string' || typeof child === 'number') {
|
|
1205
|
-
const processed = typeof child === 'string' ? processTemplateString(child) : null;
|
|
1206
|
-
if (processed !== null) {
|
|
1207
|
-
runs.push({ content: processed });
|
|
1208
|
-
}
|
|
1209
|
-
else {
|
|
1210
|
-
runs.push({ content: String(child) });
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
else if (isValidElement(child) && child.type === Text) {
|
|
1214
|
-
const childProps = child.props;
|
|
1215
|
-
const run = {
|
|
1216
|
-
content: flattenTemplateTextContent(childProps.children),
|
|
1217
|
-
};
|
|
1218
|
-
if (childProps.style)
|
|
1219
|
-
run.style = mapTemplateStyle(childProps.style);
|
|
1220
|
-
if (childProps.href)
|
|
1221
|
-
run.href = childProps.href;
|
|
1222
|
-
runs.push(run);
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
kind.runs = runs;
|
|
1277
|
+
kind.runs = buildTemplateTextRuns(props.children);
|
|
1226
1278
|
}
|
|
1227
1279
|
else {
|
|
1228
1280
|
kind.content = flattenTemplateTextContent(props.children);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formepdf/preact",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "Preact-native adapter for Forme PDF engine. Same components, same props as @formepdf/react, without the React runtime.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"test:watch": "vitest"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@formepdf/shared": "0.
|
|
21
|
+
"@formepdf/shared": "0.25.0"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
24
|
"preact": "^10.19.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@formepdf/react": "0.
|
|
27
|
+
"@formepdf/react": "0.25.0",
|
|
28
28
|
"@types/react": "^19.0.0",
|
|
29
29
|
"preact": "^10.19.0",
|
|
30
30
|
"react": "^19.0.0",
|