@pilotiq/pilotiq 0.8.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/CHANGELOG.md +213 -0
  3. package/dist/Pilotiq.d.ts +55 -0
  4. package/dist/Pilotiq.d.ts.map +1 -1
  5. package/dist/Pilotiq.js +21 -0
  6. package/dist/Pilotiq.js.map +1 -1
  7. package/dist/Resource.d.ts +39 -0
  8. package/dist/Resource.d.ts.map +1 -1
  9. package/dist/Resource.js +30 -0
  10. package/dist/Resource.js.map +1 -1
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/pageData/helpers.d.ts +19 -1
  15. package/dist/pageData/helpers.d.ts.map +1 -1
  16. package/dist/pageData/helpers.js +33 -0
  17. package/dist/pageData/helpers.js.map +1 -1
  18. package/dist/pageData/navigation.d.ts +17 -1
  19. package/dist/pageData/navigation.d.ts.map +1 -1
  20. package/dist/pageData/navigation.js +14 -0
  21. package/dist/pageData/navigation.js.map +1 -1
  22. package/dist/pageData/resourcePages.d.ts.map +1 -1
  23. package/dist/pageData/resourcePages.js +17 -2
  24. package/dist/pageData/resourcePages.js.map +1 -1
  25. package/dist/pageData.d.ts +1 -1
  26. package/dist/pageData.d.ts.map +1 -1
  27. package/dist/pageData.js +1 -1
  28. package/dist/pageData.js.map +1 -1
  29. package/dist/react/AppShell.d.ts +5 -0
  30. package/dist/react/AppShell.d.ts.map +1 -1
  31. package/dist/react/AppShell.js +1 -1
  32. package/dist/react/AppShell.js.map +1 -1
  33. package/dist/react/FormCollabBindingRegistry.d.ts +71 -1
  34. package/dist/react/FormCollabBindingRegistry.d.ts.map +1 -1
  35. package/dist/react/FormCollabBindingRegistry.js.map +1 -1
  36. package/dist/react/FormStateContext.d.ts +17 -0
  37. package/dist/react/FormStateContext.d.ts.map +1 -1
  38. package/dist/react/FormStateContext.js +44 -3
  39. package/dist/react/FormStateContext.js.map +1 -1
  40. package/dist/react/RecordWrapperGate.d.ts +19 -6
  41. package/dist/react/RecordWrapperGate.d.ts.map +1 -1
  42. package/dist/react/RecordWrapperGate.js +18 -8
  43. package/dist/react/RecordWrapperGate.js.map +1 -1
  44. package/dist/react/fields/MarkdownInput.d.ts.map +1 -1
  45. package/dist/react/fields/MarkdownInput.js +105 -3
  46. package/dist/react/fields/MarkdownInput.js.map +1 -1
  47. package/dist/react/fields/TextLikeInput.d.ts +10 -0
  48. package/dist/react/fields/TextLikeInput.d.ts.map +1 -1
  49. package/dist/react/fields/TextLikeInput.js +179 -0
  50. package/dist/react/fields/TextLikeInput.js.map +1 -1
  51. package/dist/react/fields/textDelta.d.ts +44 -0
  52. package/dist/react/fields/textDelta.d.ts.map +1 -0
  53. package/dist/react/fields/textDelta.js +80 -0
  54. package/dist/react/fields/textDelta.js.map +1 -0
  55. package/dist/react/index.d.ts +2 -2
  56. package/dist/react/index.d.ts.map +1 -1
  57. package/dist/react/index.js +1 -1
  58. package/dist/react/index.js.map +1 -1
  59. package/dist/react/parseRecordEditUrl.d.ts +33 -9
  60. package/dist/react/parseRecordEditUrl.d.ts.map +1 -1
  61. package/dist/react/parseRecordEditUrl.js +40 -2
  62. package/dist/react/parseRecordEditUrl.js.map +1 -1
  63. package/package.json +1 -1
  64. package/src/Pilotiq.ts +64 -0
  65. package/src/Resource.test.ts +44 -0
  66. package/src/Resource.ts +58 -0
  67. package/src/index.ts +2 -0
  68. package/src/pageData/helpers.ts +40 -1
  69. package/src/pageData/navigation.ts +32 -1
  70. package/src/pageData/resourcePages.ts +17 -1
  71. package/src/pageData.test.ts +137 -0
  72. package/src/pageData.ts +1 -0
  73. package/src/react/AppShell.tsx +6 -1
  74. package/src/react/FormCollabBindingRegistry.ts +63 -1
  75. package/src/react/FormStateContext.tsx +62 -3
  76. package/src/react/RecordWrapperGate.tsx +26 -8
  77. package/src/react/fields/MarkdownInput.tsx +100 -3
  78. package/src/react/fields/TextLikeInput.tsx +203 -1
  79. package/src/react/fields/textDelta.test.ts +141 -0
  80. package/src/react/fields/textDelta.ts +86 -0
  81. package/src/react/index.ts +9 -1
  82. package/src/react/parseRecordEditUrl.test.ts +48 -1
  83. package/src/react/parseRecordEditUrl.ts +52 -13
