@cosmicdrift/kumiko-samples 0.417.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.
- package/package.json +1 -1
- package/packages/bundled-features/package.json +6 -1
- package/packages/bundled-features/src/agent-tools/api-docs/schema-fields.ts +14 -0
- package/packages/bundled-features/src/agent-tools/changes.json +6 -0
- package/packages/bundled-features/src/auth-email-password/index.ts +1 -1
- package/packages/bundled-features/src/auth-email-password/signup-activation.ts +2 -5
- package/packages/bundled-features/src/auth-email-password/signup-token-store.ts +0 -6
- package/packages/bundled-features/src/cap-counter/changes.json +6 -0
- package/packages/bundled-features/src/cap-counter/stock-cap-guard.ts +46 -13
- package/packages/bundled-features/src/channel-whatsapp/changes.json +1 -1
- package/packages/bundled-features/src/document-ingest-foundation/changes.json +6 -0
- package/packages/bundled-features/src/document-ingest-foundation/feature.ts +13 -1
- package/packages/bundled-features/src/document-ingest-foundation/report-ingest-progress.ts +23 -6
- package/packages/bundled-features/src/signature/README.md +4 -4
- package/packages/bundled-features/src/signature/changes.json +18 -0
- package/packages/bundled-features/src/signature/entity.ts +3 -1
- package/packages/bundled-features/src/signature/feature.ts +16 -3
- package/packages/bundled-features/src/signature/handlers/lifecycle.write.ts +33 -1
- package/packages/bundled-features/src/signature/i18n.ts +1 -0
- package/packages/bundled-features/src/signature/list-cursor-seal.ts +59 -0
- package/packages/bundled-features/src/signature/options.ts +2 -2
- package/packages/bundled-features/src/signature/payloads.ts +14 -2
- package/packages/bundled-features/src/signature/png.ts +25 -4
- package/packages/bundled-features/src/signature/subject-queries.ts +47 -7
- package/packages/bundled-features/src/signature/sweep.ts +32 -8
- package/packages/bundled-features/src/signature/web/constants.ts +12 -0
- package/packages/bundled-features/src/subscription-paddle/changes.json +1 -1
- package/packages/bundled-features/src/subscription-stripe/changes.json +6 -0
- package/packages/bundled-features/src/subscription-stripe/plugin-methods.ts +2 -0
- package/packages/bundled-features/src/subscription-stripe/verify-webhook.ts +21 -16
- package/packages/bundled-features/src/template-resolver/changes.json +38 -0
- package/packages/bundled-features/src/template-resolver/compile-document-body.ts +3 -8
- package/packages/bundled-features/src/template-resolver/compile-document-styles.ts +102 -0
- package/packages/bundled-features/src/template-resolver/document-frames.ts +25 -0
- package/packages/bundled-features/src/template-resolver/document-op-blocks.ts +568 -0
- package/packages/bundled-features/src/template-resolver/document-ops.ts +136 -126
- package/packages/bundled-features/src/template-resolver/document-print-only.ts +41 -0
- package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +41 -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 +19 -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 +122 -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 +29 -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 +25 -6
- package/packages/bundled-features/src/template-resolver/template-preview.ts +6 -7
- package/packages/bundled-features/src/template-resolver/template-validation.ts +10 -3
- package/packages/bundled-features/src/template-resolver/web/constants.ts +29 -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 +34 -2
- package/packages/bundled-features/src/template-resolver/web/document-editor-fonts.ts +13 -0
- 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 +196 -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 +246 -0
- package/packages/bundled-features/src/template-resolver/web/document-repeat-strip.ts +104 -0
- package/packages/bundled-features/src/template-resolver/web/document-schema.ts +29 -9
- package/packages/bundled-features/src/template-resolver/web/document-style-css.ts +107 -0
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-bubble.tsx +24 -2
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-content.tsx +66 -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 +24 -4
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-helpers.ts +149 -4
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar-groups.tsx +290 -10
- package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar.tsx +90 -8
- 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 +28 -0
- package/packages/bundled-features/src/template-resolver/web/index.ts +34 -0
- package/packages/bundled-features/src/template-resolver/web/pdf-constants.ts +6 -0
- 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/packages/bundled-features/src/tenant-inactivity/handlers/keep-account.write.ts +7 -3
- package/packages/bundled-features/src/tenant-lifecycle/changes.json +6 -0
- package/packages/bundled-features/src/tenant-lifecycle/run-tenant-destroy.ts +90 -42
- 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/run-config.ts +1 -0
- package/samples/apps/use-all-bundled/feature-manifest.json +238 -1
- package/samples/recipes/crypto-shredding-hr/README.md +1 -1
- package/samples/recipes/crypto-shredding-hr/src/feature.ts +1 -1
- package/samples/recipes/list-table-options/README.md +9 -3
- package/samples/recipes/list-table-options/src/feature.ts +123 -3
- 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,428 @@
|
|
|
1
|
+
import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
2
|
+
import type { Editor, JSONContent } from "@tiptap/core";
|
|
3
|
+
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|
4
|
+
import type { Selection } from "@tiptap/pm/state";
|
|
5
|
+
import { type DocumentFrameAttributes, FRAME_PRESETS } from "./document-print-style.ts";
|
|
6
|
+
import {
|
|
7
|
+
type DocumentFrameBox,
|
|
8
|
+
FRAME_FLOW_GAP_MM,
|
|
9
|
+
SENDER_LINE_SIZE_PT,
|
|
10
|
+
} from "./document-style-css.ts";
|
|
11
|
+
|
|
12
|
+
export type DocumentFrameControl = {
|
|
13
|
+
readonly attrs: DocumentFrameAttributes;
|
|
14
|
+
readonly update: (patch: Partial<DocumentFrameAttributes>) => void;
|
|
15
|
+
readonly remove: () => void;
|
|
16
|
+
readonly senderLine: boolean;
|
|
17
|
+
readonly setSenderLine: (on: boolean) => void;
|
|
18
|
+
/** Variable name of the placeholder in the last paragraph, null without one. */
|
|
19
|
+
readonly recipient: string | null;
|
|
20
|
+
readonly setRecipient: (variableName: string | null) => void;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const FRAME_NODE_NAME = "documentFrame";
|
|
24
|
+
export const FRAME_SELECTED_ATTRIBUTE = "data-frame-selected";
|
|
25
|
+
export const FRAME_STEP_MM = 0.5;
|
|
26
|
+
export const FRAME_MIN_SIZE_MM = 10;
|
|
27
|
+
export const FRAME_HANDLES = ["nw", "n", "ne", "e", "se", "s", "sw", "w"] as const;
|
|
28
|
+
export type FrameHandle = (typeof FRAME_HANDLES)[number];
|
|
29
|
+
|
|
30
|
+
const TEXT_STYLE_MARK = "documentTextStyle";
|
|
31
|
+
const DIN_5008_FORM_A_Y_MM = 27;
|
|
32
|
+
const DIN_5008_FORM_B_Y_MM = 45;
|
|
33
|
+
|
|
34
|
+
const ADDRESS_FRAME_BOX: DocumentFrameBox = { xMm: 20, yMm: 45, widthMm: 85, heightMm: 45 };
|
|
35
|
+
const TEXT_FRAME_BOX: DocumentFrameBox = { xMm: 125, yMm: 50, widthMm: 65, heightMm: 34 };
|
|
36
|
+
|
|
37
|
+
export function roundToHalfMm(value: number): number {
|
|
38
|
+
return Math.round(value / FRAME_STEP_MM) * FRAME_STEP_MM;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function moveFrameBox(box: DocumentFrameBox, dxMm: number, dyMm: number): DocumentFrameBox {
|
|
42
|
+
return {
|
|
43
|
+
...box,
|
|
44
|
+
xMm: Math.max(0, roundToHalfMm(box.xMm + dxMm)),
|
|
45
|
+
yMm: Math.max(0, roundToHalfMm(box.yMm + dyMm)),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The edge opposite the dragged corner stays put, so a clamp never shifts the whole frame.
|
|
50
|
+
export function resizeFrameBox(
|
|
51
|
+
box: DocumentFrameBox,
|
|
52
|
+
handle: FrameHandle,
|
|
53
|
+
dxMm: number,
|
|
54
|
+
dyMm: number,
|
|
55
|
+
): DocumentFrameBox {
|
|
56
|
+
const right = box.xMm + box.widthMm;
|
|
57
|
+
const bottom = box.yMm + box.heightMm;
|
|
58
|
+
const movesLeft = handle === "nw" || handle === "sw" || handle === "w";
|
|
59
|
+
const movesRight = handle === "ne" || handle === "se" || handle === "e";
|
|
60
|
+
const movesTop = handle === "nw" || handle === "ne" || handle === "n";
|
|
61
|
+
const movesBottom = handle === "sw" || handle === "se" || handle === "s";
|
|
62
|
+
const left = movesLeft
|
|
63
|
+
? Math.min(Math.max(0, roundToHalfMm(box.xMm + dxMm)), right - FRAME_MIN_SIZE_MM)
|
|
64
|
+
: box.xMm;
|
|
65
|
+
const top = movesTop
|
|
66
|
+
? Math.min(Math.max(0, roundToHalfMm(box.yMm + dyMm)), bottom - FRAME_MIN_SIZE_MM)
|
|
67
|
+
: box.yMm;
|
|
68
|
+
const newRight = movesRight
|
|
69
|
+
? Math.max(left + FRAME_MIN_SIZE_MM, roundToHalfMm(right + dxMm))
|
|
70
|
+
: right;
|
|
71
|
+
const newBottom = movesBottom
|
|
72
|
+
? Math.max(top + FRAME_MIN_SIZE_MM, roundToHalfMm(bottom + dyMm))
|
|
73
|
+
: bottom;
|
|
74
|
+
return { xMm: left, yMm: top, widthMm: newRight - left, heightMm: newBottom - top };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const FRAME_SNAP_TOLERANCE_MM = 1.5;
|
|
78
|
+
|
|
79
|
+
export type FrameSnapTargets = {
|
|
80
|
+
readonly xLinesMm: readonly number[];
|
|
81
|
+
readonly yLinesMm: readonly number[];
|
|
82
|
+
readonly widthsMm: readonly number[];
|
|
83
|
+
readonly heightsMm: readonly number[];
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export type FrameGuides = { readonly xMm: number | null; readonly yMm: number | null };
|
|
87
|
+
|
|
88
|
+
type AxisMode = "move" | "low" | "high" | "none";
|
|
89
|
+
type AxisSnap = { readonly start: number; readonly size: number; readonly guide: number | null };
|
|
90
|
+
|
|
91
|
+
function nearestWithin(
|
|
92
|
+
value: number,
|
|
93
|
+
candidates: readonly number[],
|
|
94
|
+
toleranceMm: number,
|
|
95
|
+
): number | null {
|
|
96
|
+
let best: number | null = null;
|
|
97
|
+
for (const candidate of candidates) {
|
|
98
|
+
const distance = Math.abs(candidate - value);
|
|
99
|
+
if (distance <= toleranceMm && (best === null || distance < Math.abs(best - value))) {
|
|
100
|
+
best = candidate;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return best;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function snapMove(
|
|
107
|
+
start: number,
|
|
108
|
+
size: number,
|
|
109
|
+
lines: readonly number[],
|
|
110
|
+
toleranceMm: number,
|
|
111
|
+
): AxisSnap {
|
|
112
|
+
const unchanged: AxisSnap = { start, size, guide: null };
|
|
113
|
+
const end = start + size;
|
|
114
|
+
const leading = nearestWithin(start, lines, toleranceMm);
|
|
115
|
+
const trailing = nearestWithin(end, lines, toleranceMm);
|
|
116
|
+
const leadingDistance = leading === null ? Infinity : Math.abs(leading - start);
|
|
117
|
+
const trailingDistance = trailing === null ? Infinity : Math.abs(trailing - end);
|
|
118
|
+
if (leading !== null && leadingDistance <= trailingDistance) {
|
|
119
|
+
return { start: leading, size, guide: leading };
|
|
120
|
+
}
|
|
121
|
+
if (trailing !== null) {
|
|
122
|
+
const snappedStart = trailing - size;
|
|
123
|
+
return snappedStart >= 0 ? { start: snappedStart, size, guide: trailing } : unchanged;
|
|
124
|
+
}
|
|
125
|
+
return unchanged;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The dragged edge snaps to a line or the size to another frame's size; the fixed edge stays.
|
|
129
|
+
function snapResize(
|
|
130
|
+
start: number,
|
|
131
|
+
size: number,
|
|
132
|
+
mode: "low" | "high",
|
|
133
|
+
lines: readonly number[],
|
|
134
|
+
sizes: readonly number[],
|
|
135
|
+
toleranceMm: number,
|
|
136
|
+
): AxisSnap {
|
|
137
|
+
const unchanged: AxisSnap = { start, size, guide: null };
|
|
138
|
+
const end = start + size;
|
|
139
|
+
const dragged = mode === "low" ? start : end;
|
|
140
|
+
const fixed = mode === "low" ? end : start;
|
|
141
|
+
const line = nearestWithin(dragged, lines, toleranceMm);
|
|
142
|
+
const matchedSize = nearestWithin(size, sizes, toleranceMm);
|
|
143
|
+
const lineDistance = line === null ? Infinity : Math.abs(line - dragged);
|
|
144
|
+
const sizeDistance = matchedSize === null ? Infinity : Math.abs(matchedSize - size);
|
|
145
|
+
const resolve = (candidateSize: number, guide: number | null): AxisSnap => {
|
|
146
|
+
const candidateStart = mode === "low" ? fixed - candidateSize : start;
|
|
147
|
+
return candidateSize >= FRAME_MIN_SIZE_MM && candidateStart >= 0
|
|
148
|
+
? { start: candidateStart, size: candidateSize, guide }
|
|
149
|
+
: unchanged;
|
|
150
|
+
};
|
|
151
|
+
if (line !== null && lineDistance <= sizeDistance) {
|
|
152
|
+
return resolve(mode === "low" ? fixed - line : line - fixed, line);
|
|
153
|
+
}
|
|
154
|
+
if (matchedSize !== null) return resolve(matchedSize, null);
|
|
155
|
+
return unchanged;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function snapAxis(
|
|
159
|
+
start: number,
|
|
160
|
+
size: number,
|
|
161
|
+
mode: AxisMode,
|
|
162
|
+
lines: readonly number[],
|
|
163
|
+
sizes: readonly number[],
|
|
164
|
+
toleranceMm: number,
|
|
165
|
+
): AxisSnap {
|
|
166
|
+
if (mode === "none") return { start, size, guide: null };
|
|
167
|
+
if (mode === "move") return snapMove(start, size, lines, toleranceMm);
|
|
168
|
+
return snapResize(start, size, mode, lines, sizes, toleranceMm);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function snapFrameBox(
|
|
172
|
+
box: DocumentFrameBox,
|
|
173
|
+
gesture: FrameHandle | "move",
|
|
174
|
+
targets: FrameSnapTargets,
|
|
175
|
+
toleranceMm: number = FRAME_SNAP_TOLERANCE_MM,
|
|
176
|
+
): { readonly box: DocumentFrameBox; readonly guides: FrameGuides } {
|
|
177
|
+
const xMode: AxisMode =
|
|
178
|
+
gesture === "move"
|
|
179
|
+
? "move"
|
|
180
|
+
: gesture.includes("w")
|
|
181
|
+
? "low"
|
|
182
|
+
: gesture.includes("e")
|
|
183
|
+
? "high"
|
|
184
|
+
: "none";
|
|
185
|
+
const yMode: AxisMode =
|
|
186
|
+
gesture === "move"
|
|
187
|
+
? "move"
|
|
188
|
+
: gesture.includes("n")
|
|
189
|
+
? "low"
|
|
190
|
+
: gesture.includes("s")
|
|
191
|
+
? "high"
|
|
192
|
+
: "none";
|
|
193
|
+
const x = snapAxis(box.xMm, box.widthMm, xMode, targets.xLinesMm, targets.widthsMm, toleranceMm);
|
|
194
|
+
const y = snapAxis(
|
|
195
|
+
box.yMm,
|
|
196
|
+
box.heightMm,
|
|
197
|
+
yMode,
|
|
198
|
+
targets.yLinesMm,
|
|
199
|
+
targets.heightsMm,
|
|
200
|
+
toleranceMm,
|
|
201
|
+
);
|
|
202
|
+
return {
|
|
203
|
+
box: { xMm: x.start, yMm: y.start, widthMm: x.size, heightMm: y.size },
|
|
204
|
+
guides: { xMm: x.guide, yMm: y.guide },
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export type PageEdgesMm = {
|
|
209
|
+
readonly marginLeftMm: number;
|
|
210
|
+
readonly marginRightMm: number;
|
|
211
|
+
readonly marginTopMm: number;
|
|
212
|
+
readonly widthMm: number;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export function collectSnapTargets(
|
|
216
|
+
doc: ProseMirrorNode,
|
|
217
|
+
ownPos: number,
|
|
218
|
+
page: PageEdgesMm,
|
|
219
|
+
): FrameSnapTargets {
|
|
220
|
+
const xLinesMm = [page.marginLeftMm, page.widthMm - page.marginRightMm];
|
|
221
|
+
const yLinesMm = [page.marginTopMm];
|
|
222
|
+
const widthsMm: number[] = [];
|
|
223
|
+
const heightsMm: number[] = [];
|
|
224
|
+
doc.forEach((child, offset) => {
|
|
225
|
+
if (child.type.name !== FRAME_NODE_NAME || offset === ownPos) return;
|
|
226
|
+
const other = frameAttributesOf(child);
|
|
227
|
+
xLinesMm.push(other.xMm, other.xMm + other.widthMm);
|
|
228
|
+
yLinesMm.push(other.yMm, other.yMm + other.heightMm);
|
|
229
|
+
widthsMm.push(other.widthMm);
|
|
230
|
+
heightsMm.push(other.heightMm);
|
|
231
|
+
});
|
|
232
|
+
return { xLinesMm, yLinesMm, widthsMm, heightsMm };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export type FrameTagKind = "addressFormA" | "addressFormB" | "address" | "text";
|
|
236
|
+
|
|
237
|
+
export function frameTagKind(preset: DocumentFrameAttributes["preset"], yMm: number): FrameTagKind {
|
|
238
|
+
if (preset !== "address") return "text";
|
|
239
|
+
if (Math.abs(yMm - DIN_5008_FORM_A_Y_MM) < FRAME_STEP_MM) return "addressFormA";
|
|
240
|
+
if (Math.abs(yMm - DIN_5008_FORM_B_Y_MM) < FRAME_STEP_MM) return "addressFormB";
|
|
241
|
+
return "address";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function frameFlowBottomMm(doc: ProseMirrorNode): number {
|
|
245
|
+
let bottom = 0;
|
|
246
|
+
doc.forEach((child) => {
|
|
247
|
+
if (child.type.name !== FRAME_NODE_NAME) return;
|
|
248
|
+
const attrs = frameAttributesOf(child);
|
|
249
|
+
if (attrs.reserveFlow)
|
|
250
|
+
bottom = Math.max(bottom, attrs.yMm + attrs.heightMm + FRAME_FLOW_GAP_MM);
|
|
251
|
+
});
|
|
252
|
+
return bottom;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function numberAttr(value: unknown): number {
|
|
256
|
+
return typeof value === "number" ? value : 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function frameAttributesOf(node: ProseMirrorNode): DocumentFrameAttributes {
|
|
260
|
+
const { xMm, yMm, widthMm, heightMm, preset, reserveFlow } = node.attrs;
|
|
261
|
+
return {
|
|
262
|
+
xMm: numberAttr(xMm),
|
|
263
|
+
yMm: numberAttr(yMm),
|
|
264
|
+
widthMm: numberAttr(widthMm),
|
|
265
|
+
heightMm: numberAttr(heightMm),
|
|
266
|
+
preset: isOneOf(FRAME_PRESETS, preset) ? preset : null,
|
|
267
|
+
reserveFlow: reserveFlow !== false,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const EMPTY_PARAGRAPH: JSONContent = { type: "paragraph" };
|
|
272
|
+
|
|
273
|
+
function senderLineParagraph(): JSONContent {
|
|
274
|
+
const marks = [{ type: TEXT_STYLE_MARK, attrs: { sizePt: SENDER_LINE_SIZE_PT } }];
|
|
275
|
+
return {
|
|
276
|
+
type: "paragraph",
|
|
277
|
+
content: [
|
|
278
|
+
{ type: "variable", attrs: { name: "tenantName" }, marks },
|
|
279
|
+
{ type: "text", text: " · ", marks },
|
|
280
|
+
{ type: "variable", attrs: { name: "companyAddress" }, marks },
|
|
281
|
+
],
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function frameNode(
|
|
286
|
+
box: DocumentFrameBox,
|
|
287
|
+
preset: DocumentFrameAttributes["preset"],
|
|
288
|
+
content: JSONContent[],
|
|
289
|
+
reserveFlow: boolean,
|
|
290
|
+
): JSONContent {
|
|
291
|
+
return { type: FRAME_NODE_NAME, attrs: { ...box, preset, reserveFlow }, content };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function addressFrameContent(): JSONContent {
|
|
295
|
+
return frameNode(ADDRESS_FRAME_BOX, "address", [senderLineParagraph(), EMPTY_PARAGRAPH], true);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function textFrameContent(): JSONContent {
|
|
299
|
+
return frameNode(TEXT_FRAME_BOX, "text", [EMPTY_PARAGRAPH], false);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function columnsTableContent(): JSONContent {
|
|
303
|
+
return {
|
|
304
|
+
type: "table",
|
|
305
|
+
attrs: { borders: "none" },
|
|
306
|
+
content: [
|
|
307
|
+
{
|
|
308
|
+
type: "tableRow",
|
|
309
|
+
content: [1, 2, 3, 4].map(() => ({ type: "tableCell", content: [EMPTY_PARAGRAPH] })),
|
|
310
|
+
},
|
|
311
|
+
],
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Frames are only valid as top-level body nodes, ahead of the flowing text.
|
|
316
|
+
export function insertFrame(editor: Editor, frame: JSONContent): void {
|
|
317
|
+
let pos = 0;
|
|
318
|
+
const { doc } = editor.state;
|
|
319
|
+
for (let index = 0; index < doc.childCount; index++) {
|
|
320
|
+
const child = doc.child(index);
|
|
321
|
+
if (child.type.name !== FRAME_NODE_NAME) break;
|
|
322
|
+
pos += child.nodeSize;
|
|
323
|
+
}
|
|
324
|
+
editor.chain().focus().insertContentAt(pos, frame).setNodeSelection(pos).run();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export type FrameLocation = { readonly pos: number; readonly node: ProseMirrorNode };
|
|
328
|
+
|
|
329
|
+
export function frameLocationOf(selection: Selection): FrameLocation | null {
|
|
330
|
+
const { $from, from, to } = selection;
|
|
331
|
+
const after = $from.nodeAfter;
|
|
332
|
+
if (after?.type.name === FRAME_NODE_NAME && from + after.nodeSize === to) {
|
|
333
|
+
return { pos: from, node: after };
|
|
334
|
+
}
|
|
335
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
336
|
+
const node = $from.node(depth);
|
|
337
|
+
if (node.type.name === FRAME_NODE_NAME) return { pos: $from.before(depth), node };
|
|
338
|
+
}
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function isSenderLine(paragraph: ProseMirrorNode | null | undefined): boolean {
|
|
343
|
+
if (paragraph === null || paragraph === undefined) return false;
|
|
344
|
+
if (paragraph.type.name !== "paragraph" || paragraph.childCount === 0) return false;
|
|
345
|
+
let everyChildSmall = true;
|
|
346
|
+
paragraph.forEach((child) => {
|
|
347
|
+
const small = child.marks.some(
|
|
348
|
+
(mark) => mark.type.name === TEXT_STYLE_MARK && mark.attrs["sizePt"] === SENDER_LINE_SIZE_PT,
|
|
349
|
+
);
|
|
350
|
+
if (!small) everyChildSmall = false;
|
|
351
|
+
});
|
|
352
|
+
return everyChildSmall;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function recipientOf(frame: ProseMirrorNode): string | null {
|
|
356
|
+
const last = frame.lastChild;
|
|
357
|
+
if (last === null || last.type.name !== "paragraph") return null;
|
|
358
|
+
if (frame.childCount === 1 && isSenderLine(last)) return null;
|
|
359
|
+
let name: string | null = null;
|
|
360
|
+
last.forEach((child) => {
|
|
361
|
+
const value: unknown = child.attrs["name"];
|
|
362
|
+
if (name === null && child.type.name === "variable" && typeof value === "string") name = value;
|
|
363
|
+
});
|
|
364
|
+
return name;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function buildFrameControl(editor: Editor, location: FrameLocation): DocumentFrameControl {
|
|
368
|
+
const { pos } = location;
|
|
369
|
+
const currentFrame = (): ProseMirrorNode | null => {
|
|
370
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
371
|
+
return node !== null && node.type.name === FRAME_NODE_NAME ? node : null;
|
|
372
|
+
};
|
|
373
|
+
return {
|
|
374
|
+
attrs: frameAttributesOf(location.node),
|
|
375
|
+
update: (patch) => {
|
|
376
|
+
const frame = currentFrame();
|
|
377
|
+
if (frame === null) return;
|
|
378
|
+
editor.view.dispatch(
|
|
379
|
+
editor.state.tr.setNodeMarkup(pos, undefined, { ...frame.attrs, ...patch }),
|
|
380
|
+
);
|
|
381
|
+
},
|
|
382
|
+
remove: () => {
|
|
383
|
+
const frame = currentFrame();
|
|
384
|
+
if (frame === null) return;
|
|
385
|
+
editor.view.dispatch(editor.state.tr.delete(pos, pos + frame.nodeSize));
|
|
386
|
+
},
|
|
387
|
+
senderLine: isSenderLine(location.node.firstChild),
|
|
388
|
+
setSenderLine: (on) => {
|
|
389
|
+
const frame = currentFrame();
|
|
390
|
+
const first = frame?.firstChild;
|
|
391
|
+
if (frame === null || first === null || first === undefined) return;
|
|
392
|
+
const present = isSenderLine(first);
|
|
393
|
+
const { tr, schema } = editor.state;
|
|
394
|
+
if (on && !present) {
|
|
395
|
+
tr.insert(pos + 1, schema.nodeFromJSON(senderLineParagraph()));
|
|
396
|
+
} else if (!on && present) {
|
|
397
|
+
const end = pos + 1 + first.nodeSize;
|
|
398
|
+
if (frame.childCount === 1)
|
|
399
|
+
tr.replaceWith(pos + 1, end, schema.nodeFromJSON(EMPTY_PARAGRAPH));
|
|
400
|
+
else tr.delete(pos + 1, end);
|
|
401
|
+
} else return;
|
|
402
|
+
editor.view.dispatch(tr);
|
|
403
|
+
},
|
|
404
|
+
recipient: recipientOf(location.node),
|
|
405
|
+
setRecipient: (variableName) => {
|
|
406
|
+
const frame = currentFrame();
|
|
407
|
+
const last = frame?.lastChild;
|
|
408
|
+
if (frame === null || last === null || last === undefined) return;
|
|
409
|
+
const { tr, schema } = editor.state;
|
|
410
|
+
const variable =
|
|
411
|
+
variableName === null ? null : schema.nodes["variable"]?.create({ name: variableName });
|
|
412
|
+
const frameEnd = pos + frame.nodeSize - 1;
|
|
413
|
+
const lastIsRecipient =
|
|
414
|
+
last.type.name === "paragraph" && !(frame.childCount === 1 && isSenderLine(last));
|
|
415
|
+
if (!lastIsRecipient) {
|
|
416
|
+
const paragraph = schema.nodes["paragraph"]?.create(null, variable ?? undefined);
|
|
417
|
+
if (paragraph === undefined) return;
|
|
418
|
+
tr.insert(frameEnd, paragraph);
|
|
419
|
+
} else {
|
|
420
|
+
const from = frameEnd - last.nodeSize + 1;
|
|
421
|
+
const to = frameEnd - 1;
|
|
422
|
+
if (variable === null || variable === undefined) tr.delete(from, to);
|
|
423
|
+
else tr.replaceWith(from, to, variable);
|
|
424
|
+
}
|
|
425
|
+
editor.view.dispatch(tr);
|
|
426
|
+
},
|
|
427
|
+
};
|
|
428
|
+
}
|
|
@@ -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
|
+
}
|