pptxgenjs-plus-jsx 4.1.17
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/LICENSE +23 -0
- package/README.md +713 -0
- package/dist/components.d.ts +565 -0
- package/dist/components.js +228 -0
- package/dist/components.js.map +1 -0
- package/dist/context.d.ts +147 -0
- package/dist/context.js +167 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +1 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-runtime.d.ts +63 -0
- package/dist/jsx-runtime.js +85 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/node.d.ts +129 -0
- package/dist/node.js +188 -0
- package/dist/node.js.map +1 -0
- package/dist/render.d.ts +61 -0
- package/dist/render.js +1125 -0
- package/dist/render.js.map +1 -0
- package/package.json +75 -0
package/dist/render.js
ADDED
|
@@ -0,0 +1,1125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rendering engine for pptxgenjsx.
|
|
3
|
+
*
|
|
4
|
+
* All tree-traversal functions are async to support PptxNodePromise resolution.
|
|
5
|
+
* Component factories and type definitions are in `./components.ts`.
|
|
6
|
+
*/
|
|
7
|
+
import PptxGenJS from "pptxgenjs-plus";
|
|
8
|
+
import { assertLeafHasNoChildren, isLeafNodeType, isPptxNode, resolveChild, createNode, } from "./node.js";
|
|
9
|
+
import { useGroupContext, withDeckContext, withSlideContext, withGroupContext, } from "./context.js";
|
|
10
|
+
// ════════════════════════════════════════════════════════════════════
|
|
11
|
+
// RENDERING ENGINE
|
|
12
|
+
// ════════════════════════════════════════════════════════════════════
|
|
13
|
+
// ── EMU (English Metric Unit) ─────────────────────────────
|
|
14
|
+
// OOXML uses EMU as its base unit. PptxGenJS stores presLayout
|
|
15
|
+
// dimensions in EMU. 1 inch = 914400 EMU.
|
|
16
|
+
const EMU_PER_INCH = 914400;
|
|
17
|
+
// ── Reserved prop keys (not passed to pptxgenjs) ─────────────────
|
|
18
|
+
const RESERVED_PROPS = new Set([
|
|
19
|
+
"children",
|
|
20
|
+
"options",
|
|
21
|
+
"text",
|
|
22
|
+
"runs",
|
|
23
|
+
"data",
|
|
24
|
+
"rows",
|
|
25
|
+
"cells",
|
|
26
|
+
"eleId",
|
|
27
|
+
"render",
|
|
28
|
+
"component",
|
|
29
|
+
]);
|
|
30
|
+
// ── Shape type sets ───────────────────────────────────────────────
|
|
31
|
+
const CHART_TYPES = new Set([
|
|
32
|
+
"Chart",
|
|
33
|
+
"AreaChart",
|
|
34
|
+
"BarChart",
|
|
35
|
+
"Bar3DChart",
|
|
36
|
+
"BubbleChart",
|
|
37
|
+
"DoughnutChart",
|
|
38
|
+
"LineChart",
|
|
39
|
+
"PieChart",
|
|
40
|
+
"RadarChart",
|
|
41
|
+
"ScatterChart",
|
|
42
|
+
]);
|
|
43
|
+
const SHAPE_TYPES = new Set([
|
|
44
|
+
"Shape",
|
|
45
|
+
"Rect",
|
|
46
|
+
"RoundRect",
|
|
47
|
+
"Ellipse",
|
|
48
|
+
"Oval",
|
|
49
|
+
"Triangle",
|
|
50
|
+
"RightTriangle",
|
|
51
|
+
"Diamond",
|
|
52
|
+
"Pentagon",
|
|
53
|
+
"Hexagon",
|
|
54
|
+
"Star",
|
|
55
|
+
"Star4",
|
|
56
|
+
"Star5",
|
|
57
|
+
"Star6",
|
|
58
|
+
"Star8",
|
|
59
|
+
"Star10",
|
|
60
|
+
"Arc",
|
|
61
|
+
"BlockArc",
|
|
62
|
+
"PieShape",
|
|
63
|
+
"CustomGeometry",
|
|
64
|
+
"LeftArrow",
|
|
65
|
+
"RightArrow",
|
|
66
|
+
"UpArrow",
|
|
67
|
+
"DownArrow",
|
|
68
|
+
"LeftRightArrow",
|
|
69
|
+
"UpDownArrow",
|
|
70
|
+
"Chevron",
|
|
71
|
+
"Cloud",
|
|
72
|
+
"Heart",
|
|
73
|
+
"Donut",
|
|
74
|
+
"Plus",
|
|
75
|
+
]);
|
|
76
|
+
/**
|
|
77
|
+
* Create a PptxGenJS instance from a Deck or Slide node tree.
|
|
78
|
+
* Resolves any PptxNodePromise wrappers during traversal.
|
|
79
|
+
*/
|
|
80
|
+
export async function createPptx(root, options = {}) {
|
|
81
|
+
const node = await resolveChild(root);
|
|
82
|
+
const pptx = options.pptx ?? new PptxGenJS();
|
|
83
|
+
if (node.type === "Deck") {
|
|
84
|
+
applyDeckProps(pptx, node.props);
|
|
85
|
+
const total = await countDeckSlides(node);
|
|
86
|
+
const width = pptx.presLayout.width / EMU_PER_INCH;
|
|
87
|
+
const height = pptx.presLayout.height / EMU_PER_INCH;
|
|
88
|
+
const ctx = {
|
|
89
|
+
pptx,
|
|
90
|
+
slideIndex: 0,
|
|
91
|
+
total,
|
|
92
|
+
width,
|
|
93
|
+
height,
|
|
94
|
+
};
|
|
95
|
+
const deckCtx = { width, height };
|
|
96
|
+
await withDeckContext(deckCtx, () => processDeckChildren(node, ctx));
|
|
97
|
+
return pptx;
|
|
98
|
+
}
|
|
99
|
+
if (node.type === "Slide") {
|
|
100
|
+
const width = pptx.presLayout.width / EMU_PER_INCH;
|
|
101
|
+
const height = pptx.presLayout.height / EMU_PER_INCH;
|
|
102
|
+
const ctx = {
|
|
103
|
+
pptx,
|
|
104
|
+
slideIndex: 0,
|
|
105
|
+
total: 1,
|
|
106
|
+
width,
|
|
107
|
+
height,
|
|
108
|
+
};
|
|
109
|
+
const deckCtx = { width, height };
|
|
110
|
+
await withDeckContext(deckCtx, () => processSlide(node, ctx));
|
|
111
|
+
return pptx;
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`createPptx expected a Deck or Slide root, got ${node.type}.`);
|
|
114
|
+
}
|
|
115
|
+
// ── Slide counting ────────────────────────────────────────────────
|
|
116
|
+
/** Count all `<Slide>` nodes in the node tree recursively.
|
|
117
|
+
*
|
|
118
|
+
* Must resolve PptxNodePromise wrappers since all non-string component
|
|
119
|
+
* factories are deferred (lazy-evaluated during tree traversal).
|
|
120
|
+
* Structural factories (Slide, Fragment, etc.) do not need rendering
|
|
121
|
+
* context, so resolving them here is safe.
|
|
122
|
+
*/
|
|
123
|
+
async function countDeckSlides(node) {
|
|
124
|
+
if (node.type === "Slide")
|
|
125
|
+
return 1;
|
|
126
|
+
let count = 0;
|
|
127
|
+
for (const rawChild of node.children) {
|
|
128
|
+
if (isPptxNode(rawChild)) {
|
|
129
|
+
count += await countDeckSlides(rawChild);
|
|
130
|
+
}
|
|
131
|
+
// ── handle PptxNodePromise ────────────────────────────────────
|
|
132
|
+
// Since all non-string component factories now return PptxNodePromise
|
|
133
|
+
// (deferred execution), Slide/Fragment/Raw wrappers at the Deck level
|
|
134
|
+
// are PptxNodePromise instances and must be resolved to count slides.
|
|
135
|
+
else {
|
|
136
|
+
const child = await resolveChild(rawChild);
|
|
137
|
+
if (isPptxNode(child)) {
|
|
138
|
+
count += await countDeckSlides(child);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return count;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Render to a .pptx file.
|
|
146
|
+
* Returns the file name (as per PptxGenJS.writeFile).
|
|
147
|
+
*/
|
|
148
|
+
export async function renderPptx(root, options = {}) {
|
|
149
|
+
const { pptx, ...writeOptions } = options;
|
|
150
|
+
const instance = await createPptx(root, { pptx });
|
|
151
|
+
return instance.writeFile(writeOptions);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Write to a buffer / ArrayBuffer / Blob.
|
|
155
|
+
*/
|
|
156
|
+
export async function writePptx(root, options = {}) {
|
|
157
|
+
const { pptx, ...writeOptions } = options;
|
|
158
|
+
const instance = await createPptx(root, { pptx });
|
|
159
|
+
return instance.write(writeOptions);
|
|
160
|
+
}
|
|
161
|
+
/** Alias for renderPptx. */
|
|
162
|
+
export const render = renderPptx;
|
|
163
|
+
/** Alias for writePptx. */
|
|
164
|
+
export const write = writePptx;
|
|
165
|
+
// ── Deck props ────────────────────────────────────────────────────
|
|
166
|
+
function applyDeckProps(pptx, props) {
|
|
167
|
+
const { layouts, sections, masters, layout, title, author, company, subject, revision, rtlMode, theme, created, modified, mediaOnError, } = props;
|
|
168
|
+
layouts?.forEach((l) => pptx.defineLayout(l));
|
|
169
|
+
sections?.forEach((s) => pptx.addSection(s));
|
|
170
|
+
masters?.forEach((m) => pptx.defineSlideMaster(m));
|
|
171
|
+
if (typeof layout === "string") {
|
|
172
|
+
pptx.layout = layout;
|
|
173
|
+
}
|
|
174
|
+
else if (layout) {
|
|
175
|
+
pptx.defineLayout(layout);
|
|
176
|
+
pptx.layout = layout.name;
|
|
177
|
+
}
|
|
178
|
+
setIfDefined(pptx, "title", title);
|
|
179
|
+
setIfDefined(pptx, "author", author);
|
|
180
|
+
setIfDefined(pptx, "company", company);
|
|
181
|
+
setIfDefined(pptx, "subject", subject);
|
|
182
|
+
setIfDefined(pptx, "revision", revision);
|
|
183
|
+
setIfDefined(pptx, "rtlMode", rtlMode);
|
|
184
|
+
setIfDefined(pptx, "theme", theme);
|
|
185
|
+
setIfDefined(pptx, "created", created);
|
|
186
|
+
setIfDefined(pptx, "modified", modified);
|
|
187
|
+
setIfDefined(pptx, "mediaOnError", mediaOnError);
|
|
188
|
+
}
|
|
189
|
+
async function processDeckChildren(node, ctx) {
|
|
190
|
+
for (const rawChild of node.children) {
|
|
191
|
+
const child = await resolveChild(rawChild);
|
|
192
|
+
if (!isPptxNode(child))
|
|
193
|
+
continue;
|
|
194
|
+
// Leaf components (Layout, TableToSlides) at deck level must not carry
|
|
195
|
+
// children either — fail loudly instead of silently ignoring them.
|
|
196
|
+
assertLeafHasNoChildren(child.type, child.children);
|
|
197
|
+
switch (child.type) {
|
|
198
|
+
case "Layout":
|
|
199
|
+
ctx.pptx.defineLayout(child.props);
|
|
200
|
+
break;
|
|
201
|
+
case "Master":
|
|
202
|
+
ctx.pptx.defineSlideMaster(buildMaster(child));
|
|
203
|
+
break;
|
|
204
|
+
case "Section":
|
|
205
|
+
await processSection(child, ctx);
|
|
206
|
+
break;
|
|
207
|
+
case "Slide":
|
|
208
|
+
await processSlide(child, ctx);
|
|
209
|
+
break;
|
|
210
|
+
case "TableToSlides":
|
|
211
|
+
processTableToSlides(child, ctx);
|
|
212
|
+
break;
|
|
213
|
+
case "Raw":
|
|
214
|
+
child.props.render({ pptx: ctx.pptx, node: child });
|
|
215
|
+
break;
|
|
216
|
+
case "Fragment":
|
|
217
|
+
await processDeckChildren(child, ctx);
|
|
218
|
+
break;
|
|
219
|
+
default:
|
|
220
|
+
throw new Error(`${child.type} must be inside a Slide, Section, or Master.`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// ── Section ───────────────────────────────────────────────────────
|
|
225
|
+
async function processSection(node, ctx) {
|
|
226
|
+
const props = node.props;
|
|
227
|
+
ctx.pptx.addSection({ title: props.title, order: props.order });
|
|
228
|
+
for (const rawChild of node.children) {
|
|
229
|
+
const child = await resolveChild(rawChild);
|
|
230
|
+
if (!isPptxNode(child))
|
|
231
|
+
continue;
|
|
232
|
+
if (child.type !== "Slide") {
|
|
233
|
+
throw new Error(`Section children must be Slide nodes. Got ${child.type}.`);
|
|
234
|
+
}
|
|
235
|
+
await processSlide(child, ctx, props.title);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// ── Slide ─────────────────────────────────────────────────────────
|
|
239
|
+
async function processSlide(node, ctx, sectionTitle) {
|
|
240
|
+
const props = node.props;
|
|
241
|
+
// Increment and capture the 1-based slide index
|
|
242
|
+
ctx.slideIndex += 1;
|
|
243
|
+
const index = ctx.slideIndex;
|
|
244
|
+
const total = ctx.total;
|
|
245
|
+
const resolvedSectionTitle = props.sectionTitle ?? sectionTitle;
|
|
246
|
+
const slide = ctx.pptx.addSlide({
|
|
247
|
+
masterName: props.masterName,
|
|
248
|
+
sectionTitle: resolvedSectionTitle,
|
|
249
|
+
transition: props.transition,
|
|
250
|
+
});
|
|
251
|
+
setIfDefined(slide, "background", props.background);
|
|
252
|
+
setIfDefined(slide, "color", props.color);
|
|
253
|
+
setIfDefined(slide, "hidden", props.hidden);
|
|
254
|
+
setIfDefined(slide, "slideNumber", props.slideNumber);
|
|
255
|
+
const slideCtx = {
|
|
256
|
+
index,
|
|
257
|
+
total,
|
|
258
|
+
sectionTitle: resolvedSectionTitle,
|
|
259
|
+
};
|
|
260
|
+
// ── Lazy `component` prop — like React Router's `component={() => import('./path')}` ──
|
|
261
|
+
if (props.component) {
|
|
262
|
+
const module = await props.component();
|
|
263
|
+
const LazyComponent = module.default;
|
|
264
|
+
// IMPORTANT: Both the factory call AND rendering must happen inside
|
|
265
|
+
// withSlideContext so that useSlideContext() / useDeckContext() /
|
|
266
|
+
// useGroupContext() have access to the rendering context throughout
|
|
267
|
+
// the entire slide content evaluation and rendering pipeline.
|
|
268
|
+
await withSlideContext(slideCtx, async () => {
|
|
269
|
+
const lazyNode = await resolveChild(LazyComponent({}));
|
|
270
|
+
await renderSlideElement(lazyNode, { pptx: ctx.pptx, slide, target: slide });
|
|
271
|
+
});
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
// ── Direct children ──
|
|
275
|
+
// All non-string component factories are now deferred via PptxNodePromise
|
|
276
|
+
// thunks. resolveChild() calls the thunk synchronously, inside the
|
|
277
|
+
// withSlideContext scope, so useSlideContext() / useDeckContext() /
|
|
278
|
+
// useGroupContext() work correctly.
|
|
279
|
+
await withSlideContext(slideCtx, async () => {
|
|
280
|
+
for (const rawChild of node.children) {
|
|
281
|
+
const child = await resolveChild(rawChild);
|
|
282
|
+
if (!isPptxNode(child))
|
|
283
|
+
continue;
|
|
284
|
+
await renderSlideElement(child, { pptx: ctx.pptx, slide, target: slide });
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
function isSlideTarget(target) {
|
|
289
|
+
return typeof target.addTable === "function";
|
|
290
|
+
}
|
|
291
|
+
function requireSlideTarget(target, type) {
|
|
292
|
+
if (!isSlideTarget(target)) {
|
|
293
|
+
throw new Error(`${type} cannot be placed inside <Group>. PresentationML p:grpSp only holds shapes, text, images, and nested groups.`);
|
|
294
|
+
}
|
|
295
|
+
return target;
|
|
296
|
+
}
|
|
297
|
+
async function renderSlideElement(node, ctx) {
|
|
298
|
+
const { target } = ctx;
|
|
299
|
+
assertLeafHasNoChildren(node.type, node.children);
|
|
300
|
+
if (CHART_TYPES.has(node.type)) {
|
|
301
|
+
renderChart(node, requireSlideTarget(target, node.type));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (SHAPE_TYPES.has(node.type)) {
|
|
305
|
+
renderShape(node, target);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
switch (node.type) {
|
|
309
|
+
case "Text":
|
|
310
|
+
await renderText(node, target);
|
|
311
|
+
break;
|
|
312
|
+
case "WordArt":
|
|
313
|
+
await renderWordArt(node, requireSlideTarget(target, "WordArt"));
|
|
314
|
+
break;
|
|
315
|
+
case "Line":
|
|
316
|
+
renderLine(node, target);
|
|
317
|
+
break;
|
|
318
|
+
case "LineBetween":
|
|
319
|
+
renderLineBetween(node, target);
|
|
320
|
+
break;
|
|
321
|
+
case "Image":
|
|
322
|
+
renderImage(node, target);
|
|
323
|
+
break;
|
|
324
|
+
case "Media":
|
|
325
|
+
renderMedia(node, requireSlideTarget(target, "Media"));
|
|
326
|
+
break;
|
|
327
|
+
case "Table":
|
|
328
|
+
await renderTable(node, requireSlideTarget(target, "Table"));
|
|
329
|
+
break;
|
|
330
|
+
case "Notes":
|
|
331
|
+
requireSlideTarget(target, "Notes").addNotes(getTextContent(node, node.props.text));
|
|
332
|
+
break;
|
|
333
|
+
case "Raw":
|
|
334
|
+
await node.props.render({ pptx: ctx.pptx, slide: ctx.slide, group: isSlideTarget(target) ? undefined : target, node });
|
|
335
|
+
break;
|
|
336
|
+
case "Group":
|
|
337
|
+
await renderGroup(node, ctx);
|
|
338
|
+
break;
|
|
339
|
+
case "Fragment":
|
|
340
|
+
for (const rawChild of node.children) {
|
|
341
|
+
const child = await resolveChild(rawChild);
|
|
342
|
+
if (isPptxNode(child)) {
|
|
343
|
+
await renderSlideElement(child, ctx);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
break;
|
|
347
|
+
default:
|
|
348
|
+
throw new Error(`${node.type} cannot be rendered directly on a slide.`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
/** Resolve deferred children so a sync `addGroup` builder can emit them. */
|
|
352
|
+
async function materializeNode(node) {
|
|
353
|
+
if (node.type !== "Fragment" && node.type !== "Group" && node.type !== "Text" && node.type !== "WordArt") {
|
|
354
|
+
return node;
|
|
355
|
+
}
|
|
356
|
+
const children = [];
|
|
357
|
+
for (const raw of node.children) {
|
|
358
|
+
const child = await resolveChild(raw);
|
|
359
|
+
children.push(isPptxNode(child) ? await materializeNode(child) : child);
|
|
360
|
+
}
|
|
361
|
+
return createNode(node.type, { ...node.props, children });
|
|
362
|
+
}
|
|
363
|
+
async function renderGroup(node, ctx) {
|
|
364
|
+
const props = node.props;
|
|
365
|
+
const parentGroup = useGroupContext();
|
|
366
|
+
const relX = resolveCoord(props.x, parentGroup.width, 0);
|
|
367
|
+
const relY = resolveCoord(props.y, parentGroup.height, 0);
|
|
368
|
+
const grpW = resolveCoord(props.w, parentGroup.width, parentGroup.width);
|
|
369
|
+
const grpH = resolveCoord(props.h, parentGroup.height, parentGroup.height);
|
|
370
|
+
const groupInfo = {
|
|
371
|
+
x: parentGroup.x + relX,
|
|
372
|
+
y: parentGroup.y + relY,
|
|
373
|
+
width: grpW,
|
|
374
|
+
height: grpH,
|
|
375
|
+
relative: true,
|
|
376
|
+
};
|
|
377
|
+
const children = [];
|
|
378
|
+
await withGroupContext(groupInfo, async () => {
|
|
379
|
+
for (const rawChild of node.children) {
|
|
380
|
+
const child = await resolveChild(rawChild);
|
|
381
|
+
if (isPptxNode(child))
|
|
382
|
+
children.push(await materializeNode(child));
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
ctx.target.addGroup({
|
|
386
|
+
x: relX,
|
|
387
|
+
y: relY,
|
|
388
|
+
w: grpW,
|
|
389
|
+
h: grpH,
|
|
390
|
+
rotate: props.rotate,
|
|
391
|
+
shadow: props.shadow,
|
|
392
|
+
objectName: props.objectName,
|
|
393
|
+
}, (group) => {
|
|
394
|
+
void withGroupContext(groupInfo, () => {
|
|
395
|
+
for (const child of children) {
|
|
396
|
+
emitResolvedElement(child, { pptx: ctx.pptx, slide: ctx.slide, target: group });
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
/** Sync emit for already-materialized nodes (used inside addGroup's sync builder). */
|
|
402
|
+
function emitResolvedElement(node, ctx) {
|
|
403
|
+
const { target } = ctx;
|
|
404
|
+
assertLeafHasNoChildren(node.type, node.children);
|
|
405
|
+
if (CHART_TYPES.has(node.type)) {
|
|
406
|
+
renderChart(node, requireSlideTarget(target, node.type));
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (SHAPE_TYPES.has(node.type)) {
|
|
410
|
+
renderShape(node, target);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
switch (node.type) {
|
|
414
|
+
case "Text":
|
|
415
|
+
renderTextResolved(node, target);
|
|
416
|
+
break;
|
|
417
|
+
case "WordArt":
|
|
418
|
+
renderWordArtResolved(node, requireSlideTarget(target, "WordArt"));
|
|
419
|
+
break;
|
|
420
|
+
case "Line":
|
|
421
|
+
renderLine(node, target);
|
|
422
|
+
break;
|
|
423
|
+
case "LineBetween":
|
|
424
|
+
renderLineBetween(node, target);
|
|
425
|
+
break;
|
|
426
|
+
case "Image":
|
|
427
|
+
renderImage(node, target);
|
|
428
|
+
break;
|
|
429
|
+
case "Media":
|
|
430
|
+
renderMedia(node, requireSlideTarget(target, "Media"));
|
|
431
|
+
break;
|
|
432
|
+
case "Table":
|
|
433
|
+
throw new Error("Table cannot be placed inside <Group>.");
|
|
434
|
+
case "Notes":
|
|
435
|
+
throw new Error("Notes cannot be placed inside <Group>.");
|
|
436
|
+
case "Raw":
|
|
437
|
+
void node.props.render({ pptx: ctx.pptx, slide: ctx.slide, group: target, node });
|
|
438
|
+
break;
|
|
439
|
+
case "Group":
|
|
440
|
+
emitResolvedGroup(node, ctx);
|
|
441
|
+
break;
|
|
442
|
+
case "Fragment":
|
|
443
|
+
for (const child of node.children) {
|
|
444
|
+
if (isPptxNode(child))
|
|
445
|
+
emitResolvedElement(child, ctx);
|
|
446
|
+
}
|
|
447
|
+
break;
|
|
448
|
+
default:
|
|
449
|
+
throw new Error(`${node.type} cannot be rendered directly on a slide.`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function emitResolvedGroup(node, ctx) {
|
|
453
|
+
const props = node.props;
|
|
454
|
+
const parentGroup = useGroupContext();
|
|
455
|
+
const relX = resolveCoord(props.x, parentGroup.width, 0);
|
|
456
|
+
const relY = resolveCoord(props.y, parentGroup.height, 0);
|
|
457
|
+
const grpW = resolveCoord(props.w, parentGroup.width, parentGroup.width);
|
|
458
|
+
const grpH = resolveCoord(props.h, parentGroup.height, parentGroup.height);
|
|
459
|
+
const groupInfo = {
|
|
460
|
+
x: parentGroup.x + relX,
|
|
461
|
+
y: parentGroup.y + relY,
|
|
462
|
+
width: grpW,
|
|
463
|
+
height: grpH,
|
|
464
|
+
relative: true,
|
|
465
|
+
};
|
|
466
|
+
ctx.target.addGroup({
|
|
467
|
+
x: relX,
|
|
468
|
+
y: relY,
|
|
469
|
+
w: grpW,
|
|
470
|
+
h: grpH,
|
|
471
|
+
rotate: props.rotate,
|
|
472
|
+
shadow: props.shadow,
|
|
473
|
+
objectName: props.objectName,
|
|
474
|
+
}, (group) => {
|
|
475
|
+
void withGroupContext(groupInfo, () => {
|
|
476
|
+
for (const child of node.children) {
|
|
477
|
+
if (isPptxNode(child)) {
|
|
478
|
+
emitResolvedElement(child, { pptx: ctx.pptx, slide: ctx.slide, target: group });
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
// ── Coordinate helpers ─────────────────────────────────────────────
|
|
485
|
+
/**
|
|
486
|
+
* Resolve a pptxgenjs Coord (number or `${number}%`) to an absolute
|
|
487
|
+
* number using a reference dimension (e.g. group width for x / w,
|
|
488
|
+
* group height for y / h).
|
|
489
|
+
*
|
|
490
|
+
* - `5` → 5
|
|
491
|
+
* - `"50%"` → `0.5 * reference`
|
|
492
|
+
* - `undefined` / `null` → `fallback` (default 0)
|
|
493
|
+
*/
|
|
494
|
+
function resolveCoord(value, reference, fallback = 0) {
|
|
495
|
+
if (value == null)
|
|
496
|
+
return fallback;
|
|
497
|
+
if (typeof value === "number")
|
|
498
|
+
return value;
|
|
499
|
+
// `${number}%` — e.g. "50%"
|
|
500
|
+
const pct = parseFloat(value);
|
|
501
|
+
if (isNaN(pct))
|
|
502
|
+
return fallback;
|
|
503
|
+
if (!isFinite(pct))
|
|
504
|
+
return fallback;
|
|
505
|
+
return (pct / 100) * reference;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Resolve percentage coordinates against the current group's canvas.
|
|
509
|
+
* Inside a `<Group>`, child x/y stay group-relative (the group origin is
|
|
510
|
+
* applied by `p:grpSp`, not by adding an offset here). Direct slide
|
|
511
|
+
* children are returned unchanged so pptxgenjs can accept native `%` coords.
|
|
512
|
+
*/
|
|
513
|
+
function offsetXY(props) {
|
|
514
|
+
const { relative, width: grpW, height: grpH } = useGroupContext();
|
|
515
|
+
if (!relative)
|
|
516
|
+
return { ...props };
|
|
517
|
+
const resolvedW = resolveCoord(props.w, grpW, props.w);
|
|
518
|
+
const resolvedH = resolveCoord(props.h, grpH, props.h);
|
|
519
|
+
return {
|
|
520
|
+
...props,
|
|
521
|
+
x: resolveCoord(props.x, grpW, 0),
|
|
522
|
+
y: resolveCoord(props.y, grpH, 0),
|
|
523
|
+
w: resolvedW,
|
|
524
|
+
h: resolvedH,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
// ── Individual element renderers ──────────────────────────────────
|
|
528
|
+
async function renderText(node, target) {
|
|
529
|
+
const props = offsetXY(node.props);
|
|
530
|
+
const content = await resolveTextContent(node, props);
|
|
531
|
+
target.addText(content, mergeOptions(props.options, props));
|
|
532
|
+
}
|
|
533
|
+
function renderTextResolved(node, target) {
|
|
534
|
+
const props = offsetXY(node.props);
|
|
535
|
+
const content = textContentFromResolved(node, props);
|
|
536
|
+
target.addText(content, mergeOptions(props.options, props));
|
|
537
|
+
}
|
|
538
|
+
async function renderWordArt(node, slide) {
|
|
539
|
+
const props = offsetXY(node.props);
|
|
540
|
+
const content = await resolveTextContent(node, props);
|
|
541
|
+
slide.addWordArt(content, mergeOptions(props.options, props));
|
|
542
|
+
}
|
|
543
|
+
function renderWordArtResolved(node, slide) {
|
|
544
|
+
const props = offsetXY(node.props);
|
|
545
|
+
const content = textContentFromResolved(node, props);
|
|
546
|
+
slide.addWordArt(content, mergeOptions(props.options, props));
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Resolve the text content for a `<Text>` node following the documented
|
|
550
|
+
* input priority:
|
|
551
|
+
*
|
|
552
|
+
* 1. child `<TextRun />` nodes (normalized into pptxgenjs `TextProps[]`)
|
|
553
|
+
* 2. the `runs` prop (rich text array)
|
|
554
|
+
* 3. the `text` prop
|
|
555
|
+
* 4. plain-string children
|
|
556
|
+
*
|
|
557
|
+
* Mixing multiple modes drops the lower-priority content by design —
|
|
558
|
+
* prefer a single input mode per `<Text>`.
|
|
559
|
+
*/
|
|
560
|
+
async function resolveTextContent(node, props) {
|
|
561
|
+
const textRuns = await collectTextRuns(node);
|
|
562
|
+
if (textRuns)
|
|
563
|
+
return textRuns;
|
|
564
|
+
if (props.runs !== undefined)
|
|
565
|
+
return deepClone(props.runs);
|
|
566
|
+
if (props.text !== undefined)
|
|
567
|
+
return deepClone(props.text);
|
|
568
|
+
return getTextContent(node);
|
|
569
|
+
}
|
|
570
|
+
/** Like {@link resolveTextContent} but assumes children are already materialized. */
|
|
571
|
+
function textContentFromResolved(node, props) {
|
|
572
|
+
const textRuns = collectTextRunsFromChildren(node.children);
|
|
573
|
+
if (textRuns)
|
|
574
|
+
return textRuns;
|
|
575
|
+
if (props.runs !== undefined)
|
|
576
|
+
return deepClone(props.runs);
|
|
577
|
+
if (props.text !== undefined)
|
|
578
|
+
return deepClone(props.text);
|
|
579
|
+
return getTextContent(node);
|
|
580
|
+
}
|
|
581
|
+
function renderShape(node, target) {
|
|
582
|
+
const props = offsetXY(node.props);
|
|
583
|
+
target.addShape(props.shape, mergeOptions(props.options, props, ["shape"]));
|
|
584
|
+
}
|
|
585
|
+
function renderLine(node, target) {
|
|
586
|
+
const props = offsetXY(node.props);
|
|
587
|
+
target.addShape("line", mergeOptions(props.options, props));
|
|
588
|
+
}
|
|
589
|
+
function renderLineBetween(node, target) {
|
|
590
|
+
const { width: grpW, height: grpH } = useGroupContext();
|
|
591
|
+
const props = node.props;
|
|
592
|
+
const x1 = resolveCoord(props.x1, grpW, 0);
|
|
593
|
+
const y1 = resolveCoord(props.y1, grpH, 0);
|
|
594
|
+
const x2 = resolveCoord(props.x2, grpW, 0);
|
|
595
|
+
const y2 = resolveCoord(props.y2, grpH, 0);
|
|
596
|
+
const x = Math.min(x1, x2);
|
|
597
|
+
const y = Math.min(y1, y2);
|
|
598
|
+
const w = Math.abs(x2 - x1);
|
|
599
|
+
const h = Math.abs(y2 - y1);
|
|
600
|
+
target.addShape("line", {
|
|
601
|
+
...mergeOptions(props.options, props, ["x1", "y1", "x2", "y2"]),
|
|
602
|
+
x,
|
|
603
|
+
y,
|
|
604
|
+
w,
|
|
605
|
+
h,
|
|
606
|
+
flipH: x2 < x1,
|
|
607
|
+
flipV: y2 < y1,
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
function renderImage(node, target) {
|
|
611
|
+
const props = offsetXY(node.props);
|
|
612
|
+
target.addImage(mergeOptions(props.options, props, ["shape"]));
|
|
613
|
+
}
|
|
614
|
+
function renderMedia(node, slide) {
|
|
615
|
+
const props = offsetXY(node.props);
|
|
616
|
+
slide.addMedia(mergeOptions(props.options, props, ["shape"]));
|
|
617
|
+
}
|
|
618
|
+
function renderChart(node, slide) {
|
|
619
|
+
const props = offsetXY(node.props);
|
|
620
|
+
slide.addChart(props.type, deepClone(props.data), mergeOptions(props.options, props, ["type", "data", "shape"]));
|
|
621
|
+
}
|
|
622
|
+
async function renderTable(node, slide) {
|
|
623
|
+
const props = offsetXY(node.props);
|
|
624
|
+
const rows = props.rows ?? (await resolveTableRows(node));
|
|
625
|
+
slide.addTable(deepClone(rows), mergeOptions(props.options, props, ["shape"]));
|
|
626
|
+
}
|
|
627
|
+
async function resolveTableRows(node) {
|
|
628
|
+
const children = await resolveChildren(node);
|
|
629
|
+
const rows = [];
|
|
630
|
+
for (const child of children) {
|
|
631
|
+
if (isPptxNode(child) && child.type === "TableRow") {
|
|
632
|
+
rows.push(await extractTableRow(child));
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
return rows;
|
|
636
|
+
}
|
|
637
|
+
function processTableToSlides(node, ctx) {
|
|
638
|
+
const props = node.props;
|
|
639
|
+
ctx.pptx.tableToSlides(props.eleId, mergeOptions(props.options, props, ["eleId"]));
|
|
640
|
+
}
|
|
641
|
+
// ── Table helpers ─────────────────────────────────────────────────
|
|
642
|
+
async function extractTableRow(node) {
|
|
643
|
+
if (node.type !== "TableRow") {
|
|
644
|
+
throw new Error(`Table children must be TableRow nodes. Got ${node.type}.`);
|
|
645
|
+
}
|
|
646
|
+
const props = node.props;
|
|
647
|
+
if (props.cells)
|
|
648
|
+
return props.cells;
|
|
649
|
+
const children = await resolveChildren(node);
|
|
650
|
+
const cells = [];
|
|
651
|
+
for (const child of children) {
|
|
652
|
+
if (isPptxNode(child) && child.type === "TableCell") {
|
|
653
|
+
cells.push(extractTableCell(child));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return cells;
|
|
657
|
+
}
|
|
658
|
+
function extractTableCell(node) {
|
|
659
|
+
if (node.type !== "TableCell") {
|
|
660
|
+
throw new Error(`TableRow children must be TableCell nodes. Got ${node.type}.`);
|
|
661
|
+
}
|
|
662
|
+
const props = node.props;
|
|
663
|
+
return {
|
|
664
|
+
text: deepClone(props.text ?? getTextContent(node)),
|
|
665
|
+
options: mergeOptions(props.options, props),
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
// ── Master helpers ────────────────────────────────────────────────
|
|
669
|
+
function buildMaster(node) {
|
|
670
|
+
const props = node.props;
|
|
671
|
+
const { children, objects = [], ...rest } = props;
|
|
672
|
+
const childObjects = children.filter(isPptxNode).map(extractMasterObject);
|
|
673
|
+
return {
|
|
674
|
+
...rest,
|
|
675
|
+
objects: deepClone([...objects, ...childObjects]),
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
function extractMasterObject(node) {
|
|
679
|
+
assertLeafHasNoChildren(node.type, node.children);
|
|
680
|
+
if (SHAPE_TYPES.has(node.type)) {
|
|
681
|
+
const props = node.props;
|
|
682
|
+
const opts = mergeOptions(props.options, props, ["shape"]);
|
|
683
|
+
if (props.shape === "rect")
|
|
684
|
+
return { rect: opts };
|
|
685
|
+
if (props.shape === "line")
|
|
686
|
+
return { line: opts };
|
|
687
|
+
throw new Error("Master only supports rect and line shape objects directly. Use the objects prop or Raw for other master shapes.");
|
|
688
|
+
}
|
|
689
|
+
switch (node.type) {
|
|
690
|
+
case "Text": {
|
|
691
|
+
const props = node.props;
|
|
692
|
+
return {
|
|
693
|
+
text: {
|
|
694
|
+
text: getTextContent(node, typeof props.text === "string" ? props.text : undefined),
|
|
695
|
+
options: mergeOptions(props.options, props),
|
|
696
|
+
},
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
case "Image":
|
|
700
|
+
return { image: mergeOptions(node.props.options, node.props, ["shape"]) };
|
|
701
|
+
case "Line":
|
|
702
|
+
return { line: mergeOptions(node.props.options, node.props, ["shape"]) };
|
|
703
|
+
case "LineBetween": {
|
|
704
|
+
const props = node.props;
|
|
705
|
+
const x = Math.min(props.x1, props.x2);
|
|
706
|
+
const y = Math.min(props.y1, props.y2);
|
|
707
|
+
const w = Math.abs(props.x2 - props.x1);
|
|
708
|
+
const h = Math.abs(props.y2 - props.y1);
|
|
709
|
+
return {
|
|
710
|
+
line: {
|
|
711
|
+
...mergeOptions(props.options, props, ["x1", "y1", "x2", "y2"]),
|
|
712
|
+
x,
|
|
713
|
+
y,
|
|
714
|
+
w,
|
|
715
|
+
h,
|
|
716
|
+
flipH: props.x2 < props.x1,
|
|
717
|
+
flipV: props.y2 < props.y1,
|
|
718
|
+
},
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
case "Placeholder": {
|
|
722
|
+
const props = node.props;
|
|
723
|
+
return {
|
|
724
|
+
placeholder: {
|
|
725
|
+
options: props.options,
|
|
726
|
+
text: getTextContent(node, props.text),
|
|
727
|
+
},
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
default:
|
|
731
|
+
throw new Error(`${node.type} is not supported inside Master. Use the objects prop or Raw for advanced master content.`);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
// ── Text helpers ──────────────────────────────────────────────────
|
|
735
|
+
/**
|
|
736
|
+
* Resolve all PptxNodePromise children in a node's children array.
|
|
737
|
+
*
|
|
738
|
+
* With our deferred thunk approach, all non-string component factory calls
|
|
739
|
+
* return PptxNodePromise instances. Node children arrays may contain a mix
|
|
740
|
+
* of resolved PptxNodes and unresolved PptxNodePromises. This helper
|
|
741
|
+
* resolves all promises and returns a clean array of resolved PptxNodes
|
|
742
|
+
* (plus primitives).
|
|
743
|
+
*
|
|
744
|
+
* Must be called from an async function WITHIN a rendering context
|
|
745
|
+
* (slide / deck / group) so that thunk factories inherit the correct
|
|
746
|
+
* AsyncLocalStorage context.
|
|
747
|
+
*/
|
|
748
|
+
async function resolveChildren(node) {
|
|
749
|
+
const resolved = [];
|
|
750
|
+
for (const rawChild of node.children) {
|
|
751
|
+
resolved.push(await resolveChild(rawChild));
|
|
752
|
+
}
|
|
753
|
+
return resolved;
|
|
754
|
+
}
|
|
755
|
+
async function collectTextRuns(node) {
|
|
756
|
+
return collectTextRunsFromChildren(await resolveChildren(node));
|
|
757
|
+
}
|
|
758
|
+
function collectTextRunsFromChildren(children) {
|
|
759
|
+
const hasTextRun = children.some((c) => isPptxNode(c) && c.type === "TextRun");
|
|
760
|
+
if (!hasTextRun)
|
|
761
|
+
return undefined;
|
|
762
|
+
const runs = [];
|
|
763
|
+
for (const child of children) {
|
|
764
|
+
if (isPptxNode(child) && child.type === "TextRun") {
|
|
765
|
+
runs.push(extractTextRun(child));
|
|
766
|
+
}
|
|
767
|
+
else if (typeof child === "number" || (typeof child === "string" && child.trim() !== "")) {
|
|
768
|
+
runs.push({ text: String(child), options: {} });
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
return runs.length > 0 ? runs : undefined;
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Normalize a `<TextRun>` node into a pptxgenjs `TextProps` entry:
|
|
775
|
+
* `{ text, options }`.
|
|
776
|
+
*
|
|
777
|
+
* - `text` comes from the run's `text` prop, falling back to its string
|
|
778
|
+
* children (`<TextRun options={{...}}>Hello</TextRun>`).
|
|
779
|
+
* - `options` merges the run's `options` object with any top-level
|
|
780
|
+
* formatting props (`bold`, `fontSize`, `color`, ...).
|
|
781
|
+
*/
|
|
782
|
+
function extractTextRun(node) {
|
|
783
|
+
const props = node.props;
|
|
784
|
+
const text = props.text !== undefined ? props.text : getTextContent(node);
|
|
785
|
+
const options = mergeOptions(props.options, props);
|
|
786
|
+
// Always emit a `text` key: an empty run renders as an empty string
|
|
787
|
+
// instead of silently producing an option-less TextProps entry.
|
|
788
|
+
return { text: text !== undefined ? text : "", options };
|
|
789
|
+
}
|
|
790
|
+
function getTextContent(node, fallback) {
|
|
791
|
+
if (fallback !== undefined)
|
|
792
|
+
return fallback;
|
|
793
|
+
return node.children
|
|
794
|
+
.filter((c) => typeof c === "string" || typeof c === "number")
|
|
795
|
+
.map(String)
|
|
796
|
+
.join("");
|
|
797
|
+
}
|
|
798
|
+
// ── Props / options utilities ─────────────────────────────────────
|
|
799
|
+
/**
|
|
800
|
+
* Merge component top-level props with an `options` object.
|
|
801
|
+
* Top-level props that match pptxgenjs option keys override `options`.
|
|
802
|
+
* Reserved/structural props (children, options, etc.) are excluded.
|
|
803
|
+
*/
|
|
804
|
+
function mergeOptions(options, props, exclude = []) {
|
|
805
|
+
const result = deepClone(options ?? {});
|
|
806
|
+
const skip = new Set(exclude);
|
|
807
|
+
for (const [key, value] of Object.entries(props)) {
|
|
808
|
+
if (!RESERVED_PROPS.has(key) && !skip.has(key) && value !== undefined) {
|
|
809
|
+
result[key] = deepClone(value);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
return result;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Deep-clone plain objects and arrays.
|
|
816
|
+
* Preserves PptxGenJS class instances and other non-plain objects.
|
|
817
|
+
*/
|
|
818
|
+
function deepClone(value) {
|
|
819
|
+
if (Array.isArray(value)) {
|
|
820
|
+
return value.map((item) => deepClone(item));
|
|
821
|
+
}
|
|
822
|
+
if (value && typeof value === "object") {
|
|
823
|
+
const proto = Object.getPrototypeOf(value);
|
|
824
|
+
if (proto === Object.prototype || proto === null) {
|
|
825
|
+
const result = {};
|
|
826
|
+
for (const [key, val] of Object.entries(value)) {
|
|
827
|
+
result[key] = deepClone(val);
|
|
828
|
+
}
|
|
829
|
+
return result;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
return value;
|
|
833
|
+
}
|
|
834
|
+
function setIfDefined(obj, key, value) {
|
|
835
|
+
if (value !== undefined) {
|
|
836
|
+
obj[key] = value;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
// ── Valid child sets ──────────────────────────────────────────────
|
|
840
|
+
const DECK_CHILDREN = new Set([
|
|
841
|
+
"Layout",
|
|
842
|
+
"Master",
|
|
843
|
+
"Section",
|
|
844
|
+
"Slide",
|
|
845
|
+
"TableToSlides",
|
|
846
|
+
"Raw",
|
|
847
|
+
"Fragment",
|
|
848
|
+
]);
|
|
849
|
+
const SECTION_CHILDREN = new Set(["Slide", "Fragment"]);
|
|
850
|
+
const SLIDE_CHILDREN = new Set([
|
|
851
|
+
"Group",
|
|
852
|
+
"Text",
|
|
853
|
+
"Shape",
|
|
854
|
+
"Rect",
|
|
855
|
+
"RoundRect",
|
|
856
|
+
"Ellipse",
|
|
857
|
+
"Oval",
|
|
858
|
+
"Triangle",
|
|
859
|
+
"RightTriangle",
|
|
860
|
+
"Diamond",
|
|
861
|
+
"Pentagon",
|
|
862
|
+
"Hexagon",
|
|
863
|
+
"Star",
|
|
864
|
+
"Star4",
|
|
865
|
+
"Star5",
|
|
866
|
+
"Star6",
|
|
867
|
+
"Star8",
|
|
868
|
+
"Star10",
|
|
869
|
+
"Line",
|
|
870
|
+
"LineBetween",
|
|
871
|
+
"Arc",
|
|
872
|
+
"BlockArc",
|
|
873
|
+
"PieShape",
|
|
874
|
+
"CustomGeometry",
|
|
875
|
+
"LeftArrow",
|
|
876
|
+
"RightArrow",
|
|
877
|
+
"UpArrow",
|
|
878
|
+
"DownArrow",
|
|
879
|
+
"LeftRightArrow",
|
|
880
|
+
"UpDownArrow",
|
|
881
|
+
"Chevron",
|
|
882
|
+
"Cloud",
|
|
883
|
+
"Heart",
|
|
884
|
+
"Donut",
|
|
885
|
+
"Plus",
|
|
886
|
+
"Image",
|
|
887
|
+
"Media",
|
|
888
|
+
"Chart",
|
|
889
|
+
"AreaChart",
|
|
890
|
+
"BarChart",
|
|
891
|
+
"Bar3DChart",
|
|
892
|
+
"BubbleChart",
|
|
893
|
+
"DoughnutChart",
|
|
894
|
+
"LineChart",
|
|
895
|
+
"PieChart",
|
|
896
|
+
"RadarChart",
|
|
897
|
+
"ScatterChart",
|
|
898
|
+
"Table",
|
|
899
|
+
"Notes",
|
|
900
|
+
"WordArt",
|
|
901
|
+
"Raw",
|
|
902
|
+
"Fragment",
|
|
903
|
+
]);
|
|
904
|
+
const TABLE_CHILDREN = new Set(["TableRow", "Fragment"]);
|
|
905
|
+
const TABLE_ROW_CHILDREN = new Set(["TableCell", "Fragment"]);
|
|
906
|
+
function getValidChildTypes(type) {
|
|
907
|
+
switch (type) {
|
|
908
|
+
case "Deck":
|
|
909
|
+
return DECK_CHILDREN;
|
|
910
|
+
case "Section":
|
|
911
|
+
return SECTION_CHILDREN;
|
|
912
|
+
case "Slide":
|
|
913
|
+
return SLIDE_CHILDREN;
|
|
914
|
+
case "Table":
|
|
915
|
+
return TABLE_CHILDREN;
|
|
916
|
+
case "TableRow":
|
|
917
|
+
return TABLE_ROW_CHILDREN;
|
|
918
|
+
default:
|
|
919
|
+
return undefined;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
// ── Validation entry point ────────────────────────────────────────
|
|
923
|
+
/**
|
|
924
|
+
* Validate a Deck or Slide tree for common mistakes.
|
|
925
|
+
* Returns a list of issues (errors and warnings).
|
|
926
|
+
*
|
|
927
|
+
* Accepts both resolved PptxNode and deferred PptxNodePromise (which is
|
|
928
|
+
* resolved first). Only resolves **already-resolved** children during
|
|
929
|
+
* traversal — PptxNodePromise (deferred component thunks) are skipped
|
|
930
|
+
* because their factories may require rendering context (slide / deck /
|
|
931
|
+
* group) that is NOT available during validation.
|
|
932
|
+
*
|
|
933
|
+
* This means validation only checks the structural skeleton (Deck → Slide
|
|
934
|
+
* → intrinsic elements like Text, Rect, etc.) and skips any user-defined
|
|
935
|
+
* component boundaries until rendering.
|
|
936
|
+
*
|
|
937
|
+
* Leaf components (shapes, images, charts, ...) are reported as
|
|
938
|
+
* `child.leaf` errors when they carry already-resolved children. The
|
|
939
|
+
* render pipeline enforces the same rule authoritatively via
|
|
940
|
+
* `assertLeafHasNoChildren()`, so an empty issues array never means the
|
|
941
|
+
* whole tree was fully validated.
|
|
942
|
+
*/
|
|
943
|
+
export async function validateDeck(root) {
|
|
944
|
+
const node = await resolveChild(root);
|
|
945
|
+
const issues = [];
|
|
946
|
+
if (node.type !== "Deck" && node.type !== "Slide") {
|
|
947
|
+
issues.push({
|
|
948
|
+
code: "root.invalid",
|
|
949
|
+
level: "error",
|
|
950
|
+
path: node.type,
|
|
951
|
+
message: `Root must be Deck or Slide, got ${node.type}.`,
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
await walk(node, node.type);
|
|
955
|
+
return issues;
|
|
956
|
+
async function walk(node, path) {
|
|
957
|
+
validateLeafChildren(node, path, issues);
|
|
958
|
+
validateNodeProps(node, path, issues);
|
|
959
|
+
for (const rawChild of node.children) {
|
|
960
|
+
// Skip PptxNodePromise — component factories may need rendering
|
|
961
|
+
// context that is not available during validation. This means the
|
|
962
|
+
// validator only checks the structural skeleton plus already-resolved
|
|
963
|
+
// nodes; for ordinary JSX the render-time leaf assertion is the
|
|
964
|
+
// authoritative guard (see assertLeafHasNoChildren).
|
|
965
|
+
if (!isPptxNode(rawChild))
|
|
966
|
+
continue;
|
|
967
|
+
validateChildType(node, rawChild, `${path}/${rawChild.type}`, issues);
|
|
968
|
+
await walk(rawChild, `${path}/${rawChild.type}`);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Report leaf components that carry children (`child.leaf`).
|
|
974
|
+
*
|
|
975
|
+
* Only **already-resolved** child nodes are checked: deferred
|
|
976
|
+
* PptxNodePromise thunks (which is what ordinary JSX produces) are not
|
|
977
|
+
* executed during validation because their factories may require a
|
|
978
|
+
* rendering context. The render pipeline always enforces the same rule
|
|
979
|
+
* via `assertLeafHasNoChildren()`.
|
|
980
|
+
*/
|
|
981
|
+
function validateLeafChildren(parent, path, issues) {
|
|
982
|
+
if (!isLeafNodeType(parent.type))
|
|
983
|
+
return;
|
|
984
|
+
for (const child of parent.children) {
|
|
985
|
+
// Mirrors assertLeafHasNoChildren(): both node children AND smuggled
|
|
986
|
+
// primitives (hand-built trees) are rejected, keeping validation and
|
|
987
|
+
// rendering symmetric.
|
|
988
|
+
if (isPptxNode(child)) {
|
|
989
|
+
issues.push({
|
|
990
|
+
code: "child.leaf",
|
|
991
|
+
level: "error",
|
|
992
|
+
path: `${path}/${child.type}`,
|
|
993
|
+
message: `${child.type} is not a valid child of leaf component ${parent.type}.`,
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
else {
|
|
997
|
+
issues.push({
|
|
998
|
+
code: "child.leaf",
|
|
999
|
+
level: "error",
|
|
1000
|
+
path,
|
|
1001
|
+
message: `Leaf component ${parent.type} cannot contain text or expression children.`,
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
function validateChildType(parent, child, path, issues) {
|
|
1007
|
+
const validChildren = getValidChildTypes(parent.type);
|
|
1008
|
+
if (validChildren && !validChildren.has(child.type)) {
|
|
1009
|
+
issues.push({
|
|
1010
|
+
code: "child.invalid",
|
|
1011
|
+
level: "error",
|
|
1012
|
+
path,
|
|
1013
|
+
message: `${child.type} is not a valid child of ${parent.type}.`,
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
function validateNodeProps(node, path, issues) {
|
|
1018
|
+
const props = node.props;
|
|
1019
|
+
if (node.type === "RoundRect" && "angleRange" in props) {
|
|
1020
|
+
issues.push({
|
|
1021
|
+
code: "shape.prop.invalid",
|
|
1022
|
+
level: "warning",
|
|
1023
|
+
path,
|
|
1024
|
+
message: "RoundRect ignores angleRange; use Arc, PieShape, or BlockArc for angle ranges.",
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
if ((node.type === "Arc" || node.type === "PieShape") && "arcThicknessRatio" in props) {
|
|
1028
|
+
issues.push({
|
|
1029
|
+
code: "shape.prop.invalid",
|
|
1030
|
+
level: "warning",
|
|
1031
|
+
path,
|
|
1032
|
+
message: `${node.type} ignores arcThicknessRatio; use BlockArc for arc thickness.`,
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
if (node.type === "Text") {
|
|
1036
|
+
const ignored = findIgnoredTextInput(node, props);
|
|
1037
|
+
if (ignored.length > 0) {
|
|
1038
|
+
issues.push({
|
|
1039
|
+
code: "text.input.mixed",
|
|
1040
|
+
level: "warning",
|
|
1041
|
+
path,
|
|
1042
|
+
message: `Mixed Text input (${ignored.join(" and ")} ignored): content resolves by ` +
|
|
1043
|
+
"priority (<TextRun> children → runs → text → plain children). " +
|
|
1044
|
+
"Prefer <TextRun> children for rich text.",
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
if (node.type === "LineBetween") {
|
|
1049
|
+
for (const key of ["x1", "y1", "x2", "y2"]) {
|
|
1050
|
+
const val = props[key];
|
|
1051
|
+
if (val == null ||
|
|
1052
|
+
(typeof val !== "number" && typeof val !== "string") ||
|
|
1053
|
+
(typeof val === "number" && Number.isNaN(val))) {
|
|
1054
|
+
issues.push({
|
|
1055
|
+
code: "line.endpoint.invalid",
|
|
1056
|
+
level: "error",
|
|
1057
|
+
path,
|
|
1058
|
+
message: `LineBetween requires ${key} to be a Coord (number or percentage string).`,
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
if (node.type === "CustomGeometry" &&
|
|
1064
|
+
(!Array.isArray(props.points) || props.points.length === 0)) {
|
|
1065
|
+
issues.push({
|
|
1066
|
+
code: "shape.prop.missing",
|
|
1067
|
+
level: "error",
|
|
1068
|
+
path,
|
|
1069
|
+
message: "CustomGeometry requires a non-empty points array.",
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
if ((node.type === "Image" || node.type === "Media") &&
|
|
1073
|
+
!("path" in props) &&
|
|
1074
|
+
!("data" in props) &&
|
|
1075
|
+
!("link" in props)) {
|
|
1076
|
+
issues.push({
|
|
1077
|
+
code: "asset.source.missing",
|
|
1078
|
+
level: "warning",
|
|
1079
|
+
path,
|
|
1080
|
+
message: `${node.type} usually needs path, data, or link.`,
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Detect mixed `<Text>` inputs where a lower-priority source is silently
|
|
1086
|
+
* ignored by resolveTextContent().
|
|
1087
|
+
*
|
|
1088
|
+
* Every input mode is supported (priority: <TextRun> children → runs →
|
|
1089
|
+
* text → plain children), so mixing them is not an error — but it is
|
|
1090
|
+
* confusing, and <TextRun> children are the recommended way to express
|
|
1091
|
+
* rich text. Returns descriptions of the ignored sources (empty array
|
|
1092
|
+
* when nothing is ignored).
|
|
1093
|
+
*
|
|
1094
|
+
* Plain-string children mixed with <TextRun> children are NOT ignored —
|
|
1095
|
+
* collectTextRuns() converts them into default-style runs (documented
|
|
1096
|
+
* feature), so that combination is not reported.
|
|
1097
|
+
*/
|
|
1098
|
+
function findIgnoredTextInput(node, props) {
|
|
1099
|
+
const ignored = [];
|
|
1100
|
+
const hasRunChildren = node.children.some((c) => isPptxNode(c) && c.type === "TextRun");
|
|
1101
|
+
const hasPlainChildren = node.children.some((c) => typeof c === "number" || (typeof c === "string" && c.trim() !== ""));
|
|
1102
|
+
const hasRuns = props.runs !== undefined;
|
|
1103
|
+
const hasText = props.text !== undefined;
|
|
1104
|
+
if (hasRunChildren) {
|
|
1105
|
+
if (hasRuns)
|
|
1106
|
+
ignored.push("the `runs` prop");
|
|
1107
|
+
if (hasText)
|
|
1108
|
+
ignored.push("the `text` prop");
|
|
1109
|
+
return ignored;
|
|
1110
|
+
}
|
|
1111
|
+
if (hasRuns) {
|
|
1112
|
+
if (hasText)
|
|
1113
|
+
ignored.push("the `text` prop");
|
|
1114
|
+
if (hasPlainChildren)
|
|
1115
|
+
ignored.push("plain-string children");
|
|
1116
|
+
return ignored;
|
|
1117
|
+
}
|
|
1118
|
+
if (hasText && hasPlainChildren) {
|
|
1119
|
+
ignored.push("plain-string children");
|
|
1120
|
+
}
|
|
1121
|
+
return ignored;
|
|
1122
|
+
}
|
|
1123
|
+
/** Alias for backward compatibility. */
|
|
1124
|
+
export const validatePptxTree = validateDeck;
|
|
1125
|
+
//# sourceMappingURL=render.js.map
|