@cosmicdrift/kumiko-bundled-features 0.418.0 → 0.419.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 (114) hide show
  1. package/dist/template-resolver/compile-document-body.js +3 -6
  2. package/dist/template-resolver/compile-document-styles.d.ts +3 -0
  3. package/dist/template-resolver/compile-document-styles.js +84 -0
  4. package/dist/template-resolver/document-frames.d.ts +3 -0
  5. package/dist/template-resolver/document-frames.js +19 -0
  6. package/dist/template-resolver/document-op-blocks.d.ts +495 -0
  7. package/dist/template-resolver/document-op-blocks.js +430 -0
  8. package/dist/template-resolver/document-ops.d.ts +1809 -37
  9. package/dist/template-resolver/document-ops.js +97 -104
  10. package/dist/template-resolver/document-print-only.d.ts +10 -0
  11. package/dist/template-resolver/document-print-only.js +24 -0
  12. package/dist/template-resolver/document-sanitizer.d.ts +1 -1
  13. package/dist/template-resolver/document-sanitizer.js +24 -13
  14. package/dist/template-resolver/feature.d.ts +3 -0
  15. package/dist/template-resolver/feature.js +14 -1
  16. package/dist/template-resolver/handlers/apply-layout.write.d.ts +29 -0
  17. package/dist/template-resolver/handlers/apply-layout.write.js +118 -0
  18. package/dist/template-resolver/handlers/create-layout.write.d.ts +15 -0
  19. package/dist/template-resolver/handlers/create-layout.write.js +78 -0
  20. package/dist/template-resolver/handlers/document-base.d.ts +2 -2
  21. package/dist/template-resolver/handlers/document-blocks.query.d.ts +10 -3
  22. package/dist/template-resolver/handlers/document-blocks.query.js +13 -8
  23. package/dist/template-resolver/handlers/document-region.d.ts +19 -0
  24. package/dist/template-resolver/handlers/document-region.js +27 -0
  25. package/dist/template-resolver/handlers/failure-reason.d.ts +2 -0
  26. package/dist/template-resolver/handlers/failure-reason.js +12 -0
  27. package/dist/template-resolver/handlers/import-bundle-system.write.js +4 -0
  28. package/dist/template-resolver/handlers/layout-source.query.d.ts +3 -3
  29. package/dist/template-resolver/handlers/layout-source.query.js +6 -2
  30. package/dist/template-resolver/handlers/overview.query.d.ts +8 -0
  31. package/dist/template-resolver/handlers/overview.query.js +12 -6
  32. package/dist/template-resolver/handlers/patch-draft.write.d.ts +905 -4
  33. package/dist/template-resolver/handlers/patch-draft.write.js +10 -6
  34. package/dist/template-resolver/handlers/publish-templates.write.d.ts +40 -0
  35. package/dist/template-resolver/handlers/publish-templates.write.js +61 -0
  36. package/dist/template-resolver/handlers/set.write.js +4 -1
  37. package/dist/template-resolver/handlers/template-write.js +7 -2
  38. package/dist/template-resolver/handlers/tenant-settings.d.ts +17 -0
  39. package/dist/template-resolver/handlers/tenant-settings.js +91 -0
  40. package/dist/template-resolver/handlers/toggle-status.write.d.ts +14 -9
  41. package/dist/template-resolver/handlers/toggle-status.write.js +11 -7
  42. package/dist/template-resolver/handlers/upsert-system.write.js +15 -9
  43. package/dist/template-resolver/handlers/usage.query.d.ts +14 -0
  44. package/dist/template-resolver/handlers/usage.query.js +80 -12
  45. package/dist/template-resolver/handlers/validate.query.d.ts +2 -0
  46. package/dist/template-resolver/handlers/validate.query.js +10 -0
  47. package/dist/template-resolver/i18n.js +3 -0
  48. package/dist/template-resolver/index.d.ts +7 -2
  49. package/dist/template-resolver/index.js +6 -2
  50. package/dist/template-resolver/layout-regions.d.ts +4 -5
  51. package/dist/template-resolver/layout-regions.js +38 -11
  52. package/dist/template-resolver/print-document.js +3 -1
  53. package/dist/template-resolver/seeding.js +4 -0
  54. package/dist/template-resolver/template-delivery-policy.d.ts +1 -0
  55. package/dist/template-resolver/template-delivery-policy.js +9 -1
  56. package/dist/template-resolver/template-edit-policy.d.ts +5 -1
  57. package/dist/template-resolver/template-edit-policy.js +29 -1
  58. package/dist/template-resolver/template-meta.d.ts +27 -2
  59. package/dist/template-resolver/template-meta.js +21 -5
  60. package/dist/template-resolver/template-preview.js +8 -8
  61. package/dist/template-resolver/template-validation.js +10 -3
  62. package/dist/template-resolver/web/constants.d.ts +7 -1
  63. package/dist/template-resolver/web/constants.js +21 -1
  64. package/dist/template-resolver/web/document-blocks.d.ts +1 -0
  65. package/dist/template-resolver/web/document-blocks.js +2 -0
  66. package/dist/template-resolver/web/document-editor-extensions.d.ts +9 -1
  67. package/dist/template-resolver/web/document-editor-extensions.js +27 -2
  68. package/dist/template-resolver/web/document-editor-fonts.d.ts +10 -0
  69. package/dist/template-resolver/web/document-editor-fonts.js +13 -0
  70. package/dist/template-resolver/web/document-fonts.d.ts +4 -0
  71. package/dist/template-resolver/web/document-fonts.js +18 -0
  72. package/dist/template-resolver/web/document-frame-control.d.ts +61 -0
  73. package/dist/template-resolver/web/document-frame-control.js +329 -0
  74. package/dist/template-resolver/web/document-frame-node-view.d.ts +4 -0
  75. package/dist/template-resolver/web/document-frame-node-view.js +173 -0
  76. package/dist/template-resolver/web/document-print-decorations.d.ts +12 -0
  77. package/dist/template-resolver/web/document-print-decorations.js +147 -0
  78. package/dist/template-resolver/web/document-print-defaults.d.ts +14 -0
  79. package/dist/template-resolver/web/document-print-defaults.js +25 -0
  80. package/dist/template-resolver/web/document-print-style.d.ts +71 -0
  81. package/dist/template-resolver/web/document-print-style.js +199 -0
  82. package/dist/template-resolver/web/document-repeat-strip.d.ts +8 -0
  83. package/dist/template-resolver/web/document-repeat-strip.js +78 -0
  84. package/dist/template-resolver/web/document-schema.js +23 -9
  85. package/dist/template-resolver/web/document-style-css.d.ts +29 -0
  86. package/dist/template-resolver/web/document-style-css.js +76 -0
  87. package/dist/template-resolver/web/document-tiptap-bubble.js +13 -3
  88. package/dist/template-resolver/web/document-tiptap-content.d.ts +2 -1
  89. package/dist/template-resolver/web/document-tiptap-content.js +36 -4
  90. package/dist/template-resolver/web/document-tiptap-context.d.ts +6 -2
  91. package/dist/template-resolver/web/document-tiptap-context.js +2 -1
  92. package/dist/template-resolver/web/document-tiptap-editor.d.ts +1 -1
  93. package/dist/template-resolver/web/document-tiptap-editor.js +13 -5
  94. package/dist/template-resolver/web/document-tiptap-helpers.d.ts +30 -1
  95. package/dist/template-resolver/web/document-tiptap-helpers.js +103 -2
  96. package/dist/template-resolver/web/document-tiptap-toolbar-groups.d.ts +5 -0
  97. package/dist/template-resolver/web/document-tiptap-toolbar-groups.js +89 -6
  98. package/dist/template-resolver/web/document-tiptap-toolbar.js +45 -8
  99. package/dist/template-resolver/web/document-toolbar-blocks.d.ts +17 -0
  100. package/dist/template-resolver/web/i18n.js +19 -0
  101. package/dist/template-resolver/web/index.d.ts +8 -2
  102. package/dist/template-resolver/web/index.js +6 -1
  103. package/dist/template-resolver/web/pdf-constants.d.ts +3 -0
  104. package/dist/template-resolver/web/pdf-constants.js +3 -0
  105. package/dist/template-resolver/web/qualified-names.d.ts +7 -0
  106. package/dist/template-resolver/web/qualified-names.js +6 -0
  107. package/dist/template-resolver/web/use-document-tiptap-model.d.ts +6 -2
  108. package/dist/template-resolver/web/use-document-tiptap-model.js +38 -3
  109. package/dist/tenant/company-config.d.ts +6 -0
  110. package/dist/tenant/company-config.js +41 -0
  111. package/dist/tenant/feature.js +11 -0
  112. package/dist/tenant/i18n.js +14 -0
  113. package/package.json +16 -11
  114. package/src/template-resolver/changes.json +38 -0
