@formancy/report-builder-react 0.1.0 → 0.3.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/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
2
2
  import { actionForKey, editorText, targetBeside, targetNear } from "@formancy/report-builder-core";
3
3
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
4
- //#region src/canvas.tsx
4
+ // src/canvas.tsx
5
5
  /** A box's place on the sheet: physical left and top, because a page's coordinates run from its top-left whatever the language. */
6
6
  function placed(box) {
7
7
  return {
@@ -27,6 +27,11 @@ function Ruler({ orientation, ticks, length, label }) {
27
27
  }, tick.mm))
28
28
  });
29
29
  }
30
+ /** What a box says beyond its name, as its description: that it runs over, and that a word it prints is missing in the language edited. */
31
+ function described$1(box, t) {
32
+ const said = [box.overflows ? t("canvas.overflow") : void 0, box.missing].filter((sentence) => sentence !== void 0);
33
+ return said.length === 0 ? void 0 : said.join(". ");
34
+ }
30
35
  function selection$1(box) {
31
36
  return box.column === void 0 ? {
32
37
  type: "select",
@@ -86,8 +91,9 @@ function Canvas({ state, t, canvas: canvasRef, sheet: sheetRef, drag, select })
86
91
  "data-kind": box.kind,
87
92
  "data-selected": box.selected ? "true" : void 0,
88
93
  "data-overflows": box.overflows ? "true" : void 0,
94
+ "data-missing": box.missing === void 0 ? void 0 : "true",
89
95
  "data-dragging": box.column === void 0 && box.id === moving ? "true" : void 0,
90
- title: box.overflows ? t("canvas.overflow") : void 0,
96
+ title: described$1(box, t),
91
97
  style: placed(box.px),
92
98
  onPointerDown: (event) => {
93
99
  if (box.column === void 0) drag.press(event, { id: box.id });
@@ -105,6 +111,28 @@ function Canvas({ state, t, canvas: canvasRef, sheet: sheetRef, drag, select })
105
111
  children: box.problems
106
112
  }) : null]
107
113
  }, box.key)),
114
+ canvas.paymentPart === null ? null : /* @__PURE__ */ jsxs("div", {
115
+ role: "img",
116
+ "aria-label": canvas.paymentPart.label,
117
+ "data-report-part": "payment-part",
118
+ "data-selected": canvas.paymentPart.selected ? "true" : void 0,
119
+ "data-missing": canvas.paymentPart.missing === void 0 ? void 0 : "true",
120
+ title: canvas.paymentPart.missing,
121
+ style: placed(canvas.paymentPart.px),
122
+ onClick: () => select({
123
+ type: "select",
124
+ part: "paymentPart"
125
+ }),
126
+ children: [/* @__PURE__ */ jsx("span", {
127
+ "data-report-part": "region-label",
128
+ children: canvas.paymentPart.label
129
+ }), canvas.paymentPart.problems > 0 ? /* @__PURE__ */ jsx("span", {
130
+ "data-report-part": "badge",
131
+ "data-severity": canvas.paymentPart.severity,
132
+ "aria-hidden": "true",
133
+ children: canvas.paymentPart.problems
134
+ }) : null]
135
+ }),
108
136
  placing === null ? null : dropTargets.map((target) => /* @__PURE__ */ jsx("div", {
109
137
  role: "img",
110
138
  "aria-label": target.label,
@@ -117,8 +145,7 @@ function Canvas({ state, t, canvas: canvasRef, sheet: sheetRef, drag, select })
117
145
  ]
118
146
  });
119
147
  }
120
- //#endregion
121
- //#region src/diagnostics.tsx
148
+ // src/diagnostics.tsx
122
149
  function Diagnostics({ state, t, ids, list, follow }) {
123
150
  return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("ul", {
124
151
  ref: list,
@@ -143,8 +170,84 @@ function Diagnostics({ state, t, ids, list, follow }) {
143
170
  })
144
171
  }), state.diagnostics.length === 0 ? /* @__PURE__ */ jsx("p", { children: t("problems.none") }) : null] });
145
172
  }
