@cosmicdrift/kumiko-samples 0.418.0 → 0.419.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/package.json +1 -1
  2. package/packages/bundled-features/package.json +6 -1
  3. package/packages/bundled-features/src/template-resolver/changes.json +38 -0
  4. package/packages/bundled-features/src/template-resolver/compile-document-body.ts +3 -8
  5. package/packages/bundled-features/src/template-resolver/compile-document-styles.ts +102 -0
  6. package/packages/bundled-features/src/template-resolver/document-frames.ts +25 -0
  7. package/packages/bundled-features/src/template-resolver/document-op-blocks.ts +568 -0
  8. package/packages/bundled-features/src/template-resolver/document-ops.ts +136 -126
  9. package/packages/bundled-features/src/template-resolver/document-print-only.ts +41 -0
  10. package/packages/bundled-features/src/template-resolver/document-sanitizer.ts +41 -19
  11. package/packages/bundled-features/src/template-resolver/feature.ts +15 -0
  12. package/packages/bundled-features/src/template-resolver/handlers/apply-layout.write.ts +137 -0
  13. package/packages/bundled-features/src/template-resolver/handlers/create-layout.write.ts +87 -0
  14. package/packages/bundled-features/src/template-resolver/handlers/document-base.ts +2 -2
  15. package/packages/bundled-features/src/template-resolver/handlers/document-blocks.query.ts +13 -8
  16. package/packages/bundled-features/src/template-resolver/handlers/document-region.ts +47 -0
  17. package/packages/bundled-features/src/template-resolver/handlers/failure-reason.ts +13 -0
  18. package/packages/bundled-features/src/template-resolver/handlers/import-bundle-system.write.ts +3 -0
  19. package/packages/bundled-features/src/template-resolver/handlers/layout-source.query.ts +17 -5
  20. package/packages/bundled-features/src/template-resolver/handlers/overview.query.ts +16 -4
  21. package/packages/bundled-features/src/template-resolver/handlers/patch-draft.write.ts +19 -6
  22. package/packages/bundled-features/src/template-resolver/handlers/publish-templates.write.ts +69 -0
  23. package/packages/bundled-features/src/template-resolver/handlers/set.write.ts +7 -0
  24. package/packages/bundled-features/src/template-resolver/handlers/template-write.ts +15 -3
  25. package/packages/bundled-features/src/template-resolver/handlers/tenant-settings.ts +122 -0
  26. package/packages/bundled-features/src/template-resolver/handlers/toggle-status.write.ts +25 -10
  27. package/packages/bundled-features/src/template-resolver/handlers/upsert-system.write.ts +7 -3
  28. package/packages/bundled-features/src/template-resolver/handlers/usage.query.ts +131 -18
  29. package/packages/bundled-features/src/template-resolver/handlers/validate.query.ts +10 -0
  30. package/packages/bundled-features/src/template-resolver/i18n.ts +3 -0
  31. package/packages/bundled-features/src/template-resolver/index.ts +29 -0
  32. package/packages/bundled-features/src/template-resolver/layout-regions.ts +54 -16
  33. package/packages/bundled-features/src/template-resolver/print-document.ts +3 -1
  34. package/packages/bundled-features/src/template-resolver/seeding.ts +4 -0
  35. package/packages/bundled-features/src/template-resolver/template-delivery-policy.ts +11 -1
  36. package/packages/bundled-features/src/template-resolver/template-edit-policy.ts +37 -1
  37. package/packages/bundled-features/src/template-resolver/template-meta.ts +25 -6
  38. package/packages/bundled-features/src/template-resolver/template-preview.ts +6 -7
  39. package/packages/bundled-features/src/template-resolver/template-validation.ts +10 -3
  40. package/packages/bundled-features/src/template-resolver/web/constants.ts +29 -0
  41. package/packages/bundled-features/src/template-resolver/web/document-blocks.ts +5 -0
  42. package/packages/bundled-features/src/template-resolver/web/document-editor-extensions.ts +34 -2
  43. package/packages/bundled-features/src/template-resolver/web/document-editor-fonts.ts +13 -0
  44. package/packages/bundled-features/src/template-resolver/web/document-fonts.ts +21 -0
  45. package/packages/bundled-features/src/template-resolver/web/document-frame-control.ts +428 -0
  46. package/packages/bundled-features/src/template-resolver/web/document-frame-node-view.tsx +207 -0
  47. package/packages/bundled-features/src/template-resolver/web/document-print-decorations.ts +196 -0
  48. package/packages/bundled-features/src/template-resolver/web/document-print-defaults.ts +59 -0
  49. package/packages/bundled-features/src/template-resolver/web/document-print-style.ts +246 -0
  50. package/packages/bundled-features/src/template-resolver/web/document-repeat-strip.ts +104 -0
  51. package/packages/bundled-features/src/template-resolver/web/document-schema.ts +29 -9
  52. package/packages/bundled-features/src/template-resolver/web/document-style-css.ts +107 -0
  53. package/packages/bundled-features/src/template-resolver/web/document-tiptap-bubble.tsx +24 -2
  54. package/packages/bundled-features/src/template-resolver/web/document-tiptap-content.tsx +66 -7
  55. package/packages/bundled-features/src/template-resolver/web/document-tiptap-context.ts +13 -1
  56. package/packages/bundled-features/src/template-resolver/web/document-tiptap-editor.tsx +24 -4
  57. package/packages/bundled-features/src/template-resolver/web/document-tiptap-helpers.ts +149 -4
  58. package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar-groups.tsx +290 -10
  59. package/packages/bundled-features/src/template-resolver/web/document-tiptap-toolbar.tsx +90 -8
  60. package/packages/bundled-features/src/template-resolver/web/document-toolbar-blocks.ts +20 -0
  61. package/packages/bundled-features/src/template-resolver/web/fontsource-css.d.ts +2 -0
  62. package/packages/bundled-features/src/template-resolver/web/i18n.ts +28 -0
  63. package/packages/bundled-features/src/template-resolver/web/index.ts +34 -0
  64. package/packages/bundled-features/src/template-resolver/web/pdf-constants.ts +6 -0
  65. package/packages/bundled-features/src/template-resolver/web/qualified-names.ts +7 -0
  66. package/packages/bundled-features/src/template-resolver/web/use-document-tiptap-model.ts +74 -5
  67. package/packages/bundled-features/src/tenant/company-config.ts +48 -0
  68. package/packages/bundled-features/src/tenant/feature.ts +11 -0
  69. package/packages/bundled-features/src/tenant/i18n.ts +14 -0
  70. package/samples/apps/ui-walkthrough/src/app/auth-constants.ts +51 -0
  71. package/samples/apps/ui-walkthrough/src/app/seed-print-editor-tenants.ts +118 -0
  72. package/samples/apps/ui-walkthrough/src/app/seed-templates.ts +2 -0
  73. package/samples/apps/ui-walkthrough/src/app/server.ts +9 -1
  74. package/samples/apps/ui-walkthrough/src/app/template-seed-data.ts +27 -1
  75. package/samples/apps/ui-walkthrough/src/run-config.ts +1 -0
  76. package/samples/apps/use-all-bundled/feature-manifest.json +236 -0
  77. package/samples/recipes/pdf-letterhead/README.md +23 -0
  78. package/samples/recipes/pdf-letterhead/package.json +12 -0
  79. package/samples/recipes/pdf-letterhead/src/letterhead.ts +92 -0
