@cosmicdrift/kumiko-bundled-features 0.418.0 → 0.420.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.
Files changed (129) hide show
  1. package/dist/shared/index.d.ts +1 -0
  2. package/dist/shared/index.js +1 -0
  3. package/dist/shared/tenant-scoped-subject.d.ts +11 -0
  4. package/dist/shared/tenant-scoped-subject.js +18 -0
  5. package/dist/signature/feature.js +1 -1
  6. package/dist/signature/grant.d.ts +1 -1
  7. package/dist/signature/grant.js +7 -12
  8. package/dist/signature/png.js +35 -10
  9. package/dist/template-resolver/api.d.ts +2 -0
  10. package/dist/template-resolver/api.js +11 -7
  11. package/dist/template-resolver/compile-document-body.js +9 -11
  12. package/dist/template-resolver/compile-document-styles.d.ts +3 -0
  13. package/dist/template-resolver/compile-document-styles.js +122 -0
  14. package/dist/template-resolver/document-frames.d.ts +4 -0
  15. package/dist/template-resolver/document-frames.js +56 -0
  16. package/dist/template-resolver/document-op-blocks.d.ts +729 -0
  17. package/dist/template-resolver/document-op-blocks.js +521 -0
  18. package/dist/template-resolver/document-ops.d.ts +2743 -37
  19. package/dist/template-resolver/document-ops.js +128 -106
  20. package/dist/template-resolver/document-print-only.d.ts +11 -0
  21. package/dist/template-resolver/document-print-only.js +47 -0
  22. package/dist/template-resolver/document-sanitizer.d.ts +1 -1
  23. package/dist/template-resolver/document-sanitizer.js +29 -13
  24. package/dist/template-resolver/feature.d.ts +3 -0
  25. package/dist/template-resolver/feature.js +14 -1
  26. package/dist/template-resolver/handlers/apply-layout.write.d.ts +29 -0
  27. package/dist/template-resolver/handlers/apply-layout.write.js +118 -0
  28. package/dist/template-resolver/handlers/create-layout.write.d.ts +15 -0
  29. package/dist/template-resolver/handlers/create-layout.write.js +78 -0
  30. package/dist/template-resolver/handlers/document-base.d.ts +2 -2
  31. package/dist/template-resolver/handlers/document-blocks.query.d.ts +10 -3
  32. package/dist/template-resolver/handlers/document-blocks.query.js +13 -8
  33. package/dist/template-resolver/handlers/document-region.d.ts +19 -0
  34. package/dist/template-resolver/handlers/document-region.js +27 -0
  35. package/dist/template-resolver/handlers/failure-reason.d.ts +2 -0
  36. package/dist/template-resolver/handlers/failure-reason.js +12 -0
  37. package/dist/template-resolver/handlers/import-bundle-system.write.js +4 -0
  38. package/dist/template-resolver/handlers/layout-source.query.d.ts +3 -3
  39. package/dist/template-resolver/handlers/layout-source.query.js +6 -2
  40. package/dist/template-resolver/handlers/overview.query.d.ts +8 -0
  41. package/dist/template-resolver/handlers/overview.query.js +12 -6
  42. package/dist/template-resolver/handlers/patch-draft.write.d.ts +1369 -4
  43. package/dist/template-resolver/handlers/patch-draft.write.js +11 -6
  44. package/dist/template-resolver/handlers/publish-templates.write.d.ts +40 -0
  45. package/dist/template-resolver/handlers/publish-templates.write.js +61 -0
  46. package/dist/template-resolver/handlers/set.write.js +4 -1
  47. package/dist/template-resolver/handlers/template-write.js +7 -2
  48. package/dist/template-resolver/handlers/tenant-settings.d.ts +23 -0
  49. package/dist/template-resolver/handlers/tenant-settings.js +101 -0
  50. package/dist/template-resolver/handlers/toggle-status.write.d.ts +14 -9
  51. package/dist/template-resolver/handlers/toggle-status.write.js +11 -7
  52. package/dist/template-resolver/handlers/upsert-system.write.js +15 -9
  53. package/dist/template-resolver/handlers/usage.query.d.ts +14 -0
  54. package/dist/template-resolver/handlers/usage.query.js +80 -12
  55. package/dist/template-resolver/handlers/validate.query.d.ts +2 -0
  56. package/dist/template-resolver/handlers/validate.query.js +10 -0
  57. package/dist/template-resolver/i18n.js +3 -0
  58. package/dist/template-resolver/index.d.ts +7 -2
  59. package/dist/template-resolver/index.js +6 -2
  60. package/dist/template-resolver/layout-regions.d.ts +4 -5
  61. package/dist/template-resolver/layout-regions.js +38 -11
  62. package/dist/template-resolver/print-document.js +3 -1
  63. package/dist/template-resolver/seeding.js +4 -0
  64. package/dist/template-resolver/template-delivery-policy.d.ts +1 -0
  65. package/dist/template-resolver/template-delivery-policy.js +9 -1
  66. package/dist/template-resolver/template-edit-policy.d.ts +5 -1
  67. package/dist/template-resolver/template-edit-policy.js +29 -1
  68. package/dist/template-resolver/template-meta.d.ts +37 -2
  69. package/dist/template-resolver/template-meta.js +27 -5
  70. package/dist/template-resolver/template-preview.js +22 -10
  71. package/dist/template-resolver/template-validation.js +10 -3
  72. package/dist/template-resolver/web/constants.d.ts +7 -1
  73. package/dist/template-resolver/web/constants.js +21 -1
  74. package/dist/template-resolver/web/document-blocks.d.ts +1 -0
  75. package/dist/template-resolver/web/document-blocks.js +2 -0
  76. package/dist/template-resolver/web/document-editor-extensions.d.ts +9 -1
  77. package/dist/template-resolver/web/document-editor-extensions.js +69 -19
  78. package/dist/template-resolver/web/document-editor-fonts.d.ts +10 -0
  79. package/dist/template-resolver/web/document-editor-fonts.js +13 -0
  80. package/dist/template-resolver/web/document-editor.js +3 -1
  81. package/dist/template-resolver/web/document-fonts.d.ts +4 -0
  82. package/dist/template-resolver/web/document-fonts.js +18 -0
  83. package/dist/template-resolver/web/document-frame-control.d.ts +61 -0
  84. package/dist/template-resolver/web/document-frame-control.js +329 -0
  85. package/dist/template-resolver/web/document-frame-node-view.d.ts +4 -0
  86. package/dist/template-resolver/web/document-frame-node-view.js +173 -0
  87. package/dist/template-resolver/web/document-print-decorations.d.ts +12 -0
  88. package/dist/template-resolver/web/document-print-decorations.js +194 -0
  89. package/dist/template-resolver/web/document-print-defaults.d.ts +14 -0
  90. package/dist/template-resolver/web/document-print-defaults.js +25 -0
  91. package/dist/template-resolver/web/document-print-style.d.ts +150 -0
  92. package/dist/template-resolver/web/document-print-style.js +311 -0
  93. package/dist/template-resolver/web/document-repeat-strip.d.ts +8 -0
  94. package/dist/template-resolver/web/document-repeat-strip.js +54 -0
  95. package/dist/template-resolver/web/document-schema.d.ts +5 -0
  96. package/dist/template-resolver/web/document-schema.js +70 -19
  97. package/dist/template-resolver/web/document-style-css.d.ts +62 -0
  98. package/dist/template-resolver/web/document-style-css.js +140 -0
  99. package/dist/template-resolver/web/document-tiptap-bubble.js +83 -10
  100. package/dist/template-resolver/web/document-tiptap-content.d.ts +2 -1
  101. package/dist/template-resolver/web/document-tiptap-content.js +39 -4
  102. package/dist/template-resolver/web/document-tiptap-context.d.ts +6 -2
  103. package/dist/template-resolver/web/document-tiptap-context.js +2 -1
  104. package/dist/template-resolver/web/document-tiptap-editor.d.ts +1 -1
  105. package/dist/template-resolver/web/document-tiptap-editor.js +13 -5
  106. package/dist/template-resolver/web/document-tiptap-helpers.d.ts +62 -3
  107. package/dist/template-resolver/web/document-tiptap-helpers.js +199 -23
  108. package/dist/template-resolver/web/document-tiptap-toolbar-groups.d.ts +16 -0
  109. package/dist/template-resolver/web/document-tiptap-toolbar-groups.js +127 -6
  110. package/dist/template-resolver/web/document-tiptap-toolbar.js +57 -15
  111. package/dist/template-resolver/web/document-toolbar-blocks.d.ts +17 -0
  112. package/dist/template-resolver/web/i18n.js +21 -0
  113. package/dist/template-resolver/web/index.d.ts +8 -2
  114. package/dist/template-resolver/web/index.js +6 -1
  115. package/dist/template-resolver/web/pdf-constants.d.ts +8 -0
  116. package/dist/template-resolver/web/pdf-constants.js +9 -0
  117. package/dist/template-resolver/web/placeholder-text.d.ts +2 -1
  118. package/dist/template-resolver/web/placeholder-text.js +5 -2
  119. package/dist/template-resolver/web/qualified-names.d.ts +7 -0
  120. package/dist/template-resolver/web/qualified-names.js +6 -0
  121. package/dist/template-resolver/web/use-document-tiptap-model.d.ts +6 -2
  122. package/dist/template-resolver/web/use-document-tiptap-model.js +38 -3
  123. package/dist/tenant/company-config.d.ts +6 -0
  124. package/dist/tenant/company-config.js +41 -0
  125. package/dist/tenant/feature.js +11 -0
  126. package/dist/tenant/i18n.js +14 -0
  127. package/package.json +24 -11
  128. package/src/signature/changes.json +6 -0
  129. package/src/template-resolver/changes.json +62 -0
