@actuate-media/cms-admin 0.11.0 → 0.12.1

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 (183) hide show
  1. package/LICENSE +21 -21
  2. package/dist/__tests__/components/comment-mark.test.d.ts +2 -0
  3. package/dist/__tests__/components/comment-mark.test.d.ts.map +1 -0
  4. package/dist/__tests__/components/comment-mark.test.js +89 -0
  5. package/dist/__tests__/components/comment-mark.test.js.map +1 -0
  6. package/dist/__tests__/components/comment-side-panel.test.d.ts +2 -0
  7. package/dist/__tests__/components/comment-side-panel.test.d.ts.map +1 -0
  8. package/dist/__tests__/components/comment-side-panel.test.js +291 -0
  9. package/dist/__tests__/components/comment-side-panel.test.js.map +1 -0
  10. package/dist/__tests__/components/notification-bell.test.d.ts +2 -0
  11. package/dist/__tests__/components/notification-bell.test.d.ts.map +1 -0
  12. package/dist/__tests__/components/notification-bell.test.js +200 -0
  13. package/dist/__tests__/components/notification-bell.test.js.map +1 -0
  14. package/dist/__tests__/components/offline-status.test.d.ts +2 -0
  15. package/dist/__tests__/components/offline-status.test.d.ts.map +1 -0
  16. package/dist/__tests__/components/offline-status.test.js +56 -0
  17. package/dist/__tests__/components/offline-status.test.js.map +1 -0
  18. package/dist/__tests__/components/presence-chips-helpers.test.d.ts +2 -0
  19. package/dist/__tests__/components/presence-chips-helpers.test.d.ts.map +1 -0
  20. package/dist/__tests__/components/presence-chips-helpers.test.js +35 -0
  21. package/dist/__tests__/components/presence-chips-helpers.test.js.map +1 -0
  22. package/dist/__tests__/fields/component-block-helpers.test.d.ts +7 -0
  23. package/dist/__tests__/fields/component-block-helpers.test.d.ts.map +1 -0
  24. package/dist/__tests__/fields/component-block-helpers.test.js +592 -0
  25. package/dist/__tests__/fields/component-block-helpers.test.js.map +1 -0
  26. package/dist/__tests__/lib/active-editor.test.d.ts +2 -0
  27. package/dist/__tests__/lib/active-editor.test.d.ts.map +1 -0
  28. package/dist/__tests__/lib/active-editor.test.js +57 -0
  29. package/dist/__tests__/lib/active-editor.test.js.map +1 -0
  30. package/dist/__tests__/lib/coauthor-client.test.d.ts +2 -0
  31. package/dist/__tests__/lib/coauthor-client.test.d.ts.map +1 -0
  32. package/dist/__tests__/lib/coauthor-client.test.js +173 -0
  33. package/dist/__tests__/lib/coauthor-client.test.js.map +1 -0
  34. package/dist/__tests__/lib/collaboration-provider.test.d.ts +2 -0
  35. package/dist/__tests__/lib/collaboration-provider.test.d.ts.map +1 -0
  36. package/dist/__tests__/lib/collaboration-provider.test.js +335 -0
  37. package/dist/__tests__/lib/collaboration-provider.test.js.map +1 -0
  38. package/dist/__tests__/lib/comment-anchor.test.d.ts +2 -0
  39. package/dist/__tests__/lib/comment-anchor.test.d.ts.map +1 -0
  40. package/dist/__tests__/lib/comment-anchor.test.js +173 -0
  41. package/dist/__tests__/lib/comment-anchor.test.js.map +1 -0
  42. package/dist/__tests__/lib/comments-client.test.d.ts +2 -0
  43. package/dist/__tests__/lib/comments-client.test.d.ts.map +1 -0
  44. package/dist/__tests__/lib/comments-client.test.js +202 -0
  45. package/dist/__tests__/lib/comments-client.test.js.map +1 -0
  46. package/dist/__tests__/lib/notifications-client.test.d.ts +2 -0
  47. package/dist/__tests__/lib/notifications-client.test.d.ts.map +1 -0
  48. package/dist/__tests__/lib/notifications-client.test.js +155 -0
  49. package/dist/__tests__/lib/notifications-client.test.js.map +1 -0
  50. package/dist/__tests__/lib/slash-command.test.d.ts +2 -0
  51. package/dist/__tests__/lib/slash-command.test.d.ts.map +1 -0
  52. package/dist/__tests__/lib/slash-command.test.js +123 -0
  53. package/dist/__tests__/lib/slash-command.test.js.map +1 -0
  54. package/dist/actuate-admin.css +1 -1
  55. package/dist/components/AICoauthorBubbleMenu.d.ts +18 -0
  56. package/dist/components/AICoauthorBubbleMenu.d.ts.map +1 -0
  57. package/dist/components/AICoauthorBubbleMenu.js +113 -0
  58. package/dist/components/AICoauthorBubbleMenu.js.map +1 -0
  59. package/dist/components/CoauthorResultPopover.d.ts +28 -0
  60. package/dist/components/CoauthorResultPopover.d.ts.map +1 -0
  61. package/dist/components/CoauthorResultPopover.js +42 -0
  62. package/dist/components/CoauthorResultPopover.js.map +1 -0
  63. package/dist/components/CommandPalette.d.ts +1 -1
  64. package/dist/components/CommandPalette.d.ts.map +1 -1
  65. package/dist/components/CommandPalette.js +138 -40
  66. package/dist/components/CommandPalette.js.map +1 -1
  67. package/dist/components/CommentMark.d.ts +54 -0
  68. package/dist/components/CommentMark.d.ts.map +1 -0
  69. package/dist/components/CommentMark.js +104 -0
  70. package/dist/components/CommentMark.js.map +1 -0
  71. package/dist/components/CommentSidePanel.d.ts +86 -0
  72. package/dist/components/CommentSidePanel.d.ts.map +1 -0
  73. package/dist/components/CommentSidePanel.js +165 -0
  74. package/dist/components/CommentSidePanel.js.map +1 -0
  75. package/dist/components/LivePreview.d.ts.map +1 -1
  76. package/dist/components/LivePreview.js +80 -16
  77. package/dist/components/LivePreview.js.map +1 -1
  78. package/dist/components/NotificationBell.d.ts +45 -0
  79. package/dist/components/NotificationBell.d.ts.map +1 -0
  80. package/dist/components/NotificationBell.js +90 -0
  81. package/dist/components/NotificationBell.js.map +1 -0
  82. package/dist/components/OfflineStatus.d.ts +39 -0
  83. package/dist/components/OfflineStatus.d.ts.map +1 -0
  84. package/dist/components/OfflineStatus.js +47 -0
  85. package/dist/components/OfflineStatus.js.map +1 -0
  86. package/dist/components/PresenceChips.d.ts +31 -0
  87. package/dist/components/PresenceChips.d.ts.map +1 -0
  88. package/dist/components/PresenceChips.js +58 -0
  89. package/dist/components/PresenceChips.js.map +1 -0
  90. package/dist/components/TipTapEditor.d.ts +61 -1
  91. package/dist/components/TipTapEditor.d.ts.map +1 -1
  92. package/dist/components/TipTapEditor.js +211 -8
  93. package/dist/components/TipTapEditor.js.map +1 -1
  94. package/dist/components/ui/Badge.d.ts +1 -1
  95. package/dist/fields/ComponentBlockField.d.ts +25 -0
  96. package/dist/fields/ComponentBlockField.d.ts.map +1 -0
  97. package/dist/fields/ComponentBlockField.js +74 -0
  98. package/dist/fields/ComponentBlockField.js.map +1 -0
  99. package/dist/fields/FieldRenderer.d.ts +3 -0
  100. package/dist/fields/FieldRenderer.d.ts.map +1 -1
  101. package/dist/fields/FieldRenderer.js +3 -1
  102. package/dist/fields/FieldRenderer.js.map +1 -1
  103. package/dist/fields/PropInput.d.ts +14 -0
  104. package/dist/fields/PropInput.d.ts.map +1 -0
  105. package/dist/fields/PropInput.js +163 -0
  106. package/dist/fields/PropInput.js.map +1 -0
  107. package/dist/fields/component-block-helpers.d.ts +96 -0
  108. package/dist/fields/component-block-helpers.d.ts.map +1 -0
  109. package/dist/fields/component-block-helpers.js +323 -0
  110. package/dist/fields/component-block-helpers.js.map +1 -0
  111. package/dist/fields/index.d.ts +4 -0
  112. package/dist/fields/index.d.ts.map +1 -1
  113. package/dist/fields/index.js +2 -0
  114. package/dist/fields/index.js.map +1 -1
  115. package/dist/index.d.ts +10 -0
  116. package/dist/index.d.ts.map +1 -1
  117. package/dist/index.js +5 -0
  118. package/dist/index.js.map +1 -1
  119. package/dist/lib/active-editor.d.ts +6 -0
  120. package/dist/lib/active-editor.d.ts.map +1 -0
  121. package/dist/lib/active-editor.js +35 -0
  122. package/dist/lib/active-editor.js.map +1 -0
  123. package/dist/lib/coauthor-client.d.ts +72 -0
  124. package/dist/lib/coauthor-client.d.ts.map +1 -0
  125. package/dist/lib/coauthor-client.js +98 -0
  126. package/dist/lib/coauthor-client.js.map +1 -0
  127. package/dist/lib/collaboration-provider.d.ts +142 -0
  128. package/dist/lib/collaboration-provider.d.ts.map +1 -0
  129. package/dist/lib/collaboration-provider.js +168 -0
  130. package/dist/lib/collaboration-provider.js.map +1 -0
  131. package/dist/lib/comment-anchor.d.ts +86 -0
  132. package/dist/lib/comment-anchor.d.ts.map +1 -0
  133. package/dist/lib/comment-anchor.js +139 -0
  134. package/dist/lib/comment-anchor.js.map +1 -0
  135. package/dist/lib/comments-client.d.ts +106 -0
  136. package/dist/lib/comments-client.d.ts.map +1 -0
  137. package/dist/lib/comments-client.js +138 -0
  138. package/dist/lib/comments-client.js.map +1 -0
  139. package/dist/lib/notifications-client.d.ts +68 -0
  140. package/dist/lib/notifications-client.d.ts.map +1 -0
  141. package/dist/lib/notifications-client.js +85 -0
  142. package/dist/lib/notifications-client.js.map +1 -0
  143. package/dist/lib/slash-command.d.ts +39 -0
  144. package/dist/lib/slash-command.d.ts.map +1 -0
  145. package/dist/lib/slash-command.js +50 -0
  146. package/dist/lib/slash-command.js.map +1 -0
  147. package/package.json +20 -4
  148. package/src/__tests__/components/comment-mark.test.ts +99 -0
  149. package/src/__tests__/components/comment-side-panel.test.tsx +343 -0
  150. package/src/__tests__/components/notification-bell.test.tsx +213 -0
  151. package/src/__tests__/components/offline-status.test.tsx +66 -0
  152. package/src/__tests__/components/presence-chips-helpers.test.ts +44 -0
  153. package/src/__tests__/fields/component-block-helpers.test.ts +674 -0
  154. package/src/__tests__/lib/active-editor.test.ts +70 -0
  155. package/src/__tests__/lib/coauthor-client.test.ts +213 -0
  156. package/src/__tests__/lib/collaboration-provider.test.ts +425 -0
  157. package/src/__tests__/lib/comment-anchor.test.ts +188 -0
  158. package/src/__tests__/lib/comments-client.test.ts +239 -0
  159. package/src/__tests__/lib/notifications-client.test.ts +195 -0
  160. package/src/__tests__/lib/slash-command.test.ts +146 -0
  161. package/src/components/AICoauthorBubbleMenu.tsx +187 -0
  162. package/src/components/CoauthorResultPopover.tsx +193 -0
  163. package/src/components/CommandPalette.tsx +397 -209
  164. package/src/components/CommentMark.ts +138 -0
  165. package/src/components/CommentSidePanel.tsx +521 -0
  166. package/src/components/LivePreview.tsx +117 -11
  167. package/src/components/NotificationBell.tsx +242 -0
  168. package/src/components/OfflineStatus.tsx +90 -0
  169. package/src/components/PresenceChips.tsx +150 -0
  170. package/src/components/TipTapEditor.tsx +310 -12
  171. package/src/fields/ComponentBlockField.tsx +179 -0
  172. package/src/fields/FieldRenderer.tsx +8 -0
  173. package/src/fields/PropInput.tsx +552 -0
  174. package/src/fields/component-block-helpers.ts +341 -0
  175. package/src/fields/index.ts +4 -0
  176. package/src/index.ts +27 -0
  177. package/src/lib/active-editor.ts +39 -0
  178. package/src/lib/coauthor-client.ts +160 -0
  179. package/src/lib/collaboration-provider.ts +306 -0
  180. package/src/lib/comment-anchor.ts +179 -0
  181. package/src/lib/comments-client.ts +208 -0
  182. package/src/lib/notifications-client.ts +146 -0
  183. package/src/lib/slash-command.ts +67 -0