146
- //#endregion
147
- //#region src/outline.tsx
173
+ // src/image-chooser.tsx
174
+ /**
175
+ * Add image…: the palette's last button, where the host keeps images
176
+ * (0033), and the file input it opens, hidden beside it.
177
+ *
178
+ * The button sends `chooseImage` and, when the editor will take a file
179
+ * now, calls the input's `click()` at once, in the same activation -- a
180
+ * click, Enter or Space -- which opens the platform's chooser; awaiting
181
+ * anything first would lose the activation, and the browser would open
182
+ * nothing. The input is outside the accessibility tree and never has
183
+ * focus: the keyboard stays on the button throughout.
184
+ *
185
+ * On the input's change the file is taken into a local before `value` is
186
+ * cleared, because in a browser clearing it empties `files`; it is cleared
187
+ * so that the same file chosen again is a change again. A chooser closed
188
+ * without a file changes nothing, and nothing is sent. What the file is,
189
+ * and whether it is added, is the editor's to say, in the live region.
190
+ */
191
+ function ImageChooser({ state, t, ids, choose, add }) {
192
+ const input = useRef(null);
193
+ const { images } = state;
194
+ if (!images.store) return null;
195
+ const why = `${ids}-unavailable-add-image`;
196
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("button", {
197
+ type: "button",
198
+ "data-report-part": "palette-item",
199
+ "aria-disabled": state.can.addImage ? void 0 : true,
200
+ "aria-describedby": images.unavailable === void 0 ? void 0 : why,
201
+ "aria-busy": images.adding === null ? void 0 : true,
202
+ onClick: () => {
203
+ if (choose()) input.current?.click();
204
+ },
205
+ children: [t("images.add"), images.unavailable === void 0 ? null : /* @__PURE__ */ jsx("span", {
206
+ id: why,
207
+ hidden: true,
208
+ children: images.unavailable
209
+ })]
210
+ }), /* @__PURE__ */ jsx("input", {
211
+ ref: input,
212
+ type: "file",
213
+ hidden: true,
214
+ accept: images.accept,
215
+ onChange: (event) => {
216
+ const chosen = event.currentTarget;
217
+ const file = chosen.files?.[0];
218
+ chosen.value = "";
219
+ if (file !== void 0) add(file);
220
+ }
221
+ })] });
222
+ }
223
+ // src/image-library.tsx
224
+ function ImageLibrary({ state, t, ids, remove }) {
225
+ const { assets } = state.images;
226
+ if (assets.length === 0) return null;
227
+ return /* @__PURE__ */ jsx("div", {
228
+ role: "group",
229
+ "aria-label": t("images.heading"),
230
+ children: assets.map((image, at) => {
231
+ const why = `${ids}-image-${String(at)}-in-use`;
232
+ return /* @__PURE__ */ jsxs("div", {
233
+ "data-report-part": "property",
234
+ children: [/* @__PURE__ */ jsx("span", { children: image.label }), /* @__PURE__ */ jsxs("button", {
235
+ type: "button",
236
+ "aria-label": image.removeLabel,
237
+ "aria-disabled": image.inUse === void 0 ? void 0 : true,
238
+ "aria-describedby": image.inUse === void 0 ? void 0 : why,
239
+ onClick: () => remove(image.id),
240
+ children: [t("images.remove"), image.inUse === void 0 ? null : /* @__PURE__ */ jsx("span", {
241
+ id: why,
242
+ hidden: true,
243
+ children: image.inUse
244
+ })]
245
+ })]
246
+ }, image.id);
247
+ })
248
+ });
249
+ }
250
+ // src/outline.tsx
148
251
  function rows(nodes, into = []) {
149
252
  nodes.forEach((node, at) => {
150
253
  into.push({
@@ -158,9 +261,23 @@ function rows(nodes, into = []) {
158
261
  }
159
262
  /** The ids of what describes a row: how many problems it has, and that it is wider than its place. */
160
263
  function described(node, problems, overflow) {
161
- const ids = [...node.problems > 0 ? [problems] : [], ...node.overflows ? [overflow] : []];
264
+ const ids = [...node.problems > 0 ? [problems] : [], ...node.overflows === true ? [overflow] : []];
162
265
  return ids.length === 0 ? void 0 : ids.join(" ");
163
266
  }
267
+ /** A row's count of problems: drawn, and said as its description; inside the name it would make "Introduction" read "Introduction 1". */
268
+ function Problems({ row, id, t }) {
269
+ if (row.problems === 0) return null;
270
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("span", {
271
+ "data-report-part": "badge",
272
+ "data-severity": row.severity,
273
+ "aria-hidden": "true",
274
+ children: row.problems
275
+ }), /* @__PURE__ */ jsx("span", {
276
+ id,
277
+ hidden: true,
278
+ children: t("outline.problems", { count: row.problems })
279
+ })] });
280
+ }
164
281
  function selection(node) {
165
282
  return node.column === void 0 ? {
166
283
  type: "select",
@@ -176,8 +293,8 @@ function Outline({ state, t, ids, tree, drag, act }) {
176
293
  region,
177
294
  rows: rows(region.nodes)
178
295
  }));
179
- const all = regions.flatMap((region) => region.rows);
180
- const stop = all.find((row) => row.node.selected)?.node.key ?? all[0]?.node.key;
296
+ const all = [...regions.flatMap((region) => region.rows.map((row) => row.node)), ...state.outlineParts];
297
+ const stop = all.find((row) => row.selected)?.key ?? all[0]?.key;
181
298
  const empty = `${ids}-outline-empty`;
182
299
  if (stop === void 0) return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", {
183
300
  ref: tree,
@@ -191,13 +308,13 @@ function Outline({ state, t, ids, tree, drag, act }) {
191
308
  "data-report-part": "outline-empty",
192
309
  children: t("outline.empty")
193
310
  })] });