@@ -0,0 +1,107 @@
1
+ // Pure CSS-string builders over already validated values. Shared by the server compile and the
2
+ // editor decorations, so the PDF and the editor measure the same way. The custom properties
3
+ // (--paper-pt, --paper-mm, ...) default to real units for print; the editor overrides them to scale.
4
+ import { DOCUMENT_FONT_PRINT_STACKS, type DocumentFont } from "./document-fonts.ts";
5
+
6
+ export type DocumentTextStyle = {
7
+ readonly color?: string | null;
8
+ readonly font?: DocumentFont | null;
9
+ readonly sizePt?: number | null;
10
+ };
11
+
12
+ export type DocumentBlockStyle = {
13
+ readonly lineHeight?: number | null;
14
+ readonly spaceBeforePt?: number | null;
15
+ readonly spaceAfterPt?: number | null;
16
+ };
17
+
18
+ export type DocumentFrameBox = {
19
+ readonly xMm: number;
20
+ readonly yMm: number;
21
+ readonly widthMm: number;
22
+ readonly heightMm: number;
23
+ };
24
+
25
+ function pt(value: number): string {
26
+ return `calc(${value} * var(--paper-pt,1pt))`;
27
+ }
28
+
29
+ function mm(value: number): string {
30
+ return `calc(${value} * var(--paper-mm,1mm))`;
31
+ }
32
+
33
+ function declarations(parts: ReadonlyArray<string | null>): string {
34
+ return parts.filter((part): part is string => part !== null).join(";");
35
+ }
36
+
37
+ export function documentTextStyleCss(style: DocumentTextStyle): string {
38
+ return declarations([
39
+ style.color == null ? null : `color:${style.color}`,
40
+ style.font == null ? null : `font-family:${DOCUMENT_FONT_PRINT_STACKS[style.font]}`,
41
+ style.sizePt == null ? null : `font-size:${pt(style.sizePt)}`,
42
+ ]);
43
+ }
44
+
45
+ export function documentBlockStyleCss(style: DocumentBlockStyle): string {
46
+ return declarations([
47
+ style.lineHeight == null ? null : `line-height:${style.lineHeight}`,
48
+ style.spaceBeforePt == null ? null : `margin-top:${pt(style.spaceBeforePt)}`,
49
+ style.spaceAfterPt == null ? null : `margin-bottom:${pt(style.spaceAfterPt)}`,
50
+ ]);
51
+ }
52
+
53
+ // Chromium clips body content at the margin box, so a frame that sits in the margin is moved
54
+ // to the content edge; the first-page margin override in the print CSS makes that edge reach the frame.
55
+ export function documentFrameCss(frame: DocumentFrameBox): string {
56
+ return declarations([
57
+ "position:absolute",
58
+ "overflow:hidden",
59
+ `--frame-x:${mm(frame.xMm)}`,
60
+ `left:max(0px,calc(${frame.xMm} * var(--paper-mm,1mm) - var(--page-margin-left,0px)))`,
61
+ `top:max(0px,calc(${frame.yMm} * var(--paper-mm,1mm) - var(--first-page-top,0px)))`,
62
+ `width:${mm(frame.widthMm)}`,
63
+ `height:${mm(frame.heightMm)}`,
64
+ ]);
65
+ }
66
+
67
+ export function documentCellStyleCss(style: { readonly background?: string | null }): string {
68
+ return style.background == null ? "" : `background-color:${style.background}`;
69
+ }
70
+
71
+ // Gap between the bottom of a flow-reserving frame and the first flowing line, shared by the
72
+ // editor's reserved area and the PDF's first-page spacer.
73
+ export const FRAME_FLOW_GAP_MM = 8;
74
+
75
+ // Effective paragraph defaults when nothing is set: the PDF body and the editor paper both use
76
+ // them, so a line sits at the same height in both.
77
+ export const PARAGRAPH_DEFAULT_LINE_HEIGHT = 1.2;
78
+ // One "line" (the body size times the default line height) is the spacing unit of paragraphs,
79
+ // headings, tables and lists, so text sits on a line grid with one blank line between blocks.
80
+ export function documentLinePt(fontSizePt: number): number {
81
+ return Number((fontSizePt * PARAGRAPH_DEFAULT_LINE_HEIGHT).toFixed(2));
82
+ }
83
+
84
+ export const DOCUMENT_LINE_VARIABLE = "--doc-line";
85
+
86
+ export const SENDER_LINE_SIZE_PT = 7;
87
+ const SENDER_LINE_INSET_MM = 1.7;
88
+ const SENDER_LINE_RECIPIENT_GAP_MM = 4;
89
+ const SENDER_LINE_RULE_COLOR = "#8a877e";
90
+
91
+ // Every paragraph of an address frame starts at the page's text margin, or inset when the frame
92
+ // sits right of it; the sender line also carries the rule and the gap to the recipient. Only
93
+ // emitted for a real sender line, so an address frame without one starts with the recipient.
94
+ export function addressParagraphCss(isSenderLine: boolean): string {
95
+ return declarations([
96
+ `margin-left:max(${mm(SENDER_LINE_INSET_MM)},calc(var(--paper-margin-left,0px) - var(--frame-x,0px)))`,
97
+ `padding-right:${mm(SENDER_LINE_INSET_MM)}`,
98
+ "margin-bottom:0",
99
+ ...(isSenderLine
100
+ ? [
101
+ `border-bottom:1px solid ${SENDER_LINE_RULE_COLOR}`,
102
+ `margin-bottom:${mm(SENDER_LINE_RECIPIENT_GAP_MM)}`,
103
+ `padding-bottom:${mm(0.7)}`,
104
+ ]
105
+ : []),
106
+ ]);
107
+ }
@@ -84,6 +84,24 @@ function renderBlockOptionsBar(c: TiptapContext, selectedBlock: SelectedBlock):
84
84
  <code>{selectedBlock.detail}</code>
