@formepdf/preact 0.25.0 → 0.27.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.d.ts +7 -0
- package/dist/serialize.js +167 -26
- package/package.json +3 -3
package/dist/serialize.d.ts
CHANGED
|
@@ -5,6 +5,13 @@ import type { FormeDocument } from './types.js';
|
|
|
5
5
|
* Serialize a React element tree into a Forme JSON document object.
|
|
6
6
|
* The top-level element must be a <Document> (or a component that returns one).
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* Warnings the serializer raises (a dropped list child) collected during a
|
|
10
|
+
* call and attached to the document under this symbol, where
|
|
11
|
+
* `@formepdf/core` picks them up into the render's `warnings`. A symbol
|
|
12
|
+
* key never reaches `JSON.stringify`, so the engine input is unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare const SERIALIZER_WARNINGS: unique symbol;
|
|
8
15
|
export declare function serialize(element: ReactElement): FormeDocument;
|
|
9
16
|
/**
|
|
10
17
|
* Serialize a React element tree into a Forme template JSON document.
|
package/dist/serialize.js
CHANGED
|
@@ -109,18 +109,92 @@ function callComponent(fn, props) {
|
|
|
109
109
|
throw err;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
|
+
// ─── User components in child lists ─────────────────────────────────
|
|
113
|
+
//
|
|
114
|
+
// Every Forme primitive is a function component that returns null: it is
|
|
115
|
+
// recognised by identity and serialized by its own arm, never called. Any
|
|
116
|
+
// other function component is the user's, and what it RETURNS stands in its
|
|
117
|
+
// place. That result can be an array or a fragment as well as one element, so
|
|
118
|
+
// it has to be flattened exactly like inline children are. Serializing it as
|
|
119
|
+
// a single child lost both shapes (issue #159): an array fails
|
|
120
|
+
// `isValidElement`, and a fragment has no serializer arm.
|
|
121
|
+
const FORME_PRIMITIVES = new Set([
|
|
122
|
+
Document, Page, View, Text, H1, H2, H3, H4, H5, H6, OrderedList, UnorderedList, ListItem,
|
|
123
|
+
Strong, Em, Code, Link, Image, Table, Row, Cell, Fixed, Svg, QrCode, Barcode, Canvas,
|
|
124
|
+
Watermark, PageBreak, BarChart, LineChart, PieChart, AreaChart, DotPlot, TextField,
|
|
125
|
+
Checkbox, Dropdown, RadioButton,
|
|
126
|
+
]);
|
|
127
|
+
function isUserComponent(child) {
|
|
128
|
+
return isValidElement(child)
|
|
129
|
+
&& typeof child.type === 'function'
|
|
130
|
+
&& !FORME_PRIMITIVES.has(child.type)
|
|
131
|
+
&& !isDocumentType(child.type);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Replace each user component in an already-flattened child list with its
|
|
135
|
+
* own flattened result, recursively, so a component contributes its nodes in
|
|
136
|
+
* place: none for null, several for an array or fragment.
|
|
137
|
+
*/
|
|
138
|
+
function expandComponents(children, flatten) {
|
|
139
|
+
const out = [];
|
|
140
|
+
for (const child of children) {
|
|
141
|
+
if (isUserComponent(child)) {
|
|
142
|
+
const result = callComponent(child.type, child.props);
|
|
143
|
+
out.push(...expandComponents(flatten(result), flatten));
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
out.push(child);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Where exactly one node fits (an `$each` template, an expression branch, a
|
|
153
|
+
* component passed straight to a single-child slot), a component result of
|
|
154
|
+
* several nodes is grouped in a plain View rather than cut to its first.
|
|
155
|
+
*/
|
|
156
|
+
function oneNode(nodes) {
|
|
157
|
+
if (nodes.length === 0)
|
|
158
|
+
return null;
|
|
159
|
+
if (nodes.length === 1)
|
|
160
|
+
return nodes[0];
|
|
161
|
+
return { kind: { type: 'View' }, style: {}, children: nodes };
|
|
162
|
+
}
|
|
112
163
|
// ─── Public API ──────────────────────────────────────────────────────
|
|
113
164
|
/**
|
|
114
165
|
* Serialize a React element tree into a Forme JSON document object.
|
|
115
166
|
* The top-level element must be a <Document> (or a component that returns one).
|
|
116
167
|
*/
|
|
168
|
+
/**
|
|
169
|
+
* Warnings the serializer raises (a dropped list child) collected during a
|
|
170
|
+
* call and attached to the document under this symbol, where
|
|
171
|
+
* `@formepdf/core` picks them up into the render's `warnings`. A symbol
|
|
172
|
+
* key never reaches `JSON.stringify`, so the engine input is unchanged.
|
|
173
|
+
*/
|
|
174
|
+
export const SERIALIZER_WARNINGS = Symbol.for('formepdf.serializerWarnings');
|
|
175
|
+
let collectedWarnings = null;
|
|
117
176
|
export function serialize(element) {
|
|
177
|
+
const outer = collectedWarnings;
|
|
178
|
+
const warnings = [];
|
|
179
|
+
collectedWarnings = warnings;
|
|
180
|
+
try {
|
|
181
|
+
const doc = serializeDocument(element);
|
|
182
|
+
if (warnings.length > 0) {
|
|
183
|
+
doc[SERIALIZER_WARNINGS] = warnings;
|
|
184
|
+
}
|
|
185
|
+
return doc;
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
collectedWarnings = outer;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function serializeDocument(element) {
|
|
118
192
|
element = resolveElement(element);
|
|
119
193
|
if (!isDocumentType(element.type)) {
|
|
120
194
|
throw new Error('Top-level element must be <Document>');
|
|
121
195
|
}
|
|
122
196
|
const props = element.props;
|
|
123
|
-
const childElements = flattenChildren(props.children);
|
|
197
|
+
const childElements = expandComponents(flattenChildren(props.children), flattenChildren);
|
|
124
198
|
// Separate Page children from content children
|
|
125
199
|
const pageNodes = [];
|
|
126
200
|
const contentNodes = [];
|
|
@@ -367,19 +441,14 @@ function serializeChild(child, parent = null) {
|
|
|
367
441
|
const props = element.props;
|
|
368
442
|
const childElements = flattenChildren(props.children);
|
|
369
443
|
const nodes = serializeChildren(childElements, parent);
|
|
370
|
-
return nodes
|
|
371
|
-
kind: { type: 'View' },
|
|
372
|
-
style: {},
|
|
373
|
-
children: nodes,
|
|
374
|
-
};
|
|
444
|
+
return oneNode(nodes);
|
|
375
445
|
}
|
|
376
|
-
// Unknown component
|
|
446
|
+
// Unknown component: call it and serialize whatever it returned, with the
|
|
447
|
+
// same flattening inline children get. Child lists expand components before
|
|
448
|
+
// they reach here (`serializeChildren`), so this only runs where one node fits.
|
|
377
449
|
if (typeof element.type === 'function') {
|
|
378
450
|
const result = callComponent(element.type, element.props);
|
|
379
|
-
|
|
380
|
-
return serializeChild(result, parent);
|
|
381
|
-
}
|
|
382
|
-
return null;
|
|
451
|
+
return oneNode(serializeChildren(flattenChildren(result), parent));
|
|
383
452
|
}
|
|
384
453
|
return null;
|
|
385
454
|
}
|
|
@@ -486,6 +555,8 @@ function serializeText(element) {
|
|
|
486
555
|
}
|
|
487
556
|
const MAIN_CTX = {
|
|
488
557
|
flatten: (c) => flattenChildren(c),
|
|
558
|
+
nodeChildren: (c) => expandComponents(flattenChildren(c), flattenChildren),
|
|
559
|
+
isMarker: () => false,
|
|
489
560
|
style: (s) => mapStyle(s),
|
|
490
561
|
textContent: (c) => flattenTextContent(c),
|
|
491
562
|
isRunChild: (c) => inlineDefaults(c.type) !== null,
|
|
@@ -495,6 +566,8 @@ const MAIN_CTX = {
|
|
|
495
566
|
};
|
|
496
567
|
const TEMPLATE_CTX = {
|
|
497
568
|
flatten: (c) => flattenTemplateChildren(c),
|
|
569
|
+
nodeChildren: (c) => expandComponents(flattenTemplateChildren(c), flattenTemplateChildren),
|
|
570
|
+
isMarker: (c) => isEachMarker(c) || isExprMarker(c),
|
|
498
571
|
style: (s) => mapTemplateStyle(s),
|
|
499
572
|
textContent: (c) => flattenTemplateTextContent(c),
|
|
500
573
|
// The template path's own convention for Text, kept exactly: a nested
|
|
@@ -539,11 +612,33 @@ function buildList(element, ordered, ctx) {
|
|
|
539
612
|
? mapListMarker(props.type, 'decimal')
|
|
540
613
|
: mapListMarker(props.marker, 'disc');
|
|
541
614
|
const start = typeof props.start === 'number' && props.start >= 1 ? props.start : 1;
|
|
542
|
-
// Children must be ListItems
|
|
543
|
-
//
|
|
544
|
-
//
|
|
545
|
-
|
|
546
|
-
|
|
615
|
+
// Children must be ListItems. User components are expanded first, so one
|
|
616
|
+
// that returns ListItems counts as ListItems. On the template path a
|
|
617
|
+
// `.map()` or expression marker stands for the ListItems it will produce
|
|
618
|
+
// once data is bound: it is serialized and kept when every node it can
|
|
619
|
+
// produce is a ListItem (issue #161: this filter used to drop the marker,
|
|
620
|
+
// and with it the whole list). Null, booleans and whitespace are dropped
|
|
621
|
+
// quietly as JSX noise; anything else is dropped with a warning, because
|
|
622
|
+
// the engine only lays out ListItems here.
|
|
623
|
+
const listName = ordered ? 'OrderedList' : 'UnorderedList';
|
|
624
|
+
const children = [];
|
|
625
|
+
for (const c of ctx.nodeChildren(props.children)) {
|
|
626
|
+
if (isValidElement(c) && c.type === ListItem) {
|
|
627
|
+
children.push(buildListItem(c, ctx));
|
|
628
|
+
}
|
|
629
|
+
else if (ctx.isMarker(c)) {
|
|
630
|
+
const node = ctx.child(c, null);
|
|
631
|
+
if (node !== null && producesOnlyListItems(node)) {
|
|
632
|
+
children.push(node);
|
|
633
|
+
}
|
|
634
|
+
else if (node !== null) {
|
|
635
|
+
warnDroppedListChild(listName, 'a .map() or expression that does not produce <ListItem>s');
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
else if (!isJsxNoise(c)) {
|
|
639
|
+
warnDroppedListChild(listName, describeChild(c));
|
|
640
|
+
}
|
|
641
|
+
}
|
|
547
642
|
const node = {
|
|
548
643
|
kind: { type: 'List', ordered, marker_type: markerType, start },
|
|
549
644
|
style: ctx.style(props.style),
|
|
@@ -562,7 +657,7 @@ function buildListItem(element, ctx) {
|
|
|
562
657
|
// we serialize whatever the user put inside as the node's children. This
|
|
563
658
|
// covers plain strings, JSX text, nested lists, formatted runs via
|
|
564
659
|
// <Text>, etc.
|
|
565
|
-
const rawChildren = ctx.
|
|
660
|
+
const rawChildren = ctx.nodeChildren(props.children);
|
|
566
661
|
const children = [];
|
|
567
662
|
for (const c of rawChildren) {
|
|
568
663
|
if (typeof c === 'string' || typeof c === 'number') {
|
|
@@ -574,7 +669,9 @@ function buildListItem(element, ctx) {
|
|
|
574
669
|
children: [],
|
|
575
670
|
});
|
|
576
671
|
}
|
|
577
|
-
else if (isValidElement(c)) {
|
|
672
|
+
else if (isValidElement(c) || ctx.isMarker(c)) {
|
|
673
|
+
// A marker here is a `.map()` or expression inside the item itself,
|
|
674
|
+
// e.g. a nested list's items or a run of <Text>s from data.
|
|
578
675
|
const node = ctx.child(c, null);
|
|
579
676
|
if (node)
|
|
580
677
|
children.push(node);
|
|
@@ -590,6 +687,52 @@ function buildListItem(element, ctx) {
|
|
|
590
687
|
node.sourceLocation = loc;
|
|
591
688
|
return node;
|
|
592
689
|
}
|
|
690
|
+
/**
|
|
691
|
+
* Does every node this serialized value can produce have kind ListItem?
|
|
692
|
+
* A plain node answers for itself; an `$each` or expression node answers for
|
|
693
|
+
* the node values it holds (its template, its branches), since those are
|
|
694
|
+
* what it becomes once data is bound.
|
|
695
|
+
*/
|
|
696
|
+
function producesOnlyListItems(value) {
|
|
697
|
+
if (value === null || typeof value !== 'object')
|
|
698
|
+
return true;
|
|
699
|
+
const obj = value;
|
|
700
|
+
const kind = obj.kind;
|
|
701
|
+
if (kind && typeof kind === 'object' && 'type' in kind)
|
|
702
|
+
return kind.type === 'ListItem';
|
|
703
|
+
return Object.values(obj).every((v) => Array.isArray(v) ? v.every(producesOnlyListItems) : producesOnlyListItems(v));
|
|
704
|
+
}
|
|
705
|
+
/** Null, booleans and whitespace-only text: the gaps between JSX children. */
|
|
706
|
+
function isJsxNoise(c) {
|
|
707
|
+
return c === null || c === undefined || typeof c === 'boolean'
|
|
708
|
+
|| (typeof c === 'string' && c.trim() === '');
|
|
709
|
+
}
|
|
710
|
+
function describeChild(c) {
|
|
711
|
+
if (isValidElement(c)) {
|
|
712
|
+
const type = c.type;
|
|
713
|
+
const name = typeof type === 'string'
|
|
714
|
+
? type
|
|
715
|
+
: (type?.displayName
|
|
716
|
+
|| type?.name
|
|
717
|
+
|| 'element');
|
|
718
|
+
return `<${name}>`;
|
|
719
|
+
}
|
|
720
|
+
if (typeof c === 'string' || typeof c === 'number')
|
|
721
|
+
return `text ${JSON.stringify(String(c))}`;
|
|
722
|
+
return typeof c;
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* The serializer has no warnings channel of its own (render `warnings` come
|
|
726
|
+
* from the engine, which never sees what was dropped here), so a dropped
|
|
727
|
+
* list child is reported the way the deprecated `signature` prop is.
|
|
728
|
+
*/
|
|
729
|
+
function warnDroppedListChild(listName, what) {
|
|
730
|
+
const message = `[Forme] <${listName}> dropped ${what}: only <ListItem> children are rendered in a list. `
|
|
731
|
+
+ `Wrap the content in <ListItem>.`;
|
|
732
|
+
// The console still gets it: renderDocument returns bytes only.
|
|
733
|
+
console.warn(message);
|
|
734
|
+
collectedWarnings?.push(message);
|
|
735
|
+
}
|
|
593
736
|
function serializeHeading(element, level) {
|
|
594
737
|
return buildHeading(element, level, MAIN_CTX);
|
|
595
738
|
}
|
|
@@ -949,7 +1092,7 @@ function flattenChildren(children) {
|
|
|
949
1092
|
}
|
|
950
1093
|
function serializeChildren(children, parent = null) {
|
|
951
1094
|
const nodes = [];
|
|
952
|
-
for (const child of children) {
|
|
1095
|
+
for (const child of expandComponents(children, flattenChildren)) {
|
|
953
1096
|
const node = serializeChild(child, parent);
|
|
954
1097
|
if (node)
|
|
955
1098
|
nodes.push(node);
|
|
@@ -1004,7 +1147,7 @@ export function serializeTemplate(element) {
|
|
|
1004
1147
|
throw new Error('Top-level element must be <Document>');
|
|
1005
1148
|
}
|
|
1006
1149
|
const props = element.props;
|
|
1007
|
-
const childElements = flattenTemplateChildren(props.children);
|
|
1150
|
+
const childElements = expandComponents(flattenTemplateChildren(props.children), flattenTemplateChildren);
|
|
1008
1151
|
const pageNodes = [];
|
|
1009
1152
|
const contentNodes = [];
|
|
1010
1153
|
for (const child of childElements) {
|
|
@@ -1218,13 +1361,11 @@ function serializeTemplateChild(child, parent = null) {
|
|
|
1218
1361
|
validateNesting('Page', parent);
|
|
1219
1362
|
return serializeTemplatePage(element);
|
|
1220
1363
|
}
|
|
1221
|
-
// Unknown function component
|
|
1364
|
+
// Unknown function component: call it and serialize its result with the
|
|
1365
|
+
// same flattening inline children get (see the `serializeChild` fallback).
|
|
1222
1366
|
if (typeof element.type === 'function') {
|
|
1223
1367
|
const result = callComponent(element.type, element.props);
|
|
1224
|
-
|
|
1225
|
-
return serializeTemplateChild(result, parent);
|
|
1226
|
-
}
|
|
1227
|
-
return null;
|
|
1368
|
+
return oneNode(serializeTemplateChildren(flattenTemplateChildren(result), parent));
|
|
1228
1369
|
}
|
|
1229
1370
|
return null;
|
|
1230
1371
|
}
|
|
@@ -1332,7 +1473,7 @@ function serializeTemplateFixed(element) {
|
|
|
1332
1473
|
}
|
|
1333
1474
|
function serializeTemplateChildren(children, parent = null) {
|
|
1334
1475
|
const nodes = [];
|
|
1335
|
-
for (const child of children) {
|
|
1476
|
+
for (const child of expandComponents(children, flattenTemplateChildren)) {
|
|
1336
1477
|
const node = serializeTemplateChild(child, parent);
|
|
1337
1478
|
if (node !== null)
|
|
1338
1479
|
nodes.push(node);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formepdf/preact",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.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.27.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.27.0",
|
|
28
28
|
"@types/react": "^19.0.0",
|
|
29
29
|
"preact": "^10.19.0",
|
|
30
30
|
"react": "^19.0.0",
|