@@ -0,0 +1,341 @@
1
+ /**
2
+ * Pure helpers behind `<ComponentBlockField>`. Extracted so vitest can
3
+ * exercise them in node without spinning up jsdom — the React component
4
+ * just composes these into UI.
5
+ *
6
+ * Kept structurally typed (no peer-dep import of types here would be
7
+ * cleaner, but the `PropType` discriminated union is the contract we
8
+ * promise consumers, so we lean on the package's types).
9
+ */
10
+
11
+ import {
12
+ detectDiscriminator,
13
+ findVariant,
14
+ } from '@actuate-media/component-blocks/discriminated-union'
15
+ import type {
16
+ DiscriminatedUnion,
17
+ DiscriminatedVariant,
18
+ } from '@actuate-media/component-blocks/discriminated-union'
19
+ import type { ComponentSpec, Manifest, PropSpec, PropType } from '@actuate-media/component-blocks'
20
+
21
+ import type { ComponentBlockValue } from './ComponentBlockField.js'
22
+
23
+ /**
24
+ * Compute the list of components an editor can pick from given the
25
+ * manifest + the optional allow-list. Returns the full list when no
26
+ * filter is provided; returns `[]` when every entry is filtered out
27
+ * (the caller renders an error in that case).
28
+ */
29
+ export function getAllowedComponents(
30
+ manifest: Manifest,
31
+ allow: string[] | undefined,
32
+ ): ComponentSpec[] {
33
+ if (!allow || allow.length === 0) return manifest.components
34
+ return manifest.components.filter((c) => allow.includes(c.name))
35
+ }
36
+
37
+ /**
38
+ * JSON-encode any value so it round-trips cleanly through the
39
+ * JsonFallback textarea. Previously the helper short-circuited for
40
+ * strings (returning the raw string), which made the textarea
41
+ * stateful: typing `"hello"` parsed to the JS string `hello`,
42
+ * re-rendered as bare `hello`, then `JSON.parse('hello')` threw on
43
+ * the next edit and the field "locked" as a raw string. Always
44
+ * stringifying keeps the textarea visually consistent with the
45
+ * underlying JSON value.
46
+ *
47
+ * Falls back to `String(v)` for values that can't be JSON-encoded
48
+ * (e.g. circular refs) so the field remains editable.
49
+ */
50
+ export function safeJsonStringify(v: unknown): string {
51
+ try {
52
+ return JSON.stringify(v, null, 2)
53
+ } catch {
54
+ return String(v)
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Map an `<select>` change-event value to an enum-prop value.
60
+ *
61
+ * The placeholder `<option value="">Select…</option>` clears the field
62
+ * to `undefined`; any other value is the stringified array index into
63
+ * `options`. Extracted so we can test the clear semantics without
64
+ * spinning up jsdom: previously the inline handler used `Number("")`,
65
+ * which evaluates to `0` and silently picked the first option instead
66
+ * of clearing.
67
+ */
68
+ export function parseEnumSelection<T>(raw: string, options: readonly T[]): T | undefined {
69
+ if (raw === '') return undefined
70
+ const idx = Number(raw)
71
+ if (!Number.isInteger(idx) || idx < 0 || idx >= options.length) return undefined
72
+ return options[idx]
73
+ }
74
+
75
+ /**
76
+ * Produce a sensible empty value for a freshly-created array item or
77
+ * required prop. The admin form generator never wants `undefined` in
78
+ * an array; that breaks downstream PropInput defaults and confuses
79
+ * validators.
80
+ */
81
+ export function defaultForType(type: PropType): unknown {
82
+ switch (type.kind) {
83
+ case 'string':
84
+ return ''
85
+ case 'number':
86
+ return 0
87
+ case 'boolean':
88
+ return false
89
+ case 'enum':
90
+ return type.values[0] ?? ''
91
+ case 'literal':
92
+ return type.value
93
+ case 'array':
94
+ return []
95
+ case 'object':
96
+ return Object.fromEntries(
97
+ type.fields.filter((f) => f.required).map((f) => [f.name, defaultForType(f.type)]),
98
+ )
99
+ case 'union': {
100
+ // Discriminated unions DO have a sensible structural default:
101
+ // pick the first variant and seed its required fields (plus the
102
+ // discriminator). This makes a Hero with a `cta: Link | Modal`
103
+ // prop render an editable form on first paint instead of forcing
104
+ // the editor through the JSON textarea fallback.
105
+ const detected = detectDiscriminator(type)
106
+ if (detected) {
107
+ const variant = detected.variants[0]!
108
+ const out: Record<string, unknown> = { [detected.field]: variant.value }
109
+ for (const field of variant.remainingFields) {
110
+ if (field.required) out[field.name] = defaultForType(field.type)
111
+ }
112
+ return out
113
+ }
114
+ return null
115
+ }
116
+ default:
117
+ // `reference`, `unknown`: there's no single sensible structural
118
+ // default. We return `null` instead of `undefined` so the value
119
+ // survives a JSON round-trip — `JSON.stringify` turns
120
+ // `undefined` array items into `null` anyway, which would
121
+ // contradict the documented "never undefined in an array"
122
+ // invariant and surface as the literal string `null` in the
123
+ // JsonFallback textarea.
124
+ return null
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Seed an empty props object for a freshly-picked component using each
130
+ * prop's explicit `defaultValue` when set, else the structural default
131
+ * from {@link defaultForType} (only for required props — optional
132
+ * unset props stay absent so they render as empty inputs).
133
+ */
134
+ export function seedPropsForComponent(spec: ComponentSpec): Record<string, unknown> {
135
+ const out: Record<string, unknown> = {}
136
+ for (const prop of spec.props) {
137
+ if (prop.defaultValue !== undefined && prop.defaultValue !== null) {
138
+ out[prop.name] = prop.defaultValue
139
+ continue
140
+ }
141
+ if (prop.required) {
142
+ const seed = defaultForType(prop.type)
143
+ // For non-discriminated `union` / `reference` / `unknown` kinds
144
+ // defaultForType returns `null` (so arrays survive a JSON
145
+ // round-trip), but seeding a top-level required prop with
146
+ // `null` would immediately trip the validator's "Missing
147
+ // required prop" branch on first render — the user would see
148
+ // a red error before they touched anything. Leave those
149
+ // required props absent so the absence and the validation
150
+ // message describe the same state honestly; the user provides
151
+ // a value via the JsonFallback. Discriminated unions return
152
+ // a structural default and seed cleanly.
153
+ if (seed !== null) {
154
+ out[prop.name] = seed
155
+ }
156
+ }
157
+ }
158
+ return out
159
+ }
160
+
161
+ /**
162
+ * Compute the next value for a discriminated-union field when the
163
+ * editor switches variants. Pure helper so the React component stays
164
+ * thin and the seeding rules can be unit-tested without rendering.
165
+ *
166
+ * Seeding rules:
167
+ * 1. The discriminator field is always written to the variant's
168
+ * tag value.
169
+ * 2. Optional fields shared by both variants survive the switch
170
+ * (preserves "edited the same label" intent across, e.g.,
171
+ * Link → Modal where both carry `label`).
172
+ * 3. Required fields exclusive to the new variant are seeded by
173
+ * priority: explicit `field.defaultValue` first (matches
174
+ * `seedPropsForComponent`), else the structural default from
175
+ * `defaultForType` — but only when that default is non-null.
176
+ * A `null` from `defaultForType` (reference / unknown / non-
177
+ * discriminated union types) is treated as "no sensible default"
178
+ * and the field is left absent so the validator's
179
+ * "missing required prop" message and the data agree.
180
+ * 4. Fields exclusive to the previous variant are dropped so the
181
+ * value stays structurally valid against the chosen variant.
182
+ */
183
+ export function switchUnionVariant(
184
+ current: unknown,
185
+ union: DiscriminatedUnion,
186
+ variant: DiscriminatedVariant,
187
+ ): Record<string, unknown> {
188
+ const obj = isPlainObject(current) ? current : {}
189
+ const next: Record<string, unknown> = { [union.field]: variant.value }
190
+ const carryable = new Set(variant.remainingFields.map((f) => f.name))
191
+ for (const [k, v] of Object.entries(obj)) {
192
+ if (k === union.field) continue
193
+ if (carryable.has(k)) next[k] = v
194
+ }
195
+ for (const field of variant.remainingFields) {
196
+ if (!field.required || next[field.name] !== undefined) continue
197
+ if (field.defaultValue !== undefined && field.defaultValue !== null) {
198
+ next[field.name] = field.defaultValue
199
+ continue
200
+ }
201
+ const seed = defaultForType(field.type)
202
+ if (seed !== null) {
203
+ next[field.name] = seed
204
+ }
205
+ }
206
+ return next
207
+ }
208
+
209
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
210
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
211
+ }
212
+
213
+ /**
214
+ * Build a minimal structural validator that mirrors the rules the
215
+ * cms-core `validateComponentBlockValue` runs server-side, so the form
216
+ * can show feedback without a round-trip. Kept in sync with cms-core
217
+ * by inspection; the canonical implementation lives there.
218
+ */
219
+ export function buildClientValidator(
220
+ manifest: Manifest,
221
+ allow: string[] | undefined,
222
+ ): (value: ComponentBlockValue) => string | true {
223
+ // An empty `allow` array is treated the same way `getAllowedComponents`
224
+ // treats it: as "no filter". Without this, the picker shows every
225
+ // component while the validator rejects every selection, leading to
226
+ // a UI/validator deadlock.
227
+ const allowed = allow && allow.length > 0 ? new Set(allow) : null
228
+ return (value) => {
229
+ const spec = manifest.components.find((c) => c.name === value.component)
230
+ if (!spec) {
231
+ const known = manifest.components.map((c) => c.name).join(', ') || '<none>'
232
+ return `Unknown component '${value.component}'. Manifest knows: ${known}.`
233
+ }
234
+ if (allowed && !allowed.has(value.component)) {
235
+ return `Component '${value.component}' is not in the allow-list for this field.`
236
+ }
237
+ for (const propSpec of spec.props) {
238
+ const v = value.props[propSpec.name]
239
+ if (v === undefined || v === null) {
240
+ if (propSpec.required) {
241
+ return `Missing required prop '${propSpec.name}' for component '${value.component}'.`
242
+ }
243
+ continue
244
+ }
245
+ const err = clientShapeError(v, propSpec)
246
+ if (err) return err
247
+ }
248
+ return true
249
+ }
250
+ }
251
+
252
+ function clientShapeError(value: unknown, prop: PropSpec): string | null {
253
+ const t = prop.type
254
+ switch (t.kind) {
255
+ case 'string':
256
+ return typeof value === 'string' ? null : `Prop '${prop.name}' must be a string.`
257
+ case 'number':
258
+ return typeof value === 'number' && !Number.isNaN(value)
259
+ ? null
260
+ : `Prop '${prop.name}' must be a number.`
261
+ case 'boolean':
262
+ return typeof value === 'boolean' ? null : `Prop '${prop.name}' must be a boolean.`
263
+ case 'enum':
264
+ return t.values.includes(value as never)
265
+ ? null
266
+ : `Prop '${prop.name}' must be one of: ${t.values.join(', ')}.`
267
+ case 'array':
268
+ return Array.isArray(value) ? null : `Prop '${prop.name}' must be an array.`
269
+ case 'object': {
270
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
271
+ return `Prop '${prop.name}' must be an object.`
272
+ }
273
+ // Recurse: check that each required nested field is present and
274
+ // structurally compatible. Without this, passing `{}` for an
275
+ // object prop with required `label` and `href` children sailed
276
+ // through the client validator while the server-side
277
+ // validateComponentBlockValue would reject it on save — a
278
+ // silent gap in the "live structural validation" promise.
279
+ const obj = value as Record<string, unknown>
280
+ for (const field of t.fields) {
281
+ const child = obj[field.name]
282
+ if (child === undefined || child === null) {
283
+ if (field.required) {
284
+ return `Prop '${prop.name}.${field.name}' is required but missing.`
285
+ }
286
+ continue
287
+ }
288
+ const childError = clientShapeError(child, {
289
+ ...field,
290
+ // Build a dotted path so parsePerPropErrors can attribute
291
+ // the error to the nested input, not just the outer prop.
292
+ name: `${prop.name}.${field.name}`,
293
+ })
294
+ if (childError) return childError
295
+ }
296
+ return null
297
+ }
298
+ case 'union': {
299
+ // Discriminated unions get the same recursive treatment as plain
300
+ // objects: route the value into the matching variant's schema
301
+ // and validate against that. Non-discriminated unions remain
302
+ // unvalidated on the client (server still catches them) — the
303
+ // shape is too open to assert structurally.
304
+ const detected = detectDiscriminator(t)
305
+ if (!detected) return null
306
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
307
+ return `Prop '${prop.name}' must be an object with a '${detected.field}' discriminator.`
308
+ }
309
+ const variant = findVariant(value, detected)
310
+ if (!variant) {
311
+ const allowed = detected.variants.map((v) => JSON.stringify(v.value)).join(', ')
312
+ return `Prop '${prop.name}.${detected.field}' must be one of: ${allowed}.`
313
+ }
314
+ // Recurse into the variant's fields, scoped to the prop path.
315
+ // We treat the variant as an inline object so the existing
316
+ // object-branch logic applies verbatim.
317
+ return clientShapeError(value, {
318
+ name: prop.name,
319
+ required: prop.required,
320
+ type: variant.type,
321
+ })
322
+ }
323
+ default:
324
+ return null
325
+ }
326
+ }
327
+
328
+ /**
329
+ * The validator returns a single error string. The form wants to show
330
+ * the message under the specific input that triggered it — so we parse
331
+ * the message back into a sparse `{ propPath → message }` map. Pure
332
+ * function so it's trivial to unit-test.
333
+ */
334
+ export function parsePerPropErrors(message: string | null): Record<string, string> {
335
+ if (!message) return {}
336
+ const propMatch = /Prop '([^']+)'/.exec(message)
337
+ if (propMatch) return { [propMatch[1]!]: message }
338
+ const missing = /Missing required prop '([^']+)'/.exec(message)
339
+ if (missing) return { [missing[1]!]: message }
340
+ return {}
341
+ }
@@ -15,3 +15,7 @@ export type { BlockTypeDefinition } from './block-types.js'
15
15
  export { GroupField } from './GroupField.js'
