@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,207 @@
|
|
|
1
|
+
import type { NodeViewRenderer } from "@tiptap/core";
|
|
2
|
+
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|
3
|
+
import {
|
|
4
|
+
collectSnapTargets,
|
|
5
|
+
FRAME_HANDLES,
|
|
6
|
+
type FrameHandle,
|
|
7
|
+
type FrameTagKind,
|
|
8
|
+
frameAttributesOf,
|
|
9
|
+
frameTagKind,
|
|
10
|
+
moveFrameBox,
|
|
11
|
+
type PageEdgesMm,
|
|
12
|
+
resizeFrameBox,
|
|
13
|
+
snapFrameBox,
|
|
14
|
+
} from "./document-frame-control.ts";
|
|
15
|
+
import { type DocumentFrameBox, documentFrameCss } from "./document-style-css.ts";
|
|
16
|
+
|
|
17
|
+
export type FrameViewLabels = Readonly<Record<FrameTagKind, string>>;
|
|
18
|
+
|
|
19
|
+
const PAPER_SELECTOR = "[data-document-paper]";
|
|
20
|
+
const PAPER_WIDTH_ATTRIBUTE = "data-paper-width-mm";
|
|
21
|
+
const PAPER_MARGIN_ATTRIBUTES = {
|
|
22
|
+
left: "data-paper-margin-left-mm",
|
|
23
|
+
right: "data-paper-margin-right-mm",
|
|
24
|
+
top: "data-paper-margin-top-mm",
|
|
25
|
+
} as const;
|
|
26
|
+
const GUIDE_CLASS = "pointer-events-none absolute z-30 bg-primary";
|
|
27
|
+
|
|
28
|
+
// Frame states are driven by the data-frame-selected attribute a decoration puts on the frame, so
|
|
29
|
+
// a cursor inside the frame and a selected frame look the same as the side panel does.
|
|
30
|
+
const FRAME_CLASS =
|
|
31
|
+
"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";
|
|
32
|
+
const TAG_CLASS =
|
|
33
|
+
"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";
|
|
34
|
+
const TINT_CLASS =
|
|
35
|
+
"pointer-events-none absolute inset-0 hidden bg-primary/5 group-data-[frame-selected]:block";
|
|
36
|
+
const HANDLE_BASE_CLASS =
|
|
37
|
+
"absolute z-20 hidden h-[7px] w-[7px] border-[1.5px] border-primary bg-background group-data-[frame-selected]:block";
|
|
38
|
+
|
|
39
|
+
const HANDLE_CLASS: Readonly<Record<FrameHandle, string>> = {
|
|
40
|
+
nw: "-left-1 -top-1 cursor-nwse-resize",
|
|
41
|
+
n: "-top-1 left-[calc(50%-3.5px)] cursor-ns-resize",
|
|
42
|
+
ne: "-right-1 -top-1 cursor-nesw-resize",
|
|
43
|
+
e: "-right-1 top-[calc(50%-3.5px)] cursor-ew-resize",
|
|
44
|
+
se: "-bottom-1 -right-1 cursor-nwse-resize",
|
|
45
|
+
s: "-bottom-1 left-[calc(50%-3.5px)] cursor-ns-resize",
|
|
46
|
+
sw: "-bottom-1 -left-1 cursor-nesw-resize",
|
|
47
|
+
w: "-left-1 top-[calc(50%-3.5px)] cursor-ew-resize",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function boxOf(node: ProseMirrorNode): DocumentFrameBox {
|
|
51
|
+
const { xMm, yMm, widthMm, heightMm } = frameAttributesOf(node);
|
|
52
|
+
return { xMm, yMm, widthMm, heightMm };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function mmPerPixelOf(element: HTMLElement): number | null {
|
|
56
|
+
const paper = element.closest(PAPER_SELECTOR);
|
|
57
|
+
if (paper === null) return null;
|
|
58
|
+
const widthMm = Number(paper.getAttribute(PAPER_WIDTH_ATTRIBUTE));
|
|
59
|
+
const widthPx = paper.getBoundingClientRect().width;
|
|
60
|
+
return widthMm > 0 && widthPx > 0 ? widthMm / widthPx : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function numberAttribute(element: Element, name: string): number {
|
|
64
|
+
return Number(element.getAttribute(name));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function pageEdgesOf(paper: Element): PageEdgesMm {
|
|
68
|
+
return {
|
|
69
|
+
widthMm: numberAttribute(paper, PAPER_WIDTH_ATTRIBUTE),
|
|
70
|
+
marginLeftMm: numberAttribute(paper, PAPER_MARGIN_ATTRIBUTES.left),
|
|
71
|
+
marginRightMm: numberAttribute(paper, PAPER_MARGIN_ATTRIBUTES.right),
|
|
72
|
+
marginTopMm: numberAttribute(paper, PAPER_MARGIN_ATTRIBUTES.top),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function guideElement(paper: Element, axis: "x" | "y", mm: number): HTMLDivElement {
|
|
77
|
+
const guide = paper.ownerDocument.createElement("div");
|
|
78
|
+
guide.className = GUIDE_CLASS;
|
|
79
|
+
guide.setAttribute("data-testid", "document-frame-guide");
|
|
80
|
+
const position = `calc(${mm} * var(--paper-mm))`;
|
|
81
|
+
if (axis === "x") {
|
|
82
|
+
guide.classList.add("top-0", "bottom-0", "w-px");
|
|
83
|
+
guide.style.left = position;
|
|
84
|
+
} else {
|
|
85
|
+
guide.classList.add("left-0", "right-0", "h-px");
|
|
86
|
+
guide.style.top = position;
|
|
87
|
+
}
|
|
88
|
+
paper.append(guide);
|
|
89
|
+
return guide;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function createDiv(className: string, testId: string): HTMLDivElement {
|
|
93
|
+
const element = document.createElement("div");
|
|
94
|
+
element.className = className;
|
|
95
|
+
element.setAttribute("data-testid", testId);
|
|
96
|
+
element.contentEditable = "false";
|
|
97
|
+
return element;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function createFrameView(labels: FrameViewLabels): NodeViewRenderer {
|
|
101
|
+
return ({ node: initialNode, editor, getPos }) => {
|
|
102
|
+
let node = initialNode;
|
|
103
|
+
const dom = document.createElement("div");
|
|
104
|
+
dom.className = FRAME_CLASS;
|
|
105
|
+
dom.setAttribute("data-testid", "document-frame");
|
|
106
|
+
const tag = createDiv(TAG_CLASS, "document-frame-move");
|
|
107
|
+
const tint = createDiv(TINT_CLASS, "document-frame-tint");
|
|
108
|
+
const contentDOM = document.createElement("div");
|
|
109
|
+
contentDOM.className = "h-full overflow-hidden [&>:last-child]:mb-0";
|
|
110
|
+
dom.append(tint, tag, contentDOM);
|
|
111
|
+
|
|
112
|
+
const applyBox = (box: DocumentFrameBox): void => {
|
|
113
|
+
dom.style.cssText = documentFrameCss(box);
|
|
114
|
+
// The tag and handles sit outside the box; only the content is clipped.
|
|
115
|
+
dom.style.overflow = "visible";
|
|
116
|
+
};
|
|
117
|
+
const render = (): void => {
|
|
118
|
+
const attrs = frameAttributesOf(node);
|
|
119
|
+
applyBox(boxOf(node));
|
|
120
|
+
dom.setAttribute("data-frame-preset", attrs.preset ?? "");
|
|
121
|
+
tag.textContent = labels[frameTagKind(attrs.preset, attrs.yMm)];
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
let cancelGesture: (() => void) | null = null;
|
|
125
|
+
const startGesture = (event: PointerEvent, handle: FrameHandle | "move"): void => {
|
|
126
|
+
const mmPerPixel = mmPerPixelOf(dom);
|
|
127
|
+
const pos = getPos();
|
|
128
|
+
if (mmPerPixel === null || pos === undefined) return;
|
|
129
|
+
event.preventDefault();
|
|
130
|
+
event.stopPropagation();
|
|
131
|
+
editor.commands.setNodeSelection(pos);
|
|
132
|
+
const start = boxOf(node);
|
|
133
|
+
let preview = start;
|
|
134
|
+
const paper = dom.closest(PAPER_SELECTOR);
|
|
135
|
+
const targets =
|
|
136
|
+
paper === null ? null : collectSnapTargets(editor.state.doc, pos, pageEdgesOf(paper));
|
|
137
|
+
let guides: HTMLDivElement[] = [];
|
|
138
|
+
const clearGuides = (): void => {
|
|
139
|
+
for (const guide of guides) guide.remove();
|
|
140
|
+
guides = [];
|
|
141
|
+
};
|
|
142
|
+
const onMove = (move: PointerEvent): void => {
|
|
143
|
+
const dx = (move.clientX - event.clientX) * mmPerPixel;
|
|
144
|
+
const dy = (move.clientY - event.clientY) * mmPerPixel;
|
|
145
|
+
const raw =
|
|
146
|
+
handle === "move" ? moveFrameBox(start, dx, dy) : resizeFrameBox(start, handle, dx, dy);
|
|
147
|
+
clearGuides();
|
|
148
|
+
if (paper === null || targets === null || move.altKey) {
|
|
149
|
+
preview = raw;
|
|
150
|
+
} else {
|
|
151
|
+
const snapped = snapFrameBox(raw, handle, targets);
|
|
152
|
+
preview = snapped.box;
|
|
153
|
+
if (snapped.guides.xMm !== null)
|
|
154
|
+
guides.push(guideElement(paper, "x", snapped.guides.xMm));
|
|
155
|
+
if (snapped.guides.yMm !== null)
|
|
156
|
+
guides.push(guideElement(paper, "y", snapped.guides.yMm));
|
|
157
|
+
}
|
|
158
|
+
applyBox(preview);
|
|
159
|
+
};
|
|
160
|
+
const stop = (): void => {
|
|
161
|
+
clearGuides();
|
|
162
|
+
dom.ownerDocument.removeEventListener("pointermove", onMove);
|
|
163
|
+
dom.ownerDocument.removeEventListener("pointerup", onUp);
|
|
164
|
+
cancelGesture = null;
|
|
165
|
+
};
|
|
166
|
+
const onUp = (): void => {
|
|
167
|
+
stop();
|
|
168
|
+
const current = getPos();
|
|
169
|
+
if (current === undefined) return;
|
|
170
|
+
editor.view.dispatch(
|
|
171
|
+
editor.state.tr.setNodeMarkup(current, undefined, { ...node.attrs, ...preview }),
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
dom.ownerDocument.addEventListener("pointermove", onMove);
|
|
175
|
+
dom.ownerDocument.addEventListener("pointerup", onUp);
|
|
176
|
+
cancelGesture = stop;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
tag.addEventListener("pointerdown", (event) => startGesture(event, "move"));
|
|
180
|
+
if (editor.isEditable) {
|
|
181
|
+
for (const handle of FRAME_HANDLES) {
|
|
182
|
+
const element = createDiv(
|
|
183
|
+
`${HANDLE_BASE_CLASS} ${HANDLE_CLASS[handle]}`,
|
|
184
|
+
`document-frame-handle-${handle}`,
|
|
185
|
+
);
|
|
186
|
+
element.addEventListener("pointerdown", (event) => startGesture(event, handle));
|
|
187
|
+
dom.append(element);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
render();
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
dom,
|
|
194
|
+
contentDOM,
|
|
195
|
+
update: (updated) => {
|
|
196
|
+
if (updated.type !== node.type) return false;
|
|
197
|
+
node = updated;
|
|
198
|
+
render();
|
|
199
|
+
return true;
|
|
200
|
+
},
|
|
201
|
+
ignoreMutation: (mutation) =>
|
|
202
|
+
mutation.type === "selection" ? false : !contentDOM.contains(mutation.target),
|
|
203
|
+
stopEvent: (event) => event.target instanceof Node && !contentDOM.contains(event.target),
|
|
204
|
+
destroy: () => cancelGesture?.(),
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
|
|
5
|
+
import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
6
|
+
import { Extension } from "@tiptap/core";
|
|
7
|
+
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|
8
|
+
import { Plugin, PluginKey, type Selection } from "@tiptap/pm/state";
|
|
9
|
+
import { Decoration, DecorationSet } from "@tiptap/pm/view";
|
|
10
|
+
import type { DocumentFont } from "./document-fonts.ts";
|
|
11
|
+
import { DOCUMENT_FONTS } from "./document-fonts.ts";
|
|
12
|
+
import type { DocumentFrameControl, FrameLocation } from "./document-frame-control.ts";
|
|
13
|
+
import {
|
|
14
|
+
buildFrameControl,
|
|
15
|
+
FRAME_NODE_NAME,
|
|
16
|
+
FRAME_SELECTED_ATTRIBUTE,
|
|
17
|
+
frameFlowBottomMm,
|
|
18
|
+
frameLocationOf,
|
|
19
|
+
isSenderLine,
|
|
20
|
+
} from "./document-frame-control.ts";
|
|
21
|
+
import { DOCUMENT_TABLE_ALIGNMENTS, readRowLineAttributes } from "./document-print-style.ts";
|
|
22
|
+
import { type RepeatStripOptions, repeatStripDecorations } from "./document-repeat-strip.ts";
|
|
23
|
+
import {
|
|
24
|
+
addressParagraphCss,
|
|
25
|
+
DOCUMENT_LINE_VARIABLE,
|
|
26
|
+
documentBlockStyleCss,
|
|
27
|
+
documentCellStyleCss,
|
|
28
|
+
documentDividerCss,
|
|
29
|
+
documentTableCss,
|
|
30
|
+
documentTextStyleCss,
|
|
31
|
+
PARAGRAPH_DEFAULT_LINE_HEIGHT,
|
|
32
|
+
resolveRowEdges,
|
|
33
|
+
} from "./document-style-css.ts";
|
|
34
|
+
|
|
35
|
+
// Metric-compatible web fonts (@fontsource) stand in for the print service's installed fonts.
|
|
36
|
+
const EDITOR_FONT_STACKS: Readonly<Record<DocumentFont, string>> = {
|
|
37
|
+
arial: 'Arimo,"Liberation Sans",Arial,sans-serif',
|
|
38
|
+
times: 'Tinos,"Liberation Serif","Times New Roman",serif',
|
|
39
|
+
courier: 'Cousine,"Liberation Mono","Courier New",monospace',
|
|
40
|
+
calibri: "Carlito,Calibri,sans-serif",
|
|
41
|
+
cambria: "Caladea,Cambria,serif",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Fonts are cosmetic: the stacks fall back to system fonts when the chunk fails to load.
|
|
45
|
+
export function loadDocumentEditorFonts(): Promise<unknown> {
|
|
46
|
+
return import("./document-editor-fonts.ts").catch(() => undefined);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const TEXT_STYLE_MARK = "documentTextStyle";
|
|
50
|
+
const BLOCK_STYLE_NODES: readonly string[] = ["paragraph", "heading"];
|
|
51
|
+
const CELL_NODES: readonly string[] = ["tableCell", "tableHeader"];
|
|
52
|
+
|
|
53
|
+
function numberOrNull(value: unknown): number | null {
|
|
54
|
+
return typeof value === "number" ? value : null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function stringOrNull(value: unknown): string | null {
|
|
58
|
+
return typeof value === "string" ? value : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function textStyleDecoration(node: ProseMirrorNode, from: number): Decoration | null {
|
|
62
|
+
const mark = node.marks.find((candidate) => candidate.type.name === TEXT_STYLE_MARK);
|
|
63
|
+
if (mark === undefined) return null;
|
|
64
|
+
const font = mark.attrs["font"];
|
|
65
|
+
const declarations = [
|
|
66
|
+
documentTextStyleCss({
|
|
67
|
+
color: stringOrNull(mark.attrs["color"]),
|
|
68
|
+
sizePt: numberOrNull(mark.attrs["sizePt"]),
|
|
69
|
+
}),
|
|
70
|
+
isOneOf(DOCUMENT_FONTS, font) ? `font-family:${EDITOR_FONT_STACKS[font]}` : "",
|
|
71
|
+
].filter((part) => part !== "");
|
|
72
|
+
return declarations.length === 0
|
|
73
|
+
? null
|
|
74
|
+
: Decoration.inline(from, from + node.nodeSize, { style: declarations.join(";") });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isAddressFrame(node: ProseMirrorNode | null): boolean {
|
|
78
|
+
return node?.type.name === FRAME_NODE_NAME && node.attrs["preset"] === "address";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function nodeDecoration(
|
|
82
|
+
node: ProseMirrorNode,
|
|
83
|
+
from: number,
|
|
84
|
+
parent: ProseMirrorNode | null,
|
|
85
|
+
index: number,
|
|
86
|
+
): Decoration | null {
|
|
87
|
+
const name = node.type.name;
|
|
88
|
+
const to = from + node.nodeSize;
|
|
89
|
+
if (BLOCK_STYLE_NODES.includes(name)) {
|
|
90
|
+
const style = [
|
|
91
|
+
documentBlockStyleCss({
|
|
92
|
+
lineHeight: numberOrNull(node.attrs["lineHeight"]),
|
|
93
|
+
spaceBeforePt: numberOrNull(node.attrs["spaceBeforePt"]),
|
|
94
|
+
spaceAfterPt: numberOrNull(node.attrs["spaceAfterPt"]),
|
|
95
|
+
}),
|
|
96
|
+
name === "paragraph" && isAddressFrame(parent)
|
|
97
|
+
? addressParagraphCss(index === 0 && isSenderLine(node))
|
|
98
|
+
: "",
|
|
99
|
+
]
|
|
100
|
+
.filter((part) => part !== "")
|
|
101
|
+
.join(";");
|
|
102
|
+
return style === "" ? null : Decoration.node(from, to, { style });
|
|
103
|
+
}
|
|
104
|
+
if (name === "divider") {
|
|
105
|
+
const style = documentDividerCss({
|
|
106
|
+
color: stringOrNull(node.attrs["color"]),
|
|
107
|
+
weightPt: numberOrNull(node.attrs["weightPt"]),
|
|
108
|
+
});
|
|
109
|
+
return style === "" ? null : Decoration.node(from, to, { style });
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Cell padding, outer-column insets and row rules depend on the table and row around a cell, so
|
|
115
|
+
// the table decorates its own cells; the rules mirror the PDF compile's edge resolution.
|
|
116
|
+
function tableDecorations(table: ProseMirrorNode, from: number): Decoration[] {
|
|
117
|
+
const align = table.attrs["align"];
|
|
118
|
+
const tableStyle = documentTableCss({
|
|
119
|
+
widthMm: numberOrNull(table.attrs["widthMm"]),
|
|
120
|
+
align: isOneOf(DOCUMENT_TABLE_ALIGNMENTS, align) ? align : null,
|
|
121
|
+
});
|
|
122
|
+
const decorations: Decoration[] = [];
|
|
123
|
+
const tableAttrs: Record<string, string> = {
|
|
124
|
+
...(table.attrs["borders"] === "none" ? { "data-borders": "none" } : {}),
|
|
125
|
+
...(tableStyle === "" ? {} : { style: tableStyle }),
|
|
126
|
+
};
|
|
127
|
+
if (Object.keys(tableAttrs).length > 0) {
|
|
128
|
+
decorations.push(Decoration.node(from, from + table.nodeSize, tableAttrs));
|
|
129
|
+
}
|
|
130
|
+
const paddingMm = numberOrNull(table.attrs["cellPaddingMm"]);
|
|
131
|
+
const rows: { node: ProseMirrorNode; pos: number }[] = [];
|
|
132
|
+
table.forEach((row, offset) => {
|
|
133
|
+
rows.push({ node: row, pos: from + 1 + offset });
|
|
134
|
+
});
|
|
135
|
+
const edges = resolveRowEdges(
|
|
136
|
+
rows.map((row) => ({
|
|
137
|
+
...readRowLineAttributes(row.node.attrs),
|
|
138
|
+
repeats: typeof row.node.attrs["each"] === "string",
|
|
139
|
+
})),
|
|
140
|
+
);
|
|
141
|
+
rows.forEach((row, rowIndex) => {
|
|
142
|
+
row.node.forEach((cell, offset, cellIndex) => {
|
|
143
|
+
if (!CELL_NODES.includes(cell.type.name)) return;
|
|
144
|
+
const style = documentCellStyleCss({
|
|
145
|
+
background: stringOrNull(cell.attrs["background"]),
|
|
146
|
+
paddingMm,
|
|
147
|
+
isFirstInRow: cellIndex === 0,
|
|
148
|
+
isLastInRow: cellIndex === row.node.childCount - 1,
|
|
149
|
+
edges: edges[rowIndex],
|
|
150
|
+
});
|
|
151
|
+
if (style === "") return;
|
|
152
|
+
const cellFrom = row.pos + 1 + offset;
|
|
153
|
+
decorations.push(Decoration.node(cellFrom, cellFrom + cell.nodeSize, { style }));
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
return decorations;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Frames are not rebuilt by the selection alone, so the selected one is marked by a decoration the
|
|
160
|
+
// node view styles; the same location drives the side panel.
|
|
161
|
+
function buildDecorations(
|
|
162
|
+
doc: ProseMirrorNode,
|
|
163
|
+
selection: Selection,
|
|
164
|
+
extra: readonly Decoration[],
|
|
165
|
+
): DecorationSet {
|
|
166
|
+
const decorations: Decoration[] = [];
|
|
167
|
+
const selectedFrame = frameLocationOf(selection);
|
|
168
|
+
doc.descendants((node, pos, parent, index) => {
|
|
169
|
+
if (node.type.name === FRAME_NODE_NAME && selectedFrame?.pos === pos) {
|
|
170
|
+
decorations.push(
|
|
171
|
+
Decoration.node(pos, pos + node.nodeSize, { [FRAME_SELECTED_ATTRIBUTE]: "" }),
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
if (node.type.name === "table") decorations.push(...tableDecorations(node, pos));
|
|
175
|
+
const decoration = node.isInline
|
|
176
|
+
? textStyleDecoration(node, pos)
|
|
177
|
+
: nodeDecoration(node, pos, parent, index);
|
|
178
|
+
if (decoration !== null) decorations.push(decoration);
|
|
179
|
+
});
|
|
180
|
+
return DecorationSet.create(doc, [...decorations, ...extra]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const FLOW_PADDING_CSS =
|
|
184
|
+
"padding-top:max(0px,calc((var(--frame-flow-bottom) - var(--page-margin-top-mm,0)) * var(--paper-mm,1mm)))";
|
|
185
|
+
// The paper's own text size and the shared paragraph defaults, so editor lines measure like the PDF's.
|
|
186
|
+
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})`;
|
|
187
|
+
|
|
188
|
+
export type DocumentPrintEditingOptions = {
|
|
189
|
+
/** Frames exist (PDF body): reserve the flow area below frames that push the text down. */
|
|
190
|
+
readonly frames: boolean;
|
|
191
|
+
readonly reportFrameControl: ((control: DocumentFrameControl | null) => void) | null;
|
|
192
|
+
/** Shows the table strip that switches the repeated header row; null renders none. */
|
|
193
|
+
readonly repeatStrip: RepeatStripOptions | null;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const DECORATION_KEY = new PluginKey<DecorationSet>("documentPrintDecorations");
|
|
197
|
+
|
|
198
|
+
export const DocumentPrintEditing = Extension.create<DocumentPrintEditingOptions>({
|
|
199
|
+
name: "documentPrintEditing",
|
|
200
|
+
addOptions() {
|
|
201
|
+
return { frames: false, reportFrameControl: null, repeatStrip: null };
|
|
202
|
+
},
|
|
203
|
+
addProseMirrorPlugins() {
|
|
204
|
+
const { frames, reportFrameControl, repeatStrip } = this.options;
|
|
205
|
+
const { editor } = this;
|
|
206
|
+
const stripOf = (doc: ProseMirrorNode): readonly Decoration[] =>
|
|
207
|
+
repeatStrip === null || !editor.options.editable
|
|
208
|
+
? []
|
|
209
|
+
: repeatStripDecorations(doc, editor, repeatStrip);
|
|
210
|
+
const decorations = new Plugin<DecorationSet>({
|
|
211
|
+
key: DECORATION_KEY,
|
|
212
|
+
state: {
|
|
213
|
+
init: (_config, state) => buildDecorations(state.doc, state.selection, stripOf(state.doc)),
|
|
214
|
+
apply: (tr, previous) =>
|
|
215
|
+
tr.docChanged || tr.selectionSet
|
|
216
|
+
? buildDecorations(tr.doc, tr.selection, stripOf(tr.doc))
|
|
217
|
+
: previous,
|
|
218
|
+
},
|
|
219
|
+
props: {
|
|
220
|
+
decorations: (state) => DECORATION_KEY.getState(state),
|
|
221
|
+
attributes: (state): Record<string, string> =>
|
|
222
|
+
frames
|
|
223
|
+
? {
|
|
224
|
+
style: `--frame-flow-bottom:${frameFlowBottomMm(state.doc)};${FLOW_PADDING_CSS};${PAGE_TEXT_CSS}`,
|
|
225
|
+
}
|
|
226
|
+
: {},
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
if (!frames || reportFrameControl === null) return [decorations];
|
|
230
|
+
const reporter = new Plugin({
|
|
231
|
+
view: () => {
|
|
232
|
+
let last: FrameLocation | null = null;
|
|
233
|
+
return {
|
|
234
|
+
update: (view) => {
|
|
235
|
+
const location = view.editable ? frameLocationOf(view.state.selection) : null;
|
|
236
|
+
if (location?.pos === last?.pos && location?.node === last?.node) return;
|
|
237
|
+
last = location;
|
|
238
|
+
reportFrameControl(location === null ? null : buildFrameControl(editor, location));
|
|
239
|
+
},
|
|
240
|
+
destroy: () => reportFrameControl(null),
|
|
241
|
+
};
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
return [decorations, reporter];
|
|
245
|
+
},
|
|
246
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Translate } from "@cosmicdrift/kumiko-renderer/headless";
|
|
2
|
+
import { DOCUMENT_FONT_LABELS } from "./document-fonts.ts";
|
|
3
|
+
import { documentLinePt, PARAGRAPH_DEFAULT_LINE_HEIGHT } from "./document-style-css.ts";
|
|
4
|
+
import type { DocumentLayoutValues } from "./document-toolbar-blocks.ts";
|
|
5
|
+
import { PDF_DEFAULT_FONT_SIZE_PT } from "./pdf-constants.ts";
|
|
6
|
+
|
|
7
|
+
export type PrintDefaultLabels = {
|
|
8
|
+
readonly font: string;
|
|
9
|
+
readonly size: string;
|
|
10
|
+
readonly lineHeight: string;
|
|
11
|
+
readonly spaceBefore: string;
|
|
12
|
+
readonly spaceAfter: string;
|
|
13
|
+
readonly formatSize: (value: number) => string;
|
|
14
|
+
readonly formatLineHeight: (value: number) => string;
|
|
15
|
+
readonly formatSpaceBefore: (value: number) => string;
|
|
16
|
+
readonly formatSpaceAfter: (value: number) => string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// The first option of every print select stands for "inherit"; it shows the value the layout
|
|
20
|
+
// gives, or the control's own name when the layout is not known (mail, no layout).
|
|
21
|
+
export function printDefaultLabelsOf(
|
|
22
|
+
t: Translate,
|
|
23
|
+
locale: string,
|
|
24
|
+
values: DocumentLayoutValues,
|
|
25
|
+
): PrintDefaultLabels {
|
|
26
|
+
const formatNumber = (value: number): string => new Intl.NumberFormat(locale).format(value);
|
|
27
|
+
const formatSize = (value: number): string =>
|
|
28
|
+
t("documentDesigner.editor.defaultSize", { value: formatNumber(value) });
|
|
29
|
+
const formatLineHeight = (value: number): string =>
|
|
30
|
+
t("documentDesigner.editor.defaultLineHeight", { value: formatNumber(value) });
|
|
31
|
+
const formatSpaceBefore = (value: number): string =>
|
|
32
|
+
t("documentDesigner.editor.defaultSpaceBefore", { value: formatNumber(value) });
|
|
33
|
+
const formatSpaceAfter = (value: number): string =>
|
|
34
|
+
t("documentDesigner.editor.defaultSpaceAfter", { value: formatNumber(value) });
|
|
35
|
+
const known = <T>(value: T | undefined, label: (known: T) => string, fallbackKey: string) =>
|
|
36
|
+
value === undefined ? t(fallbackKey) : label(value);
|
|
37
|
+
return {
|
|
38
|
+
font: known(
|
|
39
|
+
values.fontFamily,
|
|
40
|
+
(font) => DOCUMENT_FONT_LABELS[font],
|
|
41
|
+
"documentDesigner.editor.font",
|
|
42
|
+
),
|
|
43
|
+
size: known(values.fontSizePt, formatSize, "documentDesigner.editor.fontSize"),
|
|
44
|
+
// Without a layout value the paragraph still has the shared print default, so show that.
|
|
45
|
+
lineHeight: formatLineHeight(values.lineHeight ?? PARAGRAPH_DEFAULT_LINE_HEIGHT),
|
|
46
|
+
spaceBefore: known(
|
|
47
|
+
values.spaceBeforePt,
|
|
48
|
+
formatSpaceBefore,
|
|
49
|
+
"documentDesigner.editor.spaceBeforeShort",
|
|
50
|
+
),
|
|
51
|
+
spaceAfter: formatSpaceAfter(
|
|
52
|
+
values.spaceAfterPt ?? documentLinePt(values.fontSizePt ?? PDF_DEFAULT_FONT_SIZE_PT),
|
|
53
|
+
),
|
|
54
|
+
formatSize,
|
|
55
|
+
formatLineHeight,
|
|
56
|
+
formatSpaceBefore,
|
|
57
|
+
formatSpaceAfter,
|
|
58
|
+
};
|
|
59
|
+
}
|