@cosmicdrift/kumiko-samples 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.
- package/package.json +1 -1
- package/packages/bundled-features/package.json +11 -1
- package/packages/bundled-features/src/shared/index.ts +6 -0
- package/packages/bundled-features/src/shared/tenant-scoped-subject.ts +28 -0
- package/packages/bundled-features/src/signature/changes.json +6 -0
- package/packages/bundled-features/src/signature/feature.ts +1 -1
- package/packages/bundled-features/src/signature/grant.ts +13 -14
- package/packages/bundled-features/src/signature/png.ts +33 -12
- package/packages/bundled-features/src/template-resolver/api.ts +17 -7
- package/packages/bundled-features/src/template-resolver/changes.json +62 -0
- package/packages/bundled-features/src/template-resolver/compile-document-body.ts +10 -9
- package/packages/bundled-features/src/template-resolver/compile-document-styles.ts +170 -0
- package/packages/bundled-features/src/template-resolver/document-frames.ts +68 -0
- package/packages/bundled-features/src/template-resolver/document-op-blocks.ts +682 -0
- package/packages/bundled-features/src/template-resolver/document-ops.ts +170 -128
- package/packages/bundled-features/src/template-resolver/document-print-only.ts +65 -0
- package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +53 -19
- package/packages/bundled-features/src/template-resolver/feature.ts +15 -0
- package/packages/bundled-features/src/template-resolver/handlers/apply-layout.write.ts +137 -0
- package/packages/bundled-features/src/template-resolver/handlers/create-layout.write.ts +87 -0
- package/packages/bundled-features/src/template-resolver/handlers/document-base.ts +2 -2
- package/packages/bundled-features/src/template-resolver/handlers/document-blocks.query.ts +13 -8
- package/packages/bundled-features/src/template-resolver/handlers/document-region.ts +47 -0
- package/packages/bundled-features/src/template-resolver/handlers/failure-reason.ts +13 -0
- package/packages/bundled-features/src/template-resolver/handlers/import-bundle-system.write.ts +3 -0
- package/packages/bundled-features/src/template-resolver/handlers/layout-source.query.ts +17 -5
- package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +16 -4
- package/packages/bundled-features/src/template-resolver/handlers/patch-draft.write.ts +20 -6
- package/packages/bundled-features/src/template-resolver/handlers/publish-templates.write.ts +69 -0
- package/packages/bundled-features/src/template-resolver/handlers/set.write.ts +7 -0
- package/packages/bundled-features/src/template-resolver/handlers/template-write.ts +15 -3
- package/packages/bundled-features/src/template-resolver/handlers/tenant-settings.ts +159 -0
- package/packages/bundled-features/src/template-resolver/handlers/toggle-status.write.ts +25 -10
- package/packages/bundled-features/src/template-resolver/handlers/upsert-system.write.ts +7 -3
- package/packages/bundled-features/src/template-resolver/handlers/usage.query.ts +131 -18
- package/packages/bundled-features/src/template-resolver/handlers/validate.query.ts +10 -0
- package/packages/bundled-features/src/template-resolver/i18n.ts +3 -0
- package/packages/bundled-features/src/template-resolver/index.ts +39 -0
- package/packages/bundled-features/src/template-resolver/layout-regions.ts +54 -16
- package/packages/bundled-features/src/template-resolver/print-document.ts +3 -1
- package/packages/bundled-features/src/template-resolver/seeding.ts +4 -0
- package/packages/bundled-features/src/template-resolver/template-delivery-policy.ts +11 -1
- package/packages/bundled-features/src/template-resolver/template-edit-policy.ts +37 -1
- package/packages/bundled-features/src/template-resolver/template-meta.ts +31 -6
- package/packages/bundled-features/src/template-resolver/template-preview.ts +29 -9
- package/packages/bundled-features/src/template-resolver/template-validation.ts +10 -3
- package/packages/bundled-features/src/template-resolver/web/constants.ts +33 -0
- package/packages/bundled-features/src/template-resolver/web/document-blocks.ts +5 -0
- package/packages/bundled-features/src/template-resolver/web/document-editor-extensions.ts +93 -25
- package/packages/bundled-features/src/template-resolver/web/document-editor-fonts.ts +13 -0
- package/packages/bundled-features/src/template-resolver/web/document-editor.tsx +4 -1
- package/packages/bundled-features/src/template-resolver/web/document-fonts.ts +21 -0
- package/packages/bundled-features/src/template-resolver/web/document-frame-control.ts +428 -0
- package/packages/bundled-features/src/template-resolver/web/document-frame-node-view.tsx +207 -0
- package/packages/bundled-features/src/template-resolver/web/document-print-decorations.ts +246 -0
- package/packages/bundled-features/src/template-resolver/web/document-print-defaults.ts +59 -0
- package/packages/bundled-features/src/template-resolver/web/document-print-style.ts +381 -0
- package/packages/bundled-features/src/template-resolver/web/document-repeat-strip.ts +80 -0
- package/packages/bundled-features/src/template-resolver/web/document-schema.ts +87 -19
- package/packages/bundled-features/src/template-resolver/web/document-style-css.ts +214 -0
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-bubble.tsx +185 -16
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-content.tsx +70 -7
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-context.ts +13 -1
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-editor.tsx +25 -4
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-helpers.ts +276 -23
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar-groups.tsx +449 -10
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar.tsx +108 -17
- package/packages/bundled-features/src/template-resolver/web/document-toolbar-blocks.ts +20 -0
- package/packages/bundled-features/src/template-resolver/web/fontsource-css.d.ts +2 -0
- package/packages/bundled-features/src/template-resolver/web/i18n.ts +32 -0
- package/packages/bundled-features/src/template-resolver/web/index.ts +38 -0
- package/packages/bundled-features/src/template-resolver/web/pdf-constants.ts +16 -0
- package/packages/bundled-features/src/template-resolver/web/placeholder-text.ts +6 -3
- package/packages/bundled-features/src/template-resolver/web/qualified-names.ts +7 -0
- package/packages/bundled-features/src/template-resolver/web/use-document-tiptap-model.ts +74 -5
- package/packages/bundled-features/src/tenant/company-config.ts +48 -0
- package/packages/bundled-features/src/tenant/feature.ts +11 -0
- package/packages/bundled-features/src/tenant/i18n.ts +14 -0
- package/samples/apps/ui-walkthrough/src/app/auth-constants.ts +51 -0
- package/samples/apps/ui-walkthrough/src/app/seed-print-editor-tenants.ts +118 -0
- package/samples/apps/ui-walkthrough/src/app/seed-templates.ts +2 -0
- package/samples/apps/ui-walkthrough/src/app/server.ts +9 -1
- package/samples/apps/ui-walkthrough/src/app/template-seed-data.ts +27 -1
- package/samples/apps/ui-walkthrough/src/features/rental/i18n.ts +8 -0
- package/samples/apps/ui-walkthrough/src/features/rental/screens.ts +1 -0
- package/samples/apps/ui-walkthrough/src/run-config.ts +1 -0
- package/samples/apps/use-all-bundled/feature-manifest.json +236 -0
- package/samples/recipes/pdf-letterhead/README.md +23 -0
- package/samples/recipes/pdf-letterhead/package.json +12 -0
- package/samples/recipes/pdf-letterhead/src/letterhead.ts +92 -0
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// Print-only styling of the document model: character and paragraph styles, absolutely placed
|
|
2
|
+
// frames, table borders and cell backgrounds. Serialized as data-* attributes (never style) so
|
|
3
|
+
// the sanitizer keeps its guarantee that no CSS comes from the input; compile-document-styles.ts
|
|
4
|
+
// turns the validated values into CSS. The read* functions are the single place that validates a
|
|
5
|
+
// raw attribute, used by the tiptap parse rules here and by the compile step.
|
|
6
|
+
import { isOneOf, isPlainObject } from "@cosmicdrift/kumiko-types/type-guards";
|
|
7
|
+
import { Extension, Mark, mergeAttributes, Node } from "@tiptap/core";
|
|
8
|
+
import { DOCUMENT_FONTS, type DocumentFont } from "./document-fonts.ts";
|
|
9
|
+
import type {
|
|
10
|
+
DocumentBlockStyle,
|
|
11
|
+
DocumentFrameBox,
|
|
12
|
+
DocumentRowLine,
|
|
13
|
+
DocumentTextStyle,
|
|
14
|
+
} from "./document-style-css.ts";
|
|
15
|
+
import { HEX_COLOR_PATTERN } from "./mail-layout-constants.ts";
|
|
16
|
+
|
|
17
|
+
export const TEXT_SIZE_PT_RANGE = { min: 4, max: 72 } as const;
|
|
18
|
+
export const LINE_HEIGHT_RANGE = { min: 0.8, max: 3 } as const;
|
|
19
|
+
export const PARAGRAPH_SPACE_PT_RANGE = { min: 0, max: 200 } as const;
|
|
20
|
+
export const FRAME_MM_RANGE = { min: 0, max: 1000 } as const;
|
|
21
|
+
export const COLWIDTH_PX_RANGE = { min: 1, max: 5000 } as const;
|
|
22
|
+
export const IMAGE_WIDTH_MM_RANGE = { min: 0.1, max: 210 } as const;
|
|
23
|
+
export const DIVIDER_WEIGHT_PT_RANGE = { min: 0.25, max: 4 } as const;
|
|
24
|
+
export const CELL_PADDING_MM_RANGE = { min: 0, max: 10 } as const;
|
|
25
|
+
export const TABLE_WIDTH_MM_RANGE = IMAGE_WIDTH_MM_RANGE;
|
|
26
|
+
|
|
27
|
+
export const DOCUMENT_IMAGE_ALIGNMENTS = ["left", "center", "right"] as const;
|
|
28
|
+
export type DocumentImageAlignment = (typeof DOCUMENT_IMAGE_ALIGNMENTS)[number];
|
|
29
|
+
|
|
30
|
+
export const DOCUMENT_TABLE_ALIGNMENTS = DOCUMENT_IMAGE_ALIGNMENTS;
|
|
31
|
+
export type DocumentTableAlignment = DocumentImageAlignment;
|
|
32
|
+
|
|
33
|
+
export const FRAME_PRESETS = ["address", "text"] as const;
|
|
34
|
+
export type DocumentFramePreset = (typeof FRAME_PRESETS)[number];
|
|
35
|
+
|
|
36
|
+
type AttributeSource = { getAttribute(name: string): string | null };
|
|
37
|
+
|
|
38
|
+
type Range = { readonly min: number; readonly max: number };
|
|
39
|
+
|
|
40
|
+
const PLAIN_DECIMAL = /^\d+(\.\d+)?$/;
|
|
41
|
+
|
|
42
|
+
function readNumber(raw: string | null, range: Range): number | null {
|
|
43
|
+
if (raw === null || !PLAIN_DECIMAL.test(raw)) return null;
|
|
44
|
+
const value = Number(raw);
|
|
45
|
+
return Number.isFinite(value) && value >= range.min && value <= range.max ? value : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function readHexColor(raw: string | null): string | null {
|
|
49
|
+
return raw !== null && HEX_COLOR_PATTERN.test(raw) ? raw : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function readFont(raw: string | null): DocumentFont | null {
|
|
53
|
+
return isOneOf(DOCUMENT_FONTS, raw) ? raw : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function readTextStyleAttributes(element: AttributeSource): Required<DocumentTextStyle> {
|
|
57
|
+
return {
|
|
58
|
+
color: readHexColor(element.getAttribute("data-color")),
|
|
59
|
+
font: readFont(element.getAttribute("data-font")),
|
|
60
|
+
sizePt: readNumber(element.getAttribute("data-size-pt"), TEXT_SIZE_PT_RANGE),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function readBlockStyleAttributes(element: AttributeSource): Required<DocumentBlockStyle> {
|
|
65
|
+
return {
|
|
66
|
+
lineHeight: readNumber(element.getAttribute("data-line-height"), LINE_HEIGHT_RANGE),
|
|
67
|
+
spaceBeforePt: readNumber(element.getAttribute("data-space-before"), PARAGRAPH_SPACE_PT_RANGE),
|
|
68
|
+
spaceAfterPt: readNumber(element.getAttribute("data-space-after"), PARAGRAPH_SPACE_PT_RANGE),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type DocumentFrameAttributes = DocumentFrameBox & {
|
|
73
|
+
readonly preset: DocumentFramePreset | null;
|
|
74
|
+
readonly reserveFlow: boolean;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export function readFrameAttributes(element: AttributeSource): DocumentFrameAttributes | null {
|
|
78
|
+
const xMm = readNumber(element.getAttribute("data-x"), FRAME_MM_RANGE);
|
|
79
|
+
const yMm = readNumber(element.getAttribute("data-y"), FRAME_MM_RANGE);
|
|
80
|
+
const widthMm = readNumber(element.getAttribute("data-width"), FRAME_MM_RANGE);
|
|
81
|
+
const heightMm = readNumber(element.getAttribute("data-height"), FRAME_MM_RANGE);
|
|
82
|
+
if (xMm === null || yMm === null || widthMm === null || heightMm === null) return null;
|
|
83
|
+
const preset = element.getAttribute("data-frame-preset");
|
|
84
|
+
return {
|
|
85
|
+
xMm,
|
|
86
|
+
yMm,
|
|
87
|
+
widthMm,
|
|
88
|
+
heightMm,
|
|
89
|
+
preset: isOneOf(FRAME_PRESETS, preset) ? preset : null,
|
|
90
|
+
reserveFlow: element.getAttribute("data-frame-flow") !== "over",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function readImageWidthMm(element: AttributeSource): number | null {
|
|
95
|
+
return readNumber(element.getAttribute("data-width-mm"), IMAGE_WIDTH_MM_RANGE);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function readImageAlign(element: AttributeSource): DocumentImageAlignment | null {
|
|
99
|
+
const align = element.getAttribute("data-align");
|
|
100
|
+
return isOneOf(DOCUMENT_IMAGE_ALIGNMENTS, align) ? align : null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function readDividerColor(element: AttributeSource): string | null {
|
|
104
|
+
return readHexColor(element.getAttribute("data-color"));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function readDividerWeightPt(element: AttributeSource): number | null {
|
|
108
|
+
return readNumber(element.getAttribute("data-weight-pt"), DIVIDER_WEIGHT_PT_RANGE);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function readCellPaddingMm(element: AttributeSource): number | null {
|
|
112
|
+
return readNumber(element.getAttribute("data-cell-padding-mm"), CELL_PADDING_MM_RANGE);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function readTableWidthMm(element: AttributeSource): number | null {
|
|
116
|
+
return readNumber(element.getAttribute("data-table-width-mm"), TABLE_WIDTH_MM_RANGE);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function readTableAlign(element: AttributeSource): DocumentTableAlignment | null {
|
|
120
|
+
const align = element.getAttribute("data-table-align");
|
|
121
|
+
return isOneOf(DOCUMENT_TABLE_ALIGNMENTS, align) ? align : null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const ROW_LINE_ABOVE_ATTRIBUTE = "data-line-above";
|
|
125
|
+
export const ROW_LINE_BELOW_ATTRIBUTE = "data-line-below";
|
|
126
|
+
|
|
127
|
+
// Serialized as "#rrggbb 0.75": one attribute per edge keeps the data-* allowlist small.
|
|
128
|
+
export function readRowLine(element: AttributeSource, attribute: string): DocumentRowLine | null {
|
|
129
|
+
const raw = element.getAttribute(attribute);
|
|
130
|
+
if (raw === null) return null;
|
|
131
|
+
const [color, weight, ...rest] = raw.split(" ");
|
|
132
|
+
const weightPt = readNumber(weight ?? null, DIVIDER_WEIGHT_PT_RANGE);
|
|
133
|
+
const hexColor = readHexColor(color ?? null);
|
|
134
|
+
return rest.length === 0 && hexColor !== null && weightPt !== null
|
|
135
|
+
? { color: hexColor, weightPt }
|
|
136
|
+
: null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function isRowLine(value: unknown): value is DocumentRowLine {
|
|
140
|
+
if (!isPlainObject(value)) return false;
|
|
141
|
+
const { color, weightPt } = value;
|
|
142
|
+
return (
|
|
143
|
+
typeof color === "string" &&
|
|
144
|
+
HEX_COLOR_PATTERN.test(color) &&
|
|
145
|
+
typeof weightPt === "number" &&
|
|
146
|
+
weightPt >= DIVIDER_WEIGHT_PT_RANGE.min &&
|
|
147
|
+
weightPt <= DIVIDER_WEIGHT_PT_RANGE.max
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function readCellBackground(element: AttributeSource): string | null {
|
|
152
|
+
return readHexColor(element.getAttribute("data-background"));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function readColwidth(element: AttributeSource): number[] | null {
|
|
156
|
+
const raw = element.getAttribute("data-colwidth");
|
|
157
|
+
if (raw === null) return null;
|
|
158
|
+
const widths = raw.split(",").map((part) => readNumber(part.trim(), COLWIDTH_PX_RANGE));
|
|
159
|
+
return widths.length > 0 && widths.every((width): width is number => width !== null)
|
|
160
|
+
? widths
|
|
161
|
+
: null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function definedAttributes(entries: ReadonlyArray<readonly [string, unknown]>) {
|
|
165
|
+
return Object.fromEntries(
|
|
166
|
+
entries.filter(([, value]) => value !== null && value !== undefined && value !== false),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const DocumentTextStyleMark = Mark.create({
|
|
171
|
+
name: "documentTextStyle",
|
|
172
|
+
addAttributes() {
|
|
173
|
+
return { color: { default: null }, font: { default: null }, sizePt: { default: null } };
|
|
174
|
+
},
|
|
175
|
+
parseHTML() {
|
|
176
|
+
return [
|
|
177
|
+
{
|
|
178
|
+
tag: "span[data-text-style]",
|
|
179
|
+
getAttrs: (element) => {
|
|
180
|
+
const style = readTextStyleAttributes(element);
|
|
181
|
+
return style.color === null && style.font === null && style.sizePt === null
|
|
182
|
+
? false
|
|
183
|
+
: style;
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
];
|
|
187
|
+
},
|
|
188
|
+
renderHTML({ mark }) {
|
|
189
|
+
return [
|
|
190
|
+
"span",
|
|
191
|
+
{
|
|
192
|
+
"data-text-style": "",
|
|
193
|
+
...definedAttributes([
|
|
194
|
+
["data-color", mark.attrs["color"]],
|
|
195
|
+
["data-font", mark.attrs["font"]],
|
|
196
|
+
["data-size-pt", mark.attrs["sizePt"]],
|
|
197
|
+
]),
|
|
198
|
+
},
|
|
199
|
+
0,
|
|
200
|
+
];
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
function numberAttribute(attribute: string, range: Range, key: string) {
|
|
205
|
+
return {
|
|
206
|
+
default: null,
|
|
207
|
+
parseHTML: (element: AttributeSource) => readNumber(element.getAttribute(attribute), range),
|
|
208
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
209
|
+
const value: unknown = attributes[key];
|
|
210
|
+
return typeof value === "number" ? { [attribute]: String(value) } : {};
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export const DocumentBlockStyleAttributes = Extension.create({
|
|
216
|
+
name: "documentBlockStyle",
|
|
217
|
+
addGlobalAttributes() {
|
|
218
|
+
return [
|
|
219
|
+
{
|
|
220
|
+
types: ["paragraph", "heading"],
|
|
221
|
+
attributes: {
|
|
222
|
+
lineHeight: numberAttribute("data-line-height", LINE_HEIGHT_RANGE, "lineHeight"),
|
|
223
|
+
spaceBeforePt: numberAttribute(
|
|
224
|
+
"data-space-before",
|
|
225
|
+
PARAGRAPH_SPACE_PT_RANGE,
|
|
226
|
+
"spaceBeforePt",
|
|
227
|
+
),
|
|
228
|
+
spaceAfterPt: numberAttribute(
|
|
229
|
+
"data-space-after",
|
|
230
|
+
PARAGRAPH_SPACE_PT_RANGE,
|
|
231
|
+
"spaceAfterPt",
|
|
232
|
+
),
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
];
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
export const documentTablePrintAttributes = {
|
|
240
|
+
borders: {
|
|
241
|
+
default: null,
|
|
242
|
+
parseHTML: (element: AttributeSource) =>
|
|
243
|
+
element.getAttribute("data-borders") === "none" ? "none" : null,
|
|
244
|
+
renderHTML: (attributes: Record<string, unknown>) =>
|
|
245
|
+
attributes["borders"] === "none" ? { "data-borders": "none" } : {},
|
|
246
|
+
},
|
|
247
|
+
cellPaddingMm: {
|
|
248
|
+
default: null,
|
|
249
|
+
parseHTML: (element: AttributeSource) => readCellPaddingMm(element),
|
|
250
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
251
|
+
const value: unknown = attributes["cellPaddingMm"];
|
|
252
|
+
return typeof value === "number" ? { "data-cell-padding-mm": String(value) } : {};
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
widthMm: {
|
|
256
|
+
default: null,
|
|
257
|
+
parseHTML: (element: AttributeSource) => readTableWidthMm(element),
|
|
258
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
259
|
+
const value: unknown = attributes["widthMm"];
|
|
260
|
+
return typeof value === "number" ? { "data-table-width-mm": String(value) } : {};
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
align: {
|
|
264
|
+
default: null,
|
|
265
|
+
parseHTML: (element: AttributeSource) => readTableAlign(element),
|
|
266
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
267
|
+
const value: unknown = attributes["align"];
|
|
268
|
+
return isOneOf(DOCUMENT_TABLE_ALIGNMENTS, value) ? { "data-table-align": value } : {};
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
function rowLineAttribute(attribute: string, key: string) {
|
|
274
|
+
return {
|
|
275
|
+
default: null,
|
|
276
|
+
parseHTML: (element: AttributeSource) => readRowLine(element, attribute),
|
|
277
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
278
|
+
const line: unknown = attributes[key];
|
|
279
|
+
return isRowLine(line) ? { [attribute]: `${line.color} ${line.weightPt}` } : {};
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export const documentRowLineAttributes = {
|
|
285
|
+
lineAbove: rowLineAttribute(ROW_LINE_ABOVE_ATTRIBUTE, "lineAbove"),
|
|
286
|
+
lineBelow: rowLineAttribute(ROW_LINE_BELOW_ATTRIBUTE, "lineBelow"),
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export function readRowLineAttributes(attrs: Readonly<Record<string, unknown>>): {
|
|
290
|
+
readonly lineAbove: DocumentRowLine | null;
|
|
291
|
+
readonly lineBelow: DocumentRowLine | null;
|
|
292
|
+
} {
|
|
293
|
+
const { lineAbove, lineBelow } = attrs;
|
|
294
|
+
return {
|
|
295
|
+
lineAbove: isRowLine(lineAbove) ? lineAbove : null,
|
|
296
|
+
lineBelow: isRowLine(lineBelow) ? lineBelow : null,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export const documentCellPrintAttributes = {
|
|
301
|
+
background: {
|
|
302
|
+
default: null,
|
|
303
|
+
parseHTML: (element: AttributeSource) => readCellBackground(element),
|
|
304
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
305
|
+
const background: unknown = attributes["background"];
|
|
306
|
+
return typeof background === "string" && HEX_COLOR_PATTERN.test(background)
|
|
307
|
+
? { "data-background": background }
|
|
308
|
+
: {};
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
colwidth: {
|
|
312
|
+
default: null,
|
|
313
|
+
parseHTML: (element: AttributeSource) => readColwidth(element),
|
|
314
|
+
renderHTML: (attributes: Record<string, unknown>) => {
|
|
315
|
+
const widths: unknown = attributes["colwidth"];
|
|
316
|
+
return Array.isArray(widths) && widths.length > 0
|
|
317
|
+
? { "data-colwidth": widths.join(",") }
|
|
318
|
+
: {};
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export function imagePrintHtmlAttributes(attrs: Record<string, unknown>): Record<string, string> {
|
|
324
|
+
const { widthMm, align } = attrs;
|
|
325
|
+
return {
|
|
326
|
+
...(typeof widthMm === "number" ? { "data-width-mm": String(widthMm) } : {}),
|
|
327
|
+
...(isOneOf(DOCUMENT_IMAGE_ALIGNMENTS, align) ? { "data-align": align } : {}),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export function dividerPrintHtmlAttributes(attrs: Record<string, unknown>): Record<string, string> {
|
|
332
|
+
const { color, weightPt } = attrs;
|
|
333
|
+
return {
|
|
334
|
+
...(typeof color === "string" && HEX_COLOR_PATTERN.test(color) ? { "data-color": color } : {}),
|
|
335
|
+
...(typeof weightPt === "number" ? { "data-weight-pt": String(weightPt) } : {}),
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export const DocumentFrame = Node.create({
|
|
340
|
+
name: "documentFrame",
|
|
341
|
+
group: "block",
|
|
342
|
+
content: "block+",
|
|
343
|
+
isolating: true,
|
|
344
|
+
defining: true,
|
|
345
|
+
addAttributes() {
|
|
346
|
+
return {
|
|
347
|
+
xMm: { default: 0 },
|
|
348
|
+
yMm: { default: 0 },
|
|
349
|
+
widthMm: { default: 0 },
|
|
350
|
+
heightMm: { default: 0 },
|
|
351
|
+
preset: { default: null },
|
|
352
|
+
reserveFlow: { default: true },
|
|
353
|
+
};
|
|
354
|
+
},
|
|
355
|
+
parseHTML() {
|
|
356
|
+
return [
|
|
357
|
+
{
|
|
358
|
+
tag: "div[data-frame]",
|
|
359
|
+
getAttrs: (element) => readFrameAttributes(element) ?? false,
|
|
360
|
+
},
|
|
361
|
+
];
|
|
362
|
+
},
|
|
363
|
+
renderHTML({ node }) {
|
|
364
|
+
const { xMm, yMm, widthMm, heightMm, preset, reserveFlow } = node.attrs;
|
|
365
|
+
return [
|
|
366
|
+
"div",
|
|
367
|
+
mergeAttributes(
|
|
368
|
+
{ "data-frame": "" },
|
|
369
|
+
{
|
|
370
|
+
"data-x": String(xMm),
|
|
371
|
+
"data-y": String(yMm),
|
|
372
|
+
"data-width": String(widthMm),
|
|
373
|
+
"data-height": String(heightMm),
|
|
374
|
+
},
|
|
375
|
+
isOneOf(FRAME_PRESETS, preset) ? { "data-frame-preset": preset } : {},
|
|
376
|
+
reserveFlow === false ? { "data-frame-flow": "over" } : {},
|
|
377
|
+
),
|
|
378
|
+
0,
|
|
379
|
+
];
|
|
380
|
+
},
|
|
381
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Editor } from "@tiptap/core";
|
|
2
|
+
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|
3
|
+
import { Decoration } from "@tiptap/pm/view";
|
|
4
|
+
import { repeatListsOf, toggleRepeatHeaderAt } from "./document-tiptap-helpers.ts";
|
|
5
|
+
|
|
6
|
+
export type RepeatStripOptions = {
|
|
7
|
+
readonly label: (listNames: readonly string[]) => string;
|
|
8
|
+
readonly headerLabel: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// Table start + first row + first cell: the widget sits in front of the first cell's content.
|
|
12
|
+
const FIRST_CELL_CONTENT_OFFSET = 3;
|
|
13
|
+
const FIRST_ROW_OFFSET_FROM_WIDGET = FIRST_CELL_CONTENT_OFFSET - 1;
|
|
14
|
+
|
|
15
|
+
// Sits in the line of space the table gets above it (one document line), never taller than that.
|
|
16
|
+
const STRIP_CLASS =
|
|
17
|
+
"absolute bottom-full left-0 z-20 box-border flex h-[calc(var(--doc-line,1.25rem)-1px)] select-none items-center gap-2.5 whitespace-nowrap rounded-full border border-input bg-card pr-0.5 pl-2 text-[calc(var(--doc-line,1.25rem)*0.5)] font-normal uppercase leading-none tracking-wide text-muted-foreground";
|
|
18
|
+
const BUTTON_CLASS =
|
|
19
|
+
"cursor-pointer rounded-full border px-1.5 py-px text-[1em] font-medium leading-none normal-case tracking-normal";
|
|
20
|
+
const BUTTON_OFF_CLASS = `${BUTTON_CLASS} border-input bg-card text-foreground`;
|
|
21
|
+
const BUTTON_ON_CLASS = `${BUTTON_CLASS} border-foreground bg-card text-foreground`;
|
|
22
|
+
|
|
23
|
+
function createStrip(
|
|
24
|
+
editor: Editor,
|
|
25
|
+
label: string,
|
|
26
|
+
headerLabel: string,
|
|
27
|
+
headerRepeats: boolean,
|
|
28
|
+
getPos: () => number | undefined,
|
|
29
|
+
): HTMLElement {
|
|
30
|
+
const strip = document.createElement("div");
|
|
31
|
+
strip.className = STRIP_CLASS;
|
|
32
|
+
strip.contentEditable = "false";
|
|
33
|
+
strip.setAttribute("data-testid", "document-repeat-strip");
|
|
34
|
+
const name = document.createElement("span");
|
|
35
|
+
name.textContent = label;
|
|
36
|
+
const button = document.createElement("button");
|
|
37
|
+
button.type = "button";
|
|
38
|
+
button.className = headerRepeats ? BUTTON_ON_CLASS : BUTTON_OFF_CLASS;
|
|
39
|
+
button.setAttribute("aria-pressed", String(headerRepeats));
|
|
40
|
+
button.setAttribute("data-testid", "document-repeat-header-toggle");
|
|
41
|
+
button.textContent = headerLabel;
|
|
42
|
+
button.addEventListener("mousedown", (event) => event.preventDefault());
|
|
43
|
+
button.addEventListener("click", () => {
|
|
44
|
+
const pos = getPos();
|
|
45
|
+
if (pos !== undefined) toggleRepeatHeaderAt(editor, pos - FIRST_ROW_OFFSET_FROM_WIDGET);
|
|
46
|
+
});
|
|
47
|
+
strip.append(name, button);
|
|
48
|
+
return strip;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Editor-only: a widget never reaches getHTML or the PDF, and it is absolutely placed on the
|
|
52
|
+
// table's top edge so it takes no height.
|
|
53
|
+
export function repeatStripDecorations(
|
|
54
|
+
doc: ProseMirrorNode,
|
|
55
|
+
editor: Editor,
|
|
56
|
+
options: RepeatStripOptions,
|
|
57
|
+
): Decoration[] {
|
|
58
|
+
const decorations: Decoration[] = [];
|
|
59
|
+
doc.descendants((node, pos) => {
|
|
60
|
+
if (node.type.name !== "table") return;
|
|
61
|
+
const lists = repeatListsOf(node);
|
|
62
|
+
const firstCellContent = node.firstChild?.firstChild;
|
|
63
|
+
if (lists.length === 0 || firstCellContent === null || firstCellContent === undefined) return;
|
|
64
|
+
const headerRepeats = node.firstChild?.attrs["repeatHeader"] === true;
|
|
65
|
+
const label = options.label(lists);
|
|
66
|
+
decorations.push(
|
|
67
|
+
Decoration.widget(
|
|
68
|
+
pos + FIRST_CELL_CONTENT_OFFSET,
|
|
69
|
+
(_view, getPos) => createStrip(editor, label, options.headerLabel, headerRepeats, getPos),
|
|
70
|
+
{
|
|
71
|
+
side: -1,
|
|
72
|
+
key: `repeat-strip:${label}:${headerRepeats}`,
|
|
73
|
+
ignoreSelection: true,
|
|
74
|
+
stopEvent: () => true,
|
|
75
|
+
},
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
return decorations;
|
|
80
|
+
}
|
|
@@ -13,6 +13,20 @@ import {
|
|
|
13
13
|
isAllowedDocumentUrlHref,
|
|
14
14
|
isDocumentPlaceholderHref,
|
|
15
15
|
} from "./document-link-policy.ts";
|
|
16
|
+
import {
|
|
17
|
+
DocumentBlockStyleAttributes,
|
|
18
|
+
DocumentFrame,
|
|
19
|
+
DocumentTextStyleMark,
|
|
20
|
+
dividerPrintHtmlAttributes,
|
|
21
|
+
documentCellPrintAttributes,
|
|
22
|
+
documentRowLineAttributes,
|
|
23
|
+
documentTablePrintAttributes,
|
|
24
|
+
imagePrintHtmlAttributes,
|
|
25
|
+
readDividerColor,
|
|
26
|
+
readDividerWeightPt,
|
|
27
|
+
readImageAlign,
|
|
28
|
+
readImageWidthMm,
|
|
29
|
+
} from "./document-print-style.ts";
|
|
16
30
|
import { isDocumentReferenceName } from "./document-reference-name.ts";
|
|
17
31
|
import { createDocumentStepsNode } from "./document-steps.ts";
|
|
18
32
|
import { attributeParseRule } from "./tiptap-attribute-parse-rule.ts";
|
|
@@ -40,26 +54,46 @@ export const DOCUMENT_NODE_NAMES = [
|
|
|
40
54
|
"images",
|
|
41
55
|
] as const;
|
|
42
56
|
|
|
57
|
+
// "long" is what a date placeholder prints without a style, "short" is the numeric 15.01.2026.
|
|
58
|
+
export const DOCUMENT_DATE_STYLES = ["short", "long"] as const;
|
|
59
|
+
export type DocumentDateStyle = (typeof DOCUMENT_DATE_STYLES)[number];
|
|
60
|
+
|
|
61
|
+
export function readDocumentDateStyle(element: {
|
|
62
|
+
getAttribute(name: string): string | null;
|
|
63
|
+
}): DocumentDateStyle | null {
|
|
64
|
+
const style = element.getAttribute("data-date-style");
|
|
65
|
+
return isOneOf(DOCUMENT_DATE_STYLES, style) ? style : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
43
68
|
export const DocumentVariable = Node.create({
|
|
44
69
|
name: "variable",
|
|
45
70
|
group: "inline",
|
|
46
71
|
inline: true,
|
|
47
72
|
atom: true,
|
|
48
73
|
addAttributes() {
|
|
49
|
-
return { name: { default: null } };
|
|
74
|
+
return { name: { default: null }, dateStyle: { default: null } };
|
|
50
75
|
},
|
|
51
76
|
parseHTML() {
|
|
52
77
|
return [
|
|
53
|
-
|
|
78
|
+
{
|
|
54
79
|
tag: "span[data-variable]",
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
80
|
+
getAttrs: (element) => {
|
|
81
|
+
const name = element.getAttribute("data-variable");
|
|
82
|
+
if (name === null || !isDocumentReferenceName(name)) return false;
|
|
83
|
+
return { name, dateStyle: readDocumentDateStyle(element) };
|
|
84
|
+
},
|
|
85
|
+
},
|
|
59
86
|
];
|
|
60
87
|
},
|
|
61
88
|
renderHTML({ node }) {
|
|
62
|
-
|
|
89
|
+
const style: unknown = node.attrs["dateStyle"];
|
|
90
|
+
return [
|
|
91
|
+
"span",
|
|
92
|
+
{
|
|
93
|
+
"data-variable": node.attrs["name"],
|
|
94
|
+
...(isOneOf(DOCUMENT_DATE_STYLES, style) ? { "data-date-style": style } : {}),
|
|
95
|
+
},
|
|
96
|
+
];
|
|
63
97
|
},
|
|
64
98
|
});
|
|
65
99
|
|
|
@@ -68,7 +102,13 @@ export const DocumentResource = Node.create({
|
|
|
68
102
|
group: "block",
|
|
69
103
|
atom: true,
|
|
70
104
|
addAttributes() {
|
|
71
|
-
return {
|
|
105
|
+
return {
|
|
106
|
+
resource: { default: null },
|
|
107
|
+
alt: { default: null },
|
|
108
|
+
bleed: { default: false },
|
|
109
|
+
widthMm: { default: null },
|
|
110
|
+
align: { default: null },
|
|
111
|
+
};
|
|
72
112
|
},
|
|
73
113
|
parseHTML() {
|
|
74
114
|
return [
|
|
@@ -77,7 +117,13 @@ export const DocumentResource = Node.create({
|
|
|
77
117
|
getAttrs: (element) => {
|
|
78
118
|
const resource = element.getAttribute("data-resource");
|
|
79
119
|
if (resource === null || !isDocumentResourceKey(resource)) return false;
|
|
80
|
-
return {
|
|
120
|
+
return {
|
|
121
|
+
resource,
|
|
122
|
+
alt: element.getAttribute("alt"),
|
|
123
|
+
bleed: element.hasAttribute("data-bleed"),
|
|
124
|
+
widthMm: readImageWidthMm(element),
|
|
125
|
+
align: readImageAlign(element),
|
|
126
|
+
};
|
|
81
127
|
},
|
|
82
128
|
},
|
|
83
129
|
];
|
|
@@ -87,9 +133,12 @@ export const DocumentResource = Node.create({
|
|
|
87
133
|
const alt = node.attrs["alt"];
|
|
88
134
|
return [
|
|
89
135
|
"img",
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
136
|
+
{
|
|
137
|
+
"data-resource": node.attrs["resource"],
|
|
138
|
+
...(alt === null || alt === undefined ? {} : { alt }),
|
|
139
|
+
...(node.attrs["bleed"] === true ? { "data-bleed": "" } : {}),
|
|
140
|
+
...imagePrintHtmlAttributes(node.attrs),
|
|
141
|
+
},
|
|
93
142
|
];
|
|
94
143
|
},
|
|
95
144
|
});
|
|
@@ -125,11 +174,22 @@ export const DocumentDivider = Node.create({
|
|
|
125
174
|
name: "divider",
|
|
126
175
|
group: "block",
|
|
127
176
|
atom: true,
|
|
177
|
+
addAttributes() {
|
|
178
|
+
return { color: { default: null }, weightPt: { default: null } };
|
|
179
|
+
},
|
|
128
180
|
parseHTML() {
|
|
129
|
-
return [
|
|
181
|
+
return [
|
|
182
|
+
{
|
|
183
|
+
tag: "hr",
|
|
184
|
+
getAttrs: (element) => ({
|
|
185
|
+
color: readDividerColor(element),
|
|
186
|
+
weightPt: readDividerWeightPt(element),
|
|
187
|
+
}),
|
|
188
|
+
},
|
|
189
|
+
];
|
|
130
190
|
},
|
|
131
|
-
renderHTML() {
|
|
132
|
-
return ["hr"];
|
|
191
|
+
renderHTML({ node }) {
|
|
192
|
+
return ["hr", dividerPrintHtmlAttributes(node.attrs)];
|
|
133
193
|
},
|
|
134
194
|
});
|
|
135
195
|
|
|
@@ -171,18 +231,22 @@ export const DocumentTableRow = TableRow.extend({
|
|
|
171
231
|
renderHTML: (attributes) =>
|
|
172
232
|
attributes["repeatHeader"] ? { "data-repeat-header": "" } : {},
|
|
173
233
|
},
|
|
234
|
+
...documentRowLineAttributes,
|
|
174
235
|
};
|
|
175
236
|
},
|
|
176
237
|
});
|
|
177
238
|
|
|
178
239
|
export const DocumentTable = Table.extend({
|
|
179
|
-
|
|
180
|
-
return
|
|
240
|
+
addAttributes() {
|
|
241
|
+
return { ...(this.parent?.() ?? {}), ...documentTablePrintAttributes };
|
|
242
|
+
},
|
|
243
|
+
renderHTML({ HTMLAttributes }) {
|
|
244
|
+
return ["table", HTMLAttributes, ["tbody", 0]];
|
|
181
245
|
},
|
|
182
246
|
});
|
|
183
247
|
|
|
184
248
|
// Span attributes are rendered only when they differ from the default, and
|
|
185
|
-
// colwidth
|
|
249
|
+
// colwidth and background only when set, so `<td><p>x</p></td>` is the
|
|
186
250
|
// canonical cell.
|
|
187
251
|
function plainObjectOrEmpty(value: unknown): Record<string, unknown> {
|
|
188
252
|
return isPlainObject(value) ? value : {};
|
|
@@ -203,8 +267,9 @@ function canonicalCellAttributes(attributes: Record<string, unknown>) {
|
|
|
203
267
|
},
|
|
204
268
|
colwidth: {
|
|
205
269
|
...plainObjectOrEmpty(attributes["colwidth"]),
|
|
206
|
-
|
|
270
|
+
...documentCellPrintAttributes.colwidth,
|
|
207
271
|
},
|
|
272
|
+
background: documentCellPrintAttributes.background,
|
|
208
273
|
};
|
|
209
274
|
}
|
|
210
275
|
|
|
@@ -347,4 +412,7 @@ export const documentExtensions: Extensions = [
|
|
|
347
412
|
DocumentTextAlign,
|
|
348
413
|
DocumentTextSize,
|
|
349
414
|
DocumentCondition,
|
|
415
|
+
DocumentTextStyleMark,
|
|
416
|
+
DocumentBlockStyleAttributes,
|
|
417
|
+
DocumentFrame,
|
|
350
418
|
];
|