@@ -0,0 +1,521 @@
1
+ import * as z from "zod";
2
+ import { documentBlocksForKind, PDF_LAYOUT_DOCUMENT_BLOCKS, } from "./web/document-blocks.js";
3
+ import { DOCUMENT_FONTS } from "./web/document-fonts.js";
4
+ import { CELL_PADDING_MM_RANGE, COLWIDTH_PX_RANGE, DIVIDER_WEIGHT_PT_RANGE, DOCUMENT_IMAGE_ALIGNMENTS, DOCUMENT_TABLE_ALIGNMENTS, FRAME_MM_RANGE, FRAME_PRESETS, IMAGE_WIDTH_MM_RANGE, LINE_HEIGHT_RANGE, PARAGRAPH_SPACE_PT_RANGE, TABLE_WIDTH_MM_RANGE, TEXT_SIZE_PT_RANGE, } from "./web/document-print-style.js";
5
+ import { DOCUMENT_DATE_STYLES, DOCUMENT_TEXT_ALIGNMENTS, isDocumentPlaceholderHref, isDocumentReferenceName, isDocumentResourceKey, } from "./web/document-schema.js";
6
+ import { HEX_COLOR_PATTERN } from "./web/mail-layout-constants.js";
7
+ import { lineToContent, singleLine } from "./web/placeholder-text.js";
8
+ export const MAX_BODY_BLOCKS = 300;
9
+ export const DOCUMENT_BLOCK_INPUT_TYPES = [
10
+ "paragraph",
11
+ "heading",
12
+ "list",
13
+ "table",
14
+ "frame",
15
+ "image",
16
+ "button",
17
+ "divider",
18
+ "pageBreak",
19
+ ];
20
+ // Text blocks and lists exist in every kind; every other input type needs its block in the kind's set.
21
+ const REQUIRED_BLOCK = {
22
+ paragraph: null,
23
+ heading: null,
24
+ list: null,
25
+ table: "table",
26
+ frame: "frame",
27
+ image: "image",
28
+ button: "button",
29
+ divider: "divider",
30
+ pageBreak: "pageBreak",
31
+ };
32
+ // A frame sits on the page of a PDF body; layout regions and mails have none.
33
+ export function allowedBlocksFor(kind) {
34
+ if (kind === "pdf-layout")
35
+ return PDF_LAYOUT_DOCUMENT_BLOCKS;
36
+ const blocks = documentBlocksForKind(kind);
37
+ return kind === "document-pdf" ? [...blocks, "frame"] : blocks;
38
+ }
39
+ function isBlockTypeAllowed(type, kind) {
40
+ const required = REQUIRED_BLOCK[type];
41
+ return required === null || allowedBlocksFor(kind).includes(required);
42
+ }
43
+ export function patchIssue(path, code, params) {
44
+ const camel = code.replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
45
+ return {
46
+ path,
47
+ code,
48
+ i18nKey: `templateResolver.errors.patch.${camel}`,
49
+ ...(params === undefined ? {} : { params }),
50
+ };
51
+ }
52
+ const singleLineText = z.string().max(2000);
53
+ const hexColor = z.string().regex(HEX_COLOR_PATTERN, "must be a #rrggbb color");
54
+ const rangeNumber = (range) => z.number().min(range.min).max(range.max);
55
+ const alignSchema = z.enum(DOCUMENT_TEXT_ALIGNMENTS);
56
+ const textStyleSchema = z.object({
57
+ font: z.enum(DOCUMENT_FONTS).optional(),
58
+ sizePt: rangeNumber(TEXT_SIZE_PT_RANGE).optional(),
59
+ color: hexColor.optional(),
60
+ });
61
+ const runSchema = textStyleSchema.extend({
62
+ text: singleLineText,
63
+ bold: z.boolean().optional(),
64
+ italic: z.boolean().optional(),
65
+ underline: z.boolean().optional(),
66
+ dateStyle: z.enum(DOCUMENT_DATE_STYLES).optional(),
67
+ });
68
+ // A line break inside a paragraph or table cell; the lines around it keep their own formatting.
69
+ const hardBreakSchema = z.object({ hardBreak: z.literal(true) });
70
+ const runsSchema = z
71
+ .array(z.union([runSchema, hardBreakSchema]))
72
+ .min(1)
73
+ .max(50);
74
+ const blockStyleSchema = z.object({
75
+ lineHeight: rangeNumber(LINE_HEIGHT_RANGE).optional(),
76
+ spaceBeforePt: rangeNumber(PARAGRAPH_SPACE_PT_RANGE).optional(),
77
+ spaceAfterPt: rangeNumber(PARAGRAPH_SPACE_PT_RANGE).optional(),
78
+ });
79
+ const textFormatShape = {
80
+ align: alignSchema.optional(),
81
+ style: blockStyleSchema.optional(),
82
+ textStyle: textStyleSchema.optional(),
83
+ };
84
+ // Either text or runs; the pair is checked when the node is built, because a refinement would hide the shape from a tool schema.
85
+ const inlineShape = { text: singleLineText.optional(), runs: runsSchema.optional() };
86
+ const cellSchema = z.object({
87
+ ...inlineShape,
88
+ background: hexColor.optional(),
89
+ align: alignSchema.optional(),
90
+ });
91
+ // A rule over or under a row, e.g. the dark line under a table header or above a total.
92
+ const rowLineSchema = z.object({
93
+ color: hexColor,
94
+ weightPt: rangeNumber(DIVIDER_WEIGHT_PT_RANGE),
95
+ });
96
+ const rowSchema = z.object({
97
+ cells: z.array(cellSchema).min(1).max(20),
98
+ lineAbove: rowLineSchema.optional(),
99
+ lineBelow: rowLineSchema.optional(),
100
+ });
101
+ const paragraphSchema = z.object({
102
+ type: z.literal("paragraph"),
103
+ ...inlineShape,
104
+ ...textFormatShape,
105
+ });
106
+ const headingSchema = z.object({
107
+ type: z.literal("heading"),
108
+ level: z.number().int().min(1).max(3),
109
+ ...inlineShape,
110
+ ...textFormatShape,
111
+ });
112
+ const listSchema = z.object({
113
+ type: z.literal("list"),
114
+ ordered: z.boolean(),
115
+ items: z
116
+ .array(z.union([singleLineText, runsSchema]))
117
+ .min(1)
118
+ .max(100),
119
+ ...textFormatShape,
120
+ });
121
+ const tableSchema = z.object({
122
+ type: z.literal("table"),
123
+ borders: z.literal("none").optional(),
124
+ cellPaddingMm: rangeNumber(CELL_PADDING_MM_RANGE).optional(),
125
+ widthMm: rangeNumber(TABLE_WIDTH_MM_RANGE).optional(),
126
+ align: z.enum(DOCUMENT_TABLE_ALIGNMENTS).optional(),
127
+ columnWidthsPx: z.array(rangeNumber(COLWIDTH_PX_RANGE)).min(1).max(20).optional(),
128
+ header: rowSchema.optional(),
129
+ rows: z.array(rowSchema).max(100),
130
+ repeat: z.object({ each: z.string().min(1).max(100), row: rowSchema }).optional(),
131
+ rowsAfterRepeat: z.array(rowSchema).max(100).optional(),
132
+ });
133
+ const imageSchema = z.object({
134
+ type: z.literal("image"),
135
+ resource: z.string().min(1).max(200),
136
+ alt: z.string().max(500).optional(),
137
+ widthMm: rangeNumber(IMAGE_WIDTH_MM_RANGE).optional(),
138
+ align: z.enum(DOCUMENT_IMAGE_ALIGNMENTS).optional(),
139
+ bleed: z.boolean().optional(),
140
+ });
141
+ const dividerSchema = z.object({
142
+ type: z.literal("divider"),
143
+ color: hexColor.optional(),
144
+ weightPt: rangeNumber(DIVIDER_WEIGHT_PT_RANGE).optional(),
145
+ });
146
+ const frameSchema = z.object({
147
+ type: z.literal("frame"),
148
+ xMm: rangeNumber(FRAME_MM_RANGE),
149
+ yMm: rangeNumber(FRAME_MM_RANGE),
150
+ widthMm: rangeNumber(FRAME_MM_RANGE),
151
+ heightMm: rangeNumber(FRAME_MM_RANGE),
152
+ preset: z.enum(FRAME_PRESETS).optional(),
153
+ reserveFlow: z.boolean().optional(),
154
+ blocks: z
155
+ .array(z.discriminatedUnion("type", [
156
+ paragraphSchema,
157
+ headingSchema,
158
+ listSchema,
159
+ tableSchema,
160
+ imageSchema,
161
+ ]))
162
+ .min(1)
163
+ .max(30),
164
+ });
165
+ export const blockInputSchema = z.discriminatedUnion("type", [
166
+ paragraphSchema,
167
+ headingSchema,
168
+ listSchema,
169
+ tableSchema,
170
+ frameSchema,
171
+ imageSchema,
172
+ z.object({ type: z.literal("button"), text: singleLineText, href: z.string().max(300) }),
173
+ dividerSchema,
174
+ z.object({ type: z.literal("pageBreak") }),
175
+ ]);
176
+ export const blockFormatSchema = z.object(textFormatShape);
177
+ function report(ctx, path, code, params) {
178
+ ctx.issues.push(patchIssue(path, code, params));
179
+ }
180
+ export function countBlocks(blocks) {
181
+ return blocks.reduce((sum, block) => sum + 1 + (block.type === "frame" ? block.blocks.length : 0), 0);
182
+ }
183
+ function isTextStyleSet(style) {
184
+ return style.font !== undefined || style.sizePt !== undefined || style.color !== undefined;
185
+ }
186
+ function textStyleMarks(style) {
187
+ if (!isTextStyleSet(style))
188
+ return [];
189
+ return [
190
+ {
191
+ type: "documentTextStyle",
192
+ attrs: { color: style.color ?? null, font: style.font ?? null, sizePt: style.sizePt ?? null },
193
+ },
194
+ ];
195
+ }
196
+ function runMarks(run) {
197
+ return [
198
+ ...(run.bold === true ? [{ type: "bold" }] : []),
199
+ ...(run.italic === true ? [{ type: "italic" }] : []),
200
+ ...(run.underline === true ? [{ type: "underline" }] : []),
201
+ ];
202
+ }
203
+ function withMarks(nodes, marks) {
204
+ return marks.length === 0
205
+ ? nodes
206
+ : nodes.map((node) => ({ ...node, marks: [...(node.marks ?? []), ...marks] }));
207
+ }
208
+ function definedStyle(style) {
209
+ return {
210
+ ...(style.font === undefined ? {} : { font: style.font }),
211
+ ...(style.sizePt === undefined ? {} : { sizePt: style.sizePt }),
212
+ ...(style.color === undefined ? {} : { color: style.color }),
213
+ };
214
+ }
215
+ function inlineNodes(ctx, path, source, blockStyle) {
216
+ const { text, runs } = source;
217
+ if ((text === undefined) === (runs === undefined)) {
218
+ report(ctx, path, "text_or_runs_required");
219
+ return [];
220
+ }
221
+ const base = blockStyle === undefined ? {} : definedStyle(blockStyle);
222
+ if (runs === undefined) {
223
+ return withMarks(lineToContent(singleLine(text ?? "")), textStyleMarks(base));
224
+ }
225
+ return runs.flatMap((run) => "hardBreak" in run
226
+ ? [{ type: "hardBreak" }]
227
+ : withMarks(lineToContent(singleLine(run.text), run.dateStyle), [
228
+ ...runMarks(run),
229
+ ...textStyleMarks({ ...base, ...definedStyle(run) }),
230
+ ]));
231
+ }
232
+ function formatAttributes(ctx, path, format) {
233
+ if (format.align !== undefined && !allowedBlocksFor(ctx.kind).includes("align")) {
234
+ report(ctx, `${path}.align`, "block_not_allowed", { type: "align", kind: ctx.kind });
235
+ }
236
+ const { style } = format;
237
+ return {
238
+ ...(format.align === undefined ? {} : { textAlign: format.align }),
239
+ ...(style?.lineHeight === undefined ? {} : { lineHeight: style.lineHeight }),
240
+ ...(style?.spaceBeforePt === undefined ? {} : { spaceBeforePt: style.spaceBeforePt }),
241
+ ...(style?.spaceAfterPt === undefined ? {} : { spaceAfterPt: style.spaceAfterPt }),
242
+ };
243
+ }
244
+ function textBlockNode(ctx, path, type, source, format, level) {
245
+ const attrs = {
246
+ ...(level === undefined ? {} : { level }),
247
+ ...formatAttributes(ctx, path, format),
248
+ };
249
+ return {
250
+ type,
251
+ ...(Object.keys(attrs).length === 0 ? {} : { attrs }),
252
+ content: inlineNodes(ctx, path, source, format.textStyle),
253
+ };
254
+ }
255
+ function listNode(ctx, path, block) {
256
+ return {
257
+ type: block.ordered ? "orderedList" : "bulletList",
258
+ content: block.items.map((item, index) => {
259
+ const source = typeof item === "string" ? { text: item } : { runs: item };
260
+ return {
261
+ type: "listItem",
262
+ content: [textBlockNode(ctx, `${path}.items.${index}`, "paragraph", source, block)],
263
+ };
264
+ }),
265
+ };
266
+ }
267
+ function rowNode(ctx, path, row, options) {
268
+ if (options.widths !== undefined && options.widths.length !== row.cells.length) {
269
+ report(ctx, `${path}.cells`, "invalid_table", {
270
+ problem: "columnWidthsPx must match the cells per row",
271
+ });
272
+ }
273
+ const attrs = {
274
+ ...options.attrs,
275
+ ...(row.lineAbove === undefined ? {} : { lineAbove: row.lineAbove }),
276
+ ...(row.lineBelow === undefined ? {} : { lineBelow: row.lineBelow }),
277
+ };
278
+ return {
279
+ type: "tableRow",
280
+ ...(Object.keys(attrs).length === 0 ? {} : { attrs }),
281
+ content: row.cells.map((cell, index) => {
282
+ const width = options.widths?.[index];
283
+ const attrs = {
284
+ ...(cell.background === undefined ? {} : { background: cell.background }),
285
+ ...(width === undefined ? {} : { colwidth: [width] }),
286
+ };
287
+ const cellPath = `${path}.cells.${index}`;
288
+ return {
289
+ type: options.header ? "tableHeader" : "tableCell",
290
+ ...(Object.keys(attrs).length === 0 ? {} : { attrs }),
291
+ content: [textBlockNode(ctx, cellPath, "paragraph", cell, { align: cell.align })],
292
+ };
293
+ }),
294
+ };
295
+ }
296
+ function reportInvalidRepeat(ctx, path, repeat) {
297
+ if (repeat === undefined)
298
+ return;
299
+ if (!isDocumentReferenceName(repeat.each)) {
300
+ report(ctx, `${path}.repeat.each`, "invalid_repeat_variable", { each: repeat.each });
301
+ }
302
+ if (!allowedBlocksFor(ctx.kind).includes("repeat")) {
303
+ report(ctx, `${path}.repeat`, "block_not_allowed", { type: "repeat", kind: ctx.kind });
304
+ }
305
+ }
306
+ function tableAttrsOf(block) {
307
+ return {
308
+ ...(block.borders === undefined ? {} : { borders: block.borders }),
309
+ ...(block.cellPaddingMm === undefined ? {} : { cellPaddingMm: block.cellPaddingMm }),
310
+ ...(block.widthMm === undefined ? {} : { widthMm: block.widthMm }),
311
+ ...(block.align === undefined ? {} : { align: block.align }),
312
+ };
313
+ }
314
+ function tableNode(ctx, path, block) {
315
+ const widths = block.columnWidthsPx;
316
+ const { header, repeat, rowsAfterRepeat } = block;
317
+ if (rowsAfterRepeat !== undefined && repeat === undefined) {
318
+ report(ctx, `${path}.rowsAfterRepeat`, "invalid_table", {
319
+ problem: "rowsAfterRepeat needs a repeat row to follow",
320
+ });
321
+ }
322
+ reportInvalidRepeat(ctx, path, repeat);
323
+ const rows = [
324
+ ...(header === undefined
325
+ ? []
326
+ : [
327
+ rowNode(ctx, `${path}.header`, header, {
328
+ header: true,
329
+ widths,
330
+ attrs: { repeatHeader: true },
331
+ }),
332
+ ]),
333
+ ...block.rows.map((row, index) => rowNode(ctx, `${path}.rows.${index}`, row, { header: false, widths })),
334
+ ...(repeat === undefined
335
+ ? []
336
+ : [
337
+ rowNode(ctx, `${path}.repeat.row`, repeat.row, {
338
+ header: false,
339
+ widths,
340
+ attrs: { each: repeat.each },
341
+ }),
342
+ ]),
343
+ ...(rowsAfterRepeat ?? []).map((row, index) => rowNode(ctx, `${path}.rowsAfterRepeat.${index}`, row, { header: false, widths })),
344
+ ];
345
+ const cellCounts = new Set(rows.map((row) => row.content?.length ?? 0));
346
+ if (rows.length === 0 || cellCounts.size > 1) {
347
+ report(ctx, path, "invalid_table", {
348
+ problem: "needs a row and the same number of cells in every row",
349
+ });
350
+ }
351
+ const tableAttrs = tableAttrsOf(block);
352
+ return {
353
+ type: "table",
354
+ ...(Object.keys(tableAttrs).length === 0 ? {} : { attrs: tableAttrs }),
355
+ content: rows,
356
+ };
357
+ }
358
+ function frameNode(ctx, path, input) {
359
+ return {
360
+ type: "documentFrame",
361
+ attrs: {
362
+ xMm: input.xMm,
363
+ yMm: input.yMm,
364
+ widthMm: input.widthMm,
365
+ heightMm: input.heightMm,
366
+ preset: input.preset ?? null,
367
+ reserveFlow: input.reserveFlow ?? true,
368
+ },
369
+ content: input.blocks.map((inner, index) => blockNode(ctx, `${path}.blocks.${index}`, inner)),
370
+ };
371
+ }
372
+ // Only the header and footer print outside the page margins, so only they can reach the paper edge.
373
+ function isBleedRegion(region) {
374
+ return region === "header" || region === "footer";
375
+ }
376
+ function dividerNode(input) {
377
+ const attrs = {
378
+ ...(input.color === undefined ? {} : { color: input.color }),
379
+ ...(input.weightPt === undefined ? {} : { weightPt: input.weightPt }),
380
+ };
381
+ return { type: "divider", ...(Object.keys(attrs).length === 0 ? {} : { attrs }) };
382
+ }
383
+ function imageNode(ctx, path, input) {
384
+ if (!isDocumentResourceKey(input.resource)) {
385
+ report(ctx, `${path}.resource`, "invalid_resource_key", { resource: input.resource });
386
+ }
387
+ if (input.bleed === true && !(ctx.kind === "pdf-layout" && isBleedRegion(ctx.region))) {
388
+ report(ctx, `${path}.bleed`, "bleed_not_allowed", { kind: ctx.kind });
389
+ }
390
+ return {
391
+ type: "resource",
392
+ attrs: {
393
+ resource: input.resource,
394
+ alt: input.alt ?? null,
395
+ ...(input.widthMm === undefined ? {} : { widthMm: input.widthMm }),
396
+ ...(input.align === undefined ? {} : { align: input.align }),
397
+ ...(input.bleed === true ? { bleed: true } : {}),
398
+ },
399
+ };
400
+ }
401
+ function buttonNode(ctx, path, input) {
402
+ if (!isDocumentPlaceholderHref(input.href)) {
403
+ report(ctx, `${path}.href`, "invalid_button_href", { href: input.href });
404
+ }
405
+ return {
406
+ type: "button",
407
+ attrs: { href: input.href },
408
+ content: lineToContent(singleLine(input.text)),
409
+ };
410
+ }
411
+ export function blockNode(ctx, path, input) {
412
+ if (!isBlockTypeAllowed(input.type, ctx.kind)) {
413
+ report(ctx, `${path}.type`, "block_not_allowed", { type: input.type, kind: ctx.kind });
414
+ }
415
+ switch (input.type) {
416
+ case "paragraph":
417
+ return textBlockNode(ctx, path, "paragraph", input, input);
418
+ case "heading":
419
+ return textBlockNode(ctx, path, "heading", input, input, input.level);
420
+ case "list":
421
+ return listNode(ctx, path, input);
422
+ case "table":
423
+ return tableNode(ctx, path, input);
424
+ case "frame":
425
+ return frameNode(ctx, path, input);
426
+ case "image":
427
+ return imageNode(ctx, path, input);
428
+ case "button":
429
+ return buttonNode(ctx, path, input);
430
+ case "divider":
431
+ return dividerNode(input);
432
+ case "pageBreak":
433
+ return { type: input.type };
434
+ }
435
+ }
436
+ const PRINT_ATTRIBUTES = [
437
+ "lineHeight",
438
+ "spaceBeforePt",
439
+ "spaceAfterPt",
440
+ "background",
441
+ "colwidth",
442
+ "borders",
443
+ "cellPaddingMm",
444
+ "lineAbove",
445
+ "lineBelow",
446
+ ];
447
+ function isPresent(value) {
448
+ return value !== undefined && value !== null;
449
+ }
450
+ // An image's bleed defaults to false, so a present attribute alone says nothing there.
451
+ function hasPrintOnlyBoxAttributes(node) {
452
+ const attrs = node.attrs;
453
+ if (node.type === "resource") {
454
+ return (attrs?.["bleed"] === true || isPresent(attrs?.["widthMm"]) || isPresent(attrs?.["align"]));
455
+ }
456
+ if (node.type === "table")
457
+ return isPresent(attrs?.["widthMm"]) || isPresent(attrs?.["align"]);
458
+ return node.type === "divider" && (isPresent(attrs?.["color"]) || isPresent(attrs?.["weightPt"]));
459
+ }
460
+ // Frames are refused as blocks, so only the styling that would ride along in other blocks counts here.
461
+ export function usesPrintStyling(node) {
462
+ if (node.marks?.some((mark) => mark.type === "documentTextStyle"))
463
+ return true;
464
+ if (PRINT_ATTRIBUTES.some((name) => isPresent(node.attrs?.[name])))
465
+ return true;
466
+ if (hasPrintOnlyBoxAttributes(node))
467
+ return true;
468
+ return (node.content ?? []).some(usesPrintStyling);
469
+ }
470
+ // Only a PDF applies print styling; a mail would keep it through the rebuild and the sanitizer refuses it on write.
471
+ export function refusePrintStyling(kind, node, path) {
472
+ return kind === "mail" && usesPrintStyling(node)
473
+ ? patchIssue(path, "print_style_not_allowed", { kind })
474
+ : null;
475
+ }
476
+ const TEXT_BLOCK_NODE_TYPES = new Set(["paragraph", "heading"]);
477
+ const INLINE_NODE_TYPES = new Set(["text", "variable"]);
478
+ function mergeTextStyleMark(node, style) {
479
+ const existing = node.marks?.find((mark) => mark.type === "documentTextStyle");
480
+ const merged = {
481
+ color: null,
482
+ font: null,
483
+ sizePt: null,
484
+ ...existing?.attrs,
485
+ ...definedStyle(style),
486
+ };
487
+ return {
488
+ ...node,
489
+ marks: [
490
+ ...(node.marks ?? []).filter((mark) => mark.type !== "documentTextStyle"),
491
+ { type: "documentTextStyle", attrs: merged },
492
+ ],
493
+ };
494
+ }
495
+ function applyTextStyle(node, style) {
496
+ if (INLINE_NODE_TYPES.has(node.type ?? ""))
497
+ return mergeTextStyleMark(node, style);
498
+ return node.content === undefined
499
+ ? node
500
+ : { ...node, content: node.content.map((child) => applyTextStyle(child, style)) };
501
+ }
502
+ function applyBlockAttributes(node, attrs) {
503
+ if (TEXT_BLOCK_NODE_TYPES.has(node.type ?? "")) {
504
+ return { ...node, attrs: { ...node.attrs, ...attrs } };
505
+ }
506
+ return node.content === undefined
507
+ ? node
508
+ : { ...node, content: node.content.map((child) => applyBlockAttributes(child, attrs)) };
509
+ }
510
+ export const FORMATTABLE_NODE_TYPES = new Set([
511
+ "paragraph",
512
+ "heading",
513
+ "bulletList",
514
+ "orderedList",
515
+ ]);
516
+ // Applies the given format to a paragraph, heading or list, keeping everything it does not mention.
517
+ export function formatNode(ctx, path, node, format) {
518
+ const attrs = formatAttributes(ctx, path, format);
519
+ const withAttrs = Object.keys(attrs).length === 0 ? node : applyBlockAttributes(node, attrs);
520
+ return format.textStyle === undefined ? withAttrs : applyTextStyle(withAttrs, format.textStyle);
521
+ }