@@ -5,6 +5,7 @@ import { Form } from './elements/Form.js'
5
5
  import { ListTab } from './Tab.js'
6
6
  import { ListTabs } from './elements/ListTabs.js'
7
7
  import {
8
+ applyEditPageHydrators,
8
9
  applyFillPipeline,
9
10
  formCreateOptionData,
10
11
  formStateData,
@@ -1202,3 +1203,139 @@ describe('tagRichTextMentionUrls — nested Repeater + Builder rows', () => {
1202
1203
  assert.equal(inner.stamped, '/admin/_form/art/mentions')
1203
1204
  })
1204
1205
  })
1206
+
1207
+ describe('applyEditPageHydrators (Pilotiq.editPageHydrator)', () => {
1208
+ class Posts extends Resource { static override label = 'Posts' }
1209
+ const ctx = (currentValues: Record<string, unknown> = {}) => ({
1210
+ resource: Posts,
1211
+ recordId: '42',
1212
+ currentValues,
1213
+ })
1214
+
1215
+ it('empty hydrators array → empty overlay', async () => {
1216
+ const overlay = await applyEditPageHydrators([], ctx())
1217
+ assert.deepEqual(overlay, {})
1218
+ })
1219
+
1220
+ it('hydrator returning null → empty overlay', async () => {
1221
+ const overlay = await applyEditPageHydrators([
1222
+ async () => null,
1223
+ ], ctx())
1224
+ assert.deepEqual(overlay, {})
1225
+ })
1226
+
1227
+ it('hydrator returning a partial → overlay carries the keys', async () => {
1228
+ const overlay = await applyEditPageHydrators([
1229
+ async () => ({ title: 'Y-Title', body: 'Y-Body' }),
1230
+ ], ctx({ title: 'DB-Title', body: 'DB-Body', author: 'DB-Author' }))
1231
+ assert.deepEqual(overlay, { title: 'Y-Title', body: 'Y-Body' })
1232
+ })
1233
+
1234
+ it('two hydrators merge in registration order (later wins on conflict)', async () => {
1235
+ const overlay = await applyEditPageHydrators([
1236
+ async () => ({ title: 'first', shared: 'first-shared' }),
1237
+ async () => ({ body: 'second', shared: 'second-shared' }),
1238
+ ], ctx())
1239
+ assert.deepEqual(overlay, {
1240
+ title: 'first',
1241
+ body: 'second',
1242
+ shared: 'second-shared',
1243
+ })
1244
+ })
1245
+
1246
+ it('hydrator that throws is swallowed; siblings still contribute', async () => {
1247
+ // Stub console.warn so the test output stays clean; restore after.
1248
+ const originalWarn = console.warn
1249
+ let warned = false
1250
+ console.warn = (..._args: unknown[]) => { warned = true }
1251
+ try {
1252
+ const overlay = await applyEditPageHydrators([
1253
+ async () => { throw new Error('boom') },
1254
+ async () => ({ title: 'sibling-survived' }),
1255
+ ], ctx())
1256
+ assert.deepEqual(overlay, { title: 'sibling-survived' })
1257
+ assert.equal(warned, true, 'console.warn should fire for thrown hydrators')
1258
+ } finally {
1259
+ console.warn = originalWarn
1260
+ }
1261
+ })
1262
+
1263
+ it('hydrator returning a non-object is skipped', async () => {
1264
+ const overlay = await applyEditPageHydrators([
1265
+ // @ts-expect-error — deliberately exercising the runtime guard
1266
+ async () => 'not-an-object',
1267
+ async () => ({ title: 'real-result' }),
1268
+ ], ctx())
1269
+ assert.deepEqual(overlay, { title: 'real-result' })
1270
+ })
1271
+
1272
+ it('hydrator receives current fill-pipeline values via ctx.currentValues', async () => {
1273
+ let seen: Record<string, unknown> | undefined
1274
+ await applyEditPageHydrators([
1275
+ async (ctx) => { seen = ctx.currentValues; return null },
1276
+ ], ctx({ title: 'DB-Title', body: 'DB-Body' }))
1277
+ assert.deepEqual(seen, { title: 'DB-Title', body: 'DB-Body' })
1278
+ })
1279
+
1280
+ it('hydrator receives resource class + recordId in ctx', async () => {
1281
+ let seenResource: unknown
1282
+ let seenRecordId: unknown
1283
+ await applyEditPageHydrators([
1284
+ async (ctx) => { seenResource = ctx.resource; seenRecordId = ctx.recordId; return null },
1285
+ ], ctx())
1286
+ assert.equal(seenResource, Posts)
1287
+ assert.equal(seenRecordId, '42')
1288
+ })
1289
+ })
1290
+
1291
+ describe('Pilotiq.editPageHydrator builder method', () => {
1292
+ it('stores hydrators on the config in registration order', () => {
1293
+ const fn1 = async () => ({ a: 1 })
1294
+ const fn2 = async () => ({ b: 2 })
1295
+ const panel = Pilotiq.make('Admin')
1296
+ .editPageHydrator(fn1)
1297
+ .editPageHydrator(fn2)
1298
+ assert.deepEqual(panel.getConfig().editPageHydrators, [fn1, fn2])
1299
+ })
1300
+
1301
+ it('absent when no hydrator registered', () => {
1302
+ const panel = Pilotiq.make('Admin')
1303
+ assert.equal(panel.getConfig().editPageHydrators, undefined)
1304
+ })
1305
+ })
1306
+
1307
+ describe('panelInfo — recordCollab map (resource collab opt-in)', () => {
1308
+ it('absent when no resource opts in', async () => {
1309
+ class Posts extends Resource { static override label = 'Posts' }
1310
+ const info = await panelInfo(Pilotiq.make('T').path('/admin').resources([Posts]))
1311
+ assert.equal((info as { recordCollab?: unknown }).recordCollab, undefined)
1312
+ })
1313
+
1314
+ it('emits an entry for each opted-in resource keyed by URL slug', async () => {
1315
+ class Posts extends Resource {
1316
+ static override label = 'Posts'
1317
+ static override collab = true as const
1318
+ }
1319
+ class Users extends Resource {
1320
+ static override label = 'Users'
1321
+ // No collab — should NOT appear in the map.
1322
+ }
1323
+ const info = await panelInfo(Pilotiq.make('T').path('/admin').resources([Posts, Users]))
1324
+ const map = (info as { recordCollab?: Record<string, unknown> }).recordCollab
1325
+ assert.deepEqual(map, {
1326
+ posts: { pages: ['edit'], presence: true },
1327
+ })
1328
+ })
1329
+
1330
+ it('honors object form of static collab (pages + presence override defaults)', async () => {
1331
+ class Posts extends Resource {
1332
+ static override label = 'Posts'
1333
+ static override collab = { pages: ['edit', 'view'] as const, presence: false }
1334
+ }
1335
+ const info = await panelInfo(Pilotiq.make('T').path('/admin').resources([Posts]))
1336
+ const map = (info as { recordCollab?: Record<string, unknown> }).recordCollab
1337
+ assert.deepEqual(map, {
1338
+ posts: { pages: ['edit', 'view'], presence: false },
1339
+ })
1340
+ })
1341
+ })
package/src/pageData.ts CHANGED
@@ -22,6 +22,7 @@ export type { ServerDataMap } from './pageData/helpers.js'
22
22
  // Re-export the URL-tag helpers + fill pipeline + server-data resolver