194
- return /* @__PURE__ */ jsx("div", {
311
+ return /* @__PURE__ */ jsxs("div", {
195
312
  ref: tree,
196
313
  role: "tree",
197
314
  "aria-label": t("pane.outline"),
198
315
  "data-report-part": "outline",
199
316
  tabIndex: -1,
200
- children: regions.map(({ region, rows: list }) => {
317
+ children: [regions.map(({ region, rows: list }) => {
201
318
  return /* @__PURE__ */ jsxs("div", {
202
319
  role: "group",
203
320
  "aria-label": region.label,
@@ -234,16 +351,11 @@ function Outline({ state, t, ids, tree, drag, act }) {
234
351
  },
235
352
  children: [
236
353
  node.label,
237
- node.problems > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("span", {
238
- "data-report-part": "badge",
239
- "data-severity": node.severity,
240
- "aria-hidden": "true",
241
- children: node.problems
242
- }), /* @__PURE__ */ jsx("span", {
354
+ /* @__PURE__ */ jsx(Problems, {
355
+ row: node,
243
356
  id: problems,
244
- hidden: true,
245
- children: t("outline.problems", { count: node.problems })
246
- })] }) : null,
357
+ t
358
+ }),
247
359
  node.overflows ? /* @__PURE__ */ jsx("span", {
248
360
  id: overflow,
249
361
  hidden: true,
@@ -254,17 +366,55 @@ function Outline({ state, t, ids, tree, drag, act }) {
254
366
  })
255
367
  ]
256
368
  }, region.region);
257
- })
369
+ }), state.outlineParts.map((part, at) => /* @__PURE__ */ jsx(PartRow, {
370
+ part,
371
+ position: at + 1,
372
+ siblings: state.outlineParts.length,
373
+ tabbable: part.key === stop,
374
+ ids,
375
+ t,
376
+ act
377
+ }, part.key))]
258
378
  });
259
379
  }
