@nivaro/react 0.2.52 → 0.2.54

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 (191) hide show
  1. package/dist/full.css +1 -1
  2. package/dist/hooks/useSessionRecorder.js +1 -1
  3. package/dist/index.d.ts +116 -2
  4. package/dist/layout-editor.d.ts +248 -2
  5. package/dist/shared/components/FormWhatsNew.js +60 -0
  6. package/dist/shared/components/ItemEditForm.js +3994 -3876
  7. package/dist/shared/components/RecordReadView.js +219 -211
  8. package/dist/shared/components/TipLayer.js +27 -27
  9. package/dist/shared/components/help-videos/api.js +51 -41
  10. package/dist/shared/components/help-videos/editor/DetailsTab.js +147 -130
  11. package/dist/shared/components/item-edit/FieldAffordances.js +7 -6
  12. package/dist/shared/components/item-edit/FieldRow.js +353 -304
  13. package/dist/shared/components/item-edit/FormHelpVideoButton.js +59 -0
  14. package/dist/shared/components/item-edit/GroupSection.js +419 -396
  15. package/dist/shared/components/item-edit/HeaderTools.js +59 -56
  16. package/dist/shared/components/item-edit/LayoutContentBlock.js +47 -24
  17. package/dist/shared/components/item-edit/lock-why.js +87 -0
  18. package/dist/shared/components/item-edit/useFormUsageBeacon.js +59 -0
  19. package/dist/shared/components/layout-editor/AddPanel.js +204 -146
  20. package/dist/shared/components/layout-editor/ArrangeButton.js +34 -0
  21. package/dist/shared/components/layout-editor/AutoArrange.js +86 -0
  22. package/dist/shared/components/layout-editor/CanvasCompare.js +79 -0
  23. package/dist/shared/components/layout-editor/ChangeNote.js +146 -0
  24. package/dist/shared/components/layout-editor/CollabParts.js +170 -0
  25. package/dist/shared/components/layout-editor/DesignNode.js +304 -115
  26. package/dist/shared/components/layout-editor/EditorCanvas.js +21 -20
  27. package/dist/shared/components/layout-editor/EditorTopBar.js +452 -175
  28. package/dist/shared/components/layout-editor/FindReplace.js +188 -0
  29. package/dist/shared/components/layout-editor/FormMapButton.js +141 -0
  30. package/dist/shared/components/layout-editor/HistoryPanel.js +234 -109
  31. package/dist/shared/components/layout-editor/LayoutEditor.js +1101 -307
  32. package/dist/shared/components/layout-editor/LayoutsRail.js +94 -67
  33. package/dist/shared/components/layout-editor/NoteMarker.js +38 -0
  34. package/dist/shared/components/layout-editor/PlacementsTable.js +187 -0
  35. package/dist/shared/components/layout-editor/ScheduleChange.js +106 -0
  36. package/dist/shared/components/layout-editor/SessionHistory.js +84 -0
  37. package/dist/shared/components/layout-editor/StructureMenu.js +413 -0
  38. package/dist/shared/components/layout-editor/UnsavedConfirm.js +103 -0
  39. package/dist/shared/components/layout-editor/ai/AiToolsButton.js +625 -0
  40. package/dist/shared/components/layout-editor/ai/ChangeCheck.js +153 -0
  41. package/dist/shared/components/layout-editor/ai/ai-tools-api.js +80 -0
  42. package/dist/shared/components/layout-editor/ai/assist-ops.js +38 -0
  43. package/dist/shared/components/layout-editor/ai/autofill-ops.js +73 -0
  44. package/dist/shared/components/layout-editor/ai/canvas-capture.js +41 -0
  45. package/dist/shared/components/layout-editor/ai-forms/AiComposer.js +271 -0
  46. package/dist/shared/components/layout-editor/ai-forms/AiFormsButton.js +182 -0
  47. package/dist/shared/components/layout-editor/ai-forms/MeaningSuggestions.js +109 -0
  48. package/dist/shared/components/layout-editor/ai-forms/SlimPanel.js +170 -0
  49. package/dist/shared/components/layout-editor/ai-forms/ai-forms-api.js +40 -0
  50. package/dist/shared/components/layout-editor/ai-forms/pending.js +25 -0
  51. package/dist/shared/components/layout-editor/ai-forms/plan.js +263 -0
  52. package/dist/shared/components/layout-editor/ai-rules/AiAskPopover.js +160 -0
  53. package/dist/shared/components/layout-editor/ai-rules/CascadeWriter.js +175 -0
  54. package/dist/shared/components/layout-editor/ai-rules/FieldRuleWriter.js +97 -0
  55. package/dist/shared/components/layout-editor/ai-rules/RuleWriter.js +94 -0
  56. package/dist/shared/components/layout-editor/ai-rules/ValidationSuggest.js +153 -0
  57. package/dist/shared/components/layout-editor/ai-rules/api.js +6 -0
  58. package/dist/shared/components/layout-editor/ai-text/AiTextTools.js +383 -0
  59. package/dist/shared/components/layout-editor/ai-text/text-ops.js +125 -0
  60. package/dist/shared/components/layout-editor/assist/AskPanel.js +278 -0
  61. package/dist/shared/components/layout-editor/assist/ExplainDiff.js +93 -0
  62. package/dist/shared/components/layout-editor/assist/ProposalsButton.js +150 -0
  63. package/dist/shared/components/layout-editor/assist/assist-api.js +63 -0
  64. package/dist/shared/components/layout-editor/assist/jump.js +142 -0
  65. package/dist/shared/components/layout-editor/assist/settings-index.js +118 -0
  66. package/dist/shared/components/layout-editor/auto-arrange.js +101 -0
  67. package/dist/shared/components/layout-editor/bulk/BulkChangesButton.js +221 -0
  68. package/dist/shared/components/layout-editor/bulk/BulkRun.js +237 -0
  69. package/dist/shared/components/layout-editor/bulk/MacrosPanel.js +230 -0
  70. package/dist/shared/components/layout-editor/bulk/bulk-api.js +65 -0
  71. package/dist/shared/components/layout-editor/bulk/field-labels.js +22 -0
  72. package/dist/shared/components/layout-editor/bulk/macro-record.js +142 -0
  73. package/dist/shared/components/layout-editor/bulk/useMacroRecorder.js +65 -0
  74. package/dist/shared/components/layout-editor/bundle-to-active.js +1 -0
  75. package/dist/shared/components/layout-editor/canvas-diff.js +157 -0
  76. package/dist/shared/components/layout-editor/collab/FormFollowShare.js +342 -0
  77. package/dist/shared/components/layout-editor/collab/collab-api.js +77 -0
  78. package/dist/shared/components/layout-editor/commands/CommandPalette.js +221 -0
  79. package/dist/shared/components/layout-editor/commands/intents.js +214 -0
  80. package/dist/shared/components/layout-editor/commands/parse.js +242 -0
  81. package/dist/shared/components/layout-editor/design-context.js +26 -6
  82. package/dist/shared/components/layout-editor/dnd/LayoutDndProvider.js +179 -163
  83. package/dist/shared/components/layout-editor/dnd/collision.js +25 -23
  84. package/dist/shared/components/layout-editor/dnd/rail-drop.js +56 -0
  85. package/dist/shared/components/layout-editor/draft-recovery.js +61 -0
  86. package/dist/shared/components/layout-editor/editor-notes.js +78 -0
  87. package/dist/shared/components/layout-editor/editors/swap-config.js +1 -0
  88. package/dist/shared/components/layout-editor/explain/CanvasRules.js +142 -0
  89. package/dist/shared/components/layout-editor/explain/default-ghosts.js +81 -0
  90. package/dist/shared/components/layout-editor/explain/driver-hover.js +62 -0
  91. package/dist/shared/components/layout-editor/explain/field-sources.js +246 -0
  92. package/dist/shared/components/layout-editor/explain/rule-graph.js +215 -0
  93. package/dist/shared/components/layout-editor/explain/use-explain.js +132 -0
  94. package/dist/shared/components/layout-editor/field-zones.js +116 -0
  95. package/dist/shared/components/layout-editor/form-map.js +138 -0
  96. package/dist/shared/components/layout-editor/history-api.js +87 -0
  97. package/dist/shared/components/layout-editor/inheritance.js +55 -0
  98. package/dist/shared/components/layout-editor/inspect/inspect-api.js +66 -0
  99. package/dist/shared/components/layout-editor/inspect/picker-preview.js +48 -0
  100. package/dist/shared/components/layout-editor/inspect/width-fit.js +87 -0
  101. package/dist/shared/components/layout-editor/inspector/AiFillSetting.js +85 -0
  102. package/dist/shared/components/layout-editor/inspector/Breadcrumb.js +36 -0
  103. package/dist/shared/components/layout-editor/inspector/BulkInspector.js +175 -0
  104. package/dist/shared/components/layout-editor/inspector/ColumnFit.js +112 -0
  105. package/dist/shared/components/layout-editor/inspector/GroupInspector.js +146 -92
  106. package/dist/shared/components/layout-editor/inspector/HelpTab.js +174 -0
  107. package/dist/shared/components/layout-editor/inspector/Inspector.js +256 -175
  108. package/dist/shared/components/layout-editor/inspector/LookTab.js +46 -40
  109. package/dist/shared/components/layout-editor/inspector/OverrideNotice.js +61 -0
  110. package/dist/shared/components/layout-editor/inspector/PickerPreview.js +134 -0
  111. package/dist/shared/components/layout-editor/inspector/PickerTab.js +167 -141
  112. package/dist/shared/components/layout-editor/inspector/RulesTab.js +175 -128
  113. package/dist/shared/components/layout-editor/inspector/Translations.js +156 -0
  114. package/dist/shared/components/layout-editor/inspector/kit.js +3 -3
  115. package/dist/shared/components/layout-editor/inspector/tab-request.js +21 -0
  116. package/dist/shared/components/layout-editor/is-typing.js +9 -0
  117. package/dist/shared/components/layout-editor/keyboard-move.js +66 -0
  118. package/dist/shared/components/layout-editor/lint/LintPanel.js +190 -0
  119. package/dist/shared/components/layout-editor/lint/lint-api.js +52 -0
  120. package/dist/shared/components/layout-editor/load-error.js +37 -0
  121. package/dist/shared/components/layout-editor/matrix/FieldStateMatrix.js +305 -0
  122. package/dist/shared/components/layout-editor/matrix/MatrixGrid.js +181 -0
  123. package/dist/shared/components/layout-editor/matrix/RoleSectionMatrix.js +147 -0
  124. package/dist/shared/components/layout-editor/matrix/matrix-api.js +48 -0
  125. package/dist/shared/components/layout-editor/matrix/role-matrix.js +36 -0
  126. package/dist/shared/components/layout-editor/matrix/state-matrix.js +145 -0
  127. package/dist/shared/components/layout-editor/nav/FindPanel.js +109 -0
  128. package/dist/shared/components/layout-editor/nav/NavTools.js +114 -0
  129. package/dist/shared/components/layout-editor/nav/OutlinePanel.js +228 -0
  130. package/dist/shared/components/layout-editor/nav/ShortcutSheet.js +31 -0
  131. package/dist/shared/components/layout-editor/nav/find.js +20 -0
  132. package/dist/shared/components/layout-editor/nav/tree.js +142 -0
  133. package/dist/shared/components/layout-editor/nav/useEditorNav.js +101 -0
  134. package/dist/shared/components/layout-editor/nav/view-prefs.js +34 -0
  135. package/dist/shared/components/layout-editor/ops/ChangeRequestsPanel.js +143 -0
  136. package/dist/shared/components/layout-editor/ops/DependencyDiagram.js +179 -0
  137. package/dist/shared/components/layout-editor/ops/LayoutOpsBlocks.js +92 -0
  138. package/dist/shared/components/layout-editor/ops/PromotePanel.js +287 -0
  139. package/dist/shared/components/layout-editor/ops/ops-api.js +122 -0
  140. package/dist/shared/components/layout-editor/overview/OverviewActions.js +15 -3
  141. package/dist/shared/components/layout-editor/overview/describe-conditions.js +18 -14
  142. package/dist/shared/components/layout-editor/overview/overview-api.js +105 -88
  143. package/dist/shared/components/layout-editor/panel-style.js +8 -7
  144. package/dist/shared/components/layout-editor/parity-keys.json.js +4 -0
  145. package/dist/shared/components/layout-editor/pin.js +25 -0
  146. package/dist/shared/components/layout-editor/presence.js +111 -0
  147. package/dist/shared/components/layout-editor/preview/PreviewModes.js +224 -0
  148. package/dist/shared/components/layout-editor/preview/PreviewPanes.js +262 -0
  149. package/dist/shared/components/layout-editor/preview/preview-api.js +57 -0
  150. package/dist/shared/components/layout-editor/preview/preview-model.js +157 -0
  151. package/dist/shared/components/layout-editor/reuse/FieldMapStep.js +70 -0
  152. package/dist/shared/components/layout-editor/reuse/FormMoreMenu.js +127 -0
  153. package/dist/shared/components/layout-editor/reuse/LibraryRows.js +150 -0
  154. package/dist/shared/components/layout-editor/reuse/NewFormStart.js +20 -0
  155. package/dist/shared/components/layout-editor/reuse/SectionReuseActions.js +291 -0
  156. package/dist/shared/components/layout-editor/reuse/field-match.js +77 -0
  157. package/dist/shared/components/layout-editor/reuse/reuse-api.js +73 -0
  158. package/dist/shared/components/layout-editor/reuse/seed.js +13 -0
  159. package/dist/shared/components/layout-editor/reuse/useFormFromRecord.js +43 -0
  160. package/dist/shared/components/layout-editor/reuse/useRailCopy.js +34 -0
  161. package/dist/shared/components/layout-editor/settings/FormSettings.js +123 -103
  162. package/dist/shared/components/layout-editor/settings/InheritanceSection.js +1 -1
  163. package/dist/shared/components/layout-editor/settings/ToolsSection.js +174 -100
  164. package/dist/shared/components/layout-editor/settings/WhoSection.js +136 -100
  165. package/dist/shared/components/layout-editor/settings/controls.js +8 -8
  166. package/dist/shared/components/layout-editor/settings/settings-registry.js +35 -11
  167. package/dist/shared/components/layout-editor/shortcuts.js +88 -0
  168. package/dist/shared/components/layout-editor/show-when.js +35 -0
  169. package/dist/shared/components/layout-editor/slot-names.js +4 -3
  170. package/dist/shared/components/layout-editor/structure-ops.js +399 -0
  171. package/dist/shared/components/layout-editor/testing/CostLimitSetting.js +42 -0
  172. package/dist/shared/components/layout-editor/testing/TestingPanels.js +551 -0
  173. package/dist/shared/components/layout-editor/testing/testing-api.js +67 -0
  174. package/dist/shared/components/layout-editor/testing/testing-model.js +393 -0
  175. package/dist/shared/components/layout-editor/testing/use-record-check.js +44 -0
  176. package/dist/shared/components/layout-editor/toolbar-kit.js +242 -0
  177. package/dist/shared/components/layout-editor/unsaved-marks.js +72 -0
  178. package/dist/shared/components/layout-editor/usage/FillChip.js +27 -0
  179. package/dist/shared/components/layout-editor/usage/UsagePanel.js +158 -0
  180. package/dist/shared/components/layout-editor/usage/suggest-sections.js +55 -0
  181. package/dist/shared/components/layout-editor/usage/usage-api.js +104 -0
  182. package/dist/shared/components/layout-editor/useLayoutDraft.js +573 -307
  183. package/dist/shared/components/support/SuggestFormChange.js +143 -0
  184. package/dist/shared/lib/ai-fill.js +37 -0
  185. package/dist/shared/lib/form-usage-beacon.js +84 -0
  186. package/dist/shared/lib/layout-i18n.js +66 -0
  187. package/dist/shared/lib/layout-lint.js +490 -0
  188. package/dist/shared/lib/layout-ops.js +110 -106
  189. package/dist/shared/lib/screenshot.js +54 -28
  190. package/dist/shared/lib/visible-when.js +34 -0
  191. package/package.json +1 -1