85
85
  )}
86
86
  </span>
87
+ {selectedBlock.type === "resource" && c.hosted && c.print === "flow" && (
88
+ <Button
89
+ type="button"
90
+ size="sm"
91
+ variant="secondary"
92
+ pressed={selectedBlock.bleed}
93
+ testId="document-image-bleed"
94
+ onClick={() => {
95
+ editor
96
+ .chain()
97
+ .focus()
98
+ .updateAttributes("resource", { bleed: !selectedBlock.bleed })
99
+ .run();
100
+ }}
101
+ >
102
+ {t("documentDesigner.editor.imageBleed")}
103
+ </Button>
104
+ )}
87
105
  <Button
88
106
  type="button"
89
107
  size="sm"
@@ -102,7 +120,8 @@ function renderBubbleRepeatBar(c: TiptapContext): ReactNode {
102
120
  const { t, Button, editor } = c;
103
121
  const repeatTable = c.activeState?.repeatTable ?? null;
104
122
  return (
105
- repeatTable !== null && (
123
+ repeatTable !== null &&
124
+ c.print === "none" && (
106
125
  <div data-testid="document-repeat-bar" className={BUBBLE_BAR_CLASS}>
107
126
  <span className="px-1.5 text-[10px] uppercase tracking-wide text-muted-foreground">
108
127
  {repeatBarLabelOf(c)}
@@ -140,7 +159,9 @@ export function renderBubbleMenu(c: TiptapContext): ReactNode {
140
159
  shouldShow={({ editor: e, element }) =>
141
160
  // A leading image is selected by default; the bubble belongs to a deliberate selection.
142
161
  (e.isFocused || element.contains(document.activeElement)) &&
143
- (e.isActive("resource") || e.isActive("button") || findRepeatTable(e) !== null)
162
+ (e.isActive("resource") ||
163
+ e.isActive("button") ||
164
+ (c.print === "none" && findRepeatTable(e) !== null))
144
165
  }
145
166
  >
146
167
  {selectedBlock !== null ? renderBlockOptionsBar(c, selectedBlock) : renderBubbleRepeatBar(c)}
@@ -153,6 +174,7 @@ export function renderInlineRepeatBar(c: TiptapContext): ReactNode {
153
174
  const repeatTable = c.activeState?.repeatTable ?? null;
154
175
  return (
155
176
  repeatTable !== null &&
177
+ c.print === "none" &&
156
178
  !c.readOnly &&
157
179
  !c.canvas && (
158
180
  <div
@@ -2,8 +2,58 @@ import { cn } from "@cosmicdrift/kumiko-renderer/web";
2
2
  import type { Editor } from "@tiptap/core";
3
3
  import { EditorContent } from "@tiptap/react";
4
4
  import type { ReactNode } from "react";
5
+ import type { DocumentPrintMode } from "./document-toolbar-blocks.ts";
5
6
 
6
- export function renderEditorContent(editor: Editor, canvas: boolean, hosted: boolean): ReactNode {
7
+ // The PDF pads cells 4px / 6px; in paper units so the canvas lines up at every zoom.
8
+ const CANVAS_CELL_PADDING_CLASS =
9
+ "[&_td]:px-[calc(1.5875*var(--paper-mm,1mm))] [&_td]:py-[calc(1.0583*var(--paper-mm,1mm))] [&_th]:px-[calc(1.5875*var(--paper-mm,1mm))] [&_th]:py-[calc(1.0583*var(--paper-mm,1mm))]";
10
+
11
+ // The PDF has no heading rules, so the canvas copies the browser defaults (size, margin, inherited line height).
12
+ function headingClasses(canvas: boolean, page: boolean): string {
13
+ return canvas
14
+ ? cn(
15
+ "[&_h1]:text-[length:2em] [&_h1]:leading-[inherit] [&_h1]:font-bold [&_h2]:text-[length:1.5em] [&_h2]:leading-[inherit] [&_h2]:font-bold [&_h3]:text-[length:1.17em] [&_h3]:leading-[inherit] [&_h3]:font-bold",
16
+ page
17
+ ? "[&_:is(h1,h2,h3)]:mt-0 [&_:is(h1,h2,h3)]:mb-(--doc-line)"
18
+ : "[&_h1]:my-[0.67em] [&_h2]:my-[0.83em] [&_h3]:my-[1em]",
19
+ " [&_:is(h1,h2,h3)_[data-variable-chip]]:text-[length:calc(var(--paper-font-size)*0.9)]",
20
+ )
21
+ : "[&_h1]:text-xl [&_h1]:font-bold [&_h2]:text-lg [&_h2]:font-bold [&_h3]:text-base [&_h3]:font-bold";
22
+ }
23
+
24
+ function tableClasses(canvas: boolean, page: boolean): string {
25
+ return canvas
26
+ ? cn(
27
+ page
28
+ ? "[&_table]:mb-(--doc-line) [&_table]:mt-0 [&_.ProseMirror>.tableWrapper:last-child_table]:mb-0"
29
+ : "[&_table]:mb-2 [&_table]:mt-6 [&_.ProseMirror>:first-child_table]:mt-0 [&_.ProseMirror>table:first-child]:mt-0",
30
+ "[&_td:last-child>p[data-size=small][data-align=right]]:whitespace-nowrap",
31
+ )
32
+ : "[&_table]:my-2";
33
+ }
34
+
35
+ // The PDF prints cells top-aligned, header like body text and no rules unless the layout sets a
36
+ // table style; the dashed outline only marks the cells while editing, like the frame outline.
37
+ function cellClasses(canvas: boolean, print: DocumentPrintMode): string {
38
+ return cn(
39
+ print !== "none"
40
+ ? cn(
41
+ "[&_td]:align-top [&_th]:align-top [&_th]:font-bold",
42
+ "[&_td]:outline-dashed [&_td]:outline-1 [&_td]:-outline-offset-1 [&_td]:outline-border [&_th]:outline-dashed [&_th]:outline-1 [&_th]:-outline-offset-1 [&_th]:outline-border",
43
+ )
44
+ : "[&_td]:border-b [&_td]:border-border [&_th]:border-b [&_th]:border-border [&_th]:text-xs [&_th]:text-muted-foreground",
45
+ canvas ? CANVAS_CELL_PADDING_CLASS : "[&_td]:px-2 [&_td]:py-1.5 [&_th]:px-2 [&_th]:py-1.5",
46
+ print === "none" && (canvas ? "[&_th]:font-bold" : "[&_th]:font-medium"),
47
+ );
48
+ }
49
+
50
+ export function renderEditorContent(
51
+ editor: Editor,
52
+ canvas: boolean,
53
+ hosted: boolean,
54
+ print: DocumentPrintMode,
55
+ ): ReactNode {
56
+ const page = print === "page";
7
57
  return (
8
58
  <EditorContent
9
59
  editor={editor}
@@ -14,16 +64,25 @@ export function renderEditorContent(editor: Editor, canvas: boolean, hosted: boo
14
64
  hosted
15
65
  ? "[&_.ProseMirror]:outline-none"
16
66
  : "[&_.ProseMirror]:min-h-40 [&_.ProseMirror]:outline-none",
67
+ // Frames are placed against the paper, so the editor root must not become their containing block.
68
+ // Important: prosemirror's own unlayered `.ProseMirror{position:relative}` beats the utilities layer.
69
+ print === "page" && "[&_.ProseMirror]:static!",
70
+ // The PDF gives every body paragraph this spacing (see PRINT_CSS); cells and list items stay flush.
71
+ page &&
72
+ "[&_p]:mb-(--doc-line) [&_ul]:mb-(--doc-line) [&_ol]:mb-(--doc-line) [&_td>p]:mb-0 [&_th>p]:mb-0 [&_li>p]:mb-0 [&_td>:last-child]:mb-0 [&_th>:last-child]:mb-0 [&_li>:last-child]:mb-0 [&_.ProseMirror>:last-child]:mb-0",
73
+ print !== "none" &&
74
+ "[&_td]:relative [&_th]:relative [&_.column-resize-handle]:pointer-events-none [&_.column-resize-handle]:absolute [&_.column-resize-handle]:-right-0.5 [&_.column-resize-handle]:bottom-0 [&_.column-resize-handle]:top-0 [&_.column-resize-handle]:w-1 [&_.column-resize-handle]:bg-primary",
75
+ "[&_[data-borders=none]_td]:border-transparent [&_[data-borders=none]_th]:border-transparent",
17
76
  "[&_ul]:list-disc [&_ol]:list-decimal [&_ul]:pl-6 [&_ol]:pl-6",
18
- "[&_h1]:text-xl [&_h1]:font-bold [&_h2]:text-lg [&_h2]:font-bold [&_h3]:text-base [&_h3]:font-bold",
77
+ headingClasses(canvas, page),
19
78
  "[&_a]:underline [&_a]:text-primary",
20
79
  "[&_strong]:font-bold [&_em]:italic [&_u]:underline",
21
- canvas
22
- ? "[&_table]:mb-2 [&_table]:mt-6 [&_.ProseMirror>:first-child_table]:mt-0 [&_.ProseMirror>table:first-child]:mt-0 [&_td:last-child>p[data-size=small][data-align=right]]:whitespace-nowrap"
23
- : "[&_table]:my-2",
80
+ tableClasses(canvas, page),
81
+ // A layout region has no content below its table; margins there only inflate the region box.
82
+ hosted && "[&_td>div]:my-0 [&_.ProseMirror>.tableWrapper:last-child_table]:mb-0",
24
83
  "[&_table]:w-full [&_table]:border-collapse",
25
- "[&_td]:border-b [&_td]:border-border [&_td]:px-2 [&_td]:py-1.5 [&_td>p]:m-0 [&_th>p]:m-0",
26
- "[&_th]:border-b [&_th]:border-border [&_th]:px-2 [&_th]:py-1.5 [&_th]:text-left [&_th]:text-xs [&_th]:font-medium [&_th]:text-muted-foreground",
84
+ "[&_td>p]:m-0 [&_th>p]:m-0 [&_th]:text-left",
85
+ cellClasses(canvas, print),
27
86
  "[&_tr[data-each]>td]:bg-muted/50",
28
87
  "[&_[data-align=left]]:text-left [&_[data-align=center]]:text-center [&_[data-align=right]]:text-right [&_[data-align=justify]]:text-justify",
29
88
  "[&_p[data-size=small]]:text-xs [&_p[data-size=small]]:text-muted-foreground",
@@ -1,16 +1,22 @@
1
1
  import type { Translate } from "@cosmicdrift/kumiko-renderer/headless";
2
2
  import type { Editor } from "@tiptap/core";
3
3
  import type { Dispatch, SetStateAction } from "react";
4
+ import type { PrintDefaultLabels } from "./document-print-defaults.ts";
4
5
  import {
5
6
  type ActiveState,
6
7
  BLOCK_PANEL,
7
8
  HEADING_LEVELS,
9
+ insertPrintBlock,
8
10
  isParagraphStyle,
9
11
  type PanelKind,
10
12
  type Primitives,
11
13
  variableLabelOf,
12
14
  } from "./document-tiptap-helpers.ts";
13
- import type { DocumentToolbarBlock } from "./document-toolbar-blocks.ts";
15
+ import type {
16
+ DocumentPrintBlock,
17
+ DocumentPrintMode,
18
+ DocumentToolbarBlock,
19
+ } from "./document-toolbar-blocks.ts";
14
20
  import type { DocumentEditorLabels } from "./use-document-tiptap-model.ts";
15
21
 
16
22
  export type TiptapContext = {
@@ -25,6 +31,8 @@ export type TiptapContext = {
25
31
  readonly canvas: boolean;
26
32
  readonly compact: boolean;
27
33
  readonly hosted: boolean;
34
+ readonly print: DocumentPrintMode;
35
+ readonly printDefaults: PrintDefaultLabels;
28
36
  readonly labels: DocumentEditorLabels;
29
37
  readonly activeState: ActiveState | null;
30
38
  readonly openPanel: PanelKind | null;
@@ -40,6 +48,7 @@ export type TiptapContext = {
40
48
  readonly stableLabels: Readonly<Record<string, string>>;
41
49
  readonly togglePanel: (kind: PanelKind) => void;
42
50
  readonly runBlock: (block: DocumentToolbarBlock) => void;
51
+ readonly runPrintBlock: (block: DocumentPrintBlock) => void;
43
52
  readonly setBlockStyle: (style: string) => void;
44
53
  readonly repeatListLabel: string | undefined;
45
54
  };
@@ -57,6 +66,8 @@ export type TiptapContextInput = Pick<
57
66
  | "canvas"
58
67
  | "compact"
59
68
  | "hosted"
69
+ | "print"
70
+ | "printDefaults"
60
71
  | "labels"
61
72
  | "activeState"
62
73
  | "openPanel"
@@ -112,6 +123,7 @@ export function buildTiptapContext(input: TiptapContextInput): TiptapContext {
112
123
  ...input,
113
124
  togglePanel,
114
125
  runBlock,
126
+ runPrintBlock: (block) => insertPrintBlock(editor, block),
115
127
  setBlockStyle,
116
128
  repeatListLabel: repeatListLabelOf(input.activeState, input.variableLabels),
117
129
  };
@@ -4,15 +4,27 @@
4
4
  // collection is rendered. Variables are inserted as `variable` nodes only,
5
5
  // never as `{{name}}` text, so the sanitizer's schema keeps them.
6
6
 
7
- import { TextareaContentEditor, usePrimitives, useTranslation } from "@cosmicdrift/kumiko-renderer";
7
+ import {
8
+ TextareaContentEditor,
9
+ useLocale,
10
+ usePrimitives,
11
+ useTranslation,
12
+ } from "@cosmicdrift/kumiko-renderer";
8
13
  import { type ReactNode, useEffect } from "react";
14
+ import { printDefaultLabelsOf } from "./document-print-defaults.ts";
9
15
  import { renderEditorContent } from "./document-tiptap-content.tsx";
10
16
  import { buildTiptapContext } from "./document-tiptap-context.ts";
11
- import { NO_LABELS, NO_NAMES, NO_PREVIEWS } from "./document-tiptap-helpers.ts";
17
+ import { NO_LABELS, NO_NAMES, NO_PREVIEWS, printModeOf } from "./document-tiptap-helpers.ts";
12
18
  import { renderHostedEditor, renderInlineEditor } from "./document-tiptap-layout.tsx";
13
- import { type DocumentEditorProps, MAIL_DOCUMENT_BLOCKS } from "./document-toolbar-blocks.ts";
19
+ import {
20
+ type DocumentEditorProps,
21
+ type DocumentLayoutValues,
22
+ MAIL_DOCUMENT_BLOCKS,
23
+ } from "./document-toolbar-blocks.ts";
14
24
  import { useDocumentTiptapModel } from "./use-document-tiptap-model.ts";
15
25
 
26
+ const NO_LAYOUT_VALUES: DocumentLayoutValues = {};
27
+
16
28
  export default function DocumentTiptapEditor({
17
29
  id,
18
30
  value,
@@ -25,16 +37,20 @@ export default function DocumentTiptapEditor({
25
37
  systemVariables = NO_NAMES,
26
38
  resourcePreviews = NO_PREVIEWS,
27
39
  chrome = "default",
40
+ layoutValues = NO_LAYOUT_VALUES,
28
41
  toolbar = "full",
29
42
  renderPaper,
30
43
  toolbarHost,
31
44
  onEditorReady,
45
+ onFrameControlChange,
32
46
  }: DocumentEditorProps): ReactNode {
33
47
  const t = useTranslation();
48
+ const locale = useLocale().locale();
34
49
  const { ActionOverflowMenu, Button, Field, Input } = usePrimitives();
35
50
  const canvas = chrome === "canvas";
36
51
  const compact = toolbar === "compact" && !canvas;
37
52
  const hosted = toolbarHost !== undefined;
53
+ const print = printModeOf(configuredBlocks, hosted, canvas);
38
54
  // A photo block needs an images variable to bind to; without one the button would open an empty panel.
39
55
  const blocks =
40
56
  imageVariables.length === 0
@@ -48,6 +64,8 @@ export default function DocumentTiptapEditor({
48
64
  onChange,
49
65
  readOnly,
50
66
  onEditorReady,
67
+ onFrameControlChange,
68
+ print,
51
69
  variableLabels,
52
70
  systemVariables,
53
71
  resourcePreviews,
@@ -94,6 +112,8 @@ export default function DocumentTiptapEditor({
94
112
  canvas,
95
113
  compact,
96
114
  hosted,
115
+ print,
116
+ printDefaults: printDefaultLabelsOf(t, locale, layoutValues),
97
117
  labels: model.labels,
98
118
  activeState: model.activeState,
99
119
  openPanel: model.openPanel,
@@ -108,7 +128,7 @@ export default function DocumentTiptapEditor({
108
128
  variableLabels,
109
129
  stableLabels: model.stableLabels,
110
130
  });
111
- const editorContent = renderEditorContent(editor, canvas, hosted);
131
+ const editorContent = renderEditorContent(editor, canvas, hosted, print);
112
132
 
113
133
  if (hosted) return renderHostedEditor(context, toolbarHost, editorContent);
114
134
 
@@ -1,7 +1,18 @@
1
1
  import type { usePrimitives } from "@cosmicdrift/kumiko-renderer";
2
2
  import { isOneOf } from "@cosmicdrift/kumiko-types/type-guards";
3
3
  import type { Editor, JSONContent } from "@tiptap/core";
4
- import type { DocumentResourcePreview, DocumentToolbarBlock } from "./document-toolbar-blocks.ts";
4
+ import {
5
+ addressFrameContent,
6
+ columnsTableContent,
7
+ insertFrame,
8
+ textFrameContent,
9
+ } from "./document-frame-control.ts";
10
+ import type {
11
+ DocumentPrintBlock,
12
+ DocumentPrintMode,
13
+ DocumentResourcePreview,
14
+ DocumentToolbarBlock,
15
+ } from "./document-toolbar-blocks.ts";
5
16
 
6
17
  export const NO_LABELS: Readonly<Record<string, string>> = {};
7
18
  export const NO_NAMES: readonly string[] = [];
@@ -35,6 +46,132 @@ export const BLOCK_PANEL: Partial<Record<DocumentToolbarBlock, PanelKind>> = {
35
46
  images: "images",
36
47
  };
37
48
 
49
+ // PDF kinds are the ones whose block set carries a page break; a hosted toolbar means a layout region.
50
+ export function printModeOf(
51
+ blocks: readonly DocumentToolbarBlock[],
52
+ hosted: boolean,
53
+ canvas: boolean,
54
+ ): DocumentPrintMode {
55
+ if (!blocks.includes("pageBreak")) return "none";
56
+ return canvas && !hosted ? "page" : "flow";
57
+ }
58
+
59
+ export const FONT_SIZES_PT = [6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 24, 36] as const;
60
+ export const LINE_HEIGHTS = [1, 1.15, 1.2, 1.25, 1.5, 2] as const;
61
+ export const PARAGRAPH_SPACES_PT = [0, 3, 6, 8, 12, 18, 24] as const;
62
+
63
+ export type PrintTextStyleState = {
64
+ readonly font: string | null;
65
+ readonly sizePt: number | null;
66
+ readonly color: string | null;
67
+ };
68
+
69
+ export type PrintBlockStyleState = {
70
+ readonly lineHeight: number | null;
71
+ readonly spaceBeforePt: number | null;
72
+ readonly spaceAfterPt: number | null;
73
+ };
74
+
75
+ export type PrintTableState = {
76
+ readonly bordersOff: boolean;
77
+ readonly cellBackground: string | null;
78
+ };
79
+
80
+ const TEXT_STYLE_MARK = "documentTextStyle";
81
+
82
+ function stringOrNull(value: unknown): string | null {
83
+ return typeof value === "string" ? value : null;
84
+ }
85
+
86
+ function numberOrNull(value: unknown): number | null {
87
+ return typeof value === "number" ? value : null;
88
+ }
89
+
90
+ function printTextStyleOf(e: Editor): PrintTextStyleState {
91
+ const attrs = e.getAttributes(TEXT_STYLE_MARK);
92
+ return {
93
+ font: stringOrNull(attrs["font"]),
94
+ sizePt: numberOrNull(attrs["sizePt"]),
95
+ color: stringOrNull(attrs["color"]),
96
+ };
97
+ }
98
+
99
+ function printBlockStyleOf(e: Editor): PrintBlockStyleState {
100
+ const attrs = e.getAttributes(e.isActive("heading") ? "heading" : "paragraph");
101
+ return {
102
+ lineHeight: numberOrNull(attrs["lineHeight"]),
103
+ spaceBeforePt: numberOrNull(attrs["spaceBeforePt"]),
104
+ spaceAfterPt: numberOrNull(attrs["spaceAfterPt"]),
105
+ };
106
+ }
107
+
108
+ type TableLocation = { readonly pos: number; readonly attrs: Record<string, unknown> };
109
+
110
+ function tableLocationOf(e: Editor): TableLocation | null {
111
+ const { $from } = e.state.selection;
112
+ for (let depth = $from.depth; depth > 0; depth--) {
113
+ const node = $from.node(depth);
114
+ if (node.type.name === "table") return { pos: $from.before(depth), attrs: node.attrs };
115
+ }
116
+ return null;
117
+ }
118
+
119
+ function printTableOf(e: Editor): PrintTableState | null {
120
+ const table = tableLocationOf(e);
121
+ if (table === null) return null;
122
+ const { $from } = e.state.selection;
123
+ let cellBackground: string | null = null;
124
+ for (let depth = $from.depth; depth > 0; depth--) {
125
+ const node = $from.node(depth);
126
+ if (node.type.name === "tableCell" || node.type.name === "tableHeader") {
127
+ cellBackground = stringOrNull(node.attrs["background"]);
128
+ break;
129
+ }
130
+ }
131
+ return { bordersOff: table.attrs["borders"] === "none", cellBackground };
132
+ }
133
+
134
+ type TextStylePatch = { readonly [K in keyof PrintTextStyleState]?: PrintTextStyleState[K] };
135
+
136
+ export function setPrintTextStyle(editor: Editor, patch: TextStylePatch): void {
137
+ const next = { ...printTextStyleOf(editor), ...patch };
138
+ const chain = editor.chain().focus();
139
+ if (next.font === null && next.sizePt === null && next.color === null) {
140
+ chain.unsetMark(TEXT_STYLE_MARK).run();
141
+ } else {
142
+ chain.setMark(TEXT_STYLE_MARK, next).run();
143
+ }
144
+ }
145
+
146
+ export function setPrintBlockStyle(editor: Editor, patch: Partial<PrintBlockStyleState>): void {
147
+ editor
148
+ .chain()
149
+ .focus()
150
+ .updateAttributes("paragraph", patch)
151
+ .updateAttributes("heading", patch)
152
+ .run();
153
+ }
154
+
155
+ export function toggleTableBorders(editor: Editor): void {
156
+ const table = tableLocationOf(editor);
157
+ if (table === null) return;
158
+ const borders = table.attrs["borders"] === "none" ? null : "none";
159
+ editor
160
+ .chain()
161
+ .focus()
162
+ .command(({ tr }) => {
163
+ tr.setNodeMarkup(table.pos, undefined, { ...table.attrs, borders });
164
+ return true;
165
+ })
166
+ .run();
167
+ }
168
+
169
+ export function insertPrintBlock(editor: Editor, block: DocumentPrintBlock): void {
170
+ if (block === "address") insertFrame(editor, addressFrameContent());
171
+ else if (block === "textFrame") insertFrame(editor, textFrameContent());
172
+ else editor.chain().focus().insertContent(columnsTableContent()).run();
173
+ }
174
+
38
175
  export type RepeatTableState = {
39
176
  readonly lists: readonly string[];
40
177
  readonly headerRepeats: boolean;
@@ -134,10 +271,11 @@ function blockStyleOf(e: Editor): "1" | "2" | "3" | "paragraph" {
134
271
 
135
272
  function selectedBlockOf(e: Editor): SelectedBlock | null {
136
273
  if (e.isActive("resource")) {
137
- return { type: "resource", detail: String(e.getAttributes("resource")["resource"]) };
274
+ const attrs = e.getAttributes("resource");
275
+ return { type: "resource", detail: String(attrs["resource"]), bleed: attrs["bleed"] === true };
138
276
  }
139
277
  if (e.isActive("button")) {
140
- return { type: "button", detail: String(e.getAttributes("button")["href"]) };
278
+ return { type: "button", detail: String(e.getAttributes("button")["href"]), bleed: false };
141
279
  }
142
280
  return null; // skip: no resource or button selected
143
281
  }
@@ -154,11 +292,18 @@ export function activeStateOf(e: Editor, showHtml: boolean) {
154
292
  orderedList: e.isActive("orderedList"),
155
293
  link: e.isActive("link"),
156
294
  repeatTable: findRepeatTable(e),
295
+ textStyle: printTextStyleOf(e),
296
+ paragraphStyle: printBlockStyleOf(e),
297
+ printTable: printTableOf(e),
157
298
  };
158
299
  }
159
300
 
160
301
  export type Primitives = ReturnType<typeof usePrimitives>;
161
302
 
162
- export type SelectedBlock = { readonly type: "resource" | "button"; readonly detail: string };
303
+ export type SelectedBlock = {
304
+ readonly type: "resource" | "button";
305
+ readonly detail: string;
306
+ readonly bleed: boolean;
307
+ };
163
308
 
164
309
  export type ActiveState = ReturnType<typeof activeStateOf>;