23
23
  // for consumers that import them through `./pageData.js`.
24
24
  export {
25
+ applyEditPageHydrators,
25
26
  applyFillPipeline,
26
27
  applyRelationshipBuilderFill,
27
28
  applyRelationshipRepeaterFill,
@@ -10,7 +10,7 @@ import type { RightPanelRegistry } from './right-panel-registry.js'
10
10
  import { RightPanelRegistryProvider } from './right-panel-registry.js'
11
11
  import { RightSidebarProvider, useRightSidebarOptional } from './RightSidebarContext.js'
12
12
  import { RightSidebar } from './RightSidebar.js'
13
- import { RecordWrapperGate } from './RecordWrapperGate.js'
13
+ import { RecordWrapperGate, type RecordCollabMap } from './RecordWrapperGate.js'
14
14
  import { useIsMobile } from './hooks/use-mobile.js'
15
15
  import type { NavItem, UserMenuMeta, DatabaseNotificationsMeta, RightSidebarMeta } from '../pageData.js'
16
16
  import type { RenderHookMap } from '../RenderHook.js'
@@ -34,6 +34,10 @@ export interface AppShellProps {
34
34
  * `panelInfo()` only ships this when at least one contribution
35
35
  * was registered AND passed the auth gate AND is non-hidden. */
36
36
  rightSidebar?: RightSidebarMeta
37
+ /** Per-resource collab opt-in map — read by `RecordWrapperGate` to
38
+ * decide whether to mount the plugin-registered RecordWrapper on
39
+ * a record edit/view URL. Absent when no resource opted in. */
40
+ recordCollab?: RecordCollabMap
37
41
  /** Pre-resolved render-hook slots for the panel chrome (body /
38
42
  * topbar / sidebar / user-menu / footer / head). Sparse map —
39
43
  * slots with no registered entries are absent. Built by
@@ -130,6 +134,7 @@ export function AppShell({ layout = 'sidebar', notifications, componentRegistry,
130
134
  <RecordWrapperGate
131
135
  basePath={props.basePath}
132
136
  {...(props.currentPath !== undefined ? { currentPath: props.currentPath } : {})}
137
+ {...(props.panel.recordCollab !== undefined ? { recordCollab: props.panel.recordCollab } : {})}
133
138
  >
134
139
  {props.children}
135
140
  </RecordWrapperGate>
@@ -1,5 +1,44 @@
1
+ import type { ElementMeta } from '../schema/Element.js'
1
2
  import type { CollabRoom } from './CollabRoomContext.js'
2
3
 
4
+ /**
5
+ * Phase F.6 — character-level edit op emitted by `TextLikeInput` and
6
+ * applied through `TextBinding.applyDelta`. `replace` covers IME / paste
7
+ * / multi-char selections; `insert` and `delete` cover the single-key
8
+ * common path. Pilotiq core stays Yjs-free — the binding impl in
9
+ * `@pilotiq-pro/collab` translates these into `Y.Text.insert / delete`
10
+ * inside a transaction.
11
+ */
12
+ export type TextDelta =
13
+ | { kind: 'insert', index: number, text: string }
14
+ | { kind: 'delete', index: number, length: number }
15
+ | { kind: 'replace', from: number, to: number, text: string }
16
+
17
+ /**
18
+ * Phase F.6 — per-field character-level CRDT handle. Issued by
19
+ * `FormCollabBinding.getTextBinding(name)` for text-shaped fields
20
+ * (`TextField / TextareaField / EmailField / SlugField / MarkdownField`);
21
+ * returns `null` for non-text fields or text fields opted out via
22
+ * `.collab(false)`. The surface stays intentionally narrow so pilotiq
23
+ * core never touches Yjs directly — same posture as `FormCollabBinding`.
24
+ *
25
+ * - `read()` returns the current full string. `TextLikeInput` calls
26
+ * this once on mount to seed its controlled value.
27
+ * - `applyDelta(delta)` is called from `onInput` events with a single
28
+ * `insert / delete / replace` op derived from the input's selection.
29
+ * - `observe(fn)` registers a remote-change listener; `fn(next)`
30
+ * receives the post-change string. Returns an unsubscribe function.
31
+ * - `destroy()` cleans up everything the handle holds. The owning
32
+ * `FormCollabBinding.destroy()` is expected to cascade — consumers
33
+ * don't need to call this directly.
34
+ */
35
+ export interface TextBinding {
36
+ read(): string
37
+ applyDelta(delta: TextDelta): void
38
+ observe(fn: (next: string) => void): () => void
39
+ destroy(): void
40
+ }
41
+
3
42
  /**
4
43
  * Binding contract that a collab plugin returns from
5
44
  * `registerFormCollabBinding` — wraps a single form's value map in a
@@ -16,8 +55,14 @@ import type { CollabRoom } from './CollabRoomContext.js'
16
55
  * - `subscribe(fn)` registers a listener that fires when REMOTE
17
56
  * changes land; `fn(snapshot)` receives the full updated map.
18
57
  * The provider re-applies this snapshot onto its React state.
58
+ * - `getTextBinding(name)` (Phase F.6) returns a `Y.Text`-backed
59
+ * handle for text-shaped fields, or `null` for non-text fields and
60
+ * text fields opted out via `.collab(false)`. The text/non-text
61
+ * allowlist lives in the binding impl — `FormStateProvider` asks
62
+ * for every field and routes per-field on the answer.
19
63
  * - `destroy()` is called on unmount — gives the plugin a chance to
20
- * remove its CRDT observer.
64
+ * remove its CRDT observer. Implementations are expected to cascade
65
+ * into every `TextBinding` they issued.
21
66
  *
22
67
  * `unknown` payloads keep pilotiq core Yjs-free; the binding owns its
23
68
  * own type knowledge. Same posture as `CollabExtensionFactory`.
@@ -29,6 +74,12 @@ export interface FormCollabBinding {
29
74
  set(name: string, value: unknown): void
30
75
  /** Subscribe to remote changes. Returns an unsubscribe function. */
31
76
  subscribe(fn: (snapshot: Record<string, unknown>) => void): () => void
77
+ /** Phase F.6 — per-field text-CRDT handle. Returns `null` for non-text
78
+ * fields or text fields opted out via `.collab(false)`. Optional so
79
+ * existing F1-era plugins keep type-checking without a no-op stub;
80
+ * when absent, every text field stays on today's whole-string LWW
81
+ * path (i.e. F.6 character-level CRDT is opt-in by impl). */
82
+ getTextBinding?(name: string): TextBinding | null
32
83
  /** Cleanup hook called when the form unmounts. */
33
84
  destroy(): void
34
85
  }
@@ -51,6 +102,17 @@ export interface FormCollabBindingFactoryArgs {
51
102
  * map already populated and skip.
52
103
  */
53
104
  initial: Record<string, unknown>
105
+ /**
106
+ * Phase F.6 — initial form meta from the server. The binding walks
107
+ * this once at construction to decide which fields are text-shaped
108
+ * (`fieldType ∈ { text, textarea, email, slug, markdown }`) and
109
+ * which have opted out via `.collab(false)`. Text fields get a
110
+ * dedicated `Y.Text` and route through `getTextBinding`; non-text
111
+ * fields stay on the `Y.Map`. The meta is captured at mount; later
112
+ * structural changes from `live()` re-resolves aren't re-walked
113
+ * (rare in practice — dynamic field add/remove is an F-followup).
114
+ */
115
+ formMeta: ElementMeta
54
116
  }
55
117
 
56
118
  export type FormCollabBindingFactory = (args: FormCollabBindingFactoryArgs) => FormCollabBinding
@@ -18,7 +18,11 @@ import {
18
18
  import { runJsHandler } from './fieldJsHandler.js'
19
19
  import { useToast } from './Toaster.js'
20
20
  import { useCollabRoom } from './CollabRoomContext.js'
21
- import { getFormCollabBinding, type FormCollabBinding } from './FormCollabBindingRegistry.js'
21
+ import {
22
+ getFormCollabBinding,
23
+ type FormCollabBinding,
24
+ type TextBinding,
25
+ } from './FormCollabBindingRegistry.js'
22
26
 
23
27
  export type FieldStatus = 'idle' | 'pending'
24
28
 
@@ -40,6 +44,13 @@ export interface FormStateApi {
40
44
  formMeta: ElementMeta
41
45
  inFlight: boolean
42
46
  fieldStatus: (name: string) => FieldStatus
47
+ /** Phase F.6 — per-field text-CRDT handles stashed at collab-room mount.
48
+ * `null` outside a room or before the binding effect has populated the
49
+ * map. The text/non-text allowlist lives in the binding impl —
50
+ * `FormStateProvider` asks for every top-level field and only stashes
51
+ * non-null answers, so a `Map.get()` hit means the binding has opted
52
+ * this field into the character-level path. */
53
+ textBindings: ReadonlyMap<string, TextBinding> | null
43
54
  }
44
55
 
45
56
  const FormStateContext = createContext<FormStateApi | null>(null)
@@ -93,6 +104,15 @@ export interface UseFieldStateResult {
93
104
  /** True while a live re-resolve POST is in flight for this field. */
94
105
  pending: boolean
95
106
  errors: string[]
107
+ /** Phase F.6 — character-level CRDT handle for text-shaped fields when
108
+ * a collab room is mounted up-tree AND the binding strategy applies
109
+ * (allowlist + `.collab() !== false`). Null in every other case —
110
+ * outside a `FormStateProvider`, outside a `<RecordCollabRoom>`, on
111
+ * non-text fields, on dotted-path inner-Repeater rows (deferred to
112
+ * F.5), and on text fields opted out via `.collab(false)`. Text input
113
+ * renderers branch on this: non-null → character-level path with
114
+ * `applyDelta + observe`; null → today's whole-string LWW path. */
115
+ textBinding: TextBinding | null
96
116
  }
97
117
 
98
118
  /** Per-field accessor. Inside a `FormStateProvider` it returns the controlled
@@ -108,6 +128,7 @@ export function useFieldState(name: string): UseFieldStateResult {
108
128
  triggerLive: () => {},
109
129
  pending: false,
110
130
  errors: [],
131
+ textBinding: null,
111
132
  }
112
133
  }
113
134
  // Dotted-path fields (inner Repeater rows) always render uncontrolled
@@ -120,6 +141,11 @@ export function useFieldState(name: string): UseFieldStateResult {
120
141
  triggerLive: (valueOverride?: unknown) => ctx.triggerLive(name, valueOverride),
121
142
  pending: ctx.fieldStatus(name) === 'pending',
122
143
  errors: ctx.errors[name] ?? [],
144
+ // Phase F.6 — dotted-path inner-Repeater rows skipped in v1 (deferred
145
+ // to F.5 alongside Y.Array row identity). Outside a collab room or
146
+ // for non-text fields, the stash returns null and the renderer falls
147
+ // back to today's whole-string LWW path.
148
+ textBinding: dotted ? null : (ctx.textBindings?.get(name) ?? null),
123
149
  }
124
150
  }
125
151
 
@@ -170,6 +196,13 @@ export function FormStateProvider({
170
196
  const [errors, setErrors] = useState<Record<string, string[]>>(initialErrors)
171
197
  const [pendingNames, setPendingNames] = useState<Set<string>>(() => new Set())
172
198
  const [inFlight, setInFlight] = useState(false)
199
+ // Phase F.6 — per-field text-CRDT stash. `null` until the collab effect
200
+ // populates it; stays `null` outside a collab room. Stored in state (not
201
+ // a ref) so consumers of `useFieldState` re-render once the bindings
202
+ // land. One extra render after collab-mount; acceptable since the
203
+ // existing `setValuesState` overlay below already triggers one when the
204
+ // room has pre-existing state.
205
+ const [textBindings, setTextBindings] = useState<ReadonlyMap<string, TextBinding> | null>(null)
173
206
 
174
207
  const { notify } = useToast()
175
208
 
@@ -215,7 +248,12 @@ export function FormStateProvider({
215
248
  useEffect(() => {
216
249
  if (!collabRoom || !bindingFactory || !formId) return
217
250
 
218
- const binding = bindingFactory({ room: collabRoom, formId, initial: valuesRef.current })
251
+ const binding = bindingFactory({
252
+ room: collabRoom,
253
+ formId,
254
+ initial: valuesRef.current,
255
+ formMeta: formMetaRef.current,
256
+ })
219
257
  bindingRef.current = binding
220
258
 
221
259
  // Lift any state already in the room (subsequent joiners — first
@@ -228,6 +266,25 @@ export function FormStateProvider({
228
266
  setValuesState((prev) => ({ ...prev, ...synced }))
229
267
  }
230
268
 
269
+ // Phase F.6 — ask the binding for a `TextBinding` on every top-level
270
+ // field name. The text/non-text allowlist lives in the binding impl,
271
+ // not in core: the binding returns `null` for non-text fields and
272
+ // text fields opted out via `.collab(false)`. `getTextBinding` is
273
+ // optional on the contract — F1-era plugins that haven't implemented
274
+ // it short-circuit the whole stash and every text field stays on the
275
+ // LWW path. We stash only the non-null answers. Cleanup is owned by
276
+ // `binding.destroy()` (expected to cascade into every issued
277
+ // `TextBinding`).
278
+ if (binding.getTextBinding) {
279
+ const textStash = new Map<string, TextBinding>()
280
+ for (const fieldName of Object.keys(valuesRef.current)) {
281
+ if (fieldName.includes('.')) continue
282
+ const tb = binding.getTextBinding(fieldName)
283
+ if (tb) textStash.set(fieldName, tb)
284
+ }
285
+ if (textStash.size > 0) setTextBindings(textStash)
286
+ }
287
+
231
288
  // Subscribe to remote changes. Local writes ALSO trigger this
232
289
  // (Yjs observers fire on local transactions too) — the per-key
233
290
  // Object.is short-circuit below collapses them into no-op renders.
@@ -249,6 +306,7 @@ export function FormStateProvider({
249
306
  unsubscribe()
250
307
  binding.destroy()
251
308
  bindingRef.current = null
309
+ setTextBindings(null)
252
310
  }
253
311
  // `valuesRef.current` is intentionally read once at mount — initial
254
312
  // values seed the binding; subsequent edits flow through `setValue`
@@ -479,7 +537,8 @@ export function FormStateProvider({
479
537
  formMeta,
480
538
  inFlight,
481
539
  fieldStatus,
482
- }), [values, setValue, triggerLive, errors, applyErrors, formMeta, inFlight, fieldStatus])
540
+ textBindings,
541
+ }), [values, setValue, triggerLive, errors, applyErrors, formMeta, inFlight, fieldStatus, textBindings])
483
542
 
484
543
  return (
485
544
  <FormStateContext.Provider value={api}>
@@ -1,37 +1,55 @@
1
1
  import { type ReactNode } from 'react'
2
2
  import { getRecordWrapper } from './RecordWrapperRegistry.js'
3
- import { parseRecordEditUrl } from './parseRecordEditUrl.js'
3
+ import { parseRecordPageUrl } from './parseRecordEditUrl.js'
4
+ import type { ResourceCollabConfig } from '../Resource.js'
5
+
6
+ /** Per-resource collab opt-in keyed by URL slug (`R.getSlug()` for
7
+ * non-clustered, `${cluster.slug}/${R.getSlug()}` for clustered). Built
8
+ * server-side by `panelInfo()` as `recordCollab`. */
9
+ export type RecordCollabMap = Record<string, ResourceCollabConfig>
4
10
 
5
11
  export interface RecordWrapperGateProps {
6
12
  currentPath?: string
7
13
  basePath: string
14
+ /** Resource opt-in map. Absent means no resource opted in (or the
15
+ * panel has no resources) — gate always passes through. */
16
+ recordCollab?: RecordCollabMap
8
17
  children: ReactNode
9
18
  }
10
19
 
11
20
  /**
12
21
  * Conditionally wraps the page tree with the plugin-registered
13
- * `RecordWrapper` when the current URL resolves to a record-bound edit
14
- * page. Pass-through in every other case:
22
+ * `RecordWrapper` when the current URL resolves to a record-bound page
23
+ * AND the underlying resource has opted into collab on that page role.
24
+ * Pass-through in every other case:
15
25
  *
16
26
  * - no plugin registered a wrapper (`getRecordWrapper() === null`)
17
- * - the URL isn't a record-edit URL (list / create / view / dashboard / …)
27
+ * - the URL isn't a record edit/view page
18
28
  * - `currentPath` not yet known on the very first SSR render
29
+ * - the resource has not opted in via `static collab` (or has opted in
30
+ * but excluded the current page role)
19
31
  *
20
32
  * Mounted once inside `AppShell` around the page content area so
21
33
  * record-scoped plugins (collab room, audit trail, …) get one
22
34
  * lifetimed mount per record-view-or-edit without each plugin having
23
35
  * to thread URL parsing into its own provider.
24
36
  *
25
- * Scope limited to `/edit` URLs in v1; view-page support (read-only
26
- * collab cursors on `ViewPage`) is a follow-up.
37
+ * v1 caveat: nested-relation edit URLs (`/articles/:parentId/comments/:childId/edit`)
38
+ * have a dynamic-id segment baked into the URL slug, so they don't match
39
+ * the resource-keyed `recordCollab` map and fall through to no-collab.
40
+ * Collab on nested-relation edits is a follow-up.
27
41
  */
28
- export function RecordWrapperGate({ currentPath, basePath, children }: RecordWrapperGateProps) {
42
+ export function RecordWrapperGate({ currentPath, basePath, recordCollab, children }: RecordWrapperGateProps) {
29
43
  const Wrapper = getRecordWrapper()
30
44
  if (!Wrapper || !currentPath) return <>{children}</>
31
45
 
32
- const identity = parseRecordEditUrl(currentPath, basePath)
46
+ const identity = parseRecordPageUrl(currentPath, basePath)
33
47
  if (!identity) return <>{children}</>
34
48
 
49
+ const cfg = recordCollab?.[identity.resourceSlug]
50
+ if (!cfg) return <>{children}</>
51
+ if (!cfg.pages.includes(identity.role)) return <>{children}</>
52
+
35
53
  return (
36
54
  <Wrapper resourceSlug={identity.resourceSlug} recordId={identity.recordId}>
37
55
  {children}
@@ -1,4 +1,4 @@
1
- import React, { useMemo, useRef, useState } from 'react'
1
+ import React, { useEffect, useMemo, useRef, useState } from 'react'
2
2
  import { marked } from 'marked'
3
3
  import {
4
4
  BoldIcon, ItalicIcon, StrikethroughIcon, LinkIcon,
@@ -8,6 +8,7 @@ import {
8
8
  import { useFieldState } from '../FormStateContext.js'
9
9
  import { useToast } from '../Toaster.js'
10
10
  import { Button } from '../ui/button.js'
11
+ import { computeDelta, preserveCursor } from './textDelta.js'
11
12
 
12
13
  type ToolbarButton =
13
14
  | 'bold' | 'italic' | 'strike' | 'link'
@@ -47,14 +48,90 @@ export function MarkdownInput({
47
48
  const fs = useFieldState(name)
48
49
  const { notify } = useToast()
49
50
  const textareaRef = useRef<HTMLTextAreaElement | null>(null)
51
+ // Phase F.6 — IME composition gate. Set between `compositionstart` /
52
+ // `compositionend`; the textarea's onChange skips `applyDelta` while
53
+ // composing so intermediate chars don't emit ops. Lives at the
54
+ // component scope so the onChange and composition handlers share it.
55
+ const isComposingRef = useRef<boolean>(false)
50
56
 
51
57
  const initial = useMemo(() => stringValue(defaultValue), [])
52
58
  const [localValue, setLocalValue] = useState<string>(initial)
53
59
  const [tab, setTab] = useState<'write' | 'preview'>('write')
54
60
  const [busy, setBusy] = useState(false)
55
61
 
56
- const value = fs.controlled ? stringValue(fs.value) : localValue
62
+ // Phase F.6 when a `<RecordCollabRoom>` is mounted and the field has
63
+ // a `TextBinding`, the textarea is bound to a `Y.Text` and edits emit
64
+ // `TextDelta`s. Mirrors the architecture in `TextLikeInput.tsx` but
65
+ // wired in-line because MarkdownInput has its own toolbar + Preview
66
+ // tab that also need to flow through the binding.
67
+ const binding = fs.textBinding
68
+ const [boundValue, setBoundValue] = useState<string>(() => binding?.read() ?? initial)
69
+ const boundValueRef = useRef<string>(boundValue)
70
+ useEffect(() => { boundValueRef.current = boundValue }, [boundValue])
71
+
72
+ // On binding swap: read current Y.Text state. If non-empty, lift it
73
+ // into local + form-map state. If empty (no peer has typed yet), leave
74
+ // the SSR-default-derived `boundValue` showing — first edit will
75
+ // emit a replace-from-empty delta that atomically populates Y.Text.
76
+ // No client-side seed: Y.Text isn't safe to seed under concurrent
77
+ // first-mounters (see @pilotiq-pro/collab `formCollabBinding.ts`).
78
+ useEffect(() => {
79
+ if (!binding) return
80
+ const next = binding.read()
81
+ if (next.length > 0) {
82
+ setBoundValue(next)
83
+ boundValueRef.current = next
84
+ fs.setValue(next)
85
+ }
86
+ // eslint-disable-next-line react-hooks/exhaustive-deps
87
+ }, [binding])
88
+
89
+ // Subscribe to remote changes. Local-echoes are filtered by the
90
+ // `next === prev` guard. Cursor preserved via the same heuristic
91
+ // used in `TextLikeInput.BoundTextInput`.
92
+ useEffect(() => {
93
+ if (!binding) return
94
+ return binding.observe((next) => {
95
+ const prev = boundValueRef.current
96
+ if (next === prev) return
97
+ const ta = textareaRef.current
98
+ const cursor = ta?.selectionStart ?? next.length
99
+ const restored = preserveCursor(prev, next, cursor)
100
+ setBoundValue(next)
101
+ boundValueRef.current = next
102
+ fs.setValue(next)
103
+ requestAnimationFrame(() => {
104
+ if (!ta) return
105
+ if (document.activeElement !== ta) return
106
+ try { ta.setSelectionRange(restored, restored) } catch { /* defensive */ }
107
+ })
108
+ })
109
+ // eslint-disable-next-line react-hooks/exhaustive-deps
110
+ }, [binding])
111
+
112
+ const value = binding
113
+ ? boundValue
114
+ : (fs.controlled ? stringValue(fs.value) : localValue)
115
+
57
116
  const setValue = (next: string): void => {
117
+ if (binding) {
118
+ // Compute against current Y.Text contents (not the local ref) so:
119
+ // - first edit against empty Y.Text → `insert@0 <whole>` atomic
120
+ // populate (no separate seed op needed);
121
+ // - after a remote-applied update or server-resolve replace, the
122
+ // delta reflects the actual current shared state, not stale
123
+ // local bookkeeping.
124
+ const before = binding.read()
125
+ if (next !== before) {
126
+ const delta = computeDelta(before, next)
127
+ if (delta) binding.applyDelta(delta)
128
+ setBoundValue(next)
129
+ boundValueRef.current = next
130
+ }
131
+ fs.setValue(next)
132
+ fs.triggerLive(next)
133
+ return
134
+ }
58
135
  if (fs.controlled) { fs.setValue(next); fs.triggerLive(next) }
59
136
  else { setLocalValue(next); fs.triggerLive(next) }
60
137
  }
@@ -285,7 +362,27 @@ export function MarkdownInput({
285
362
  placeholder={placeholder}
286
363
  disabled={disabled}
287
364
  {...(fs.controlled
288
- ? { value, onChange: (e) => setValue(e.target.value) }
365
+ ? {
366
+ value,
367
+ onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => {
368
+ // Phase F.6 — when the binding is active and the user
369
+ // is mid-IME, paint locally and hold the delta until
370
+ // compositionend so we never emit ops for the
371
+ // intermediate composing chars.
372
+ if (binding && isComposingRef.current) {
373
+ setBoundValue(e.target.value)
374
+ return
375
+ }
376
+ setValue(e.target.value)
377
+ },
378
+ ...(binding ? {
379
+ onCompositionStart: () => { isComposingRef.current = true },
380
+ onCompositionEnd: (e: React.CompositionEvent<HTMLTextAreaElement>) => {
381
+ isComposingRef.current = false
382
+ setValue(e.currentTarget.value)
383
+ },
384
+ } : {}),
385
+ }
289
386
  : { defaultValue: initial, onChange: (e) => setLocalValue(e.target.value) })}
290
387
  onPaste={onPaste}
291
388
  onKeyDown={onKeyDown}