@@ -1,16 +1,16 @@
1
- var F = Object.defineProperty;
2
- var L = (s, t, e) => t in s ? F(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
- var a = (s, t, e) => L(s, typeof t != "symbol" ? t + "" : t, e);
4
- import { useQueryClient as $, useQuery as A } from "@tanstack/react-query";
5
- import { useRef as U, useSyncExternalStore as W, useEffect as S, useCallback as p } from "react";
6
- import { toast as K } from "sonner";
7
- import { useNivaroClient as q } from "../../context.js";
8
- import { get as I, post as P } from "../../lib/commands.js";
9
- import { applyLayoutOps as h, isCanvasSlot as V, GROUP_PATCH_KEYS as M, PLACEMENT_PATCH_KEYS as R } from "../../lib/layout-ops.js";
10
- const E = {
11
- bundle: (s) => ["layout-editor", "bundle", s],
12
- versions: (s) => ["layout-editor", "versions", s]
13
- }, j = /* @__PURE__ */ new Set([
1
+ var V = Object.defineProperty;
2
+ var B = (n, e, t) => e in n ? V(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
+ var c = (n, e, t) => B(n, typeof e != "symbol" ? e + "" : e, t);
4
+ import { useQueryClient as P, useQuery as z } from "@tanstack/react-query";
5
+ import { useState as Q, useRef as Y, useSyncExternalStore as G, useEffect as b, useCallback as l } from "react";
6
+ import { toast as H } from "sonner";
7
+ import { useNivaroClient as M } from "../../context.js";
8
+ import { get as J, post as X } from "../../lib/commands.js";
9
+ import { applyLayoutOps as p, isCanvasSlot as Z, GROUP_PATCH_KEYS as ee, PLACEMENT_PATCH_KEYS as te } from "../../lib/layout-ops.js";
10
+ const C = {
11
+ bundle: (n) => ["layout-editor", "bundle", n],
12
+ versions: (n) => ["layout-editor", "versions", n]
13
+ }, se = /* @__PURE__ */ new Set([
14
14
  "name",
15
15
  "slug",
16
16
  "create_label",
@@ -23,291 +23,437 @@ const E = {
23
23
  "parent_layout_id",
24
24
  "tab_mode"
25
25
  ]);
26
- function ue(s) {
27
- return s ? s.some(
28
- (t) => t.op === "layout_update" && Object.keys(t.patch ?? {}).some((e) => j.has(e))
26
+ function qe(n) {
27
+ return n ? n.some(
28
+ (e) => e.op === "layout_update" && Object.keys(e.patch ?? {}).some((t) => se.has(t))
29
29
  ) : !0;
30
30
  }
31
- const B = 600, D = "Someone else changed this layout — reloaded", N = 100;
32
- function w(s) {
33
- const t = (r) => !!r && typeof r == "object" && Array.isArray(r.groups) && Array.isArray(r.placements) && typeof r.rev == "string";
34
- if (t(s)) return s;
35
- const e = s == null ? void 0 : s.data;
36
- return t(e) ? e : null;
31
+ const re = 600, ne = "Someone else changed this layout — reloaded", ie = (n) => `Someone else saved — your ${n} ${n === 1 ? "change was" : "changes were"} kept on top; Save again`, oe = "Someone else saved this layout and some of your changes no longer fit", ae = (n) => `Someone else saved — your ${n === 1 ? "change was" : `${n} changes were`} replayed on top of theirs`, E = (n, e) => `Someone else saved — ${n} of your changes kept, ${e} no longer ${e === 1 ? "fits" : "fit"}: redo ${e === 1 ? "it" : "them"} by hand`, w = 250;
32
+ function $(n) {
33
+ if (n.length === 0) return "Save";
34
+ if (n.length === 1) return n[0].slice(0, w);
35
+ const e = `${n.length} changes: ${n.join(", ")}`;
36
+ return e.length > w ? `${e.slice(0, w - 1)}…` : e;
37
37
  }
38
- function z(s) {
39
- return /^No group "/.test(s) ? "That section is no longer on this form" : /^"[^"]*" is not placed in /.test(s) ? "That field is no longer where it was" : /^"[^"]*" is already in /.test(s) ? "That field is already there" : /^"[^"]*" is not on this form/.test(s) ? "That item is no longer on this form" : /^"[^"]*" is not a container/.test(s) ? "Tabs can only go inside a tab group" : /^"[^"]*" is a reserved name/.test(s) ? "That name is reserved" : /^A group "[^"]*" already exists/.test(s) ? "A section with that key already exists" : /^Unknown setting "/.test(s) ? "That setting can’t be saved on this form" : s;
38
+ const ue = 100;
39
+ function q(n) {
40
+ const e = (s) => !!s && typeof s == "object" && Array.isArray(s.groups) && Array.isArray(s.placements) && typeof s.rev == "string";
41
+ if (e(n)) return n;
42
+ const t = n == null ? void 0 : n.data;
43
+ return e(t) ? t : null;
40
44
  }
41
- function O(s, t = "The layout could not be saved") {
42
- var i, u;
43
- const e = s, r = (e == null ? void 0 : e.status) ?? ((i = e == null ? void 0 : e.response) == null ? void 0 : i.status) ?? null, n = ((u = e == null ? void 0 : e.response) == null ? void 0 : u.data) ?? (e == null ? void 0 : e.response), o = typeof (n == null ? void 0 : n.error) == "string" && n.error || typeof (n == null ? void 0 : n.message) == "string" && n.message || typeof (e == null ? void 0 : e.message) == "string" && e.message || t;
44
- return { status: r, message: z(o), bundle: w(n == null ? void 0 : n.bundle) };
45
+ function he(n) {
46
+ return /^No group "/.test(n) ? "That section is no longer on this form" : /^"[^"]*" is not placed in /.test(n) ? "That field is no longer where it was" : /^"[^"]*" is already in /.test(n) ? "That field is already there" : /^"[^"]*" is not on this form/.test(n) ? "That item is no longer on this form" : /^"[^"]*" is not a container/.test(n) ? "Tabs can only go inside a tab group" : /^"[^"]*" is a reserved name/.test(n) ? "That name is reserved" : /^A group "[^"]*" already exists/.test(n) ? "A section with that key already exists" : /^Unknown setting "/.test(n) ? "That setting can’t be saved on this form" : n;
45
47
  }
46
- const Q = (s) => h(s, [{ op: "layout_update", patch: {} }]), m = (s, t) => s.placements.find((e) => e.field === t.field && e.group_key === (t.group_key ?? null));
47
- function T(s) {
48
- const t = {};
49
- for (const e of R) t[e] = s[e];
50
- return t;
48
+ function F(n, e = "The layout could not be saved") {
49
+ var o, u;
50
+ const t = n, s = (t == null ? void 0 : t.status) ?? ((o = t == null ? void 0 : t.response) == null ? void 0 : o.status) ?? null, r = ((u = t == null ? void 0 : t.response) == null ? void 0 : u.data) ?? (t == null ? void 0 : t.response), a = typeof (r == null ? void 0 : r.error) == "string" && r.error || typeof (r == null ? void 0 : r.message) == "string" && r.message || typeof (t == null ? void 0 : t.message) == "string" && t.message || e;
51
+ return { status: s, message: he(a), bundle: q(r == null ? void 0 : r.bundle) };
51
52
  }
52
- function Y(s) {
53
- const t = {};
54
- for (const e of M) t[e] = s[e];
55
- return t;
53
+ const ce = (n) => p(n, [{ op: "layout_update", patch: {} }]), S = (n, e) => n.placements.find((t) => t.field === e.field && t.group_key === (e.group_key ?? null));
54
+ function A(n) {
55
+ const e = {};
56
+ for (const t of te) e[t] = n[t];
57
+ return e;
56
58
  }
57
- function v(s, t) {
59
+ function de(n) {
58
60
  const e = {};
59
- for (const r of Object.keys(t)) e[r] = s[r] === void 0 ? null : s[r];
61
+ for (const t of ee) e[t] = n[t];
60
62
  return e;
61
63
  }
62
- function G(s, t) {
63
- switch (t.op) {
64
+ function x(n, e) {
65
+ const t = {};
66
+ for (const s of Object.keys(e)) t[s] = n[s] === void 0 ? null : n[s];
67
+ return t;
68
+ }
69
+ function le(n, e) {
70
+ switch (e.op) {
64
71
  case "place":
65
- return [{ op: "remove", at: { field: t.field, group_key: t.group_key ?? null } }];
72
+ return [{ op: "remove", at: { field: e.field, group_key: e.group_key ?? null } }];
66
73
  case "move": {
67
- const e = m(s, t.from);
68
- return e ? [
74
+ const t = S(n, e.from);
75
+ return t ? [
69
76
  {
70
77
  op: "move",
71
- from: { field: e.field, group_key: t.to_group_key ?? null },
72
- to_group_key: e.group_key,
73
- index: e.sort
78
+ from: { field: t.field, group_key: e.to_group_key ?? null },
79
+ to_group_key: t.group_key,
80
+ index: t.sort
74
81
  }
75
82
  ] : [];
76
83
  }
77
84
  case "update_placement": {
78
- const e = m(s, t.at);
79
- return e ? [
85
+ const t = S(n, e.at);
86
+ return t ? [
80
87
  {
81
88
  op: "update_placement",
82
- at: t.at,
83
- patch: v(e, t.patch ?? {})
89
+ at: e.at,
90
+ patch: x(t, e.patch ?? {})
84
91
  }
85
92
  ] : [];
86
93
  }
87
94
  case "remove": {
88
- const e = m(s, t.at);
89
- return e ? [
95
+ const t = S(n, e.at);
96
+ return t ? [
90
97
  {
91
98
  op: "place",
92
- field: e.field,
93
- group_key: e.group_key,
94
- index: e.sort,
95
- patch: T(e)
99
+ field: t.field,
100
+ group_key: t.group_key,
101
+ index: t.sort,
102
+ patch: A(t)
96
103
  }
97
104
  ] : [];
98
105
  }
99
106
  case "group_create":
100
- return [{ op: "group_remove", key: t.key, placements: "delete" }];
107
+ return [{ op: "group_remove", key: e.key, placements: "delete" }];
101
108
  case "group_update": {
102
- const e = s.groups.find((r) => r.key === t.key);
103
- return e ? [
109
+ const t = n.groups.find((s) => s.key === e.key);
110
+ return t ? [
104
111
  {
105
112
  op: "group_update",
106
- key: t.key,
107
- patch: v(e, t.patch ?? {})
113
+ key: e.key,
114
+ patch: x(t, e.patch ?? {})
108
115
  }
109
116
  ] : [];
110
117
  }
111
118
  case "group_move": {
112
- const e = s.groups.find((n) => n.key === t.key);
113
- if (e) return [{ op: "group_move", key: t.key, parent_key: e.parent_key, index: e.sort }];
114
- const r = s.placements.find((n) => n.field === t.key && V(n));
115
- return r ? [{ op: "group_move", key: t.key, parent_key: null, index: r.sort }] : [];
119
+ const t = n.groups.find((r) => r.key === e.key);
120
+ if (t) return [{ op: "group_move", key: e.key, parent_key: t.parent_key, index: t.sort }];
121
+ const s = n.placements.find((r) => r.field === e.key && Z(r));
122
+ return s ? [{ op: "group_move", key: e.key, parent_key: null, index: s.sort }] : [];
116
123
  }
117
124
  case "group_remove": {
118
- const e = s.groups.find((i) => i.key === t.key);
119
- if (!e) return [];
120
- const r = [
125
+ const t = n.groups.find((o) => o.key === e.key);
126
+ if (!t) return [];
127
+ const s = [
121
128
  {
122
129
  op: "group_create",
123
- key: e.key,
124
- label: e.label,
125
- group_type: e.type,
126
- parent_key: e.parent_key,
127
- index: e.sort,
128
- patch: Y(e)
130
+ key: t.key,
131
+ label: t.label,
132
+ group_type: t.type,
133
+ parent_key: t.parent_key,
134
+ index: t.sort,
135
+ patch: de(t)
129
136
  }
130
137
  ];
131
- return s.groups.filter((i) => i.parent_key === e.key).sort((i, u) => i.sort - u.sort).forEach((i, u) => {
132
- r.push({ op: "group_move", key: i.key, parent_key: e.key, index: u });
133
- }), s.placements.filter((i) => i.group_key === e.key).sort((i, u) => i.sort - u.sort).forEach((i, u) => {
134
- const c = t.placements === "ungroup" && !s.placements.some((l) => l.field === i.field && l.group_key === null);
135
- r.push(
136
- c ? {
138
+ return n.groups.filter((o) => o.parent_key === t.key).sort((o, u) => o.sort - u.sort).forEach((o, u) => {
139
+ s.push({ op: "group_move", key: o.key, parent_key: t.key, index: u });
140
+ }), n.placements.filter((o) => o.group_key === t.key).sort((o, u) => o.sort - u.sort).forEach((o, u) => {
141
+ const i = e.placements === "ungroup" && !n.placements.some((h) => h.field === o.field && h.group_key === null);
142
+ s.push(
143
+ i ? {
137
144
  op: "move",
138
- from: { field: i.field, group_key: null },
139
- to_group_key: e.key,
145
+ from: { field: o.field, group_key: null },
146
+ to_group_key: t.key,
140
147
  index: u
141
- } : { op: "place", field: i.field, group_key: e.key, index: u, patch: T(i) }
148
+ } : { op: "place", field: o.field, group_key: t.key, index: u, patch: A(o) }
142
149
  );
143
- }), r;
150
+ }), s;
144
151
  }
145
152
  case "layout_update":
146
- return [{ op: "layout_update", patch: v(s.layout, t.patch ?? {}) }];
153
+ return [{ op: "layout_update", patch: x(n.layout, e.patch ?? {}) }];
147
154
  default:
148
155
  return [];
149
156
  }
150
157
  }
151
- function _(s, t) {
152
- let e = Q(s), r = [];
153
- for (const n of t)
154
- r = [...G(e, n), ...r], e = h(e, [n]);
155
- return r;
158
+ const pe = (n, e) => JSON.stringify(n ?? null) === JSON.stringify(e ?? null), T = (n, e) => Object.entries(e ?? {}).every(([t, s]) => pe(n[t], s));
159
+ function fe(n, e) {
160
+ return e.length === 0 ? !1 : e.every((t) => {
161
+ if (t.op === "update_placement") {
162
+ const s = S(n, t.at);
163
+ return !!s && T(s, t.patch);
164
+ }
165
+ if (t.op === "group_update") {
166
+ const s = n.groups.find((r) => r.key === t.key);
167
+ return !!s && T(s, t.patch);
168
+ }
169
+ return t.op === "layout_update" ? T(n.layout, t.patch) : !1;
170
+ });
171
+ }
172
+ function g(n, e) {
173
+ let t = ce(n), s = [];
174
+ for (const r of e)
175
+ s = [...le(t, r), ...s], t = p(t, [r]);
176
+ return s;
156
177
  }
157
- function H(s) {
158
- const t = [];
159
- for (const e of s)
160
- if (e.op === "update_placement")
161
- for (const [r, n] of Object.entries(e.patch ?? {}))
162
- t.push({
163
- key: `p\0${e.at.field}\0${e.at.group_key ?? ""}\0${r}`,
164
- op: { op: "update_placement", at: e.at, patch: { [r]: n } }
178
+ function ve(n) {
179
+ const e = [];
180
+ for (const t of n)
181
+ if (t.op === "update_placement")
182
+ for (const [s, r] of Object.entries(t.patch ?? {}))
183
+ e.push({
184
+ key: `p\0${t.at.field}\0${t.at.group_key ?? ""}\0${s}`,
185
+ op: { op: "update_placement", at: t.at, patch: { [s]: r } }
165
186
  });
166
- else if (e.op === "group_update")
167
- for (const [r, n] of Object.entries(e.patch ?? {}))
168
- t.push({
169
- key: `g\0${e.key}\0${r}`,
170
- op: { op: "group_update", key: e.key, patch: { [r]: n } }
187
+ else if (t.op === "group_update")
188
+ for (const [s, r] of Object.entries(t.patch ?? {}))
189
+ e.push({
190
+ key: `g\0${t.key}\0${s}`,
191
+ op: { op: "group_update", key: t.key, patch: { [s]: r } }
171
192
  });
172
- else if (e.op === "layout_update")
173
- for (const [r, n] of Object.entries(e.patch ?? {}))
174
- t.push({ key: `l\0${r}`, op: { op: "layout_update", patch: { [r]: n } } });
193
+ else if (t.op === "layout_update")
194
+ for (const [s, r] of Object.entries(t.patch ?? {}))
195
+ e.push({ key: `l\0${s}`, op: { op: "layout_update", patch: { [s]: r } } });
175
196
  else return null;
176
- return t;
197
+ return e;
177
198
  }
178
- const y = (s) => s instanceof Error ? s.message : String(s);
179
- class X {
180
- constructor(t) {
181
- a(this, "server", null);
182
- a(this, "draft", null);
183
- a(this, "queue", []);
184
- a(this, "inFlight", !1);
185
- a(this, "text", /* @__PURE__ */ new Map());
186
- a(this, "undoStack", []);
187
- a(this, "redoStack", []);
188
- a(this, "outcome", "idle");
189
- a(this, "error", null);
190
- a(this, "nextId", 1);
191
- a(this, "listeners", /* @__PURE__ */ new Set());
192
- a(this, "idleWaiters", []);
193
- a(this, "view");
194
- a(this, "subscribe", (t) => (this.listeners.add(t), () => {
195
- this.listeners.delete(t);
199
+ const y = (n) => n instanceof Error ? n.message : String(n);
200
+ class ke {
201
+ constructor(e, t = {}) {
202
+ c(this, "server", null);
203
+ c(this, "draft", null);
204
+ c(this, "queue", []);
205
+ /** Manual mode: steps folded into the draft, not sent yet (oldest first). */
206
+ c(this, "unsaved", []);
207
+ c(this, "inFlight", !1);
208
+ c(this, "text", /* @__PURE__ */ new Map());
209
+ c(this, "undoStack", []);
210
+ c(this, "redoStack", []);
211
+ c(this, "outcome", "idle");
212
+ c(this, "error", null);
213
+ /** Undo / redo as they were when the first unsaved step was made: Discard puts them back. */
214
+ c(this, "checkpoint", null);
215
+ /** Labels of steps a rebase could not replay (#2652). */
216
+ c(this, "dropped", []);
217
+ c(this, "autosave");
218
+ c(this, "nextId", 1);
219
+ c(this, "listeners", /* @__PURE__ */ new Set());
220
+ c(this, "idleWaiters", []);
221
+ c(this, "view");
222
+ c(this, "subscribe", (e) => (this.listeners.add(e), () => {
223
+ this.listeners.delete(e);
196
224
  }));
197
- a(this, "getView", () => this.view);
198
- this.transport = t, this.view = this.buildView();
225
+ c(this, "getView", () => this.view);
226
+ this.transport = e, this.autosave = t.autosave ?? !1, this.view = this.buildView();
199
227
  }
200
228
  /**
201
- * A fetched bundle. Ignored while anything is pending, so typing is never
202
- * overwritten. Our own saves arrive here with the rev we already hold; a new
203
- * rev means someone else saved, and the undo steps (index-based inverses of
204
- * the old state) would undo over their change — so the history goes.
229
+ * A fetched bundle. Ignored while anything is pending or unsaved, so typing
230
+ * and unsaved steps are never overwritten (Save rebases them on a 409). Our
231
+ * own saves arrive here with the rev we already hold; a new rev means
232
+ * someone else saved, and the undo steps (index-based inverses of the old
233
+ * state) would undo over their change — so the history goes.
205
234
  */
206
- receive(t) {
207
- this.pending() || this.server && this.server.rev === t.rev || (this.server && (this.undoStack = [], this.redoStack = []), this.server = t, this.draft = t, this.emit());
208
- }
209
- apply(t, e) {
210
- if (!this.draft || t.length === 0) return;
211
- const r = (e == null ? void 0 : e.label) ?? "Edit", n = e != null && e.text ? H(t) : null;
212
- if (n) return this.applyText(n, r);
213
- this.flushText();
214
- const o = this.draft;
215
- let i;
235
+ receive(e) {
236
+ this.isBusy() || this.server && this.server.rev === e.rev || (this.server && (this.undoStack = [], this.redoStack = []), this.server = e, this.draft = e, this.emit());
237
+ }
238
+ apply(e, t) {
239
+ if (!this.draft || e.length === 0) return;
240
+ const s = (t == null ? void 0 : t.label) ?? "Edit", r = t != null && t.text ? ve(e) : null;
241
+ if (r) return this.autosave ? this.applyText(r, s) : this.stageText(r, s);
242
+ this.flushText(), this.markCheckpoint();
243
+ const a = this.draft;
244
+ let o;
216
245
  try {
217
- h(o, t), i = _(o, t);
218
- } catch (c) {
219
- this.fail(y(c));
246
+ p(a, e), o = g(a, e);
247
+ } catch (i) {
248
+ this.fail(y(i));
220
249
  return;
221
250
  }
222
251
  this.clearOutcome();
223
- const u = this.enqueue("do", t, r);
224
- (e == null ? void 0 : e.undoable) !== !1 && (this.pushUndo({ label: r, forward: t, inverse: i, batchId: u.id }), this.redoStack = []), this.refresh();
252
+ const u = this.add("do", e, s);
253
+ (t == null ? void 0 : t.undoable) !== !1 && (this.pushUndo({ label: s, forward: e, inverse: o, batchId: u.id }), this.redoStack = []), this.refresh();
225
254
  }
226
255
  // Undo / redo pop the step before checking it still applies: a step that no
227
256
  // longer fits the draft is discarded with an error, not retried.
228
257
  undo() {
229
- this.flushText();
230
- const t = this.undoStack.pop();
231
- if (!(!t || !this.draft)) {
232
- try {
233
- h(this.draft, t.inverse);
234
- } catch (e) {
235
- this.fail(`Could not undo: ${y(e)}`);
236
- return;
237
- }
238
- this.clearOutcome(), t.batchId = this.enqueue("undo", t.inverse, `Undo ${t.label}`).id, this.redoStack.push(t), this.refresh();
258
+ this.flushText(), this.markCheckpoint();
259
+ const e = this.undoStack.pop();
260
+ if (!e || !this.draft) return;
261
+ const t = this.unsaved[this.unsaved.length - 1];
262
+ if (!this.autosave && t && t.id === e.batchId) {
263
+ this.unsaved.pop(), this.clearOutcome(), this.redoStack.push(e), this.refresh();
264
+ return;
265
+ }
266
+ try {
267
+ p(this.draft, e.inverse);
268
+ } catch (s) {
269
+ this.fail(`Could not undo: ${y(s)}`);
270
+ return;
239
271
  }
272
+ this.clearOutcome(), e.batchId = this.add("undo", e.inverse, `Undo ${e.label}`).id, this.redoStack.push(e), this.refresh();
240
273
  }
241
274
  redo() {
242
- this.flushText();
243
- const t = this.redoStack.pop();
244
- if (!(!t || !this.draft)) {
275
+ this.flushText(), this.markCheckpoint();
276
+ const e = this.redoStack.pop();
277
+ if (!(!e || !this.draft)) {
245
278
  try {
246
- h(this.draft, t.forward), t.inverse = _(this.draft, t.forward);
247
- } catch (e) {
248
- this.fail(`Could not redo: ${y(e)}`);
279
+ p(this.draft, e.forward), e.inverse = g(this.draft, e.forward);
280
+ } catch (t) {
281
+ this.fail(`Could not redo: ${y(t)}`);
249
282
  return;
250
283
  }
251
- this.clearOutcome(), t.batchId = this.enqueue("redo", t.forward, `Redo ${t.label}`).id, this.undoStack.push(t), this.refresh();
284
+ this.clearOutcome(), e.batchId = this.add("redo", e.forward, `Redo ${e.label}`).id, this.undoStack.push(e), this.refresh();
252
285
  }
253
286
  }
254
- /** Send every pending text edit now (blur, unmount, before another op). */
287
+ /** -n undoes n steps, +n redoes n; stops early when a step no longer applies. */
288
+ jump(e) {
289
+ for (let t = 0; t < Math.abs(e); t++)
290
+ if ((e < 0 ? this.undoStack : this.redoStack).length === 0 || (e < 0 ? this.undo() : this.redo(), this.outcome === "error")) return;
291
+ }
292
+ /** Send every pending text edit now (blur, unmount, before another op). Manual
293
+ * mode: ends the open text burst, so the next keystroke is a new step. */
255
294
  flushText() {
256
- for (const t of [...this.text.keys()]) this.flushTextKey(t);
295
+ this.closeText();
296
+ for (const e of [...this.text.keys()]) this.flushTextKey(e);
257
297
  }
258
- /** Anything typed, queued or in flight. */
298
+ /** Anything typed, queued, in flight or unsaved (an engine worth keeping). */
259
299
  isBusy() {
260
- return this.pending();
261
- }
262
- whenIdle(t = 15e3) {
263
- return this.flushText(), this.pending() ? new Promise((e) => {
264
- let r = !1;
265
- const n = () => {
266
- r || (r = !0, clearTimeout(o), e());
267
- }, o = setTimeout(() => {
268
- this.idleWaiters = this.idleWaiters.filter((i) => i !== n), n();
269
- }, t);
270
- this.idleWaiters.push(n);
300
+ return this.pending() || this.unsaved.length > 0;
301
+ }
302
+ setAutosave(e) {
303
+ e !== this.autosave && (this.flushText(), this.autosave = e, e && this.unsaved.length > 0 ? this.save() : this.emit());
304
+ }
305
+ unsavedOps() {
306
+ return this.unsaved.flatMap((e) => e.ops);
307
+ }
308
+ /**
309
+ * Every unsaved step as one batch against the current rev. Resolves once
310
+ * nothing is in flight (saved, failed or rebased alike) — `unsavedOps()` is
311
+ * empty afterwards only when it landed.
312
+ */
313
+ save(e) {
314
+ if (this.flushText(), this.unsaved.length > 0) {
315
+ const t = this.unsaved;
316
+ this.unsaved = [], this.checkpoint = null;
317
+ const s = this.queue.length > (this.inFlight ? 1 : 0) ? this.queue[this.queue.length - 1] : void 0;
318
+ if ((s == null ? void 0 : s.kind) === "save") {
319
+ const r = [...s.parts ?? [], ...t];
320
+ s.parts = r, s.ops = r.flatMap((a) => a.ops), s.note = $(r.map((a) => a.note)), e && (s.meta = e);
321
+ } else
322
+ this.queue.push({
323
+ id: this.nextId++,
324
+ kind: "save",
325
+ ops: t.flatMap((r) => r.ops),
326
+ note: $(t.map((r) => r.note)),
327
+ parts: t,
328
+ meta: e
329
+ });
330
+ this.clearOutcome(), this.refresh();
331
+ }
332
+ return this.whenIdle(6e4);
333
+ }
334
+ /** Drop the unsaved steps: the draft, Undo and Redo go back to the last save. */
335
+ discard() {
336
+ if (this.closeText(), !(this.unsaved.length === 0 && this.dropped.length === 0)) {
337
+ if (this.checkpoint)
338
+ this.undoStack = this.checkpoint.undo, this.redoStack = this.checkpoint.redo, this.checkpoint = null;
339
+ else for (const e of [...this.unsaved].reverse()) this.rollback(e);
340
+ this.unsaved = [], this.dropped = [], this.outcome = "idle", this.error = null, this.refresh();
341
+ }
342
+ }
343
+ clearDropped() {
344
+ this.dropped.length !== 0 && (this.dropped = [], this.emit());
345
+ }
346
+ /**
347
+ * Steps from a recovered draft (#2653), applied as new changes in order. A
348
+ * step that no longer fits the layout is skipped and listed in `dropped`.
349
+ * Returns how many were applied.
350
+ */
351
+ restore(e) {
352
+ let t = 0;
353
+ for (const s of e) {
354
+ if (!this.draft) break;
355
+ try {
356
+ p(this.draft, s.ops);
357
+ } catch {
358
+ this.dropped = [...this.dropped, s.label];
359
+ continue;
360
+ }
361
+ this.apply(s.ops, { label: s.label }), t += 1;
362
+ }
363
+ return this.emit(), t;
364
+ }
365
+ whenIdle(e = 15e3) {
366
+ return this.flushText(), this.pending() ? new Promise((t) => {
367
+ let s = !1;
368
+ const r = () => {
369
+ s || (s = !0, clearTimeout(a), t());
370
+ }, a = setTimeout(() => {
371
+ this.idleWaiters = this.idleWaiters.filter((o) => o !== r), r();
372
+ }, e);
373
+ this.idleWaiters.push(r);
271
374
  }) : Promise.resolve();
272
375
  }
273
- applyText(t, e) {
274
- const r = this.draft;
376
+ applyText(e, t) {
377
+ const s = this.draft;
275
378
  try {
276
- h(
277
- r,
278
- t.map((n) => n.op)
379
+ p(
380
+ s,
381
+ e.map((r) => r.op)
279
382
  );
280
- } catch (n) {
281
- this.fail(y(n));
383
+ } catch (r) {
384
+ this.fail(y(r));
282
385
  return;
283
386
  }
284
387
  this.clearOutcome();
285
- for (const { key: n, op: o } of t) {
286
- const i = this.text.get(n);
287
- i && clearTimeout(i.timer);
288
- const u = setTimeout(() => this.flushTextKey(n), B), c = (i == null ? void 0 : i.inverse) ?? _(r, [o])[0];
289
- this.text.set(n, { op: o, inverse: c, label: e, timer: u });
388
+ for (const { key: r, op: a } of e) {
389
+ const o = this.text.get(r);
390
+ o && clearTimeout(o.timer);
391
+ const u = setTimeout(() => this.flushTextKey(r), re), i = (o == null ? void 0 : o.inverse) ?? g(s, [a])[0];
392
+ this.text.set(r, { op: a, inverse: i, label: t, timer: u });
290
393
  }
291
394
  this.redoStack = [], this.refresh();
292
395
  }
293
- flushTextKey(t) {
294
- const e = this.text.get(t);
295
- if (!e) return;
296
- clearTimeout(e.timer), this.text.delete(t);
297
- const r = this.enqueue("do", [e.op], e.label);
396
+ /**
397
+ * Manual-mode typing folds at once. Keystrokes in the same box(es) replace
398
+ * the open step's ops, so a burst is one unsaved change and one undo step,
399
+ * back to the value before its first keystroke.
400
+ */
401
+ stageText(e, t) {
402
+ const s = this.draft, r = e.map((i) => i.op);
403
+ this.markCheckpoint();
404
+ const a = e.map((i) => i.key).join("");
405
+ try {
406
+ p(s, r);
407
+ } catch (i) {
408
+ this.fail(y(i));
409
+ return;
410
+ }
411
+ this.clearOutcome();
412
+ const o = this.unsaved[this.unsaved.length - 1], u = this.undoStack[this.undoStack.length - 1];
413
+ if ((o == null ? void 0 : o.textKey) === a && (u == null ? void 0 : u.batchId) === o.id)
414
+ o.ops = r, u.forward = r;
415
+ else {
416
+ this.closeText();
417
+ const i = this.add("do", r, t);
418
+ i.textKey = a, this.pushUndo({ label: t, forward: r, inverse: g(s, r), batchId: i.id });
419
+ }
420
+ this.redoStack = [], this.refresh();
421
+ }
422
+ /** Before the first unsaved step changes Undo / Redo: remember them for Discard. */
423
+ markCheckpoint() {
424
+ this.autosave || this.unsaved.length > 0 || (this.checkpoint = {
425
+ undo: this.undoStack.map((e) => ({ ...e })),
426
+ redo: this.redoStack.map((e) => ({ ...e }))
427
+ });
428
+ }
429
+ closeText() {
430
+ const e = this.unsaved[this.unsaved.length - 1];
431
+ e != null && e.textKey && (e.textKey = void 0);
432
+ }
433
+ flushTextKey(e) {
434
+ const t = this.text.get(e);
435
+ if (!t) return;
436
+ clearTimeout(t.timer), this.text.delete(e);
437
+ const s = this.enqueue("do", [t.op], t.label);
298
438
  this.pushUndo({
299
- label: e.label,
300
- forward: [e.op],
301
- inverse: e.inverse ? [e.inverse] : [],
302
- batchId: r.id
439
+ label: t.label,
440
+ forward: [t.op],
441
+ inverse: t.inverse ? [t.inverse] : [],
442
+ batchId: s.id
303
443
  }), this.refresh();
304
444
  }
305
- enqueue(t, e, r) {
306
- const n = { id: this.nextId++, kind: t, ops: e, note: r };
307
- return this.queue.push(n), n;
445
+ /** A step: sent now (autosave) or held as unsaved (manual). */
446
+ add(e, t, s) {
447
+ if (this.autosave) return this.enqueue(e, t, s);
448
+ const r = { id: this.nextId++, kind: e, ops: t, note: s };
449
+ return this.unsaved.push(r), r;
308
450
  }
309
- pushUndo(t) {
310
- this.undoStack.push(t), this.undoStack.length > N && this.undoStack.shift();
451
+ enqueue(e, t, s) {
452
+ const r = { id: this.nextId++, kind: e, ops: t, note: s };
453
+ return this.queue.push(r), r;
454
+ }
455
+ pushUndo(e) {
456
+ this.undoStack.push(e), this.undoStack.length > ue && this.undoStack.shift();
311
457
  }
312
458
  pending() {
313
459
  return this.inFlight || this.queue.length > 0 || this.text.size > 0;
@@ -315,160 +461,280 @@ class X {
315
461
  clearOutcome() {
316
462
  this.error = null, (this.outcome === "error" || this.outcome === "conflict") && (this.outcome = "idle");
317
463
  }
318
- fail(t) {
319
- this.error = t, this.outcome = "error", this.emit();
464
+ fail(e) {
465
+ this.error = e, this.outcome = "error", this.emit();
320
466
  }
321
467
  /** A batch the server refused: forget its step (or hand an undo/redo back). */
322
- rollback(t) {
323
- const e = (r) => {
324
- const n = r.findIndex((o) => o.batchId === t.id);
325
- return n < 0 ? null : r.splice(n, 1)[0];
468
+ rollback(e) {
469
+ if (e.kind === "save") {
470
+ for (const s of [...e.parts ?? []].reverse()) this.rollback(s);
471
+ return;
472
+ }
473
+ const t = (s) => {
474
+ const r = s.findIndex((a) => a.batchId === e.id);
475
+ return r < 0 ? null : s.splice(r, 1)[0];
326
476
  };
327
- if (t.kind === "do") e(this.undoStack);
328
- else if (t.kind === "undo") {
329
- const r = e(this.redoStack);
330
- r && this.undoStack.push(r);
477
+ if (e.kind === "do") t(this.undoStack);
478
+ else if (e.kind === "undo") {
479
+ const s = t(this.redoStack);
480
+ s && this.undoStack.push(s);
331
481
  } else {
332
- const r = e(this.undoStack);
333
- r && this.redoStack.push(r);
482
+ const s = t(this.undoStack);
483
+ s && this.redoStack.push(s);
484
+ }
485
+ }
486
+ /**
487
+ * Fold `batches` onto `fresh` one by one: the ones that still fit (with
488
+ * their inverses relative to it) and the ones that no longer do.
489
+ */
490
+ refold(e, t) {
491
+ const s = [], r = [], a = /* @__PURE__ */ new Map();
492
+ let o = e;
493
+ for (const u of t)
494
+ try {
495
+ const i = g(o, u.ops);
496
+ o = p(o, u.ops), a.set(u.id, i), s.push(u);
497
+ } catch {
498
+ r.push(u);
499
+ }
500
+ return { kept: s, dropped: r, inverses: a };
501
+ }
502
+ /**
503
+ * After a rebase onto `fresh`: kept steps undo with their new inverses; a
504
+ * step saved before keeps its undo only while every value it set still
505
+ * stands (stillMine). Redo goes — it was relative to the old layout.
506
+ */
507
+ rebaseUndo(e, t, s) {
508
+ this.undoStack = this.undoStack.filter(
509
+ (r) => t.has(r.batchId) || !s.has(r.batchId) && fe(e, r.forward)
510
+ );
511
+ for (const r of this.undoStack) r.inverse = t.get(r.batchId) ?? r.inverse;
512
+ this.redoStack = [];
513
+ }
514
+ /**
515
+ * Autosave: a batch refused as stale. Every queued batch (the refused one
516
+ * first) is replayed on the server's layout and sent again; one that no
517
+ * longer fits is dropped and listed. Without the server's layout: reload.
518
+ */
519
+ conflict(e) {
520
+ if (!e) {
521
+ for (const s of this.text.values()) clearTimeout(s.timer);
522
+ this.text.clear(), this.queue = [], this.undoStack = [], this.redoStack = [], this.transport.reload(), this.outcome = "conflict", this.error = null, this.transport.notify(ne);
523
+ return;
334
524
  }
525
+ const t = this.refold(e, this.queue);
526
+ this.queue = t.kept, this.server = e, this.transport.saved(e, null), this.rebaseUndo(e, t.inverses, new Set(t.dropped.map((s) => s.id))), this.dropped = [...this.dropped, ...t.dropped.map((s) => s.note)], this.outcome = "idle", this.error = null, this.transport.notify(
527
+ t.dropped.length === 0 ? ae(t.kept.length) : E(t.kept.length, t.dropped.length)
528
+ );
335
529
  }
336
- conflict(t) {
337
- for (const e of this.text.values()) clearTimeout(e.timer);
338
- this.text.clear(), this.queue = [], this.undoStack = [], this.redoStack = [], t ? (this.server = t, this.transport.saved(t, null)) : this.transport.reload(), this.outcome = "conflict", this.error = null, this.transport.notify(D);
530
+ /**
531
+ * A Save refused as stale: fold every step it (and anything queued after it)
532
+ * carried onto the server's layout, one by one. The ones that fit stay
533
+ * unsaved on top of it, with fresh inverses, for another Save; the ones
534
+ * that no longer fit are dropped and listed for the admin to redo by hand.
535
+ */
536
+ rebase(e) {
537
+ const t = [
538
+ ...this.queue.flatMap((r) => r.kind === "save" ? r.parts ?? [] : [r]),
539
+ ...this.unsaved
540
+ ];
541
+ if (this.queue = [], this.unsaved = t, this.checkpoint = null, this.closeText(), !e) {
542
+ this.transport.reload(), this.outcome = "conflict", this.error = oe;
543
+ return;
544
+ }
545
+ const s = this.refold(e, t);
546
+ this.unsaved = s.kept, this.server = e, this.transport.saved(e, null), this.rebaseUndo(e, s.inverses, new Set(s.dropped.map((r) => r.id))), this.dropped = [...this.dropped, ...s.dropped.map((r) => r.note)], this.outcome = "idle", this.error = null, this.transport.notify(
547
+ s.dropped.length === 0 ? ie(s.kept.length) : E(s.kept.length, s.dropped.length)
548
+ );
339
549
  }
340
- /** Draft = server bundle + queued batches + typed text; then send the next batch. */
550
+ /** Draft = server bundle + queued batches + unsaved steps + typed text; then send the next batch. */
341
551
  refresh() {
342
552
  this.recompute(), this.pump(), this.emit();
343
553
  }
344
554
  recompute() {
345
555
  if (!this.server) return;
346
- let t = this.server;
347
- const e = [];
348
- for (const [r, n] of this.queue.entries())
349
- try {
350
- t = h(t, n.ops), e.push(n);
351
- } catch (o) {
352
- r === 0 && this.inFlight ? e.push(n) : (this.rollback(n), this.error ?? (this.error = y(o)), this.outcome = "error");
353
- }
354
- this.queue = e;
355
- for (const [r, n] of this.text)
556
+ let e = this.server;
557
+ const t = (r, a) => {
558
+ const o = [], u = [];
559
+ for (const [i, h] of r.entries())
560
+ try {
561
+ e = p(e, h.ops), o.push(h);
562
+ } catch (v) {
563
+ i === 0 && a ? o.push(h) : h.kind === "save" ? u.push(...h.parts ?? []) : (this.rollback(h), this.error ?? (this.error = y(v)), this.outcome = "error");
564
+ }
565
+ return { kept: o, restored: u };
566
+ }, s = t(this.queue, this.inFlight);
567
+ this.queue = s.kept, this.unsaved = t([...s.restored, ...this.unsaved], !1).kept;
568
+ for (const [r, a] of this.text)
356
569
  try {
357
- t = h(t, [n.op]);
570
+ e = p(e, [a.op]);
358
571
  } catch {
359
- clearTimeout(n.timer), this.text.delete(r);
572
+ clearTimeout(a.timer), this.text.delete(r);
360
573
  }
361
- this.draft = t;
574
+ this.draft = e;
362
575
  }
363
576
  pump() {
364
577
  if (this.inFlight || !this.server || this.queue.length === 0) return;
365
- const t = this.queue[0];
366
- this.inFlight = !0, this.transport.send(t.ops, this.server.rev, t.note).then((e) => {
367
- this.server = e, this.queue.shift(), this.transport.saved(e, t.ops), this.outcome !== "error" && (this.outcome = "saved");
368
- }).catch((e) => {
369
- const r = O(e);
370
- r.status === 409 ? this.conflict(r.bundle) : (this.queue.shift(), this.rollback(t), this.error = r.message, this.outcome = "error");
578
+ const e = this.queue[0];
579
+ this.inFlight = !0, this.transport.send(e.ops, this.server.rev, e.note, e.meta).then((t) => {
580
+ this.server = t, this.queue.shift(), this.transport.saved(t, e.ops), this.outcome !== "error" && (this.outcome = "saved");
581
+ }).catch((t) => {
582
+ const s = F(t);
583
+ s.status === 409 && e.kind === "save" ? this.rebase(s.bundle) : s.status === 409 ? this.conflict(s.bundle) : e.kind === "save" ? (this.queue.shift(), this.unsaved = [...e.parts ?? [], ...this.unsaved], this.error = s.message, this.outcome = "error") : (this.queue.shift(), this.rollback(e), this.error = s.message, this.outcome = "error");
371
584
  }).finally(() => {
372
585
  this.inFlight = !1, this.refresh();
373
586
  });
374
587
  }
588
+ sessionSteps() {
589
+ const e = new Set(this.unsaved.map((s) => s.id)), t = /* @__PURE__ */ new Set();
590
+ for (const s of this.queue) {
591
+ t.add(s.id);
592
+ for (const r of s.parts ?? []) t.add(r.id);
593
+ }
594
+ return {
595
+ done: this.undoStack.map((s) => ({
596
+ label: s.label,
597
+ state: e.has(s.batchId) ? "unsaved" : t.has(s.batchId) ? "saving" : "saved"
598
+ })),
599
+ undone: [...this.redoStack].reverse().map((s) => ({ label: s.label }))
600
+ };
601
+ }
375
602
  buildView() {
376
- var r;
377
- const t = this.undoStack[this.undoStack.length - 1], e = [...this.text.values()].pop();
603
+ var s, r;
604
+ const e = this.undoStack[this.undoStack.length - 1], t = [...this.text.values()].pop();
378
605
  return {
379
606
  bundle: this.draft,
607
+ autosave: this.autosave,
608
+ unsavedCount: this.unsaved.length,
609
+ unsavedSteps: this.unsaved.map((a) => ({ label: a.note, ops: a.ops })),
610
+ baseRev: ((s = this.server) == null ? void 0 : s.rev) ?? null,
611
+ dropped: this.dropped,
612
+ dirty: this.unsaved.length > 0,
380
613
  canUndo: this.undoStack.length > 0 || this.text.size > 0,
381
614
  canRedo: this.redoStack.length > 0,
382
- undoLabel: (e == null ? void 0 : e.label) ?? (t == null ? void 0 : t.label) ?? null,
615
+ undoLabel: (t == null ? void 0 : t.label) ?? (e == null ? void 0 : e.label) ?? null,
383
616
  redoLabel: ((r = this.redoStack[this.redoStack.length - 1]) == null ? void 0 : r.label) ?? null,
617
+ session: this.sessionSteps(),
384
618
  status: this.pending() ? "saving" : this.outcome,
385
619
  error: this.error
386
620
  };
387
621
  }
388
622
  emit() {
389
623
  this.view = this.buildView();
390
- for (const t of this.listeners) t();
624
+ for (const e of this.listeners) e();
391
625
  if (this.idleWaiters.length > 0 && !this.pending()) {
392
- const t = this.idleWaiters;
626
+ const e = this.idleWaiters;
393
627
  this.idleWaiters = [];
394
- for (const e of t) e();
628
+ for (const t of e) t();
395
629
  }
396
630
  }
397
631
  }
398
- const x = /* @__PURE__ */ new WeakMap();
399
- function J(s, t) {
400
- let e = x.get(t.qc);
401
- e || (e = /* @__PURE__ */ new Map(), x.set(t.qc, e));
402
- const r = e.get(s);
632
+ const I = /* @__PURE__ */ new WeakMap();
633
+ function ye(n, e, t) {
634
+ let s = I.get(e.qc);
635
+ s || (s = /* @__PURE__ */ new Map(), I.set(e.qc, s));
636
+ const r = s.get(n);
403
637
  if (r != null && r.engine.isBusy()) return r;
404
- const n = E.bundle(s), o = { current: t }, u = { engine: new X({
405
- send: async (c, l, d) => {
406
- const f = await o.current.client.request(
407
- P(`/collection-layouts/${s}/ops`, { ops: c, base_rev: l, note: d })
408
- ), k = w(f);
409
- if (!k) throw new Error("The server answered without the saved layout");
410
- return k;
411
- },
412
- saved: (c, l) => {
413
- var d, f;
414
- o.current.qc.setQueryData(n, c), (f = (d = o.current).onSaved) == null || f.call(d, c, l);
415
- },
416
- reload: () => {
417
- o.current.qc.invalidateQueries({ queryKey: n });
638
+ const a = C.bundle(n), o = { current: e }, i = { engine: new ke(
639
+ {
640
+ send: async (h, v, k, f) => {
641
+ const _ = await o.current.client.request(
642
+ X(`/collection-layouts/${n}/ops`, {
643
+ ops: h,
644
+ base_rev: v,
645
+ note: k,
646
+ ...f ? { reason: f.reason, ticket: f.ticket ?? null } : {}
647
+ })
648
+ ), m = q(_);
649
+ if (!m) throw new Error("The server answered without the saved layout");
650
+ return m;
651
+ },
652
+ saved: (h, v) => {
653
+ var k, f;
654
+ o.current.qc.setQueryData(a, h), (f = (k = o.current).onSaved) == null || f.call(k, h, v);
655
+ },
656
+ reload: () => {
657
+ o.current.qc.invalidateQueries({ queryKey: a });
658
+ },
659
+ notify: (h) => {
660
+ H.warning(h);
661
+ }
418
662
  },
419
- notify: (c) => {
420
- K.warning(c);
421
- }
422
- }), live: o };
423
- return e.set(s, u), u;
663
+ { autosave: t }
664
+ ), live: o };
665
+ return s.set(n, i), i;
424
666
  }
425
- function Z(s, t = !0) {
426
- const e = q();
427
- return A({
428
- queryKey: E.bundle(s ?? 0),
667
+ function ge(n, e = !0) {
668
+ const t = M();
669
+ return z({
670
+ queryKey: C.bundle(n ?? 0),
429
671
  queryFn: async () => {
430
- const r = await e.request(I(`/collection-layouts/${s}/bundle`)), n = w(r);
431
- if (!n) throw new Error("The layout could not be read");
432
- return n;
672
+ const s = await t.request(J(`/collection-layouts/${n}/bundle`)), r = q(s);
673
+ if (!r) throw new Error("The layout could not be read");
674
+ return r;
433
675
  },
434
- enabled: t && s != null,
676
+ enabled: e && n != null,
435
677
  staleTime: 15e3,
436
678
  refetchOnWindowFocus: !1
437
679
  });
438
680
  }
439
- function ae(s, t = {}) {
440
- const e = q(), r = $(), n = Z(s), o = U(null);
441
- (!o.current || o.current.id !== s || o.current.qc !== r) && (o.current = { id: s, qc: r, ...J(s, { client: e, qc: r, ...t }) }), o.current.live.current = { client: e, qc: r, onSaved: t.onSaved };
442
- const i = o.current.engine, u = W(i.subscribe, i.getView, i.getView);
443
- S(() => {
444
- n.data && i.receive(n.data);
445
- }, [i, n.data]), S(() => () => i.flushText(), [i]);
446
- const c = p(
447
- (g, C) => i.apply(g, C),
681
+ function Oe(n, e = {}) {
682
+ const t = M(), s = P(), r = ge(n), [a, o] = Q(e.autosave ?? !1), u = Y(null);
683
+ (!u.current || u.current.id !== n || u.current.qc !== s) && (u.current = {
684
+ id: n,
685
+ qc: s,
686
+ ...ye(n, { client: t, qc: s, onSaved: e.onSaved }, a)
687
+ }), u.current.live.current = { client: t, qc: s, onSaved: e.onSaved };
688
+ const i = u.current.engine, h = G(i.subscribe, i.getView, i.getView);
689
+ b(() => {
690
+ r.data && i.receive(r.data);
691
+ }, [i, r.data]), b(() => {
692
+ i.setAutosave(a);
693
+ }, [i, a]), b(() => () => i.flushText(), [i]);
694
+ const v = l(
695
+ (d, W) => i.apply(d, W),
696
+ [i]
697
+ ), k = l(() => i.undo(), [i]), f = l(() => i.redo(), [i]), _ = l(() => i.flushText(), [i]), m = l((d) => i.whenIdle(d), [i]), K = l(
698
+ (d) => {
699
+ o(d), i.setAutosave(d);
700
+ },
448
701
  [i]
449
- ), l = p(() => i.undo(), [i]), d = p(() => i.redo(), [i]), f = p(() => i.flushText(), [i]), k = p((g) => i.whenIdle(g), [i]), b = !u.bundle && n.error != null;
702
+ ), U = l(() => i.unsavedOps(), [i]), L = l((d) => i.save(d), [i]), j = l(() => i.discard(), [i]), D = l(() => i.clearDropped(), [i]), N = l((d) => i.restore(d), [i]), R = l((d) => i.jump(d), [i]), O = !h.bundle && r.error != null;
450
703
  return {
451
- ...u,
452
- status: b ? "error" : u.status,
453
- error: u.error ?? (b ? O(n.error, "The layout could not be read").message : null),
454
- apply: c,
455
- undo: l,
456
- redo: d,
457
- flush: f,
458
- whenIdle: k,
459
- isLoading: n.isLoading
704
+ ...h,
705
+ status: O ? "error" : h.status,
706
+ error: h.error ?? (O ? F(r.error, "The layout could not be read").message : null),
707
+ apply: v,
708
+ undo: k,
709
+ redo: f,
710
+ flush: _,
711
+ whenIdle: m,
712
+ setAutosave: K,
713
+ unsavedOps: U,
714
+ save: L,
715
+ discard: j,
716
+ clearDropped: D,
717
+ restore: N,
718
+ jump: R,
719
+ isLoading: r.isLoading
460
720
  };
461
721
  }
462
722
  export {
463
- D as CONFLICT_MESSAGE,
464
- X as LayoutDraftEngine,
465
- B as TEXT_DEBOUNCE_MS,
466
- z as humanizeOpError,
467
- _ as inverseOps,
468
- E as layoutDraftKeys,
469
- w as parseBundle,
470
- O as parseSaveFailure,
471
- ue as touchesOverview,
472
- Z as useLayoutBundle,
473
- ae as useLayoutDraft
723
+ ne as CONFLICT_MESSAGE,
724
+ ke as LayoutDraftEngine,
725
+ oe as REBASE_FAILED_MESSAGE,
726
+ re as TEXT_DEBOUNCE_MS,
727
+ he as humanizeOpError,
728
+ g as inverseOps,
729
+ C as layoutDraftKeys,
730
+ q as parseBundle,
731
+ F as parseSaveFailure,
732
+ E as partlyRebasedMessage,
733
+ ie as rebasedMessage,
734
+ ae as replayedMessage,
735
+ $ as saveNote,
736
+ fe as stillMine,
737
+ qe as touchesOverview,
738
+ ge as useLayoutBundle,
739
+ Oe as useLayoutDraft
474
740
  };