@formepdf/react 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 +8 -0
- package/dist/serialize.js +168 -26
- package/package.json +2 -2
package/dist/serialize.d.ts
CHANGED
|
@@ -4,6 +4,14 @@ import type { FormeDocument } from './types.js';
|
|
|
4
4
|
* Serialize a React element tree into a Forme JSON document object.
|
|
5
5
|
* The top-level element must be a <Document> (or a component that returns one).
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* Warnings the serializer raises (a dropped list child) collected during a
|
|
9
|
+
* `serialize()` call and attached to the document under this symbol, where
|
|
10
|
+
* `@formepdf/core` picks them up into the render's `warnings`. A symbol
|
|
11
|
+
* key never reaches `JSON.stringify`, so the engine input is unchanged.
|
|
12
|
+
* `Symbol.for`, so core reads it without importing this package.
|
|
13
|
+
*/
|
|
14
|
+
export declare const SERIALIZER_WARNINGS: unique symbol;
|
|
7
15
|
export declare function serialize(element: ReactElement): FormeDocument;
|
|
8
16
|
/**
|
|
9
17
|
* Serialize a React element tree into a Forme template JSON document.
|
package/dist/serialize.js
CHANGED
|
@@ -104,18 +104,93 @@ function callComponent(fn, props) {
|
|
|
104
104
|
throw err;
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
+
// ─── User components in child lists ─────────────────────────────────
|
|
108
|
+
//
|
|
109
|
+
// Every Forme primitive is a function component that returns null: it is
|
|
110
|
+
// recognised by identity and serialized by its own arm, never called. Any
|
|
111
|
+
// other function component is the user's, and what it RETURNS stands in its
|
|
112
|
+
// place. That result can be an array or a fragment as well as one element, so
|
|
113
|
+
// it has to be flattened exactly like inline children are. Serializing it as
|
|
114
|
+
// a single child lost both shapes (issue #159): an array fails
|
|
115
|
+
// `isValidElement`, and a fragment has no serializer arm.
|
|
116
|
+
const FORME_PRIMITIVES = new Set([
|
|
117
|
+
Document, Page, View, Text, H1, H2, H3, H4, H5, H6, OrderedList, UnorderedList, ListItem,
|
|
118
|
+
Strong, Em, Code, Link, Image, Table, Row, Cell, Fixed, Svg, QrCode, Barcode, Canvas,
|
|
119
|
+
Watermark, PageBreak, BarChart, LineChart, PieChart, AreaChart, DotPlot, TextField,
|
|
120
|
+
Checkbox, Dropdown, RadioButton,
|
|
121
|
+
]);
|
|
122
|
+
function isUserComponent(child) {
|
|
123
|
+
return isValidElement(child)
|
|
124
|
+
&& typeof child.type === 'function'
|
|
125
|
+
&& !FORME_PRIMITIVES.has(child.type)
|
|
126
|
+
&& !isDocumentType(child.type);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Replace each user component in an already-flattened child list with its
|
|
130
|
+
* own flattened result, recursively, so a component contributes its nodes in
|
|
131
|
+
* place: none for null, several for an array or fragment.
|
|
132
|
+
*/
|
|
133
|
+
function expandComponents(children, flatten) {
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const child of children) {
|
|
136
|
+
if (isUserComponent(child)) {
|
|
137
|
+
const result = callComponent(child.type, child.props);
|
|
138
|
+
out.push(...expandComponents(flatten(result), flatten));
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
out.push(child);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Where exactly one node fits (an `$each` template, an expression branch, a
|
|
148
|
+
* component passed straight to a single-child slot), a component result of
|
|
149
|
+
* several nodes is grouped in a plain View rather than cut to its first.
|
|
150
|
+
*/
|
|
151
|
+
function oneNode(nodes) {
|
|
152
|
+
if (nodes.length === 0)
|
|
153
|
+
return null;
|
|
154
|
+
if (nodes.length === 1)
|
|
155
|
+
return nodes[0];
|
|
156
|
+
return { kind: { type: 'View' }, style: {}, children: nodes };
|
|
157
|
+
}
|
|
107
158
|
// ─── Public API ──────────────────────────────────────────────────────
|
|
108
159
|
/**
|
|
109
160
|
* Serialize a React element tree into a Forme JSON document object.
|
|
110
161
|
* The top-level element must be a <Document> (or a component that returns one).
|
|
111
162
|
*/
|
|
163
|
+
/**
|
|
164
|
+
* Warnings the serializer raises (a dropped list child) collected during a
|
|
165
|
+
* `serialize()` call and attached to the document under this symbol, where
|
|
166
|
+
* `@formepdf/core` picks them up into the render's `warnings`. A symbol
|
|
167
|
+
* key never reaches `JSON.stringify`, so the engine input is unchanged.
|
|
168
|
+
* `Symbol.for`, so core reads it without importing this package.
|
|
169
|
+
*/
|
|
170
|
+
export const SERIALIZER_WARNINGS = Symbol.for('formepdf.serializerWarnings');
|
|
171
|
+
let collectedWarnings = null;
|
|
112
172
|
export function serialize(element) {
|
|
173
|
+
const outer = collectedWarnings;
|
|
174
|
+
const warnings = [];
|
|
175
|
+
collectedWarnings = warnings;
|
|
176
|
+
try {
|
|
177
|
+
const doc = serializeDocument(element);
|
|
178
|
+
if (warnings.length > 0) {
|
|
179
|
+
doc[SERIALIZER_WARNINGS] = warnings;
|
|
180
|
+
}
|
|
181
|
+
return doc;
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
collectedWarnings = outer;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function serializeDocument(element) {
|
|
113
188
|
element = resolveElement(element);
|
|
114
189
|
if (!isDocumentType(element.type)) {
|
|
115
190
|
throw new Error('Top-level element must be <Document>');
|
|
116
191
|
}
|
|
117
192
|
const props = element.props;
|
|
118
|
-
const childElements = flattenChildren(props.children);
|
|
193
|
+
const childElements = expandComponents(flattenChildren(props.children), flattenChildren);
|
|
119
194
|
// Separate Page children from content children
|
|
120
195
|
const pageNodes = [];
|
|
121
196
|
const contentNodes = [];
|
|
@@ -362,19 +437,14 @@ function serializeChild(child, parent = null) {
|
|
|
362
437
|
const props = element.props;
|
|
363
438
|
const childElements = flattenChildren(props.children);
|
|
364
439
|
const nodes = serializeChildren(childElements, parent);
|
|
365
|
-
return nodes
|
|
366
|
-
kind: { type: 'View' },
|
|
367
|
-
style: {},
|
|
368
|
-
children: nodes,
|
|
369
|
-
};
|
|
440
|
+
return oneNode(nodes);
|
|
370
441
|
}
|
|
371
|
-
// Unknown component
|
|
442
|
+
// Unknown component: call it and serialize whatever it returned, with the
|
|
443
|
+
// same flattening inline children get. Child lists expand components before
|
|
444
|
+
// they reach here (`serializeChildren`), so this only runs where one node fits.
|
|
372
445
|
if (typeof element.type === 'function') {
|
|
373
446
|
const result = callComponent(element.type, element.props);
|
|
374
|
-
|
|
375
|
-
return serializeChild(result, parent);
|
|
376
|
-
}
|
|
377
|
-
return null;
|
|
447
|
+
return oneNode(serializeChildren(flattenChildren(result), parent));
|
|
378
448
|
}
|
|
379
449
|
return null;
|
|
380
450
|
}
|
|
@@ -481,6 +551,8 @@ function serializeText(element) {
|
|
|
481
551
|
}
|
|
482
552
|
const MAIN_CTX = {
|
|
483
553
|
flatten: (c) => flattenChildren(c),
|
|
554
|
+
nodeChildren: (c) => expandComponents(flattenChildren(c), flattenChildren),
|
|
555
|
+
isMarker: () => false,
|
|
484
556
|
style: (s) => mapStyle(s),
|
|
485
557
|
textContent: (c) => flattenTextContent(c),
|
|
486
558
|
isRunChild: (c) => inlineDefaults(c.type) !== null,
|
|
@@ -490,6 +562,8 @@ const MAIN_CTX = {
|
|
|
490
562
|
};
|
|
491
563
|
const TEMPLATE_CTX = {
|
|
492
564
|
flatten: (c) => flattenTemplateChildren(c),
|
|
565
|
+
nodeChildren: (c) => expandComponents(flattenTemplateChildren(c), flattenTemplateChildren),
|
|
566
|
+
isMarker: (c) => isEachMarker(c) || isExprMarker(c),
|
|
493
567
|
style: (s) => mapTemplateStyle(s),
|
|
494
568
|
textContent: (c) => flattenTemplateTextContent(c),
|
|
495
569
|
// The template path's own convention for Text, kept exactly: a nested
|
|
@@ -534,11 +608,33 @@ function buildList(element, ordered, ctx) {
|
|
|
534
608
|
? mapListMarker(props.type, 'decimal')
|
|
535
609
|
: mapListMarker(props.marker, 'disc');
|
|
536
610
|
const start = typeof props.start === 'number' && props.start >= 1 ? props.start : 1;
|
|
537
|
-
// Children must be ListItems
|
|
538
|
-
//
|
|
539
|
-
//
|
|
540
|
-
|
|
541
|
-
|
|
611
|
+
// Children must be ListItems. User components are expanded first, so one
|
|
612
|
+
// that returns ListItems counts as ListItems. On the template path a
|
|
613
|
+
// `.map()` or expression marker stands for the ListItems it will produce
|
|
614
|
+
// once data is bound: it is serialized and kept when every node it can
|
|
615
|
+
// produce is a ListItem (issue #161: this filter used to drop the marker,
|
|
616
|
+
// and with it the whole list). Null, booleans and whitespace are dropped
|
|
617
|
+
// quietly as JSX noise; anything else is dropped with a warning, because
|
|
618
|
+
// the engine only lays out ListItems here.
|
|
619
|
+
const listName = ordered ? 'OrderedList' : 'UnorderedList';
|
|
620
|
+
const children = [];
|
|
621
|
+
for (const c of ctx.nodeChildren(props.children)) {
|
|
622
|
+
if (isValidElement(c) && c.type === ListItem) {
|
|
623
|
+
children.push(buildListItem(c, ctx));
|
|
624
|
+
}
|
|
625
|
+
else if (ctx.isMarker(c)) {
|
|
626
|
+
const node = ctx.child(c, null);
|
|
627
|
+
if (node !== null && producesOnlyListItems(node)) {
|
|
628
|
+
children.push(node);
|
|
629
|
+
}
|
|
630
|
+
else if (node !== null) {
|
|
631
|
+
warnDroppedListChild(listName, 'a .map() or expression that does not produce <ListItem>s');
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
else if (!isJsxNoise(c)) {
|
|
635
|
+
warnDroppedListChild(listName, describeChild(c));
|
|
636
|
+
}
|
|
637
|
+
}
|
|
542
638
|
const node = {
|
|
543
639
|
kind: { type: 'List', ordered, marker_type: markerType, start },
|
|
544
640
|
style: ctx.style(props.style),
|
|
@@ -557,7 +653,7 @@ function buildListItem(element, ctx) {
|
|
|
557
653
|
// we serialize whatever the user put inside as the node's children. This
|
|
558
654
|
// covers plain strings, JSX text, nested lists, formatted runs via
|
|
559
655
|
// <Text>, etc.
|
|
560
|
-
const rawChildren = ctx.
|
|
656
|
+
const rawChildren = ctx.nodeChildren(props.children);
|
|
561
657
|
const children = [];
|
|
562
658
|
for (const c of rawChildren) {
|
|
563
659
|
if (typeof c === 'string' || typeof c === 'number') {
|
|
@@ -569,7 +665,9 @@ function buildListItem(element, ctx) {
|
|
|
569
665
|
children: [],
|
|
570
666
|
});
|
|
571
667
|
}
|
|
572
|
-
else if (isValidElement(c)) {
|
|
668
|
+
else if (isValidElement(c) || ctx.isMarker(c)) {
|
|
669
|
+
// A marker here is a `.map()` or expression inside the item itself,
|
|
670
|
+
// e.g. a nested list's items or a run of <Text>s from data.
|
|
573
671
|
const node = ctx.child(c, null);
|
|
574
672
|
if (node)
|
|
575
673
|
children.push(node);
|
|
@@ -585,6 +683,52 @@ function buildListItem(element, ctx) {
|
|
|
585
683
|
node.sourceLocation = loc;
|
|
586
684
|
return node;
|
|
587
685
|
}
|
|
686
|
+
/**
|
|
687
|
+
* Does every node this serialized value can produce have kind ListItem?
|
|
688
|
+
* A plain node answers for itself; an `$each` or expression node answers for
|
|
689
|
+
* the node values it holds (its template, its branches), since those are
|
|
690
|
+
* what it becomes once data is bound.
|
|
691
|
+
*/
|
|
692
|
+
function producesOnlyListItems(value) {
|
|
693
|
+
if (value === null || typeof value !== 'object')
|
|
694
|
+
return true;
|
|
695
|
+
const obj = value;
|
|
696
|
+
const kind = obj.kind;
|
|
697
|
+
if (kind && typeof kind === 'object' && 'type' in kind)
|
|
698
|
+
return kind.type === 'ListItem';
|
|
699
|
+
return Object.values(obj).every((v) => Array.isArray(v) ? v.every(producesOnlyListItems) : producesOnlyListItems(v));
|
|
700
|
+
}
|
|
701
|
+
/** Null, booleans and whitespace-only text: the gaps between JSX children. */
|
|
702
|
+
function isJsxNoise(c) {
|
|
703
|
+
return c === null || c === undefined || typeof c === 'boolean'
|
|
704
|
+
|| (typeof c === 'string' && c.trim() === '');
|
|
705
|
+
}
|
|
706
|
+
function describeChild(c) {
|
|
707
|
+
if (isValidElement(c)) {
|
|
708
|
+
const type = c.type;
|
|
709
|
+
const name = typeof type === 'string'
|
|
710
|
+
? type
|
|
711
|
+
: (type?.displayName
|
|
712
|
+
|| type?.name
|
|
713
|
+
|| 'element');
|
|
714
|
+
return `<${name}>`;
|
|
715
|
+
}
|
|
716
|
+
if (typeof c === 'string' || typeof c === 'number')
|
|
717
|
+
return `text ${JSON.stringify(String(c))}`;
|
|
718
|
+
return typeof c;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Report a dropped list child on the console and to the active
|
|
722
|
+
* `serialize()` call, which hands it to the render's `warnings` (see
|
|
723
|
+
* SERIALIZER_WARNINGS).
|
|
724
|
+
*/
|
|
725
|
+
function warnDroppedListChild(listName, what) {
|
|
726
|
+
const message = `[Forme] <${listName}> dropped ${what}: only <ListItem> children are rendered in a list. `
|
|
727
|
+
+ `Wrap the content in <ListItem>.`;
|
|
728
|
+
// The console still gets it: renderDocument returns bytes only.
|
|
729
|
+
console.warn(message);
|
|
730
|
+
collectedWarnings?.push(message);
|
|
731
|
+
}
|
|
588
732
|
function serializeHeading(element, level) {
|
|
589
733
|
return buildHeading(element, level, MAIN_CTX);
|
|
590
734
|
}
|
|
@@ -944,7 +1088,7 @@ function flattenChildren(children) {
|
|
|
944
1088
|
}
|
|
945
1089
|
function serializeChildren(children, parent = null) {
|
|
946
1090
|
const nodes = [];
|
|
947
|
-
for (const child of children) {
|
|
1091
|
+
for (const child of expandComponents(children, flattenChildren)) {
|
|
948
1092
|
const node = serializeChild(child, parent);
|
|
949
1093
|
if (node)
|
|
950
1094
|
nodes.push(node);
|
|
@@ -999,7 +1143,7 @@ export function serializeTemplate(element) {
|
|
|
999
1143
|
throw new Error('Top-level element must be <Document>');
|
|
1000
1144
|
}
|
|
1001
1145
|
const props = element.props;
|
|
1002
|
-
const childElements = flattenTemplateChildren(props.children);
|
|
1146
|
+
const childElements = expandComponents(flattenTemplateChildren(props.children), flattenTemplateChildren);
|
|
1003
1147
|
const pageNodes = [];
|
|
1004
1148
|
const contentNodes = [];
|
|
1005
1149
|
for (const child of childElements) {
|
|
@@ -1213,13 +1357,11 @@ function serializeTemplateChild(child, parent = null) {
|
|
|
1213
1357
|
validateNesting('Page', parent);
|
|
1214
1358
|
return serializeTemplatePage(element);
|
|
1215
1359
|
}
|
|
1216
|
-
// Unknown function component
|
|
1360
|
+
// Unknown function component: call it and serialize its result with the
|
|
1361
|
+
// same flattening inline children get (see the `serializeChild` fallback).
|
|
1217
1362
|
if (typeof element.type === 'function') {
|
|
1218
1363
|
const result = callComponent(element.type, element.props);
|
|
1219
|
-
|
|
1220
|
-
return serializeTemplateChild(result, parent);
|
|
1221
|
-
}
|
|
1222
|
-
return null;
|
|
1364
|
+
return oneNode(serializeTemplateChildren(flattenTemplateChildren(result), parent));
|
|
1223
1365
|
}
|
|
1224
1366
|
return null;
|
|
1225
1367
|
}
|
|
@@ -1327,7 +1469,7 @@ function serializeTemplateFixed(element) {
|
|
|
1327
1469
|
}
|
|
1328
1470
|
function serializeTemplateChildren(children, parent = null) {
|
|
1329
1471
|
const nodes = [];
|
|
1330
|
-
for (const child of children) {
|
|
1472
|
+
for (const child of expandComponents(children, flattenTemplateChildren)) {
|
|
1331
1473
|
const node = serializeTemplateChild(child, parent);
|
|
1332
1474
|
if (node !== null)
|
|
1333
1475
|
nodes.push(node);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formepdf/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0",
|
|
4
4
|
"description": "JSX-to-JSON serializer for Forme PDF engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -18,7 +18,7 @@
|
|
|
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
|
"react": "^18 || ^19"
|