@@ -0,0 +1,173 @@
1
+ import { collectSnapTargets, FRAME_HANDLES, frameAttributesOf, frameTagKind, moveFrameBox, resizeFrameBox, snapFrameBox, } from "./document-frame-control.js";
2
+ import { documentFrameCss } from "./document-style-css.js";
3
+ const PAPER_SELECTOR = "[data-document-paper]";
4
+ const PAPER_WIDTH_ATTRIBUTE = "data-paper-width-mm";
5
+ const PAPER_MARGIN_ATTRIBUTES = {
6
+ left: "data-paper-margin-left-mm",
7
+ right: "data-paper-margin-right-mm",
8
+ top: "data-paper-margin-top-mm",
9
+ };
10
+ const GUIDE_CLASS = "pointer-events-none absolute z-30 bg-primary";
11
+ // Frame states are driven by the data-frame-selected attribute a decoration puts on the frame, so
12
+ // a cursor inside the frame and a selected frame look the same as the side panel does.
13
+ const FRAME_CLASS = "group z-10 border border-dashed border-muted-foreground/50 bg-card data-[frame-selected]:border-[1.5px] data-[frame-selected]:border-solid data-[frame-selected]:border-primary";
14
+ const TAG_CLASS = "absolute -top-5 left-0 z-20 flex cursor-move select-none items-center whitespace-nowrap rounded-t bg-muted-foreground px-1.5 text-[10px] text-primary-foreground group-data-[frame-selected]:bg-primary";
15
+ const TINT_CLASS = "pointer-events-none absolute inset-0 hidden bg-primary/5 group-data-[frame-selected]:block";
16
+ const HANDLE_BASE_CLASS = "absolute z-20 hidden h-[7px] w-[7px] border-[1.5px] border-primary bg-background group-data-[frame-selected]:block";
17
+ const HANDLE_CLASS = {
18
+ nw: "-left-1 -top-1 cursor-nwse-resize",
19
+ n: "-top-1 left-[calc(50%-3.5px)] cursor-ns-resize",
20
+ ne: "-right-1 -top-1 cursor-nesw-resize",
21
+ e: "-right-1 top-[calc(50%-3.5px)] cursor-ew-resize",
22
+ se: "-bottom-1 -right-1 cursor-nwse-resize",
23
+ s: "-bottom-1 left-[calc(50%-3.5px)] cursor-ns-resize",
24
+ sw: "-bottom-1 -left-1 cursor-nesw-resize",
25
+ w: "-left-1 top-[calc(50%-3.5px)] cursor-ew-resize",
26
+ };
27
+ function boxOf(node) {
28
+ const { xMm, yMm, widthMm, heightMm } = frameAttributesOf(node);
29
+ return { xMm, yMm, widthMm, heightMm };
30
+ }
31
+ function mmPerPixelOf(element) {
32
+ const paper = element.closest(PAPER_SELECTOR);
33
+ if (paper === null)
34
+ return null;
35
+ const widthMm = Number(paper.getAttribute(PAPER_WIDTH_ATTRIBUTE));
36
+ const widthPx = paper.getBoundingClientRect().width;
37
+ return widthMm > 0 && widthPx > 0 ? widthMm / widthPx : null;
38
+ }
39
+ function numberAttribute(element, name) {
40
+ return Number(element.getAttribute(name));
41
+ }
42
+ function pageEdgesOf(paper) {
43
+ return {
44
+ widthMm: numberAttribute(paper, PAPER_WIDTH_ATTRIBUTE),
45
+ marginLeftMm: numberAttribute(paper, PAPER_MARGIN_ATTRIBUTES.left),
46
+ marginRightMm: numberAttribute(paper, PAPER_MARGIN_ATTRIBUTES.right),
47
+ marginTopMm: numberAttribute(paper, PAPER_MARGIN_ATTRIBUTES.top),
48
+ };
49
+ }
50
+ function guideElement(paper, axis, mm) {
51
+ const guide = paper.ownerDocument.createElement("div");
52
+ guide.className = GUIDE_CLASS;
53
+ guide.setAttribute("data-testid", "document-frame-guide");
54
+ const position = `calc(${mm} * var(--paper-mm))`;
55
+ if (axis === "x") {
56
+ guide.classList.add("top-0", "bottom-0", "w-px");
57
+ guide.style.left = position;
58
+ }
59
+ else {
60
+ guide.classList.add("left-0", "right-0", "h-px");
61
+ guide.style.top = position;
62
+ }
63
+ paper.append(guide);
64
+ return guide;
65
+ }
66
+ function createDiv(className, testId) {
67
+ const element = document.createElement("div");
68
+ element.className = className;
69
+ element.setAttribute("data-testid", testId);
70
+ element.contentEditable = "false";
71
+ return element;
72
+ }
73
+ export function createFrameView(labels) {
74
+ return ({ node: initialNode, editor, getPos }) => {
75
+ let node = initialNode;
76
+ const dom = document.createElement("div");
77
+ dom.className = FRAME_CLASS;
78
+ dom.setAttribute("data-testid", "document-frame");
79
+ const tag = createDiv(TAG_CLASS, "document-frame-move");
80
+ const tint = createDiv(TINT_CLASS, "document-frame-tint");
81
+ const contentDOM = document.createElement("div");
82
+ contentDOM.className = "h-full overflow-hidden [&>:last-child]:mb-0";
83
+ dom.append(tint, tag, contentDOM);
84
+ const applyBox = (box) => {
85
+ dom.style.cssText = documentFrameCss(box);
86
+ // The tag and handles sit outside the box; only the content is clipped.
87
+ dom.style.overflow = "visible";
88
+ };
89
+ const render = () => {
90
+ const attrs = frameAttributesOf(node);
91
+ applyBox(boxOf(node));
92
+ dom.setAttribute("data-frame-preset", attrs.preset ?? "");
93
+ tag.textContent = labels[frameTagKind(attrs.preset, attrs.yMm)];
94
+ };
95
+ let cancelGesture = null;
96
+ const startGesture = (event, handle) => {
97
+ const mmPerPixel = mmPerPixelOf(dom);
98
+ const pos = getPos();
99
+ if (mmPerPixel === null || pos === undefined)
100
+ return;
101
+ event.preventDefault();
102
+ event.stopPropagation();
103
+ editor.commands.setNodeSelection(pos);
104
+ const start = boxOf(node);
105
+ let preview = start;
106
+ const paper = dom.closest(PAPER_SELECTOR);
107
+ const targets = paper === null ? null : collectSnapTargets(editor.state.doc, pos, pageEdgesOf(paper));
108
+ let guides = [];
109
+ const clearGuides = () => {
110
+ for (const guide of guides)
111
+ guide.remove();
112
+ guides = [];
113
+ };
114
+ const onMove = (move) => {
115
+ const dx = (move.clientX - event.clientX) * mmPerPixel;
116
+ const dy = (move.clientY - event.clientY) * mmPerPixel;
117
+ const raw = handle === "move" ? moveFrameBox(start, dx, dy) : resizeFrameBox(start, handle, dx, dy);
118
+ clearGuides();
119
+ if (paper === null || targets === null || move.altKey) {
120
+ preview = raw;
121
+ }
122
+ else {
123
+ const snapped = snapFrameBox(raw, handle, targets);
124
+ preview = snapped.box;
125
+ if (snapped.guides.xMm !== null)
126
+ guides.push(guideElement(paper, "x", snapped.guides.xMm));
127
+ if (snapped.guides.yMm !== null)
128
+ guides.push(guideElement(paper, "y", snapped.guides.yMm));
129
+ }
130
+ applyBox(preview);
131
+ };
132
+ const stop = () => {
133
+ clearGuides();
134
+ dom.ownerDocument.removeEventListener("pointermove", onMove);
135
+ dom.ownerDocument.removeEventListener("pointerup", onUp);
136
+ cancelGesture = null;
137
+ };
138
+ const onUp = () => {
139
+ stop();
140
+ const current = getPos();
141
+ if (current === undefined)
142
+ return;
143
+ editor.view.dispatch(editor.state.tr.setNodeMarkup(current, undefined, { ...node.attrs, ...preview }));
144
+ };
145
+ dom.ownerDocument.addEventListener("pointermove", onMove);
146
+ dom.ownerDocument.addEventListener("pointerup", onUp);
147
+ cancelGesture = stop;
148
+ };
149
+ tag.addEventListener("pointerdown", (event) => startGesture(event, "move"));
150
+ if (editor.isEditable) {
151
+ for (const handle of FRAME_HANDLES) {
152
+ const element = createDiv(`${HANDLE_BASE_CLASS} ${HANDLE_CLASS[handle]}`, `document-frame-handle-${handle}`);
153
+ element.addEventListener("pointerdown", (event) => startGesture(event, handle));
154
+ dom.append(element);
155
+ }
156
+ }
157
+ render();
158
+ return {
159
+ dom,
160
+ contentDOM,
161
+ update: (updated) => {
162
+ if (updated.type !== node.type)
163
+ return false;
164
+ node = updated;
165
+ render();
166
+ return true;
167
+ },
168
+ ignoreMutation: (mutation) => mutation.type === "selection" ? false : !contentDOM.contains(mutation.target),
169
+ stopEvent: (event) => event.target instanceof Node && !contentDOM.contains(event.target),
170
+ destroy: () => cancelGesture?.(),
171
+ };
172
+ };
173
+ }
@@ -0,0 +1,12 @@
1
+ import { Extension } from "@tiptap/core";
2
+ import type { DocumentFrameControl } from "./document-frame-control.ts";
3
+ import { type RepeatStripOptions } from "./document-repeat-strip.ts";
4
+ export declare function loadDocumentEditorFonts(): Promise<unknown>;
5
+ export type DocumentPrintEditingOptions = {
6
+ /** Frames exist (PDF body): reserve the flow area below frames that push the text down. */
7
+ readonly frames: boolean;
8
+ readonly reportFrameControl: ((control: DocumentFrameControl | null) => void) | null;
9
+ /** Shows the table strip that switches the repeated header row; null renders none. */
10
+ readonly repeatStrip: RepeatStripOptions | null;
11
+ };
12
+ export declare const DocumentPrintEditing: Extension<DocumentPrintEditingOptions, any>;
@@ -0,0 +1,147 @@
1
+ // Editor-only rendering of the print model: free values (font, size, spacing, cell colours, table
2
+ // borders) are drawn as ProseMirror decorations, so they never reach getHTML. The same
3
+ // document-style-css builders as the PDF compile keep the editor and the print in step.
4
+ import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
5
+ import { Extension } from "@tiptap/core";
6
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
7
+ import { Decoration, DecorationSet } from "@tiptap/pm/view";
8
+ import { DOCUMENT_FONTS } from "./document-fonts.js";
9
+ import { buildFrameControl, FRAME_NODE_NAME, FRAME_SELECTED_ATTRIBUTE, frameFlowBottomMm, frameLocationOf, isSenderLine, } from "./document-frame-control.js";
10
+ import { repeatStripDecorations } from "./document-repeat-strip.js";
11
+ import { addressParagraphCss, DOCUMENT_LINE_VARIABLE, documentBlockStyleCss, documentCellStyleCss, documentTextStyleCss, PARAGRAPH_DEFAULT_LINE_HEIGHT, } from "./document-style-css.js";
12
+ // Metric-compatible web fonts (@fontsource) stand in for the print service's installed fonts.
13
+ const EDITOR_FONT_STACKS = {
14
+ arial: 'Arimo,"Liberation Sans",Arial,sans-serif',
15
+ times: 'Tinos,"Liberation Serif","Times New Roman",serif',
16
+ courier: 'Cousine,"Liberation Mono","Courier New",monospace',
17
+ calibri: "Carlito,Calibri,sans-serif",
18
+ cambria: "Caladea,Cambria,serif",
19
+ };
20
+ // Fonts are cosmetic: the stacks fall back to system fonts when the chunk fails to load.
21
+ export function loadDocumentEditorFonts() {
22
+ return import("./document-editor-fonts.js").catch(() => undefined);
23
+ }
24
+ const TEXT_STYLE_MARK = "documentTextStyle";
25
+ const BLOCK_STYLE_NODES = ["paragraph", "heading"];
26
+ const CELL_NODES = ["tableCell", "tableHeader"];
27
+ function numberOrNull(value) {
28
+ return typeof value === "number" ? value : null;
29
+ }
30
+ function stringOrNull(value) {
31
+ return typeof value === "string" ? value : null;
32
+ }
33
+ function textStyleDecoration(node, from) {
34
+ const mark = node.marks.find((candidate) => candidate.type.name === TEXT_STYLE_MARK);
35
+ if (mark === undefined)
36
+ return null;
37
+ const font = mark.attrs["font"];
38
+ const declarations = [
39
+ documentTextStyleCss({
40
+ color: stringOrNull(mark.attrs["color"]),
41
+ sizePt: numberOrNull(mark.attrs["sizePt"]),
42
+ }),
43
+ isOneOf(DOCUMENT_FONTS, font) ? `font-family:${EDITOR_FONT_STACKS[font]}` : "",
44
+ ].filter((part) => part !== "");
45
+ return declarations.length === 0
46
+ ? null
47
+ : Decoration.inline(from, from + node.nodeSize, { style: declarations.join(";") });
48
+ }
49
+ function isAddressFrame(node) {
50
+ return node?.type.name === FRAME_NODE_NAME && node.attrs["preset"] === "address";
51
+ }
52
+ function nodeDecoration(node, from, parent, index) {
53
+ const name = node.type.name;
54
+ const to = from + node.nodeSize;
55
+ if (BLOCK_STYLE_NODES.includes(name)) {
56
+ const style = [
57
+ documentBlockStyleCss({
58
+ lineHeight: numberOrNull(node.attrs["lineHeight"]),
59
+ spaceBeforePt: numberOrNull(node.attrs["spaceBeforePt"]),
60
+ spaceAfterPt: numberOrNull(node.attrs["spaceAfterPt"]),
61
+ }),
62
+ name === "paragraph" && isAddressFrame(parent)
63
+ ? addressParagraphCss(index === 0 && isSenderLine(node))
64
+ : "",
65
+ ]
66
+ .filter((part) => part !== "")
67
+ .join(";");
68
+ return style === "" ? null : Decoration.node(from, to, { style });
69
+ }
70
+ if (CELL_NODES.includes(name)) {
71
+ const style = documentCellStyleCss({ background: stringOrNull(node.attrs["background"]) });
72
+ return style === "" ? null : Decoration.node(from, to, { style });
73
+ }
74
+ if (name === "table" && node.attrs["borders"] === "none") {
75
+ return Decoration.node(from, to, { "data-borders": "none" });
76
+ }
77
+ return null;
78
+ }
79
+ // Frames are not rebuilt by the selection alone, so the selected one is marked by a decoration the
80
+ // node view styles; the same location drives the side panel.
81
+ function buildDecorations(doc, selection, extra) {
82
+ const decorations = [];
83
+ const selectedFrame = frameLocationOf(selection);
84
+ doc.descendants((node, pos, parent, index) => {
85
+ if (node.type.name === FRAME_NODE_NAME && selectedFrame?.pos === pos) {
86
+ decorations.push(Decoration.node(pos, pos + node.nodeSize, { [FRAME_SELECTED_ATTRIBUTE]: "" }));
87
+ }
88
+ const decoration = node.isInline
89
+ ? textStyleDecoration(node, pos)
90
+ : nodeDecoration(node, pos, parent, index);
91
+ if (decoration !== null)
92
+ decorations.push(decoration);
93
+ });
94
+ return DecorationSet.create(doc, [...decorations, ...extra]);
95
+ }
96
+ const FLOW_PADDING_CSS = "padding-top:max(0px,calc((var(--frame-flow-bottom) - var(--page-margin-top-mm,0)) * var(--paper-mm,1mm)))";
97
+ // The paper's own text size and the shared paragraph defaults, so editor lines measure like the PDF's.
98
+ const PAGE_TEXT_CSS = `font-size:var(--paper-font-size,inherit);line-height:${PARAGRAPH_DEFAULT_LINE_HEIGHT};${DOCUMENT_LINE_VARIABLE}:calc(var(--paper-font-size) * ${PARAGRAPH_DEFAULT_LINE_HEIGHT})`;
99
+ const DECORATION_KEY = new PluginKey("documentPrintDecorations");
100
+ export const DocumentPrintEditing = Extension.create({
101
+ name: "documentPrintEditing",
102
+ addOptions() {
103
+ return { frames: false, reportFrameControl: null, repeatStrip: null };
104
+ },
105
+ addProseMirrorPlugins() {
106
+ const { frames, reportFrameControl, repeatStrip } = this.options;
107
+ const { editor } = this;
108
+ const stripOf = (doc) => repeatStrip === null || !editor.options.editable
109
+ ? []
110
+ : repeatStripDecorations(doc, editor, repeatStrip);
111
+ const decorations = new Plugin({
112
+ key: DECORATION_KEY,
113
+ state: {
114
+ init: (_config, state) => buildDecorations(state.doc, state.selection, stripOf(state.doc)),
115
+ apply: (tr, previous) => tr.docChanged || tr.selectionSet
116
+ ? buildDecorations(tr.doc, tr.selection, stripOf(tr.doc))
117
+ : previous,
118
+ },
119
+ props: {
120
+ decorations: (state) => DECORATION_KEY.getState(state),
121
+ attributes: (state) => frames
122
+ ? {
123
+ style: `--frame-flow-bottom:${frameFlowBottomMm(state.doc)};${FLOW_PADDING_CSS};${PAGE_TEXT_CSS}`,
124
+ }
125
+ : {},
126
+ },
127
+ });
128
+ if (!frames || reportFrameControl === null)
129
+ return [decorations];
130
+ const reporter = new Plugin({
131
+ view: () => {
132
+ let last = null;
133
+ return {
134
+ update: (view) => {
135
+ const location = view.editable ? frameLocationOf(view.state.selection) : null;
136
+ if (location?.pos === last?.pos && location?.node === last?.node)
137
+ return;
138
+ last = location;
139
+ reportFrameControl(location === null ? null : buildFrameControl(editor, location));
140
+ },
141
+ destroy: () => reportFrameControl(null),
142
+ };
143
+ },
144
+ });
145
+ return [decorations, reporter];
146
+ },
147
+ });
@@ -0,0 +1,14 @@
1
+ import type { Translate } from "@cosmicdrift/kumiko-renderer/headless";
2
+ import type { DocumentLayoutValues } from "./document-toolbar-blocks.ts";
3
+ export type PrintDefaultLabels = {
4
+ readonly font: string;
5
+ readonly size: string;
6
+ readonly lineHeight: string;
7
+ readonly spaceBefore: string;
8
+ readonly spaceAfter: string;
9
+ readonly formatSize: (value: number) => string;
10
+ readonly formatLineHeight: (value: number) => string;
11
+ readonly formatSpaceBefore: (value: number) => string;
12
+ readonly formatSpaceAfter: (value: number) => string;
13
+ };
14
+ export declare function printDefaultLabelsOf(t: Translate, locale: string, values: DocumentLayoutValues): PrintDefaultLabels;
@@ -0,0 +1,25 @@
1
+ import { DOCUMENT_FONT_LABELS } from "./document-fonts.js";
2
+ import { documentLinePt, PARAGRAPH_DEFAULT_LINE_HEIGHT } from "./document-style-css.js";
3
+ import { PDF_DEFAULT_FONT_SIZE_PT } from "./pdf-constants.js";
4
+ // The first option of every print select stands for "inherit"; it shows the value the layout
5
+ // gives, or the control's own name when the layout is not known (mail, no layout).
6
+ export function printDefaultLabelsOf(t, locale, values) {
7
+ const formatNumber = (value) => new Intl.NumberFormat(locale).format(value);
8
+ const formatSize = (value) => t("documentDesigner.editor.defaultSize", { value: formatNumber(value) });
9
+ const formatLineHeight = (value) => t("documentDesigner.editor.defaultLineHeight", { value: formatNumber(value) });
10
+ const formatSpaceBefore = (value) => t("documentDesigner.editor.defaultSpaceBefore", { value: formatNumber(value) });
11
+ const formatSpaceAfter = (value) => t("documentDesigner.editor.defaultSpaceAfter", { value: formatNumber(value) });
12
+ const known = (value, label, fallbackKey) => value === undefined ? t(fallbackKey) : label(value);
13
+ return {
14
+ font: known(values.fontFamily, (font) => DOCUMENT_FONT_LABELS[font], "documentDesigner.editor.font"),
15
+ size: known(values.fontSizePt, formatSize, "documentDesigner.editor.fontSize"),
16
+ // Without a layout value the paragraph still has the shared print default, so show that.
17
+ lineHeight: formatLineHeight(values.lineHeight ?? PARAGRAPH_DEFAULT_LINE_HEIGHT),
18
+ spaceBefore: known(values.spaceBeforePt, formatSpaceBefore, "documentDesigner.editor.spaceBeforeShort"),
19
+ spaceAfter: formatSpaceAfter(values.spaceAfterPt ?? documentLinePt(values.fontSizePt ?? PDF_DEFAULT_FONT_SIZE_PT)),
20
+ formatSize,
21
+ formatLineHeight,
22
+ formatSpaceBefore,
23
+ formatSpaceAfter,
24
+ };
25
+ }
@@ -0,0 +1,71 @@
1
+ import { Extension, Mark, Node } from "@tiptap/core";
2
+ import type { DocumentBlockStyle, DocumentFrameBox, DocumentTextStyle } from "./document-style-css.ts";
3
+ export declare const TEXT_SIZE_PT_RANGE: {
4
+ readonly min: 4;
5
+ readonly max: 72;
6
+ };
7
+ export declare const LINE_HEIGHT_RANGE: {
8
+ readonly min: 0.8;
9
+ readonly max: 3;
10
+ };
11
+ export declare const PARAGRAPH_SPACE_PT_RANGE: {
12
+ readonly min: 0;
13
+ readonly max: 200;
14
+ };
15
+ export declare const FRAME_MM_RANGE: {
16
+ readonly min: 0;
17
+ readonly max: 1000;
18
+ };
19
+ export declare const COLWIDTH_PX_RANGE: {
20
+ readonly min: 1;
21
+ readonly max: 5000;
22
+ };
23
+ export declare const FRAME_PRESETS: readonly ["address", "text"];
24
+ export type DocumentFramePreset = (typeof FRAME_PRESETS)[number];
25
+ type AttributeSource = {
26
+ getAttribute(name: string): string | null;
27
+ };
28
+ export declare function readTextStyleAttributes(element: AttributeSource): Required<DocumentTextStyle>;
29
+ export declare function readBlockStyleAttributes(element: AttributeSource): Required<DocumentBlockStyle>;
30
+ export type DocumentFrameAttributes = DocumentFrameBox & {
31
+ readonly preset: DocumentFramePreset | null;
32
+ readonly reserveFlow: boolean;
33
+ };
34
+ export declare function readFrameAttributes(element: AttributeSource): DocumentFrameAttributes | null;
35
+ export declare function readCellBackground(element: AttributeSource): string | null;
36
+ export declare function readColwidth(element: AttributeSource): number[] | null;
37
+ export declare const DocumentTextStyleMark: Mark<any, any>;
38
+ export declare const DocumentBlockStyleAttributes: Extension<any, any>;
39
+ export declare const documentTableBordersAttribute: {
40
+ borders: {
41
+ default: null;
42
+ parseHTML: (element: AttributeSource) => "none" | null;
43
+ renderHTML: (attributes: Record<string, unknown>) => {
44
+ "data-borders": string;
45
+ } | {
46
+ "data-borders"?: undefined;
47
+ };
48
+ };
49
+ };
50
+ export declare const documentCellPrintAttributes: {
51
+ background: {
52
+ default: null;
53
+ parseHTML: (element: AttributeSource) => string | null;
54
+ renderHTML: (attributes: Record<string, unknown>) => {
55
+ "data-background": string;
56
+ } | {
57
+ "data-background"?: undefined;
58
+ };
59
+ };
60
+ colwidth: {
61
+ default: null;
62
+ parseHTML: (element: AttributeSource) => number[] | null;
63
+ renderHTML: (attributes: Record<string, unknown>) => {
64
+ "data-colwidth": string;
65
+ } | {
66
+ "data-colwidth"?: undefined;
67
+ };
68
+ };
69
+ };
70
+ export declare const DocumentFrame: Node<any, any>;
71
+ export {};
@@ -0,0 +1,199 @@
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 } from "@cosmicdrift/kumiko-types/type-guards";
7
+ import { Extension, Mark, mergeAttributes, Node } from "@tiptap/core";
8
+ import { DOCUMENT_FONTS } from "./document-fonts.js";
9
+ import { HEX_COLOR_PATTERN } from "./mail-layout-constants.js";
10
+ export const TEXT_SIZE_PT_RANGE = { min: 4, max: 72 };
11
+ export const LINE_HEIGHT_RANGE = { min: 0.8, max: 3 };
12
+ export const PARAGRAPH_SPACE_PT_RANGE = { min: 0, max: 200 };
13
+ export const FRAME_MM_RANGE = { min: 0, max: 1000 };
14
+ export const COLWIDTH_PX_RANGE = { min: 1, max: 5000 };
15
+ export const FRAME_PRESETS = ["address", "text"];
16
+ const PLAIN_DECIMAL = /^\d+(\.\d+)?$/;
17
+ function readNumber(raw, range) {
18
+ if (raw === null || !PLAIN_DECIMAL.test(raw))
19
+ return null;
20
+ const value = Number(raw);
21
+ return Number.isFinite(value) && value >= range.min && value <= range.max ? value : null;
22
+ }
23
+ function readHexColor(raw) {
24
+ return raw !== null && HEX_COLOR_PATTERN.test(raw) ? raw : null;
25
+ }
26
+ function readFont(raw) {
27
+ return isOneOf(DOCUMENT_FONTS, raw) ? raw : null;
28
+ }
29
+ export function readTextStyleAttributes(element) {
30
+ return {
31
+ color: readHexColor(element.getAttribute("data-color")),
32
+ font: readFont(element.getAttribute("data-font")),
33
+ sizePt: readNumber(element.getAttribute("data-size-pt"), TEXT_SIZE_PT_RANGE),
34
+ };
35
+ }
36
+ export function readBlockStyleAttributes(element) {
37
+ return {
38
+ lineHeight: readNumber(element.getAttribute("data-line-height"), LINE_HEIGHT_RANGE),
39
+ spaceBeforePt: readNumber(element.getAttribute("data-space-before"), PARAGRAPH_SPACE_PT_RANGE),
40
+ spaceAfterPt: readNumber(element.getAttribute("data-space-after"), PARAGRAPH_SPACE_PT_RANGE),
41
+ };
42
+ }
43
+ export function readFrameAttributes(element) {
44
+ const xMm = readNumber(element.getAttribute("data-x"), FRAME_MM_RANGE);
45
+ const yMm = readNumber(element.getAttribute("data-y"), FRAME_MM_RANGE);
46
+ const widthMm = readNumber(element.getAttribute("data-width"), FRAME_MM_RANGE);
47
+ const heightMm = readNumber(element.getAttribute("data-height"), FRAME_MM_RANGE);
48
+ if (xMm === null || yMm === null || widthMm === null || heightMm === null)
49
+ return null;
50
+ const preset = element.getAttribute("data-frame-preset");
51
+ return {
52
+ xMm,
53
+ yMm,
54
+ widthMm,
55
+ heightMm,
56
+ preset: isOneOf(FRAME_PRESETS, preset) ? preset : null,
57
+ reserveFlow: element.getAttribute("data-frame-flow") !== "over",
58
+ };
59
+ }
60
+ export function readCellBackground(element) {
61
+ return readHexColor(element.getAttribute("data-background"));
62
+ }
63
+ export function readColwidth(element) {
64
+ const raw = element.getAttribute("data-colwidth");
65
+ if (raw === null)
66
+ return null;
67
+ const widths = raw.split(",").map((part) => readNumber(part.trim(), COLWIDTH_PX_RANGE));
68
+ return widths.length > 0 && widths.every((width) => width !== null)
69
+ ? widths
70
+ : null;
71
+ }
72
+ function definedAttributes(entries) {
73
+ return Object.fromEntries(entries.filter(([, value]) => value !== null && value !== undefined && value !== false));
74
+ }
75
+ export const DocumentTextStyleMark = Mark.create({
76
+ name: "documentTextStyle",
77
+ addAttributes() {
78
+ return { color: { default: null }, font: { default: null }, sizePt: { default: null } };
79
+ },
80
+ parseHTML() {
81
+ return [
82
+ {
83
+ tag: "span[data-text-style]",
84
+ getAttrs: (element) => {
85
+ const style = readTextStyleAttributes(element);
86
+ return style.color === null && style.font === null && style.sizePt === null
87
+ ? false
88
+ : style;
89
+ },
90
+ },
91
+ ];
92
+ },
93
+ renderHTML({ mark }) {
94
+ return [
95
+ "span",
96
+ {
97
+ "data-text-style": "",
98
+ ...definedAttributes([
99
+ ["data-color", mark.attrs["color"]],
100
+ ["data-font", mark.attrs["font"]],
101
+ ["data-size-pt", mark.attrs["sizePt"]],
102
+ ]),
103
+ },
104
+ 0,
105
+ ];
106
+ },
107
+ });
108
+ function numberAttribute(attribute, range, key) {
109
+ return {
110
+ default: null,
111
+ parseHTML: (element) => readNumber(element.getAttribute(attribute), range),
112
+ renderHTML: (attributes) => {
113
+ const value = attributes[key];
114
+ return typeof value === "number" ? { [attribute]: String(value) } : {};
115
+ },
116
+ };
117
+ }
118
+ export const DocumentBlockStyleAttributes = Extension.create({
119
+ name: "documentBlockStyle",
120
+ addGlobalAttributes() {
121
+ return [
122
+ {
123
+ types: ["paragraph", "heading"],
124
+ attributes: {
125
+ lineHeight: numberAttribute("data-line-height", LINE_HEIGHT_RANGE, "lineHeight"),
126
+ spaceBeforePt: numberAttribute("data-space-before", PARAGRAPH_SPACE_PT_RANGE, "spaceBeforePt"),
127
+ spaceAfterPt: numberAttribute("data-space-after", PARAGRAPH_SPACE_PT_RANGE, "spaceAfterPt"),
128
+ },
129
+ },
130
+ ];
131
+ },
132
+ });
133
+ export const documentTableBordersAttribute = {
134
+ borders: {
135
+ default: null,
136
+ parseHTML: (element) => element.getAttribute("data-borders") === "none" ? "none" : null,
137
+ renderHTML: (attributes) => attributes["borders"] === "none" ? { "data-borders": "none" } : {},
138
+ },
139
+ };
140
+ export const documentCellPrintAttributes = {
141
+ background: {
142
+ default: null,
143
+ parseHTML: (element) => readCellBackground(element),
144
+ renderHTML: (attributes) => {
145
+ const background = attributes["background"];
146
+ return typeof background === "string" && HEX_COLOR_PATTERN.test(background)
147
+ ? { "data-background": background }
148
+ : {};
149
+ },
150
+ },
151
+ colwidth: {
152
+ default: null,
153
+ parseHTML: (element) => readColwidth(element),
154
+ renderHTML: (attributes) => {
155
+ const widths = attributes["colwidth"];
156
+ return Array.isArray(widths) && widths.length > 0
157
+ ? { "data-colwidth": widths.join(",") }
158
+ : {};
159
+ },
160
+ },
161
+ };
162
+ export const DocumentFrame = Node.create({
163
+ name: "documentFrame",
164
+ group: "block",
165
+ content: "block+",
166
+ isolating: true,
167
+ defining: true,
168
+ addAttributes() {
169
+ return {
170
+ xMm: { default: 0 },
171
+ yMm: { default: 0 },
172
+ widthMm: { default: 0 },
173
+ heightMm: { default: 0 },
174
+ preset: { default: null },
175
+ reserveFlow: { default: true },
176
+ };
177
+ },
178
+ parseHTML() {
179
+ return [
180
+ {
181
+ tag: "div[data-frame]",
182
+ getAttrs: (element) => readFrameAttributes(element) ?? false,
183
+ },
184
+ ];
185
+ },
186
+ renderHTML({ node }) {
187
+ const { xMm, yMm, widthMm, heightMm, preset, reserveFlow } = node.attrs;
188
+ return [
189
+ "div",
190
+ mergeAttributes({ "data-frame": "" }, {
191
+ "data-x": String(xMm),
192
+ "data-y": String(yMm),
193
+ "data-width": String(widthMm),
194
+ "data-height": String(heightMm),
195
+ }, isOneOf(FRAME_PRESETS, preset) ? { "data-frame-preset": preset } : {}, reserveFlow === false ? { "data-frame-flow": "over" } : {}),
196
+ 0,
197
+ ];
198
+ },
199
+ });
@@ -0,0 +1,8 @@
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
+ export type RepeatStripOptions = {
5
+ readonly label: (listNames: readonly string[]) => string;
6
+ readonly headerLabel: string;
7
+ };
8
+ export declare function repeatStripDecorations(doc: ProseMirrorNode, editor: Editor, options: RepeatStripOptions): Decoration[];