@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.
- package/dist/template-resolver/compile-document-body.js +3 -6
- package/dist/template-resolver/compile-document-styles.d.ts +3 -0
- package/dist/template-resolver/compile-document-styles.js +84 -0
- package/dist/template-resolver/document-frames.d.ts +3 -0
- package/dist/template-resolver/document-frames.js +19 -0
- package/dist/template-resolver/document-op-blocks.d.ts +495 -0
- package/dist/template-resolver/document-op-blocks.js +430 -0
- package/dist/template-resolver/document-ops.d.ts +1809 -37
- package/dist/template-resolver/document-ops.js +97 -104
- package/dist/template-resolver/document-print-only.d.ts +10 -0
- package/dist/template-resolver/document-print-only.js +24 -0
- package/dist/template-resolver/document-sanitizer.d.ts +1 -1
- package/dist/template-resolver/document-sanitizer.js +24 -13
- package/dist/template-resolver/feature.d.ts +3 -0
- package/dist/template-resolver/feature.js +14 -1
- package/dist/template-resolver/handlers/apply-layout.write.d.ts +29 -0
- package/dist/template-resolver/handlers/apply-layout.write.js +118 -0
- package/dist/template-resolver/handlers/create-layout.write.d.ts +15 -0
- package/dist/template-resolver/handlers/create-layout.write.js +78 -0
- package/dist/template-resolver/handlers/document-base.d.ts +2 -2
- package/dist/template-resolver/handlers/document-blocks.query.d.ts +10 -3
- package/dist/template-resolver/handlers/document-blocks.query.js +13 -8
- package/dist/template-resolver/handlers/document-region.d.ts +19 -0
- package/dist/template-resolver/handlers/document-region.js +27 -0
- package/dist/template-resolver/handlers/failure-reason.d.ts +2 -0
- package/dist/template-resolver/handlers/failure-reason.js +12 -0
- package/dist/template-resolver/handlers/import-bundle-system.write.js +4 -0
- package/dist/template-resolver/handlers/layout-source.query.d.ts +3 -3
- package/dist/template-resolver/handlers/layout-source.query.js +6 -2
- package/dist/template-resolver/handlers/overview.query.d.ts +8 -0
- package/dist/template-resolver/handlers/overview.query.js +12 -6
- package/dist/template-resolver/handlers/patch-draft.write.d.ts +905 -4
- package/dist/template-resolver/handlers/patch-draft.write.js +10 -6
- package/dist/template-resolver/handlers/publish-templates.write.d.ts +40 -0
- package/dist/template-resolver/handlers/publish-templates.write.js +61 -0
- package/dist/template-resolver/handlers/set.write.js +4 -1
- package/dist/template-resolver/handlers/template-write.js +7 -2
- package/dist/template-resolver/handlers/tenant-settings.d.ts +17 -0
- package/dist/template-resolver/handlers/tenant-settings.js +91 -0
- package/dist/template-resolver/handlers/toggle-status.write.d.ts +14 -9
- package/dist/template-resolver/handlers/toggle-status.write.js +11 -7
- package/dist/template-resolver/handlers/upsert-system.write.js +15 -9
- package/dist/template-resolver/handlers/usage.query.d.ts +14 -0
- package/dist/template-resolver/handlers/usage.query.js +80 -12
- package/dist/template-resolver/handlers/validate.query.d.ts +2 -0
- package/dist/template-resolver/handlers/validate.query.js +10 -0
- package/dist/template-resolver/i18n.js +3 -0
- package/dist/template-resolver/index.d.ts +7 -2
- package/dist/template-resolver/index.js +6 -2
- package/dist/template-resolver/layout-regions.d.ts +4 -5
- package/dist/template-resolver/layout-regions.js +38 -11
- package/dist/template-resolver/print-document.js +3 -1
- package/dist/template-resolver/seeding.js +4 -0
- package/dist/template-resolver/template-delivery-policy.d.ts +1 -0
- package/dist/template-resolver/template-delivery-policy.js +9 -1
- package/dist/template-resolver/template-edit-policy.d.ts +5 -1
- package/dist/template-resolver/template-edit-policy.js +29 -1
- package/dist/template-resolver/template-meta.d.ts +27 -2
- package/dist/template-resolver/template-meta.js +21 -5
- package/dist/template-resolver/template-preview.js +8 -8
- package/dist/template-resolver/template-validation.js +10 -3
- package/dist/template-resolver/web/constants.d.ts +7 -1
- package/dist/template-resolver/web/constants.js +21 -1
- package/dist/template-resolver/web/document-blocks.d.ts +1 -0
- package/dist/template-resolver/web/document-blocks.js +2 -0
- package/dist/template-resolver/web/document-editor-extensions.d.ts +9 -1
- package/dist/template-resolver/web/document-editor-extensions.js +27 -2
- package/dist/template-resolver/web/document-editor-fonts.d.ts +10 -0
- package/dist/template-resolver/web/document-editor-fonts.js +13 -0
- package/dist/template-resolver/web/document-fonts.d.ts +4 -0
- package/dist/template-resolver/web/document-fonts.js +18 -0
- package/dist/template-resolver/web/document-frame-control.d.ts +61 -0
- package/dist/template-resolver/web/document-frame-control.js +329 -0
- package/dist/template-resolver/web/document-frame-node-view.d.ts +4 -0
- package/dist/template-resolver/web/document-frame-node-view.js +173 -0
- package/dist/template-resolver/web/document-print-decorations.d.ts +12 -0
- package/dist/template-resolver/web/document-print-decorations.js +147 -0
- package/dist/template-resolver/web/document-print-defaults.d.ts +14 -0
- package/dist/template-resolver/web/document-print-defaults.js +25 -0
- package/dist/template-resolver/web/document-print-style.d.ts +71 -0
- package/dist/template-resolver/web/document-print-style.js +199 -0
- package/dist/template-resolver/web/document-repeat-strip.d.ts +8 -0
- package/dist/template-resolver/web/document-repeat-strip.js +78 -0
- package/dist/template-resolver/web/document-schema.js +23 -9
- package/dist/template-resolver/web/document-style-css.d.ts +29 -0
- package/dist/template-resolver/web/document-style-css.js +76 -0
- package/dist/template-resolver/web/document-tiptap-bubble.js +13 -3
- package/dist/template-resolver/web/document-tiptap-content.d.ts +2 -1
- package/dist/template-resolver/web/document-tiptap-content.js +36 -4
- package/dist/template-resolver/web/document-tiptap-context.d.ts +6 -2
- package/dist/template-resolver/web/document-tiptap-context.js +2 -1
- package/dist/template-resolver/web/document-tiptap-editor.d.ts +1 -1
- package/dist/template-resolver/web/document-tiptap-editor.js +13 -5
- package/dist/template-resolver/web/document-tiptap-helpers.d.ts +30 -1
- package/dist/template-resolver/web/document-tiptap-helpers.js +103 -2
- package/dist/template-resolver/web/document-tiptap-toolbar-groups.d.ts +5 -0
- package/dist/template-resolver/web/document-tiptap-toolbar-groups.js +89 -6
- package/dist/template-resolver/web/document-tiptap-toolbar.js +45 -8
- package/dist/template-resolver/web/document-toolbar-blocks.d.ts +17 -0
- package/dist/template-resolver/web/i18n.js +19 -0
- package/dist/template-resolver/web/index.d.ts +8 -2
- package/dist/template-resolver/web/index.js +6 -1
- package/dist/template-resolver/web/pdf-constants.d.ts +3 -0
- package/dist/template-resolver/web/pdf-constants.js +3 -0
- package/dist/template-resolver/web/qualified-names.d.ts +7 -0
- package/dist/template-resolver/web/qualified-names.js +6 -0
- package/dist/template-resolver/web/use-document-tiptap-model.d.ts +6 -2
- package/dist/template-resolver/web/use-document-tiptap-model.js +38 -3
- package/dist/tenant/company-config.d.ts +6 -0
- package/dist/tenant/company-config.js +41 -0
- package/dist/tenant/feature.js +11 -0
- package/dist/tenant/i18n.js +14 -0
- package/package.json +16 -11
- package/src/template-resolver/changes.json +38 -0
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ListKeymap } from "@tiptap/extension-list";
|
|
2
2
|
import { Dropcursor, Gapcursor, Placeholder, UndoRedo } from "@tiptap/extensions";
|
|
3
|
-
import {
|
|
3
|
+
import { createFrameView } from "./document-frame-node-view.js";
|
|
4
|
+
import { DocumentPrintEditing, loadDocumentEditorFonts } from "./document-print-decorations.js";
|
|
5
|
+
import { DocumentFrame } from "./document-print-style.js";
|
|
6
|
+
import { DocumentButton, DocumentImages, DocumentPageBreak, DocumentResource, DocumentSteps, DocumentTable, DocumentVariable, documentExtensions, } from "./document-schema.js";
|
|
4
7
|
import { readDocumentStepsAttribute } from "./document-steps.js";
|
|
5
8
|
const DOCUMENT_RESOURCE_SIZE_TOKEN = "{size}";
|
|
6
9
|
export const DOCUMENT_BLOCK_ATTRIBUTE = "data-document-block";
|
|
@@ -11,7 +14,7 @@ function createElementWithClass(tag, className, text) {
|
|
|
11
14
|
element.textContent = text;
|
|
12
15
|
return element;
|
|
13
16
|
}
|
|
14
|
-
const CHIP_CLASS = "mx-0.5 inline-flex items-center gap-1 rounded border px-1.5
|
|
17
|
+
const CHIP_CLASS = "mx-0.5 inline-flex items-center gap-1 rounded border px-1.5 text-[0.9em]";
|
|
15
18
|
const USER_CHIP_CLASS = `${CHIP_CLASS} border-primary/30 bg-primary/10 text-primary`;
|
|
16
19
|
const SYSTEM_CHIP_CLASS = `${CHIP_CLASS} border-input bg-muted text-muted-foreground`;
|
|
17
20
|
// A chip inside a repeat row names an item field; its label is registered as `list.field`.
|
|
@@ -171,6 +174,16 @@ export function createDocumentEditorExtensions(labels) {
|
|
|
171
174
|
return DocumentSteps.extend({ addNodeView: () => createStepsView(labels) });
|
|
172
175
|
case DocumentImages.name:
|
|
173
176
|
return DocumentImages.extend({ addNodeView: () => createImagesView(labels) });
|
|
177
|
+
case DocumentTable.name:
|
|
178
|
+
return labels.print !== undefined && labels.print !== "none"
|
|
179
|
+
? DocumentTable.configure({ resizable: true })
|
|
180
|
+
: extension;
|
|
181
|
+
case DocumentFrame.name: {
|
|
182
|
+
const { frameLabels } = labels;
|
|
183
|
+
return frameLabels === undefined
|
|
184
|
+
? extension
|
|
185
|
+
: DocumentFrame.extend({ addNodeView: () => createFrameView(frameLabels) });
|
|
186
|
+
}
|
|
174
187
|
case DocumentPageBreak.name:
|
|
175
188
|
return DocumentPageBreak.extend({
|
|
176
189
|
addNodeView: () => createPageBreakView(labels.pageBreak),
|
|
@@ -179,9 +192,21 @@ export function createDocumentEditorExtensions(labels) {
|
|
|
179
192
|
return extension;
|
|
180
193
|
}
|
|
181
194
|
});
|
|
195
|
+
const print = labels.print ?? "none";
|
|
196
|
+
if (print !== "none")
|
|
197
|
+
void loadDocumentEditorFonts();
|
|
182
198
|
// No TrailingNode: it appends an empty paragraph that breaks the HTML roundtrip.
|
|
183
199
|
return [
|
|
184
200
|
...base,
|
|
201
|
+
...(print === "none"
|
|
202
|
+
? []
|
|
203
|
+
: [
|
|
204
|
+
DocumentPrintEditing.configure({
|
|
205
|
+
frames: print === "page",
|
|
206
|
+
reportFrameControl: labels.reportFrameControl ?? null,
|
|
207
|
+
repeatStrip: labels.repeatStrip ?? null,
|
|
208
|
+
}),
|
|
209
|
+
]),
|
|
185
210
|
UndoRedo,
|
|
186
211
|
Gapcursor,
|
|
187
212
|
Dropcursor,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import "@fontsource/arimo/latin-400.css";
|
|
2
|
+
import "@fontsource/arimo/latin-700.css";
|
|
3
|
+
import "@fontsource/tinos/latin-400.css";
|
|
4
|
+
import "@fontsource/tinos/latin-700.css";
|
|
5
|
+
import "@fontsource/cousine/latin-400.css";
|
|
6
|
+
import "@fontsource/cousine/latin-700.css";
|
|
7
|
+
import "@fontsource/carlito/latin-400.css";
|
|
8
|
+
import "@fontsource/carlito/latin-700.css";
|
|
9
|
+
import "@fontsource/caladea/latin-400.css";
|
|
10
|
+
import "@fontsource/caladea/latin-700.css";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference path="./fontsource-css.d.ts" />
|
|
2
|
+
// Loaded only through loadDocumentEditorFonts (a dynamic import), so the font CSS stays out of the
|
|
3
|
+
// main bundle and is evaluated once per session.
|
|
4
|
+
import "@fontsource/arimo/latin-400.css";
|
|
5
|
+
import "@fontsource/arimo/latin-700.css";
|
|
6
|
+
import "@fontsource/tinos/latin-400.css";
|
|
7
|
+
import "@fontsource/tinos/latin-700.css";
|
|
8
|
+
import "@fontsource/cousine/latin-400.css";
|
|
9
|
+
import "@fontsource/cousine/latin-700.css";
|
|
10
|
+
import "@fontsource/carlito/latin-400.css";
|
|
11
|
+
import "@fontsource/carlito/latin-700.css";
|
|
12
|
+
import "@fontsource/caladea/latin-400.css";
|
|
13
|
+
import "@fontsource/caladea/latin-700.css";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const DOCUMENT_FONTS: readonly ["arial", "times", "courier", "calibri", "cambria"];
|
|
2
|
+
export type DocumentFont = (typeof DOCUMENT_FONTS)[number];
|
|
3
|
+
export declare const DOCUMENT_FONT_LABELS: Readonly<Record<DocumentFont, string>>;
|
|
4
|
+
export declare const DOCUMENT_FONT_PRINT_STACKS: Readonly<Record<DocumentFont, string>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const DOCUMENT_FONTS = ["arial", "times", "courier", "calibri", "cambria"];
|
|
2
|
+
export const DOCUMENT_FONT_LABELS = {
|
|
3
|
+
arial: "Arial",
|
|
4
|
+
times: "Times New Roman",
|
|
5
|
+
courier: "Courier New",
|
|
6
|
+
calibri: "Calibri",
|
|
7
|
+
cambria: "Cambria",
|
|
8
|
+
};
|
|
9
|
+
// The first family is the metric-compatible font installed in the print service, so the PDF
|
|
10
|
+
// wraps lines like the Office font the author picked.
|
|
11
|
+
// Single quotes only: the stacks land in double-quoted style attributes, where a double quote ends the attribute.
|
|
12
|
+
export const DOCUMENT_FONT_PRINT_STACKS = {
|
|
13
|
+
arial: "'Liberation Sans',Arial,sans-serif",
|
|
14
|
+
times: "'Liberation Serif','Times New Roman',serif",
|
|
15
|
+
courier: "'Liberation Mono','Courier New',monospace",
|
|
16
|
+
calibri: "Carlito,Calibri,sans-serif",
|
|
17
|
+
cambria: "Caladea,Cambria,serif",
|
|
18
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Editor, JSONContent } from "@tiptap/core";
|
|
2
|
+
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|
3
|
+
import type { Selection } from "@tiptap/pm/state";
|
|
4
|
+
import { type DocumentFrameAttributes } from "./document-print-style.ts";
|
|
5
|
+
import { type DocumentFrameBox } from "./document-style-css.ts";
|
|
6
|
+
export type DocumentFrameControl = {
|
|
7
|
+
readonly attrs: DocumentFrameAttributes;
|
|
8
|
+
readonly update: (patch: Partial<DocumentFrameAttributes>) => void;
|
|
9
|
+
readonly remove: () => void;
|
|
10
|
+
readonly senderLine: boolean;
|
|
11
|
+
readonly setSenderLine: (on: boolean) => void;
|
|
12
|
+
/** Variable name of the placeholder in the last paragraph, null without one. */
|
|
13
|
+
readonly recipient: string | null;
|
|
14
|
+
readonly setRecipient: (variableName: string | null) => void;
|
|
15
|
+
};
|
|
16
|
+
export declare const FRAME_NODE_NAME = "documentFrame";
|
|
17
|
+
export declare const FRAME_SELECTED_ATTRIBUTE = "data-frame-selected";
|
|
18
|
+
export declare const FRAME_STEP_MM = 0.5;
|
|
19
|
+
export declare const FRAME_MIN_SIZE_MM = 10;
|
|
20
|
+
export declare const FRAME_HANDLES: readonly ["nw", "n", "ne", "e", "se", "s", "sw", "w"];
|
|
21
|
+
export type FrameHandle = (typeof FRAME_HANDLES)[number];
|
|
22
|
+
export declare function roundToHalfMm(value: number): number;
|
|
23
|
+
export declare function moveFrameBox(box: DocumentFrameBox, dxMm: number, dyMm: number): DocumentFrameBox;
|
|
24
|
+
export declare function resizeFrameBox(box: DocumentFrameBox, handle: FrameHandle, dxMm: number, dyMm: number): DocumentFrameBox;
|
|
25
|
+
export declare const FRAME_SNAP_TOLERANCE_MM = 1.5;
|
|
26
|
+
export type FrameSnapTargets = {
|
|
27
|
+
readonly xLinesMm: readonly number[];
|
|
28
|
+
readonly yLinesMm: readonly number[];
|
|
29
|
+
readonly widthsMm: readonly number[];
|
|
30
|
+
readonly heightsMm: readonly number[];
|
|
31
|
+
};
|
|
32
|
+
export type FrameGuides = {
|
|
33
|
+
readonly xMm: number | null;
|
|
34
|
+
readonly yMm: number | null;
|
|
35
|
+
};
|
|
36
|
+
export declare function snapFrameBox(box: DocumentFrameBox, gesture: FrameHandle | "move", targets: FrameSnapTargets, toleranceMm?: number): {
|
|
37
|
+
readonly box: DocumentFrameBox;
|
|
38
|
+
readonly guides: FrameGuides;
|
|
39
|
+
};
|
|
40
|
+
export type PageEdgesMm = {
|
|
41
|
+
readonly marginLeftMm: number;
|
|
42
|
+
readonly marginRightMm: number;
|
|
43
|
+
readonly marginTopMm: number;
|
|
44
|
+
readonly widthMm: number;
|
|
45
|
+
};
|
|
46
|
+
export declare function collectSnapTargets(doc: ProseMirrorNode, ownPos: number, page: PageEdgesMm): FrameSnapTargets;
|
|
47
|
+
export type FrameTagKind = "addressFormA" | "addressFormB" | "address" | "text";
|
|
48
|
+
export declare function frameTagKind(preset: DocumentFrameAttributes["preset"], yMm: number): FrameTagKind;
|
|
49
|
+
export declare function frameFlowBottomMm(doc: ProseMirrorNode): number;
|
|
50
|
+
export declare function frameAttributesOf(node: ProseMirrorNode): DocumentFrameAttributes;
|
|
51
|
+
export declare function addressFrameContent(): JSONContent;
|
|
52
|
+
export declare function textFrameContent(): JSONContent;
|
|
53
|
+
export declare function columnsTableContent(): JSONContent;
|
|
54
|
+
export declare function insertFrame(editor: Editor, frame: JSONContent): void;
|
|
55
|
+
export type FrameLocation = {
|
|
56
|
+
readonly pos: number;
|
|
57
|
+
readonly node: ProseMirrorNode;
|
|
58
|
+
};
|
|
59
|
+
export declare function frameLocationOf(selection: Selection): FrameLocation | null;
|
|
60
|
+
export declare function isSenderLine(paragraph: ProseMirrorNode | null | undefined): boolean;
|
|
61
|
+
export declare function buildFrameControl(editor: Editor, location: FrameLocation): DocumentFrameControl;
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
2
|
+
import { FRAME_PRESETS } from "./document-print-style.js";
|
|
3
|
+
import { FRAME_FLOW_GAP_MM, SENDER_LINE_SIZE_PT, } from "./document-style-css.js";
|
|
4
|
+
export const FRAME_NODE_NAME = "documentFrame";
|
|
5
|
+
export const FRAME_SELECTED_ATTRIBUTE = "data-frame-selected";
|
|
6
|
+
export const FRAME_STEP_MM = 0.5;
|
|
7
|
+
export const FRAME_MIN_SIZE_MM = 10;
|
|
8
|
+
export const FRAME_HANDLES = ["nw", "n", "ne", "e", "se", "s", "sw", "w"];
|
|
9
|
+
const TEXT_STYLE_MARK = "documentTextStyle";
|
|
10
|
+
const DIN_5008_FORM_A_Y_MM = 27;
|
|
11
|
+
const DIN_5008_FORM_B_Y_MM = 45;
|
|
12
|
+
const ADDRESS_FRAME_BOX = { xMm: 20, yMm: 45, widthMm: 85, heightMm: 45 };
|
|
13
|
+
const TEXT_FRAME_BOX = { xMm: 125, yMm: 50, widthMm: 65, heightMm: 34 };
|
|
14
|
+
export function roundToHalfMm(value) {
|
|
15
|
+
return Math.round(value / FRAME_STEP_MM) * FRAME_STEP_MM;
|
|
16
|
+
}
|
|
17
|
+
export function moveFrameBox(box, dxMm, dyMm) {
|
|
18
|
+
return {
|
|
19
|
+
...box,
|
|
20
|
+
xMm: Math.max(0, roundToHalfMm(box.xMm + dxMm)),
|
|
21
|
+
yMm: Math.max(0, roundToHalfMm(box.yMm + dyMm)),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
// The edge opposite the dragged corner stays put, so a clamp never shifts the whole frame.
|
|
25
|
+
export function resizeFrameBox(box, handle, dxMm, dyMm) {
|
|
26
|
+
const right = box.xMm + box.widthMm;
|
|
27
|
+
const bottom = box.yMm + box.heightMm;
|
|
28
|
+
const movesLeft = handle === "nw" || handle === "sw" || handle === "w";
|
|
29
|
+
const movesRight = handle === "ne" || handle === "se" || handle === "e";
|
|
30
|
+
const movesTop = handle === "nw" || handle === "ne" || handle === "n";
|
|
31
|
+
const movesBottom = handle === "sw" || handle === "se" || handle === "s";
|
|
32
|
+
const left = movesLeft
|
|
33
|
+
? Math.min(Math.max(0, roundToHalfMm(box.xMm + dxMm)), right - FRAME_MIN_SIZE_MM)
|
|
34
|
+
: box.xMm;
|
|
35
|
+
const top = movesTop
|
|
36
|
+
? Math.min(Math.max(0, roundToHalfMm(box.yMm + dyMm)), bottom - FRAME_MIN_SIZE_MM)
|
|
37
|
+
: box.yMm;
|
|
38
|
+
const newRight = movesRight
|
|
39
|
+
? Math.max(left + FRAME_MIN_SIZE_MM, roundToHalfMm(right + dxMm))
|
|
40
|
+
: right;
|
|
41
|
+
const newBottom = movesBottom
|
|
42
|
+
? Math.max(top + FRAME_MIN_SIZE_MM, roundToHalfMm(bottom + dyMm))
|
|
43
|
+
: bottom;
|
|
44
|
+
return { xMm: left, yMm: top, widthMm: newRight - left, heightMm: newBottom - top };
|
|
45
|
+
}
|
|
46
|
+
export const FRAME_SNAP_TOLERANCE_MM = 1.5;
|
|
47
|
+
function nearestWithin(value, candidates, toleranceMm) {
|
|
48
|
+
let best = null;
|
|
49
|
+
for (const candidate of candidates) {
|
|
50
|
+
const distance = Math.abs(candidate - value);
|
|
51
|
+
if (distance <= toleranceMm && (best === null || distance < Math.abs(best - value))) {
|
|
52
|
+
best = candidate;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return best;
|
|
56
|
+
}
|
|
57
|
+
function snapMove(start, size, lines, toleranceMm) {
|
|
58
|
+
const unchanged = { start, size, guide: null };
|
|
59
|
+
const end = start + size;
|
|
60
|
+
const leading = nearestWithin(start, lines, toleranceMm);
|
|
61
|
+
const trailing = nearestWithin(end, lines, toleranceMm);
|
|
62
|
+
const leadingDistance = leading === null ? Infinity : Math.abs(leading - start);
|
|
63
|
+
const trailingDistance = trailing === null ? Infinity : Math.abs(trailing - end);
|
|
64
|
+
if (leading !== null && leadingDistance <= trailingDistance) {
|
|
65
|
+
return { start: leading, size, guide: leading };
|
|
66
|
+
}
|
|
67
|
+
if (trailing !== null) {
|
|
68
|
+
const snappedStart = trailing - size;
|
|
69
|
+
return snappedStart >= 0 ? { start: snappedStart, size, guide: trailing } : unchanged;
|
|
70
|
+
}
|
|
71
|
+
return unchanged;
|
|
72
|
+
}
|
|
73
|
+
// The dragged edge snaps to a line or the size to another frame's size; the fixed edge stays.
|
|
74
|
+
function snapResize(start, size, mode, lines, sizes, toleranceMm) {
|
|
75
|
+
const unchanged = { start, size, guide: null };
|
|
76
|
+
const end = start + size;
|
|
77
|
+
const dragged = mode === "low" ? start : end;
|
|
78
|
+
const fixed = mode === "low" ? end : start;
|
|
79
|
+
const line = nearestWithin(dragged, lines, toleranceMm);
|
|
80
|
+
const matchedSize = nearestWithin(size, sizes, toleranceMm);
|
|
81
|
+
const lineDistance = line === null ? Infinity : Math.abs(line - dragged);
|
|
82
|
+
const sizeDistance = matchedSize === null ? Infinity : Math.abs(matchedSize - size);
|
|
83
|
+
const resolve = (candidateSize, guide) => {
|
|
84
|
+
const candidateStart = mode === "low" ? fixed - candidateSize : start;
|
|
85
|
+
return candidateSize >= FRAME_MIN_SIZE_MM && candidateStart >= 0
|
|
86
|
+
? { start: candidateStart, size: candidateSize, guide }
|
|
87
|
+
: unchanged;
|
|
88
|
+
};
|
|
89
|
+
if (line !== null && lineDistance <= sizeDistance) {
|
|
90
|
+
return resolve(mode === "low" ? fixed - line : line - fixed, line);
|
|
91
|
+
}
|
|
92
|
+
if (matchedSize !== null)
|
|
93
|
+
return resolve(matchedSize, null);
|
|
94
|
+
return unchanged;
|
|
95
|
+
}
|
|
96
|
+
function snapAxis(start, size, mode, lines, sizes, toleranceMm) {
|
|
97
|
+
if (mode === "none")
|
|
98
|
+
return { start, size, guide: null };
|
|
99
|
+
if (mode === "move")
|
|
100
|
+
return snapMove(start, size, lines, toleranceMm);
|
|
101
|
+
return snapResize(start, size, mode, lines, sizes, toleranceMm);
|
|
102
|
+
}
|
|
103
|
+
export function snapFrameBox(box, gesture, targets, toleranceMm = FRAME_SNAP_TOLERANCE_MM) {
|
|
104
|
+
const xMode = gesture === "move"
|
|
105
|
+
? "move"
|
|
106
|
+
: gesture.includes("w")
|
|
107
|
+
? "low"
|
|
108
|
+
: gesture.includes("e")
|
|
109
|
+
? "high"
|
|
110
|
+
: "none";
|
|
111
|
+
const yMode = gesture === "move"
|
|
112
|
+
? "move"
|
|
113
|
+
: gesture.includes("n")
|
|
114
|
+
? "low"
|
|
115
|
+
: gesture.includes("s")
|
|
116
|
+
? "high"
|
|
117
|
+
: "none";
|
|
118
|
+
const x = snapAxis(box.xMm, box.widthMm, xMode, targets.xLinesMm, targets.widthsMm, toleranceMm);
|
|
119
|
+
const y = snapAxis(box.yMm, box.heightMm, yMode, targets.yLinesMm, targets.heightsMm, toleranceMm);
|
|
120
|
+
return {
|
|
121
|
+
box: { xMm: x.start, yMm: y.start, widthMm: x.size, heightMm: y.size },
|
|
122
|
+
guides: { xMm: x.guide, yMm: y.guide },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export function collectSnapTargets(doc, ownPos, page) {
|
|
126
|
+
const xLinesMm = [page.marginLeftMm, page.widthMm - page.marginRightMm];
|
|
127
|
+
const yLinesMm = [page.marginTopMm];
|
|
128
|
+
const widthsMm = [];
|
|
129
|
+
const heightsMm = [];
|
|
130
|
+
doc.forEach((child, offset) => {
|
|
131
|
+
if (child.type.name !== FRAME_NODE_NAME || offset === ownPos)
|
|
132
|
+
return;
|
|
133
|
+
const other = frameAttributesOf(child);
|
|
134
|
+
xLinesMm.push(other.xMm, other.xMm + other.widthMm);
|
|
135
|
+
yLinesMm.push(other.yMm, other.yMm + other.heightMm);
|
|
136
|
+
widthsMm.push(other.widthMm);
|
|
137
|
+
heightsMm.push(other.heightMm);
|
|
138
|
+
});
|
|
139
|
+
return { xLinesMm, yLinesMm, widthsMm, heightsMm };
|
|
140
|
+
}
|
|
141
|
+
export function frameTagKind(preset, yMm) {
|
|
142
|
+
if (preset !== "address")
|
|
143
|
+
return "text";
|
|
144
|
+
if (Math.abs(yMm - DIN_5008_FORM_A_Y_MM) < FRAME_STEP_MM)
|
|
145
|
+
return "addressFormA";
|
|
146
|
+
if (Math.abs(yMm - DIN_5008_FORM_B_Y_MM) < FRAME_STEP_MM)
|
|
147
|
+
return "addressFormB";
|
|
148
|
+
return "address";
|
|
149
|
+
}
|
|
150
|
+
export function frameFlowBottomMm(doc) {
|
|
151
|
+
let bottom = 0;
|
|
152
|
+
doc.forEach((child) => {
|
|
153
|
+
if (child.type.name !== FRAME_NODE_NAME)
|
|
154
|
+
return;
|
|
155
|
+
const attrs = frameAttributesOf(child);
|
|
156
|
+
if (attrs.reserveFlow)
|
|
157
|
+
bottom = Math.max(bottom, attrs.yMm + attrs.heightMm + FRAME_FLOW_GAP_MM);
|
|
158
|
+
});
|
|
159
|
+
return bottom;
|
|
160
|
+
}
|
|
161
|
+
function numberAttr(value) {
|
|
162
|
+
return typeof value === "number" ? value : 0;
|
|
163
|
+
}
|
|
164
|
+
export function frameAttributesOf(node) {
|
|
165
|
+
const { xMm, yMm, widthMm, heightMm, preset, reserveFlow } = node.attrs;
|
|
166
|
+
return {
|
|
167
|
+
xMm: numberAttr(xMm),
|
|
168
|
+
yMm: numberAttr(yMm),
|
|
169
|
+
widthMm: numberAttr(widthMm),
|
|
170
|
+
heightMm: numberAttr(heightMm),
|
|
171
|
+
preset: isOneOf(FRAME_PRESETS, preset) ? preset : null,
|
|
172
|
+
reserveFlow: reserveFlow !== false,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
const EMPTY_PARAGRAPH = { type: "paragraph" };
|
|
176
|
+
function senderLineParagraph() {
|
|
177
|
+
const marks = [{ type: TEXT_STYLE_MARK, attrs: { sizePt: SENDER_LINE_SIZE_PT } }];
|
|
178
|
+
return {
|
|
179
|
+
type: "paragraph",
|
|
180
|
+
content: [
|
|
181
|
+
{ type: "variable", attrs: { name: "tenantName" }, marks },
|
|
182
|
+
{ type: "text", text: " · ", marks },
|
|
183
|
+
{ type: "variable", attrs: { name: "companyAddress" }, marks },
|
|
184
|
+
],
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function frameNode(box, preset, content, reserveFlow) {
|
|
188
|
+
return { type: FRAME_NODE_NAME, attrs: { ...box, preset, reserveFlow }, content };
|
|
189
|
+
}
|
|
190
|
+
export function addressFrameContent() {
|
|
191
|
+
return frameNode(ADDRESS_FRAME_BOX, "address", [senderLineParagraph(), EMPTY_PARAGRAPH], true);
|
|
192
|
+
}
|
|
193
|
+
export function textFrameContent() {
|
|
194
|
+
return frameNode(TEXT_FRAME_BOX, "text", [EMPTY_PARAGRAPH], false);
|
|
195
|
+
}
|
|
196
|
+
export function columnsTableContent() {
|
|
197
|
+
return {
|
|
198
|
+
type: "table",
|
|
199
|
+
attrs: { borders: "none" },
|
|
200
|
+
content: [
|
|
201
|
+
{
|
|
202
|
+
type: "tableRow",
|
|
203
|
+
content: [1, 2, 3, 4].map(() => ({ type: "tableCell", content: [EMPTY_PARAGRAPH] })),
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
// Frames are only valid as top-level body nodes, ahead of the flowing text.
|
|
209
|
+
export function insertFrame(editor, frame) {
|
|
210
|
+
let pos = 0;
|
|
211
|
+
const { doc } = editor.state;
|
|
212
|
+
for (let index = 0; index < doc.childCount; index++) {
|
|
213
|
+
const child = doc.child(index);
|
|
214
|
+
if (child.type.name !== FRAME_NODE_NAME)
|
|
215
|
+
break;
|
|
216
|
+
pos += child.nodeSize;
|
|
217
|
+
}
|
|
218
|
+
editor.chain().focus().insertContentAt(pos, frame).setNodeSelection(pos).run();
|
|
219
|
+
}
|
|
220
|
+
export function frameLocationOf(selection) {
|
|
221
|
+
const { $from, from, to } = selection;
|
|
222
|
+
const after = $from.nodeAfter;
|
|
223
|
+
if (after?.type.name === FRAME_NODE_NAME && from + after.nodeSize === to) {
|
|
224
|
+
return { pos: from, node: after };
|
|
225
|
+
}
|
|
226
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
227
|
+
const node = $from.node(depth);
|
|
228
|
+
if (node.type.name === FRAME_NODE_NAME)
|
|
229
|
+
return { pos: $from.before(depth), node };
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
export function isSenderLine(paragraph) {
|
|
234
|
+
if (paragraph === null || paragraph === undefined)
|
|
235
|
+
return false;
|
|
236
|
+
if (paragraph.type.name !== "paragraph" || paragraph.childCount === 0)
|
|
237
|
+
return false;
|
|
238
|
+
let everyChildSmall = true;
|
|
239
|
+
paragraph.forEach((child) => {
|
|
240
|
+
const small = child.marks.some((mark) => mark.type.name === TEXT_STYLE_MARK && mark.attrs["sizePt"] === SENDER_LINE_SIZE_PT);
|
|
241
|
+
if (!small)
|
|
242
|
+
everyChildSmall = false;
|
|
243
|
+
});
|
|
244
|
+
return everyChildSmall;
|
|
245
|
+
}
|
|
246
|
+
function recipientOf(frame) {
|
|
247
|
+
const last = frame.lastChild;
|
|
248
|
+
if (last === null || last.type.name !== "paragraph")
|
|
249
|
+
return null;
|
|
250
|
+
if (frame.childCount === 1 && isSenderLine(last))
|
|
251
|
+
return null;
|
|
252
|
+
let name = null;
|
|
253
|
+
last.forEach((child) => {
|
|
254
|
+
const value = child.attrs["name"];
|
|
255
|
+
if (name === null && child.type.name === "variable" && typeof value === "string")
|
|
256
|
+
name = value;
|
|
257
|
+
});
|
|
258
|
+
return name;
|
|
259
|
+
}
|
|
260
|
+
export function buildFrameControl(editor, location) {
|
|
261
|
+
const { pos } = location;
|
|
262
|
+
const currentFrame = () => {
|
|
263
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
264
|
+
return node !== null && node.type.name === FRAME_NODE_NAME ? node : null;
|
|
265
|
+
};
|
|
266
|
+
return {
|
|
267
|
+
attrs: frameAttributesOf(location.node),
|
|
268
|
+
update: (patch) => {
|
|
269
|
+
const frame = currentFrame();
|
|
270
|
+
if (frame === null)
|
|
271
|
+
return;
|
|
272
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...frame.attrs, ...patch }));
|
|
273
|
+
},
|
|
274
|
+
remove: () => {
|
|
275
|
+
const frame = currentFrame();
|
|
276
|
+
if (frame === null)
|
|
277
|
+
return;
|
|
278
|
+
editor.view.dispatch(editor.state.tr.delete(pos, pos + frame.nodeSize));
|
|
279
|
+
},
|
|
280
|
+
senderLine: isSenderLine(location.node.firstChild),
|
|
281
|
+
setSenderLine: (on) => {
|
|
282
|
+
const frame = currentFrame();
|
|
283
|
+
const first = frame?.firstChild;
|
|
284
|
+
if (frame === null || first === null || first === undefined)
|
|
285
|
+
return;
|
|
286
|
+
const present = isSenderLine(first);
|
|
287
|
+
const { tr, schema } = editor.state;
|
|
288
|
+
if (on && !present) {
|
|
289
|
+
tr.insert(pos + 1, schema.nodeFromJSON(senderLineParagraph()));
|
|
290
|
+
}
|
|
291
|
+
else if (!on && present) {
|
|
292
|
+
const end = pos + 1 + first.nodeSize;
|
|
293
|
+
if (frame.childCount === 1)
|
|
294
|
+
tr.replaceWith(pos + 1, end, schema.nodeFromJSON(EMPTY_PARAGRAPH));
|
|
295
|
+
else
|
|
296
|
+
tr.delete(pos + 1, end);
|
|
297
|
+
}
|
|
298
|
+
else
|
|
299
|
+
return;
|
|
300
|
+
editor.view.dispatch(tr);
|
|
301
|
+
},
|
|
302
|
+
recipient: recipientOf(location.node),
|
|
303
|
+
setRecipient: (variableName) => {
|
|
304
|
+
const frame = currentFrame();
|
|
305
|
+
const last = frame?.lastChild;
|
|
306
|
+
if (frame === null || last === null || last === undefined)
|
|
307
|
+
return;
|
|
308
|
+
const { tr, schema } = editor.state;
|
|
309
|
+
const variable = variableName === null ? null : schema.nodes["variable"]?.create({ name: variableName });
|
|
310
|
+
const frameEnd = pos + frame.nodeSize - 1;
|
|
311
|
+
const lastIsRecipient = last.type.name === "paragraph" && !(frame.childCount === 1 && isSenderLine(last));
|
|
312
|
+
if (!lastIsRecipient) {
|
|
313
|
+
const paragraph = schema.nodes["paragraph"]?.create(null, variable ?? undefined);
|
|
314
|
+
if (paragraph === undefined)
|
|
315
|
+
return;
|
|
316
|
+
tr.insert(frameEnd, paragraph);
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
const from = frameEnd - last.nodeSize + 1;
|
|
320
|
+
const to = frameEnd - 1;
|
|
321
|
+
if (variable === null || variable === undefined)
|
|
322
|
+
tr.delete(from, to);
|
|
323
|
+
else
|
|
324
|
+
tr.replaceWith(from, to, variable);
|
|
325
|
+
}
|
|
326
|
+
editor.view.dispatch(tr);
|
|
327
|
+
},
|
|
328
|
+
};
|
|
329
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { NodeViewRenderer } from "@tiptap/core";
|
|
2
|
+
import { type FrameTagKind } from "./document-frame-control.ts";
|
|
3
|
+
export type FrameViewLabels = Readonly<Record<FrameTagKind, string>>;
|
|
4
|
+
export declare function createFrameView(labels: FrameViewLabels): NodeViewRenderer;
|