260
- //#endregion
261
- //#region src/palette.tsx
262
- function Palette({ state, t, ids, drag, place }) {
263
- return /* @__PURE__ */ jsx("div", {
380
+ /**
381
+ * A part of the report's own level, as a row at the tree's top: selected by
382
+ * focus or a click, as a component's row is, and dragged nowhere, since it
383
+ * has one place.
384
+ */
385
+ function PartRow({ part, position, siblings, tabbable, ids, t, act }) {
386
+ const problems = `${ids}-problems-${part.key}`;
387
+ const select = () => act({
388
+ type: "select",
389
+ part: part.part
390
+ });
391
+ return /* @__PURE__ */ jsxs("div", {
392
+ role: "treeitem",
393
+ "aria-level": 1,
394
+ "aria-posinset": position,
395
+ "aria-setsize": siblings,
396
+ "aria-selected": part.selected,
397
+ "aria-describedby": described(part, problems, ""),
398
+ tabIndex: tabbable ? 0 : -1,
399
+ "data-report-part": "outline-item",
400
+ onFocus: () => {
401
+ if (!part.selected) select();
402
+ },
403
+ onClick: select,
404
+ children: [part.label, /* @__PURE__ */ jsx(Problems, {
405
+ row: part,
406
+ id: problems,
407
+ t
408
+ })]
409
+ });
410
+ }
411
+ // src/palette.tsx
412
+ function Palette({ state, t, ids, drag, place, chooser }) {
413
+ return /* @__PURE__ */ jsxs("div", {
264
414
  role: "group",
265
415
  "aria-label": t("pane.palette"),
266
416
  "data-report-part": "palette",
267
- children: state.palette.map((entry) => {
417
+ children: [state.palette.map((entry) => {
268
418
  const why = `${ids}-unavailable-${entry.kind}`;
269
419
  return /* @__PURE__ */ jsxs("button", {
270
420
  type: "button",
@@ -282,11 +432,10 @@ function Palette({ state, t, ids, drag, place }) {
282
432
  children: entry.unavailable
283
433
  })]
284
434
  }, entry.kind);
285
- })
435
+ }), chooser]
286
436
  });
287
437
  }
288
- //#endregion
289
- //#region src/places.tsx
438
+ // src/places.tsx
290
439
  function Places({ state, drag, t, ids, act, choose, cancel }) {
291
440
  const nowhere = `${ids}-nowhere`;
292
441
  return /* @__PURE__ */ jsxs("div", {
@@ -320,8 +469,7 @@ function Places({ state, drag, t, ids, act, choose, cancel }) {
320
469
  ]
321
470
  });
322
471
  }
323
- //#endregion
324
- //#region src/pointer-drag.ts
472
+ // src/pointer-drag.ts
325
473
  /**
326
474
  * Dragging with a pointer: a palette entry, a box on the canvas or an outline
327
475
  * row, dropped on the canvas or beside an outline row.
@@ -452,8 +600,7 @@ function usePointerDrag(editor, zones, placed) {
452
600
  }
453
601
  };
454
602
  }
455
- //#endregion
456
- //#region src/properties.tsx
603
+ // src/properties.tsx
457
604
  /**
458
605
  * The properties: one native control per field of the selection, or of the
459
606
  * report when nothing is selected, each labelled by its field's label.
@@ -463,7 +610,32 @@ function usePointerDrag(editor, zones, placed) {
463
610
  * same. A typed value is the person's until it is committed -- on leaving
464
611
  * the field, or Enter in a single-line one -- and a value the editor refuses
465
612
  * stays as typed, `aria-invalid`, with the reason beside it as its
466
- * description. Escape gives back what the report holds.
613
+ * description. Escape gives back what the report holds. Under the
614
+ * report's own fields, its images (`library`, 0033). A field at a word in
615
+ * a language besides the report's own has its note under it, which
616
+ * describes it too, and is marked while the word is missing (0063).
617
+ *
618
+ * Fields the editor groups -- a QR-bill's creditor and debtor (0039), an
619
+ * e-invoice's parties, its VAT categories and its payment instructions
620
+ * (0044) -- are a `group` named in the editor's words, their name drawn and
621
+ * hidden from the tree, which reads the group's own, and the group's
622
+ * buttons after them, one with nowhere to go `aria-disabled` and still
623
+ * sent. A button keeps the keyboard where it is as its words change, Add to
624
+ * Remove and back, unless the editor says where it goes once done (`then`):
625
+ * the same button of the group a VAT category moved to, the list's Add once
626
+ * one is removed, a new one's first field (report-builder.tsx). Add
627
+ * QR-bill, which gives the properties the bill's in place of the report's,
628
+ * takes its own button away, and the keyboard goes to the first field, the
629
+ * bill's IBAN, as it does when a Remove takes its row.
630
+ *
631
+ * Each run of a paragraph is such a group, its buttons Subscript and
632
+ * Superscript, each named after the run, showing only its own word, and
633
+ * `aria-pressed` as the run is set (0061). Pressed, one sends the
634
+ * selection of its group's text field -- its start, its end and the text
635
+ * it is of -- read from the element as the button is pressed: the one
636
+ * thing only the DOM knows, which the editor applies to the text the
637
+ * report holds and to nothing else. The keyboard then goes where the
638
+ * editor's outcome says, the run that holds the text now.
467
639
  */
468
640
  /** A text, a colour, an expression, a number or a length: typed, then committed. */
469
641
  function Typed({ field, id, validity, commit }) {
@@ -528,48 +700,129 @@ function Control(props) {
528
700
  default: return /* @__PURE__ */ jsx(Typed, { ...props });
529
701
  }
530
702
  }
531
- function Properties({ state, ids, act }) {
532
- return /* @__PURE__ */ jsx("form", {
703
+ /** One field's row: its label, its control and, after a refusal or about a problem, why. `at` is its place among every field, for its ids. */
704
+ function Row({ field, at, ids, act }) {
705
+ const id = `${ids}-field-${at}`;
706
+ const error = `${id}-error`;
707
+ const note = `${id}-note`;
708
+ const described = [field.error === void 0 ? null : error, field.note === void 0 ? null : note].filter((part) => part !== null).join(" ");
709
+ const validity = {
710
+ ...field.error === void 0 ? {} : { "aria-invalid": true },
711
+ ...described === "" ? {} : { "aria-describedby": described }
712
+ };
713
+ return /* @__PURE__ */ jsxs("div", {
714
+ "data-report-part": "property",
715
+ "data-kind": field.kind,
716
+ "data-missing": field.missing === true ? "true" : void 0,
717
+ children: [
718
+ /* @__PURE__ */ jsx("label", {
719
+ htmlFor: id,
720
+ children: field.label
721
+ }),
722
+ /* @__PURE__ */ jsx(Control, {
723
+ field,
724
+ id,
725
+ validity,
726
+ commit: (value) => act({
727
+ type: "setProperty",
728
+ key: field.key,
729
+ value
730
+ })
731
+ }),
732
+ field.error === void 0 ? null : /* @__PURE__ */ jsx("p", {
733
+ id: error,
734
+ "data-report-part": "property-error",
735
+ children: field.error
736
+ }),
737
+ field.note === void 0 ? null : /* @__PURE__ */ jsx("p", {
738
+ id: note,
739
+ "data-report-part": "property-note",
740
+ children: field.note
741
+ })
742
+ ]
743
+ });
744
+ }
745
+ /**
746
+ * What a group's button sends: a run's `setBaseline` with what its group's
747
+ * text field has selected, read from the element now, as only the DOM
748
+ * knows it (0061); any other action as the state gives it. A field bound
749
+ * to a value is a select, and has no selection: its run is set whole.
750
+ */
751
+ function sent(action, group) {
752
+ if (action.action.type !== "setBaseline") return action;
753
+ const field = group?.querySelector("input, textarea");
754
+ if (!(field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement) || field.selectionStart === null || field.selectionEnd === null) return action;
755
+ return {
756
+ ...action,
757
+ action: {
758
+ ...action.action,
759
+ selected: {
760
+ start: field.selectionStart,
761
+ end: field.selectionEnd,
762
+ text: field.value
763
+ }
764
+ }
765
+ };
766
+ }
767
+ /** A group of fields, named in the editor's words, with its buttons after them, each keyed by its place so that it keeps the keyboard as its words change. */
768
+ function Group({ group, first, ids, act, press }) {
769
+ const element = useRef(null);
770
+ return /* @__PURE__ */ jsxs("div", {
771
+ ref: element,
772
+ role: "group",
773
+ "aria-label": group.label,
774
+ "data-report-part": "property-group",
775
+ children: [
776
+ /* @__PURE__ */ jsx("span", {
777
+ "aria-hidden": "true",
778
+ children: group.label
779
+ }),
780
+ group.fields.map((field, at) => /* @__PURE__ */ jsx(Row, {
781
+ field,
782
+ at: first + at,
783
+ ids,
784
+ act
785
+ }, field.key)),
786
+ group.actions.map((action, at) => /* @__PURE__ */ jsx("button", {
787
+ type: "button",
788
+ "aria-label": action.text === void 0 ? void 0 : action.label,
789
+ "aria-pressed": action.pressed,
790
+ "aria-disabled": action.available ? void 0 : true,
791
+ onClick: () => press(sent(action, element.current)),
792
+ children: action.text ?? action.label
793
+ }, `action-${String(at)}`))
794
+ ]
795
+ });
796
+ }
797
+ function Properties({ state, ids, act, press, library }) {
798
+ let next = 0;
799
+ return /* @__PURE__ */ jsxs("form", {
533
800
  "aria-label": state.properties.heading,
534
801
  "data-report-part": "properties",
535
802
  onSubmit: (event) => event.preventDefault(),
536
- children: state.properties.fields.map((field, at) => {
537
- const id = `${ids}-field-${at}`;
538
- const error = `${id}-error`;
539
- const validity = field.error === void 0 ? {} : {
540
- "aria-invalid": true,
541
- "aria-describedby": error
542
- };
543
- return /* @__PURE__ */ jsxs("div", {
544
- "data-report-part": "property",
545
- "data-kind": field.kind,
546
- children: [
547
- /* @__PURE__ */ jsx("label", {
548
- htmlFor: id,
549
- children: field.label
550
- }),
551
- /* @__PURE__ */ jsx(Control, {
552
- field,
553
- id,
554
- validity,
555
- commit: (value) => act({
556
- type: "setProperty",
557
- key: field.key,
558
- value
559
- })
560
- }),
561
- field.error === void 0 ? null : /* @__PURE__ */ jsx("p", {
562
- id: error,
563
- "data-report-part": "property-error",
564
- children: field.error
565
- })
566
- ]
567
- }, field.key);
568
- })
803
+ children: [state.properties.rows.map((row) => {
804
+ const first = next;
805
+ if ("group" in row) {
806
+ next += row.group.fields.length;
807
+ return /* @__PURE__ */ jsx(Group, {
808
+ group: row.group,
809
+ first,
810
+ ids,
811
+ act,
812
+ press
813
+ }, row.group.key);
814
+ }
815
+ next += 1;
816
+ return /* @__PURE__ */ jsx(Row, {
817
+ field: row.field,
818
+ at: first,
819
+ ids,
820
+ act
821
+ }, row.field.key);
822
+ }), library]
569
823
  });
570
824
  }
571
- //#endregion
572
- //#region src/toolbar.tsx
825
+ // src/toolbar.tsx
573
826
  /**
574
827
  * The toolbar: undo and redo, the structural actions on the selection, the
575
828
  * zoom, and the panes a narrow screen shows one at a time.
@@ -746,8 +999,7 @@ function Toolbar({ state, t, act, placeSelection, showPane }) {
746
999
  ]
747
1000
  });
748
1001
  }
749
- //#endregion
750
- //#region src/use-editor-state.ts
1002
+ // src/use-editor-state.ts
751
1003
  /**
752
1004
  * The editor's state as a React value.
753
1005
  *
@@ -762,8 +1014,7 @@ function Toolbar({ state, t, act, placeSelection, showPane }) {
762
1014
  function useEditorState(editor) {
763
1015
  return useSyncExternalStore(editor.subscribe, editor.state, editor.state);
764
1016
  }
765
- //#endregion
766
- //#region src/report-builder.tsx
1017
+ // src/report-builder.tsx
767
1018
  /**
768
1019
  * The report builder for React: the workbench's three panes over one
769
1020
  * report-builder-core editor -- the palette and the outline, the design
@@ -780,18 +1031,40 @@ function useEditorState(editor) {
780
1031
  * The document and its history live in the session, so unmounting this and
781
1032
  * mounting another builder -- React or Angular -- over the same session
782
1033
  * loses nothing, and leaves no subscription behind.
1034
+ *
1035
+ * A host may give a view of its own -- the Studio's PDF -- which stands in
1036
+ * the design view's place while the editor says so (0029): a bare region,
1037
+ * named by the editor's `hostView`, holding what the host gave and nothing
1038
+ * of the builder's, its keys the host's.
1039
+ *
1040
+ * A report written in more than one language has a switch between them
1041
+ * beside its title, which shows and edits the words of the one chosen
1042
+ * (0063).
1043
+ *
1044
+ * Where the host gave the editor a store for images, the palette ends with
1045
+ * Add image…, and the report's own properties list its images (0033): the
1046
+ * file a person chooses goes to `editor.addImage`, which says what became
1047
+ * of it; nothing here reads it.
783
1048
  */
784
1049
  /** A field that keeps its own keys: Ctrl+Z in it undoes typing, not the report, and its arrows move its caret. A checkbox has no typing to undo. */
785
1050
  function keepsItsKeys(element) {
786
1051
  if (element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return true;
787
1052
  return element instanceof HTMLInputElement && element.type !== "checkbox";
788
1053
  }
789
- function ReportBuilder({ editor }) {
1054
+ function ReportBuilder({ editor, hostView }) {
790
1055
  const state = useEditorState(editor);
791
1056
  const t = useMemo(() => editorText(state.locale), [state.locale]);
792
1057
  const ids = useId();
793
1058
  const tree = useRef(null);
794
1059
  const canvas = useRef(null);
1060
+ /** The host's view's region, while it stands in the design view's place. */
1061
+ const view = useRef(null);
1062
+ /** The last key press the host's view had first: React bubbles one from a portal the host opened there through the view, wherever the portal's DOM is. */
1063
+ const hostKey = useRef(null);
1064
+ /** The element the keyboard last stood on in this builder, for a middle pane swapped from under it to hand the keyboard on. */
1065
+ const lastFocused = useRef(null);
1066
+ /** Whether that element stood in the properties form, for a row taken from under it to hand the keyboard to the first field. */
1067
+ const lastInProperties = useRef(false);
795
1068
  const sheet = useRef(null);
796
1069
  const fields = useRef(null);
797
1070
  const problemList = useRef(null);
@@ -802,6 +1075,11 @@ function ReportBuilder({ editor }) {
802
1075
  const wanted = useRef(null);
803
1076
  const focus = useCallback((where) => {
804
1077
  const selectedRow = () => tree.current?.querySelector("[role=\"treeitem\"][aria-selected=\"true\"]") ?? null;
1078
+ if (typeof where === "object") {
1079
+ const groups = editor.state().properties.rows.flatMap((row) => "group" in row ? [row.group.key] : []);
1080
+ const element = fields.current?.querySelectorAll("[data-report-part=\"property-group\"]")[groups.indexOf(where.group)];
1081
+ return (where.button === void 0 ? element?.querySelector("input, select, textarea") : element?.querySelectorAll(":scope > button")[where.button])?.focus();
1082
+ }
805
1083
  switch (where) {
806
1084
  case "selection": return (selectedRow() ?? tree.current)?.focus();
807
1085
  case "outline": return (selectedRow() ?? tree.current?.querySelector("[role=\"treeitem\"][tabindex=\"0\"]") ?? tree.current)?.focus();
@@ -813,24 +1091,40 @@ function ReportBuilder({ editor }) {
813
1091
  closed.current = null;
814
1092
  return from?.focus();
815
1093
  }
816
- case "canvas": return canvas.current?.focus();
1094
+ case "canvas": return (canvas.current ?? view.current)?.focus();
817
1095
  case "properties": return fields.current?.querySelector("input, select, textarea")?.focus();
818
1096
  case "diagnostics": return problemList.current?.focus();
819
1097
  }
820
- }, []);
1098
+ }, [editor]);
821
1099
  useLayoutEffect(() => {
822
1100
  const where = wanted.current;
823
1101
  if (where === null) return;
824
1102
  wanted.current = null;
825
1103
  focus(where);
826
1104
  });
827
- /** Dispatches `action`; when it succeeds, focus goes to `then` -- at once if nothing changed, else once the change is drawn. */
1105
+ useLayoutEffect(() => {
1106
+ const last = lastFocused.current;
1107
+ const dropped = document.activeElement === null || document.activeElement === document.body;
1108
+ if (last instanceof Node && !last.isConnected && dropped) focus("canvas");
1109
+ }, [state.view, focus]);
1110
+ useLayoutEffect(() => {
1111
+ const last = lastFocused.current;
1112
+ const dropped = document.activeElement === null || document.activeElement === document.body;
1113
+ if (lastInProperties.current && last instanceof Node && !last.isConnected && dropped) focus("properties");
1114
+ }, [state, focus]);
1115
+ /**
1116
+ * Dispatches `action`; when it succeeds, focus goes to `then` -- at once
1117
+ * if nothing changed, else once the change is drawn -- or where the
1118
+ * outcome says, where the action decided it only once done: a run's
1119
+ * button, whose run a split moved (0061).
1120
+ */
828
1121
  function act(action, then) {
829
1122
  const before = editor.state();
830
1123
  const outcome = editor.dispatch(action);
831
- if (then === void 0 || !outcome.ok) return outcome;
832
- if (editor.state() === before) focus(then);
833
- else wanted.current = then;
1124
+ const where = outcome.ok ? outcome.then ?? then : void 0;
1125
+ if (where === void 0) return outcome;
1126
+ if (editor.state() === before) focus(where);
1127
+ else wanted.current = where;
834
1128
  return outcome;
835
1129
  }
836
1130
  const drag = usePointerDrag(editor, {
@@ -841,7 +1135,7 @@ function ReportBuilder({ editor }) {
841
1135
  wanted.current = "selection";
842
1136
  });
843
1137
  function onKeyDown(event) {
844
- if (event.defaultPrevented || keepsItsKeys(event.target)) return;
1138
+ if (event.defaultPrevented || keepsItsKeys(event.target) || event.nativeEvent === hostKey.current) return;
845
1139
  const inTree = tree.current?.contains(event.target) === true;
846
1140
  const action = actionForKey(event, inTree ? "outline" : "editor", state.drag !== null);
847
1141
  if (action === void 0) return;
@@ -900,6 +1194,10 @@ function ReportBuilder({ editor }) {
900
1194
  type: "select",
901
1195
  id: null
902
1196
  });
1197
+ else if ("part" in target) act({
1198
+ type: "select",
1199
+ part: target.part
1200
+ }, "selection");
903
1201
  else act(target.column === void 0 ? {
904
1202
  type: "select",
905
1203
  id: target.id
@@ -911,35 +1209,68 @@ function ReportBuilder({ editor }) {
911
1209
  }
912
1210
  const choosing = state.drag?.via === "keyboard" ? state.drag : null;
913
1211
  const count = state.diagnostics.length;
1212
+ const shows = (panel) => !state.hidden.includes(panel);
1213
+ /** A side pane's Hide, named after the pane; the design view takes its room and the keyboard. */
1214
+ const hide = (panel, name) => /* @__PURE__ */ jsx("button", {
1215
+ type: "button",
1216
+ "data-report-part": "hide-pane",
1217
+ "aria-label": t("action.hidePane", { pane: name }),
1218
+ onClick: () => act({
1219
+ type: "hidePanel",
1220
+ panel
1221
+ }, "canvas"),
1222
+ children: t("action.hide")
1223
+ });
914
1224
  return /* @__PURE__ */ jsxs("section", {
915
1225
  ref: root,
916
1226
  "aria-label": t("editor.label"),
917
1227
  className: "wb",
918
1228
  "data-report-part": "editor",
919
1229
  "data-panel": state.panel,
1230
+ "data-hidden": state.hidden.length === 0 ? void 0 : state.hidden.join(" "),
920
1231
  onKeyDown,
1232
+ onFocus: (event) => {
1233
+ lastFocused.current = event.target;
1234
+ lastInProperties.current = event.target.closest("[data-report-part=\"properties\"]") !== null;
1235
+ },
921
1236
  children: [
922
1237
  /* @__PURE__ */ jsx("div", {
923
1238
  className: "wb-pane",
924
- children: /* @__PURE__ */ jsxs("header", { children: [/* @__PURE__ */ jsx("span", {
925
- "data-report-part": "title",
926
- children: state.document.title
927
- }), /* @__PURE__ */ jsx(Toolbar, {
928
- state,
929
- t,
930
- act,
931
- placeSelection,
932
- showPane: (pane) => act({
933
- type: "openPanel",
934
- panel: pane
935
- }, pane)
936
- })] })
1239
+ children: /* @__PURE__ */ jsxs("header", { children: [
1240
+ /* @__PURE__ */ jsx("span", {
1241
+ "data-report-part": "title",
1242
+ children: state.document.title
1243
+ }),
1244
+ state.languages.declared.length > 1 ? /* @__PURE__ */ jsx("select", {
1245
+ "aria-label": t("language.label"),
1246
+ "data-report-part": "language",
1247
+ value: state.languages.edited,
1248
+ onChange: (event) => act({
1249
+ type: "setLanguage",
1250
+ locale: event.target.value
1251
+ }),
1252
+ children: state.languages.declared.map((language) => /* @__PURE__ */ jsx("option", {
1253
+ value: language.locale,
1254
+ children: language.label
1255
+ }, language.locale))
1256
+ }) : null,
1257
+ /* @__PURE__ */ jsx(Toolbar, {
1258
+ state,
1259
+ t,
1260
+ act,
1261
+ placeSelection,
1262
+ showPane: (pane) => act({
1263
+ type: "openPanel",
1264
+ panel: pane
1265
+ }, pane)
1266
+ })
1267
+ ] })
937
1268
  }),
938
- /* @__PURE__ */ jsxs("section", {
1269
+ shows("outline") ? /* @__PURE__ */ jsxs("section", {
939
1270
  className: "wb-pane",
940
1271
  "data-pane": "outline",
941
1272
  children: [
942
- /* @__PURE__ */ jsx("header", { children: t("pane.outline") }),
1273
+ /* @__PURE__ */ jsxs("header", { children: [/* @__PURE__ */ jsx("span", { children: t("pane.outline") }), hide("outline", t("pane.outline"))] }),
943
1274
  /* @__PURE__ */ jsxs("div", {
944
1275
  className: "wb-body",
945
1276
  children: [/* @__PURE__ */ jsx(Palette, {
@@ -947,7 +1278,14 @@ function ReportBuilder({ editor }) {
947
1278
  t,
948
1279
  ids,
949
1280
  drag,
950
- place
1281
+ place,
1282
+ chooser: /* @__PURE__ */ jsx(ImageChooser, {
1283
+ state,
1284
+ t,
1285
+ ids,
1286
+ choose: () => act({ type: "chooseImage" }).ok,
1287
+ add: (file) => void editor.addImage(file)
1288
+ })
951
1289
  }), /* @__PURE__ */ jsx(Outline, {
952
1290
  state,
953
1291
  t,
@@ -967,8 +1305,18 @@ function ReportBuilder({ editor }) {
967
1305
  cancel: cancelPlaces
968
1306
  })
969
1307
  ]
970
- }),
971
- /* @__PURE__ */ jsxs("section", {
1308
+ }) : null,
1309
+ state.view === "host" ? /* @__PURE__ */ jsx("section", {
1310
+ ref: view,
1311
+ "aria-label": state.hostView?.name,
1312
+ "data-pane": "canvas",
1313
+ "data-report-part": "view",
1314
+ tabIndex: -1,
1315
+ onKeyDown: (event) => {
1316
+ hostKey.current = event.nativeEvent;
1317
+ },
1318
+ children: hostView
1319
+ }, "view") : /* @__PURE__ */ jsxs("section", {
972
1320
  className: "wb-pane wb-canvas",
973
1321
  "data-pane": "canvas",
974
1322
  children: [/* @__PURE__ */ jsx("header", { children: t("pane.canvas") }), /* @__PURE__ */ jsx("div", {
@@ -982,57 +1330,66 @@ function ReportBuilder({ editor }) {
982
1330
  select: (action) => act(action, "selection")
983
1331
  })
984
1332
  })]
985
- }),
986
- /* @__PURE__ */ jsxs("section", {
1333
+ }, "design"),
1334
+ shows("properties") || shows("diagnostics") ? /* @__PURE__ */ jsxs("section", {
987
1335
  className: "wb-pane",
988
- "data-pane": state.panel === "diagnostics" ? "diagnostics" : "properties",
989
- children: [
990
- /* @__PURE__ */ jsxs("header", {
991
- "data-pane": "properties",
992
- children: [/* @__PURE__ */ jsx("span", { children: state.properties.heading }), state.selection === null ? null : /* @__PURE__ */ jsx("button", {
1336
+ "data-pane": state.panel === "diagnostics" || !shows("properties") ? "diagnostics" : "properties",
1337
+ children: [shows("properties") ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("header", {
1338
+ "data-pane": "properties",
1339
+ children: [
1340
+ /* @__PURE__ */ jsx("span", { children: state.properties.heading }),
1341
+ state.selection === null ? null : /* @__PURE__ */ jsx("button", {
993
1342
  type: "button",
994
1343
  onClick: () => act({
995
1344
  type: "select",
996
1345
  id: null
997
1346
  }, "properties"),
998
1347
  children: t("properties.report")
999
- })]
1000
- }),
1001
- /* @__PURE__ */ jsx("div", {
1002
- ref: fields,
1003
- className: "wb-body",
1004
- "data-pane": "properties",
1005
- children: /* @__PURE__ */ jsx(Properties, {
1006
- state,
1007
- ids,
1008
- act
1009
- })
1010
- }),
1011
- /* @__PURE__ */ jsx("header", {
1012
- "data-pane": "diagnostics",
1013
- children: /* @__PURE__ */ jsxs("div", {
1014
- role: "status",
1015
- "aria-label": t("pane.problems"),
1016
- children: [/* @__PURE__ */ jsx("span", { children: t("pane.problems") }), count === 0 ? null : /* @__PURE__ */ jsxs(Fragment, { children: [" ", /* @__PURE__ */ jsx("span", {
1017
- "data-report-part": "badge",
1018
- "data-severity": state.diagnostics.some((entry) => entry.severity === "error") ? "error" : "warning",
1019
- children: count
1020
- })] })]
1021
- })
1022
- }),
1023
- /* @__PURE__ */ jsx("div", {
1024
- className: "wb-body",
1025
- "data-pane": "diagnostics",
1026
- children: /* @__PURE__ */ jsx(Diagnostics, {
1348
+ }),
1349
+ hide("properties", t("pane.properties"))
1350
+ ]
1351
+ }), /* @__PURE__ */ jsx("div", {
1352
+ ref: fields,
1353
+ className: "wb-body",
1354
+ "data-pane": "properties",
1355
+ children: /* @__PURE__ */ jsx(Properties, {
1356
+ state,
1357
+ ids,
1358
+ act,
1359
+ press: (action) => act(action.action, action.then),
1360
+ library: state.selection === null ? /* @__PURE__ */ jsx(ImageLibrary, {
1027
1361
  state,
1028
1362
  t,
1029
1363
  ids,
1030
- list: problemList,
1031
- follow
1032
- })
1364
+ remove: (asset) => act({
1365
+ type: "removeImage",
1366
+ asset
1367
+ }, "properties")
1368
+ }) : null
1033
1369
  })
1034
- ]
1035
- }),
1370
+ })] }) : null, shows("diagnostics") ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("header", {
1371
+ "data-pane": "diagnostics",
1372
+ children: [/* @__PURE__ */ jsxs("div", {
1373
+ role: "status",
1374
+ "aria-label": t("pane.problems"),
1375
+ children: [/* @__PURE__ */ jsx("span", { children: t("pane.problems") }), count === 0 ? null : /* @__PURE__ */ jsxs(Fragment, { children: [" ", /* @__PURE__ */ jsx("span", {
1376
+ "data-report-part": "badge",
1377
+ "data-severity": state.diagnostics.some((entry) => entry.severity === "error") ? "error" : "warning",
1378
+ children: count
1379
+ })] })]
1380
+ }), hide("diagnostics", t("pane.problems"))]
1381
+ }), /* @__PURE__ */ jsx("div", {
1382
+ className: "wb-body",
1383
+ "data-pane": "diagnostics",
1384
+ children: /* @__PURE__ */ jsx(Diagnostics, {
1385
+ state,
1386
+ t,
1387
+ ids,
1388
+ list: problemList,
1389
+ follow
1390
+ })
1391
+ })] }) : null]
1392
+ }) : null,
1036
1393
  /* @__PURE__ */ jsx("div", {
1037
1394
  className: "wb-pane",
1038
1395
  children: /* @__PURE__ */ jsx("p", {
@@ -1044,7 +1401,6 @@ function ReportBuilder({ editor }) {
1044
1401
  ]
1045
1402
  });
1046
1403
  }
1047
- //#endregion
1048
1404
  export { ReportBuilder, useEditorState };
1049
1405
 
1050
1406
  //# sourceMappingURL=index.mjs.map