16
16
  export { NavBuilderField } from './NavBuilderField.js'
17
17
  export { NumberField } from './NumberField.js'
18
+ export { ComponentBlockField } from './ComponentBlockField.js'
19
+ export type { ComponentBlockFieldProps, ComponentBlockValue } from './ComponentBlockField.js'
20
+ export { PropInput, defaultForType } from './PropInput.js'
21
+ export type { PropInputProps } from './PropInput.js'
package/src/index.ts CHANGED
@@ -97,6 +97,26 @@ export type { MediaPickerModalProps } from './components/MediaPickerModal.js'
97
97
  export { ThemeProvider, useTheme } from './components/ThemeProvider.js'
98
98
  export { FocalPointPicker } from './components/FocalPointPicker.js'
99
99
  export { PresenceIndicator } from './components/PresenceIndicator.js'
100
+ export { NotificationBell } from './components/NotificationBell.js'
101
+ export type { NotificationBellProps, NotificationsApi } from './components/NotificationBell.js'
102
+ export {
103
+ listNotifications,
104
+ unreadCount as listNotificationsUnread,
105
+ markNotificationRead,
106
+ markAllNotificationsRead,
107
+ notificationSummary,
108
+ } from './lib/notifications-client.js'
109
+ export type {
110
+ Notification,
111
+ NotificationKind,
112
+ NotificationPayload,
113
+ NotificationOutcome,
114
+ ListNotificationsOptions,
115
+ } from './lib/notifications-client.js'
116
+ export { OfflineStatus, describeOfflineStatus } from './components/OfflineStatus.js'
117
+ export type { OfflineStatusProps } from './components/OfflineStatus.js'
118
+ export { CommentSidePanel } from './components/CommentSidePanel.js'
119
+ export type { CommentSidePanelProps } from './components/CommentSidePanel.js'
100
120
  export { LocaleProvider, useLocale } from './components/LocaleProvider.js'
101
121
  export type { LocaleProviderProps } from './components/LocaleProvider.js'
102
122
  export { LocaleSwitcher } from './components/LocaleSwitcher.js'
@@ -105,3 +125,10 @@ export { useKeyboardShortcuts } from './hooks/useKeyboardShortcuts.js'
105
125
  export { cmsApi, setApiBase, ensureCsrfToken } from './lib/api.js'
106
126
  export { useApiData } from './lib/useApiData.js'
107
127
  export type { UseApiDataResult } from './lib/useApiData.js'
128
+
129
+ export { ComponentBlockField, PropInput, defaultForType } from './fields/index.js'
130
+ export type {
131
+ ComponentBlockFieldProps,
132
+ ComponentBlockValue,
133
+ PropInputProps,
134
+ } from './fields/index.js'
@@ -0,0 +1,39 @@
1
+ import type { Editor } from '@tiptap/react'
2
+
3
+ /**
4
+ * Singleton registry for the most-recently focused TipTap editor in the
5
+ * admin shell. Lets out-of-tree surfaces (Cmd+K palette, slash commands,
6
+ * future global shortcuts) target the editor a user was last working in
7
+ * without prop-drilling refs through the layout.
8
+ *
9
+ * The `TipTapEditor` component calls `setActiveEditor(editor)` from its
10
+ * `onFocus` callback and clears it on unmount. Subscribers receive a no-op
11
+ * payload — they should re-read via `getActiveEditor()` after the
12
+ * notification.
13
+ */
14
+
15
+ let activeEditor: Editor | null = null
16
+ const listeners = new Set<() => void>()
17
+
18
+ export function setActiveEditor(editor: Editor | null): void {
19
+ if (activeEditor === editor) return
20
+ activeEditor = editor
21
+ for (const listener of listeners) listener()
22
+ }
23
+
24
+ export function getActiveEditor(): Editor | null {
25
+ return activeEditor
26
+ }
27
+
28
+ export function clearActiveEditorIf(editor: Editor): void {
29
+ if (activeEditor === editor) {
30
+ setActiveEditor(null)
31
+ }
32
+ }
33
+
34
+ export function subscribeActiveEditor(listener: () => void): () => void {
35
+ listeners.add(listener)
36
+ return () => {
37
+ listeners.delete(listener)
38
+ }
39
+ }
@@ -0,0 +1,160 @@
1
+ import { cmsApi } from './api.js'
2
+
3
+ /**
4
+ * Inline AI co-author client — thin, typed wrapper around the
5
+ * `POST /ai/coauthor` endpoint exposed by `cms-core`.
6
+ *
7
+ * The endpoint implements four text-transformation micro-actions backed by
8
+ * the configured AI provider. This client lives in `cms-admin` so the
9
+ * editor (bubble menu) and the command palette (slash commands) can share
10
+ * exactly one request path, error model, and option shape.
11
+ *
12
+ * Mirrors the request body documented in `packages/cms-core/src/api/handlers.ts`:
13
+ *
14
+ * POST /ai/coauthor
15
+ * { action, text, style?, tone?, instructions?, targetLength? }
16
+ */
17
+
18
+ export type CoauthorAction = 'rewrite' | 'expand' | 'compress' | 'proofread'
19
+
20
+ /**
21
+ * Frozen tuple of every action the server accepts. Used both as a type-safe
22
+ * iteration source (bubble menu pills, slash-command registration) and by
23
+ * the runtime `isCoauthorAction` guard so callers don't drift from the
24
+ * server's allowlist.
25
+ */
26
+ export const COAUTHOR_ACTIONS = ['rewrite', 'expand', 'compress', 'proofread'] as const
27
+
28
+ const ACTION_LABELS: Record<CoauthorAction, string> = {
29
+ rewrite: 'Rewrite',
30
+ expand: 'Expand',
31
+ compress: 'Compress',
32
+ proofread: 'Proofread',
33
+ }
34
+
35
+ const ACTION_DESCRIPTIONS: Record<CoauthorAction, string> = {
36
+ rewrite: 'Rewrite the selection in a new tone or style',
37
+ expand: 'Expand the selection with more detail',
38
+ compress: 'Shorten the selection while preserving meaning',
39
+ proofread: 'Correct grammar, spelling, and punctuation',
40
+ }
41
+
42
+ /**
43
+ * Hard client-side ceiling that matches `AI_CONTEXT_MAX_CHARS` in cms-core.
44
+ * Catching this here avoids a round-trip just to get a 400 back when a user
45
+ * pastes a giant block into the bubble menu.
46
+ */
47
+ export const COAUTHOR_TEXT_MAX_CHARS = 50_000
48
+
49
+ export type ProofreadChange = {
50
+ original: string
51
+ corrected: string
52
+ reason?: string
53
+ }
54
+
55
+ export interface CoauthorOptions {
56
+ /** Free-form style cue, e.g. "more concise", "academic". Used by rewrite. */
57
+ style?: string
58
+ /** Tone cue, e.g. "friendly", "professional". Used by rewrite. */
59
+ tone?: string
60
+ /** Free-form instructions. Used by expand. */
61
+ instructions?: string
62
+ /** Target length in words. Used by compress. */
63
+ targetLength?: number
64
+ }
65
+
66
+ export interface CoauthorResult {
67
+ action: CoauthorAction
68
+ text: string
69
+ /** Populated only when `action === 'proofread'`. */
70
+ changes?: ProofreadChange[]
71
+ }
72
+
73
+ export type CoauthorOutcome =
74
+ | { ok: true; result: CoauthorResult }
75
+ | { ok: false; error: string; status: number }
76
+
77
+ export function coauthorActionLabel(action: CoauthorAction): string {
78
+ return ACTION_LABELS[action]
79
+ }
80
+
81
+ export function coauthorActionDescription(action: CoauthorAction): string {
82
+ return ACTION_DESCRIPTIONS[action]
83
+ }
84
+
85
+ export function isCoauthorAction(value: unknown): value is CoauthorAction {
86
+ return typeof value === 'string' && (COAUTHOR_ACTIONS as readonly string[]).includes(value)
87
+ }
88
+
89
+ /**
90
+ * Run a text-transformation micro-action against `/ai/coauthor`.
91
+ *
92
+ * Returns a discriminated outcome so callers can switch on `outcome.ok`
93
+ * without a try/catch — the underlying `cmsApi` already turns every
94
+ * non-2xx and every network failure into a structured error envelope.
95
+ *
96
+ * Pre-flight validation is intentionally cheap (whitespace + length) so the
97
+ * server stays the single source of truth for richer auth/rate-limit logic.
98
+ */
99
+ export async function coauthorText(
100
+ action: CoauthorAction,
101
+ text: string,
102
+ opts: CoauthorOptions = {},
103
+ ): Promise<CoauthorOutcome> {
104
+ if (!isCoauthorAction(action)) {
105
+ return { ok: false, error: `Unknown action "${String(action)}"`, status: 0 }
106
+ }
107
+ if (typeof text !== 'string' || text.trim().length === 0) {
108
+ return { ok: false, error: 'No text provided', status: 0 }
109
+ }
110
+ if (text.length > COAUTHOR_TEXT_MAX_CHARS) {
111
+ return {
112
+ ok: false,
113
+ error: `Text exceeds ${COAUTHOR_TEXT_MAX_CHARS.toLocaleString()} character limit`,
114
+ status: 0,
115
+ }
116
+ }
117
+
118
+ const body: Record<string, unknown> = { action, text }
119
+ if (opts.style !== undefined) body.style = opts.style
120
+ if (opts.tone !== undefined) body.tone = opts.tone
121
+ if (opts.instructions !== undefined) body.instructions = opts.instructions
122
+ if (opts.targetLength !== undefined) body.targetLength = opts.targetLength
123
+
124
+ const res = await cmsApi<{
125
+ action?: CoauthorAction
126
+ text?: unknown
127
+ changes?: unknown
128
+ }>('/ai/coauthor', {
129
+ method: 'POST',
130
+ body: JSON.stringify(body),
131
+ })
132
+
133
+ if (res.error || !res.data) {
134
+ return {
135
+ ok: false,
136
+ error: res.error ?? `Request failed (${res.status})`,
137
+ status: res.status,
138
+ }
139
+ }
140
+
141
+ if (typeof res.data.text !== 'string') {
142
+ return {
143
+ ok: false,
144
+ error: 'Malformed response from /ai/coauthor (missing text)',
145
+ status: res.status,
146
+ }
147
+ }
148
+
149
+ return {
150
+ ok: true,
151
+ result: {
152
+ action: res.data.action ?? action,
153
+ text: res.data.text,
154
+ changes:
155
+ action === 'proofread' && Array.isArray(res.data.changes)
156
+ ? (res.data.changes as ProofreadChange[])
157
+ : undefined,
158
+ },
159
+ }
160
+ }