@portabletext/plugin-sdk-value 7.0.35 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {DocumentHandle} from '@sanity/sdk-react'
2
2
  import {JSX} from 'react'
3
+ import {Patch} from '@portabletext/editor'
3
4
  import {PortableTextBlock} from '@portabletext/editor'
4
5
 
5
6
  /**
@@ -18,6 +19,22 @@ declare type ValueSyncConfig = {
18
19
  getRemoteValue: () => PortableTextBlock[] | null | undefined
19
20
  pushValue: (value: PortableTextBlock[]) => void
20
21
  onRemoteValueChange: (callback: () => void) => () => void
22
+ /**
23
+ * Optional patch channel. When provided, operational patches from other
24
+ * clients are applied directly to the editor (in every state) instead of
25
+ * waiting for a whole-value diff, and local editor patches are pushed
26
+ * through `pushPatches`. The whole-value sync remains as a fallback for
27
+ * anything the patch channel cannot express.
28
+ */
29
+ onRemotePatches?: (
30
+ callback: (patches: Patch[]) => void,
31
+ ) => (() => void) | undefined
32
+ /**
33
+ * Pushes the editor's own operational patches to the remote store. May
34
+ * throw when a patch cannot be converted, in which case the plugin falls
35
+ * back to pushing the whole value.
36
+ */
37
+ pushPatches?: (patches: Patch[]) => void
21
38
  }
22
39
 
23
40
  /**
package/dist/index.js CHANGED
@@ -3,8 +3,9 @@ import { c } from "react/compiler-runtime";
3
3
  import { useEditor } from "@portabletext/editor";
4
4
  import { diffValue } from "@sanity/diff-patch";
5
5
  import { parsePath } from "@sanity/json-match";
6
- import { useEditDocument, useSanityInstance, getDocumentState } from "@sanity/sdk-react";
6
+ import { useEditDocument, useSanityInstance, useApplyDocumentActions, getDocumentState, subscribeDocumentEvents, editDocument } from "@sanity/sdk-react";
7
7
  import { useActorRef } from "@xstate/react";
8
+ import "react";
8
9
  import { fromCallback, setup } from "xstate";
9
10
  const ARRAYIFY_ERROR_MESSAGE = "Unexpected path format from diffValue output. Please report this issue.";
10
11
  function* getSegments(node) {
@@ -86,6 +87,225 @@ function convertPatches(patches) {
86
87
  }
87
88
  }));
88
89
  }
90
+ const STRINGIFY_ERROR_MESSAGE = "Unable to convert an editor patch path to a Sanity path expression.";
91
+ function stringifyPatchPath(path) {
92
+ let result = "";
93
+ for (const segment of path)
94
+ if (typeof segment == "string")
95
+ result = result === "" ? segment : `${result}.${segment}`;
96
+ else if (typeof segment == "number")
97
+ result = `${result}[${segment}]`;
98
+ else if (typeof segment == "object" && segment !== null && "_key" in segment)
99
+ result = `${result}[_key=="${segment._key}"]`;
100
+ else
101
+ throw new Error(STRINGIFY_ERROR_MESSAGE);
102
+ return result;
103
+ }
104
+ function prefixPathExpression(prefix, expression) {
105
+ return expression === "" ? prefix : expression.startsWith("[") ? `${prefix}${expression}` : `${prefix}.${expression}`;
106
+ }
107
+ function convertPatchesToSanity(patches, options) {
108
+ return patches.map((patch) => {
109
+ const pathExpression = prefixPathExpression(options.prefix, stringifyPatchPath(patch.path));
110
+ switch (patch.type) {
111
+ case "set":
112
+ return {
113
+ set: {
114
+ [pathExpression]: patch.value
115
+ }
116
+ };
117
+ case "setIfMissing":
118
+ return {
119
+ setIfMissing: {
120
+ [pathExpression]: patch.value
121
+ }
122
+ };
123
+ case "unset":
124
+ return patch.path.length === 0 ? {
125
+ set: {
126
+ [pathExpression]: []
127
+ }
128
+ } : {
129
+ unset: [pathExpression]
130
+ };
131
+ case "diffMatchPatch":
132
+ return {
133
+ diffMatchPatch: {
134
+ [pathExpression]: patch.value
135
+ }
136
+ };
137
+ case "inc":
138
+ return {
139
+ inc: {
140
+ [pathExpression]: patch.value
141
+ }
142
+ };
143
+ case "dec":
144
+ return {
145
+ dec: {
146
+ [pathExpression]: patch.value
147
+ }
148
+ };
149
+ case "insert":
150
+ return {
151
+ insert: {
152
+ [patch.position]: pathExpression,
153
+ items: patch.items
154
+ }
155
+ };
156
+ default:
157
+ throw new Error(STRINGIFY_ERROR_MESSAGE);
158
+ }
159
+ });
160
+ }
161
+ function segmentsEqual(a, b) {
162
+ return typeof a == "string" || typeof a == "number" ? a === b : typeof b == "string" || typeof b == "number" || Array.isArray(a) || Array.isArray(b) ? !1 : a._key === b._key;
163
+ }
164
+ function pathsEqual(a, b) {
165
+ return a.length === b.length && a.every((segment, index) => segmentsEqual(segment, b[index]));
166
+ }
167
+ function findSidecarArrayPath(path) {
168
+ let expectNode = !0;
169
+ for (let index = 0; index < path.length; index++) {
170
+ const segment = path[index];
171
+ if (typeof segment == "string")
172
+ expectNode = segment === "children";
173
+ else {
174
+ if (!expectNode)
175
+ return path.slice(0, index);
176
+ expectNode = !1;
177
+ }
178
+ }
179
+ return null;
180
+ }
181
+ function getValueAtPath(value, path) {
182
+ let current = value;
183
+ for (const segment of path) {
184
+ if (current == null)
185
+ return;
186
+ if (typeof segment == "number") {
187
+ if (!Array.isArray(current))
188
+ return;
189
+ current = current[segment < 0 ? current.length + segment : segment];
190
+ } else if (typeof segment == "string") {
191
+ if (typeof current != "object" || Array.isArray(current))
192
+ return;
193
+ current = current[segment];
194
+ } else {
195
+ if (Array.isArray(segment) || !Array.isArray(current))
196
+ return;
197
+ current = current.find((item) => typeof item == "object" && item !== null && !Array.isArray(item) && item._key === segment._key);
198
+ }
199
+ }
200
+ return current;
201
+ }
202
+ function toEngineSafePatches(patches, targetValue) {
203
+ const safe = [], sidecarPaths = [];
204
+ for (const patch of patches) {
205
+ const sidecarPath = findSidecarArrayPath(patch.path);
206
+ if (!sidecarPath) {
207
+ safe.push(patch);
208
+ continue;
209
+ }
210
+ sidecarPaths.some((existing) => pathsEqual(existing, sidecarPath)) || sidecarPaths.push(sidecarPath);
211
+ }
212
+ for (const sidecarPath of sidecarPaths) {
213
+ const value = getValueAtPath(targetValue, sidecarPath);
214
+ safe.push(value === void 0 ? {
215
+ type: "unset",
216
+ path: sidecarPath,
217
+ origin: "remote"
218
+ } : {
219
+ type: "set",
220
+ path: sidecarPath,
221
+ value,
222
+ origin: "remote"
223
+ });
224
+ }
225
+ return safe;
226
+ }
227
+ function toMergeableMarkDefsPatches(patches, getCurrentValue) {
228
+ return patches.flatMap((patch) => {
229
+ if (patch.type !== "set" || patch.path.at(-1) !== "markDefs" || !Array.isArray(patch.value))
230
+ return [patch];
231
+ const currentValue = getCurrentValue();
232
+ if (!currentValue)
233
+ return [patch];
234
+ const root = currentValue, storeMarkDefs = getValueAtPath(root, patch.path), storeBlock = getValueAtPath(root, patch.path.slice(0, -1));
235
+ if (!Array.isArray(storeMarkDefs) || typeof storeBlock != "object" || storeBlock === null)
236
+ return [patch];
237
+ const local = patch.value, store = storeMarkDefs;
238
+ if (local.some((item) => item._key === void 0))
239
+ return [patch];
240
+ const referencedKeys = new Set((storeBlock.children ?? []).flatMap((child) => child.marks ?? [])), storeByKey = new Map(store.map((item) => [item._key, item])), localKeys = new Set(local.map((item) => item._key)), origin = patch.origin, ops = [], inserted = local.filter((item) => !storeByKey.has(item._key));
241
+ inserted.length > 0 && ops.push({
242
+ type: "insert",
243
+ origin,
244
+ position: "after",
245
+ path: [...patch.path, -1],
246
+ items: inserted
247
+ });
248
+ for (const item of local) {
249
+ const existing = storeByKey.get(item._key);
250
+ existing && JSON.stringify(existing) !== JSON.stringify(item) && ops.push({
251
+ type: "set",
252
+ origin,
253
+ path: [...patch.path, {
254
+ _key: item._key
255
+ }],
256
+ value: item
257
+ });
258
+ }
259
+ for (const item of store)
260
+ item._key !== void 0 && !localKeys.has(item._key) && !referencedKeys.has(item._key) && ops.push({
261
+ type: "unset",
262
+ origin,
263
+ path: [...patch.path, {
264
+ _key: item._key
265
+ }]
266
+ });
267
+ return ops;
268
+ });
269
+ }
270
+ function canApplyToValue(patch, value) {
271
+ if (!value)
272
+ return !0;
273
+ const root = value;
274
+ switch (patch.type) {
275
+ // unset needs the node itself; insert needs the sibling at `path`;
276
+ // diffMatchPatch needs the existing string
277
+ case "unset":
278
+ case "insert":
279
+ case "diffMatchPatch":
280
+ return getValueAtPath(root, patch.path) !== void 0;
281
+ // set creates its target property, so only the parent must resolve
282
+ case "set":
283
+ return patch.path.length === 0 || getValueAtPath(root, patch.path.slice(0, -1)) !== void 0;
284
+ default:
285
+ return !0;
286
+ }
287
+ }
288
+ function scopeRemotePatches(patches, fieldPath) {
289
+ const prefix = arrayifyPath(fieldPath), converted = convertPatches(patches), scoped = [];
290
+ for (const patch of converted) {
291
+ const overlap = Math.min(patch.path.length, prefix.length);
292
+ let touchesField = !0;
293
+ for (let index = 0; index < overlap; index++)
294
+ if (!segmentsEqual(patch.path[index], prefix[index])) {
295
+ touchesField = !1;
296
+ break;
297
+ }
298
+ if (touchesField) {
299
+ if (patch.path.length <= prefix.length)
300
+ return null;
301
+ scoped.push({
302
+ ...patch,
303
+ path: patch.path.slice(prefix.length)
304
+ });
305
+ }
306
+ }
307
+ return scoped;
308
+ }
89
309
  function applySync({
90
310
  editor,
91
311
  getRemoteValue
@@ -93,12 +313,29 @@ function applySync({
93
313
  const remoteValue = getRemoteValue();
94
314
  if (!remoteValue)
95
315
  return;
96
- const snapshot = editor.getSnapshot().context.value, patches = convertPatches(diffValue(snapshot, remoteValue));
97
- patches.length && editor.send({
98
- type: "patches",
99
- patches,
100
- snapshot
101
- });
316
+ const snapshot = editor.getSnapshot().context.value;
317
+ let patches;
318
+ try {
319
+ patches = toEngineSafePatches(convertPatches(diffValue(snapshot, remoteValue)), remoteValue);
320
+ } catch {
321
+ editor.send({
322
+ type: "update value",
323
+ value: remoteValue
324
+ });
325
+ return;
326
+ }
327
+ if (patches.length) {
328
+ editor.send({
329
+ type: "patches",
330
+ patches,
331
+ snapshot
332
+ });
333
+ const valueAfterPatches = editor.getSnapshot().context.value;
334
+ diffValue(valueAfterPatches, remoteValue).length > 0 && editor.send({
335
+ type: "update value",
336
+ value: remoteValue
337
+ });
338
+ }
102
339
  }
103
340
  const listenToEditor = fromCallback(({
104
341
  sendBack,
@@ -111,7 +348,8 @@ const listenToEditor = fromCallback(({
111
348
  }), mutationSubscription = input.editor.on("mutation", (event) => {
112
349
  sendBack({
113
350
  type: "mutation flushed",
114
- value: event.value
351
+ value: event.value,
352
+ patches: event.patches
115
353
  });
116
354
  });
117
355
  return () => {
@@ -124,6 +362,14 @@ const listenToEditor = fromCallback(({
124
362
  sendBack({
125
363
  type: "remote value changed"
126
364
  });
365
+ })), listenToRemotePatches = fromCallback(({
366
+ sendBack,
367
+ input
368
+ }) => input.onRemotePatches?.((patches) => {
369
+ sendBack({
370
+ type: "remote patches received",
371
+ patches
372
+ });
127
373
  })), valueSyncMachine = setup({
128
374
  types: {
129
375
  context: {},
@@ -159,11 +405,25 @@ const listenToEditor = fromCallback(({
159
405
  getRemoteValue: context.getRemoteValue
160
406
  });
161
407
  });
408
+ },
409
+ "apply remote patches": ({
410
+ context,
411
+ event
412
+ }) => {
413
+ if (event.type !== "remote patches received")
414
+ return;
415
+ const snapshot = context.editor.getSnapshot().context.value, remoteValue = context.getRemoteValue(), patches = (remoteValue ? toEngineSafePatches(event.patches, remoteValue) : event.patches).filter((patch) => canApplyToValue(patch, snapshot));
416
+ patches.length !== 0 && context.editor.send({
417
+ type: "patches",
418
+ patches,
419
+ snapshot
420
+ });
162
421
  }
163
422
  },
164
423
  actors: {
165
424
  "listen to editor": listenToEditor,
166
- "listen to remote": listenToRemote
425
+ "listen to remote": listenToRemote,
426
+ "listen to remote patches": listenToRemotePatches
167
427
  }
168
428
  }).createMachine({
169
429
  id: "value sync",
@@ -172,7 +432,8 @@ const listenToEditor = fromCallback(({
172
432
  }) => ({
173
433
  editor: input.editor,
174
434
  getRemoteValue: input.getRemoteValue,
175
- onRemoteValueChange: input.onRemoteValueChange
435
+ onRemoteValueChange: input.onRemoteValueChange,
436
+ onRemotePatches: input.onRemotePatches
176
437
  }),
177
438
  entry: ["send initial value"],
178
439
  invoke: [{
@@ -189,7 +450,19 @@ const listenToEditor = fromCallback(({
189
450
  }) => ({
190
451
  onRemoteValueChange: context.onRemoteValueChange
191
452
  })
453
+ }, {
454
+ src: "listen to remote patches",
455
+ input: ({
456
+ context
457
+ }) => ({
458
+ onRemotePatches: context.onRemotePatches
459
+ })
192
460
  }],
461
+ // Operational patches from other clients apply immediately in every
462
+ // state; the editor merges them with any in-flight local changes.
463
+ // Application is best-effort, so each state also arranges a follow-up
464
+ // whole-value repair: immediately (deferred a microtask) when no local
465
+ // edits are in flight, or after the next mutation flush when they are.
193
466
  initial: "idle",
194
467
  states: {
195
468
  idle: {
@@ -199,6 +472,9 @@ const listenToEditor = fromCallback(({
199
472
  },
200
473
  "remote value changed": {
201
474
  actions: ["apply sync"]
475
+ },
476
+ "remote patches received": {
477
+ actions: ["apply remote patches", "defer then apply sync"]
202
478
  }
203
479
  }
204
480
  },
@@ -211,6 +487,10 @@ const listenToEditor = fromCallback(({
211
487
  },
212
488
  "remote value changed": {
213
489
  target: "pending sync"
490
+ },
491
+ "remote patches received": {
492
+ target: "pending sync",
493
+ actions: ["apply remote patches"]
214
494
  }
215
495
  }
216
496
  },
@@ -221,6 +501,9 @@ const listenToEditor = fromCallback(({
221
501
  },
222
502
  "remote value changed": {
223
503
  target: "idle"
504
+ },
505
+ "remote patches received": {
506
+ actions: ["apply remote patches", "defer then apply sync"]
224
507
  }
225
508
  }
226
509
  },
@@ -231,17 +514,26 @@ const listenToEditor = fromCallback(({
231
514
  target: "pushing to remote",
232
515
  actions: ["push to remote", "defer then apply sync"]
233
516
  },
234
- "remote value changed": {}
517
+ "remote value changed": {},
518
+ "remote patches received": {
519
+ actions: ["apply remote patches"]
520
+ }
235
521
  }
236
522
  }
237
523
  }
238
524
  });
525
+ function isRemotePatchesEvent(event) {
526
+ return event.type === "remote-patches";
527
+ }
528
+ function getPublishedDocumentId(id) {
529
+ return id.startsWith("drafts.") ? id.slice(7) : id.startsWith("versions.") ? id.split(".").slice(2).join(".") : id;
530
+ }
239
531
  function SDKValuePlugin(props) {
240
- const $ = c(9), {
532
+ const $ = c(20), {
241
533
  documentId,
242
534
  documentType,
243
535
  path
244
- } = props, setSdkValue = useEditDocument(props), instance = useSanityInstance(props);
536
+ } = props, setSdkValue = useEditDocument(props), instance = useSanityInstance(props), applyActions = useApplyDocumentActions();
245
537
  let t0;
246
538
  $[0] !== documentId || $[1] !== documentType || $[2] !== instance || $[3] !== path ? (t0 = getDocumentState(instance, {
247
539
  documentId,
@@ -253,34 +545,75 @@ function SDKValuePlugin(props) {
253
545
  subscribe
254
546
  } = t0;
255
547
  let t1;
256
- return $[5] !== getCurrent || $[6] !== setSdkValue || $[7] !== subscribe ? (t1 = /* @__PURE__ */ jsx(ValueSyncPlugin, { getRemoteValue: getCurrent, pushValue: setSdkValue, onRemoteValueChange: subscribe }), $[5] = getCurrent, $[6] = setSdkValue, $[7] = subscribe, $[8] = t1) : t1 = $[8], t1;
548
+ $[5] !== documentId || $[6] !== instance || $[7] !== path ? (t1 = (callback) => subscribeDocumentEvents(instance, {
549
+ eventHandler: (event) => {
550
+ const candidate = event;
551
+ if (!isRemotePatchesEvent(candidate) || candidate.origin !== "remote" || getPublishedDocumentId(candidate.documentId) !== getPublishedDocumentId(documentId))
552
+ return;
553
+ let patches;
554
+ try {
555
+ patches = scopeRemotePatches(candidate.patches, path);
556
+ } catch {
557
+ return;
558
+ }
559
+ patches && patches.length > 0 && callback(patches);
560
+ }
561
+ }), $[5] = documentId, $[6] = instance, $[7] = path, $[8] = t1) : t1 = $[8];
562
+ const onRemotePatches = t1;
563
+ let t2;
564
+ $[9] !== applyActions || $[10] !== documentId || $[11] !== documentType || $[12] !== path ? (t2 = (patches_0) => {
565
+ const sanityPatches = convertPatchesToSanity(patches_0, {
566
+ prefix: path
567
+ }), action = {
568
+ ...editDocument({
569
+ documentId,
570
+ documentType
571
+ }, sanityPatches),
572
+ preserveOperations: !0
573
+ };
574
+ applyActions(action);
575
+ }, $[9] = applyActions, $[10] = documentId, $[11] = documentType, $[12] = path, $[13] = t2) : t2 = $[13];
576
+ const pushPatches = t2;
577
+ let t3;
578
+ return $[14] !== getCurrent || $[15] !== onRemotePatches || $[16] !== pushPatches || $[17] !== setSdkValue || $[18] !== subscribe ? (t3 = /* @__PURE__ */ jsx(ValueSyncPlugin, { getRemoteValue: getCurrent, pushValue: setSdkValue, onRemoteValueChange: subscribe, onRemotePatches, pushPatches }), $[14] = getCurrent, $[15] = onRemotePatches, $[16] = pushPatches, $[17] = setSdkValue, $[18] = subscribe, $[19] = t3) : t3 = $[19], t3;
257
579
  }
258
580
  function ValueSyncPlugin(props) {
259
- const $ = c(6), {
581
+ const $ = c(8), {
260
582
  getRemoteValue,
261
583
  pushValue,
262
- onRemoteValueChange
584
+ onRemoteValueChange,
585
+ onRemotePatches,
586
+ pushPatches
263
587
  } = props, editor = useEditor();
264
588
  let t0;
265
- $[0] !== pushValue ? (t0 = valueSyncMachine.provide({
589
+ $[0] !== pushPatches || $[1] !== pushValue ? (t0 = valueSyncMachine.provide({
266
590
  actions: {
267
591
  "push to remote": (t12) => {
268
592
  const {
269
593
  context,
270
594
  event
271
595
  } = t12;
272
- event.type === "mutation flushed" && pushValue(event.value ?? context.editor.getSnapshot().context.value);
596
+ if (event.type === "mutation flushed") {
597
+ if (pushPatches && event.patches.length > 0)
598
+ try {
599
+ pushPatches(toMergeableMarkDefsPatches(event.patches, context.getRemoteValue));
600
+ return;
601
+ } catch {
602
+ }
603
+ pushValue(event.value ?? context.editor.getSnapshot().context.value);
604
+ }
273
605
  }
274
606
  }
275
- }), $[0] = pushValue, $[1] = t0) : t0 = $[1];
607
+ }), $[0] = pushPatches, $[1] = pushValue, $[2] = t0) : t0 = $[2];
276
608
  let t1;
277
- return $[2] !== editor || $[3] !== getRemoteValue || $[4] !== onRemoteValueChange ? (t1 = {
609
+ return $[3] !== editor || $[4] !== getRemoteValue || $[5] !== onRemotePatches || $[6] !== onRemoteValueChange ? (t1 = {
278
610
  input: {
279
611
  editor,
280
612
  getRemoteValue,
281
- onRemoteValueChange
613
+ onRemoteValueChange,
614
+ onRemotePatches
282
615
  }
283
- }, $[2] = editor, $[3] = getRemoteValue, $[4] = onRemoteValueChange, $[5] = t1) : t1 = $[5], useActorRef(t0, t1), null;
616
+ }, $[3] = editor, $[4] = getRemoteValue, $[5] = onRemotePatches, $[6] = onRemoteValueChange, $[7] = t1) : t1 = $[7], useActorRef(t0, t1), null;
284
617
  }
285
618
  export {
286
619
  SDKValuePlugin,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/plugin.sdk-value.tsx"],"sourcesContent":["import {\n useEditor,\n type Editor,\n type PortableTextBlock,\n type Patch as PtePatch,\n} from '@portabletext/editor'\nimport type {\n JSONValue,\n Path,\n PathSegment,\n InsertPatch as PteInsertPatch,\n} from '@portabletext/patches'\nimport {diffValue, type SanityPatchOperations} from '@sanity/diff-patch'\nimport {\n parsePath,\n type ExprNode,\n type PathNode,\n type SegmentNode,\n type ThisNode,\n} from '@sanity/json-match'\nimport {\n getDocumentState,\n useEditDocument,\n useSanityInstance,\n type DocumentHandle,\n} from '@sanity/sdk-react'\nimport {useActorRef} from '@xstate/react'\nimport {fromCallback, setup, type AnyEventObject} from 'xstate'\n\ntype InsertPatch = Required<Pick<SanityPatchOperations, 'insert'>>\n\nconst ARRAYIFY_ERROR_MESSAGE =\n 'Unexpected path format from diffValue output. Please report this issue.'\n\nfunction* getSegments(\n node: PathNode,\n): Generator<Exclude<SegmentNode, ThisNode>> {\n if (node.base) {\n yield* getSegments(node.base)\n }\n if (node.segment.type !== 'This') {\n yield node.segment\n }\n}\n\nfunction isKeyPath(node: ExprNode): node is PathNode {\n if (node.type !== 'Path') {\n return false\n }\n if (node.base) {\n return false\n }\n if (node.recursive) {\n return false\n }\n if (node.segment.type !== 'Identifier') {\n return false\n }\n return node.segment.name === '_key'\n}\n\nexport function arrayifyPath(pathExpr: string): Path {\n const node = parsePath(pathExpr)\n if (!node) {\n return []\n }\n if (node.type !== 'Path') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n\n return Array.from(getSegments(node)).map((segment): PathSegment => {\n if (segment.type === 'Identifier') {\n return segment.name\n }\n if (segment.type !== 'Subscript') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n if (segment.elements.length !== 1) {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n\n const [element] = segment.elements\n if (element.type === 'Number') {\n return element.value\n }\n\n if (element.type !== 'Comparison') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n if (element.operator !== '==') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n const keyPathNode = [element.left, element.right].find(isKeyPath)\n if (!keyPathNode) {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n const other = element.left === keyPathNode ? element.right : element.left\n if (other.type !== 'String') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n return {_key: other.value}\n })\n}\n\nexport function convertPatches(patches: SanityPatchOperations[]): PtePatch[] {\n return patches.flatMap((p) => {\n return Object.entries(p).flatMap(([type, values]): PtePatch[] => {\n const origin = 'remote'\n\n switch (type) {\n case 'set':\n case 'setIfMissing':\n case 'diffMatchPatch':\n case 'inc':\n case 'dec': {\n return Object.entries(values).map(\n ([pathExpr, value]) =>\n ({type, value, origin, path: arrayifyPath(pathExpr)}) as PtePatch,\n )\n }\n case 'unset': {\n if (!Array.isArray(values)) {\n return []\n }\n return values.map(arrayifyPath).map((path) => ({type, origin, path}))\n }\n case 'insert': {\n const {items, ...rest} = values as InsertPatch['insert']\n type InsertPosition = PteInsertPatch['position']\n const position = Object.keys(rest).at(0) as InsertPosition | undefined\n\n if (!position) {\n return []\n }\n const pathExpr = (rest as {[K in InsertPosition]: string})[position]\n const insertPatch: PteInsertPatch = {\n type,\n origin,\n position,\n path: arrayifyPath(pathExpr),\n items: items as JSONValue[],\n }\n\n return [insertPatch]\n }\n\n default: {\n return []\n }\n }\n })\n })\n}\n\nfunction applySync({\n editor,\n getRemoteValue,\n}: {\n editor: Editor\n getRemoteValue: () => PortableTextBlock[] | null | undefined\n}) {\n const remoteValue = getRemoteValue()\n\n if (!remoteValue) {\n return\n }\n\n const snapshot = editor.getSnapshot().context.value\n const patches = convertPatches(diffValue(snapshot, remoteValue))\n\n if (patches.length) {\n editor.send({type: 'patches', patches, snapshot})\n }\n}\n\nconst listenToEditor = fromCallback<AnyEventObject, {editor: Editor}>(\n ({sendBack, input}) => {\n const patchSubscription = input.editor.on('patch', () => {\n sendBack({type: 'patch emitted'})\n })\n\n const mutationSubscription = input.editor.on('mutation', (event) => {\n sendBack({type: 'mutation flushed', value: event.value})\n })\n\n return () => {\n patchSubscription.unsubscribe()\n mutationSubscription.unsubscribe()\n }\n },\n)\n\nconst listenToRemote = fromCallback<\n AnyEventObject,\n {onRemoteValueChange: ValueSyncConfig['onRemoteValueChange']}\n>(({sendBack, input}) => {\n return input.onRemoteValueChange(() => {\n sendBack({type: 'remote value changed'})\n })\n})\n\nconst valueSyncMachine = setup({\n types: {\n context: {} as {\n editor: Editor\n getRemoteValue: ValueSyncConfig['getRemoteValue']\n onRemoteValueChange: ValueSyncConfig['onRemoteValueChange']\n },\n input: {} as {\n editor: Editor\n getRemoteValue: ValueSyncConfig['getRemoteValue']\n onRemoteValueChange: ValueSyncConfig['onRemoteValueChange']\n },\n events: {} as\n | {type: 'patch emitted'}\n | {type: 'mutation flushed'; value: PortableTextBlock[] | undefined}\n | {type: 'remote value changed'},\n },\n actions: {\n 'send initial value': ({context}) => {\n context.editor.send({\n type: 'update value',\n value: context.getRemoteValue() ?? [],\n })\n },\n 'push to remote': () => {\n throw new Error('push to remote must be provided via .provide()')\n },\n 'apply sync': ({context}) => {\n applySync({\n editor: context.editor,\n getRemoteValue: context.getRemoteValue,\n })\n },\n 'defer then apply sync': ({context}) => {\n queueMicrotask(() => {\n applySync({\n editor: context.editor,\n getRemoteValue: context.getRemoteValue,\n })\n })\n },\n },\n actors: {\n 'listen to editor': listenToEditor,\n 'listen to remote': listenToRemote,\n },\n}).createMachine({\n id: 'value sync',\n context: ({input}) => ({\n editor: input.editor,\n getRemoteValue: input.getRemoteValue,\n onRemoteValueChange: input.onRemoteValueChange,\n }),\n entry: ['send initial value'],\n invoke: [\n {\n src: 'listen to editor',\n input: ({context}) => ({editor: context.editor}),\n },\n {\n src: 'listen to remote',\n input: ({context}) => ({\n onRemoteValueChange: context.onRemoteValueChange,\n }),\n },\n ],\n initial: 'idle',\n states: {\n 'idle': {\n on: {\n 'patch emitted': {\n target: 'local write',\n },\n 'remote value changed': {\n actions: ['apply sync'],\n },\n },\n },\n 'local write': {\n on: {\n 'patch emitted': {},\n 'mutation flushed': {\n target: 'pushing to remote',\n actions: ['push to remote'],\n },\n 'remote value changed': {\n target: 'pending sync',\n },\n },\n },\n 'pushing to remote': {\n on: {\n 'patch emitted': {\n target: 'local write',\n },\n 'remote value changed': {\n target: 'idle',\n },\n },\n },\n 'pending sync': {\n on: {\n 'patch emitted': {},\n 'mutation flushed': {\n target: 'pushing to remote',\n actions: ['push to remote', 'defer then apply sync'],\n },\n 'remote value changed': {},\n },\n },\n },\n})\n\ninterface SDKValuePluginProps extends DocumentHandle {\n path: string\n}\n\n/**\n * @public\n */\nexport function SDKValuePlugin(props: SDKValuePluginProps) {\n const {documentId, documentType, path} = props\n const setSdkValue = useEditDocument(props)\n const instance = useSanityInstance(props)\n\n const handle = {documentId, documentType, path}\n const {getCurrent, subscribe} = getDocumentState<PortableTextBlock[]>(\n instance,\n handle,\n )\n\n return (\n <ValueSyncPlugin\n getRemoteValue={getCurrent}\n pushValue={setSdkValue}\n onRemoteValueChange={subscribe}\n />\n )\n}\n\n/**\n * @internal\n */\ntype ValueSyncConfig = {\n getRemoteValue: () => PortableTextBlock[] | null | undefined\n pushValue: (value: PortableTextBlock[]) => void\n onRemoteValueChange: (callback: () => void) => () => void\n}\n\n/**\n * NOTE: You are probably looking for SDKValuePlugin instead of this.\n * This is a lower-level plugin that only handles syncing the value\n * between the editor and a remote source. It does not know anything\n * about Sanity documents or how to fetch/update them.\n *\n * May be removed in the future, do not rely on this directly.\n *\n * @internal\n */\nexport function ValueSyncPlugin(props: ValueSyncConfig) {\n const {getRemoteValue, pushValue, onRemoteValueChange} = props\n const editor = useEditor()\n\n useActorRef(\n valueSyncMachine.provide({\n actions: {\n 'push to remote': ({context, event}) => {\n if (event.type !== 'mutation flushed') {\n return\n }\n\n pushValue(event.value ?? context.editor.getSnapshot().context.value)\n },\n },\n }),\n {\n input: {\n editor,\n getRemoteValue,\n onRemoteValueChange,\n },\n },\n )\n\n return null\n}\n"],"names":["ARRAYIFY_ERROR_MESSAGE","getSegments","node","base","segment","type","isKeyPath","recursive","name","arrayifyPath","pathExpr","parsePath","Error","Array","from","map","elements","length","element","value","operator","keyPathNode","left","right","find","other","_key","convertPatches","patches","flatMap","p","Object","entries","values","origin","path","isArray","items","rest","position","keys","at","applySync","editor","getRemoteValue","remoteValue","snapshot","getSnapshot","context","diffValue","send","listenToEditor","fromCallback","sendBack","input","patchSubscription","on","mutationSubscription","event","unsubscribe","listenToRemote","onRemoteValueChange","valueSyncMachine","setup","types","events","actions","send initial value","push to remote","apply sync","defer then apply sync","queueMicrotask","actors","createMachine","id","entry","invoke","src","initial","states","target","SDKValuePlugin","props","$","_c","documentId","documentType","setSdkValue","useEditDocument","instance","useSanityInstance","t0","getDocumentState","getCurrent","subscribe","t1","ValueSyncPlugin","pushValue","useEditor","provide","useActorRef"],"mappings":";;;;;;;;AA+BA,MAAMA,yBACJ;AAEF,UAAUC,YACRC,MAC2C;AACvCA,OAAKC,SACP,OAAOF,YAAYC,KAAKC,IAAI,IAE1BD,KAAKE,QAAQC,SAAS,WACxB,MAAMH,KAAKE;AAEf;AAEA,SAASE,UAAUJ,MAAkC;AAUnD,SATIA,KAAKG,SAAS,UAGdH,KAAKC,QAGLD,KAAKK,aAGLL,KAAKE,QAAQC,SAAS,eACjB,KAEFH,KAAKE,QAAQI,SAAS;AAC/B;AAEO,SAASC,aAAaC,UAAwB;AACnD,QAAMR,OAAOS,UAAUD,QAAQ;AAC/B,MAAI,CAACR;AACH,WAAO,CAAA;AAET,MAAIA,KAAKG,SAAS;AAChB,UAAM,I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CAAAA,QAAA;AAAC,cAAA;AAAA,UAAA/C;AAAAA,UAAAU;AAAAA,QAAAA,IAAAqC;AACbrC,cAAKrD,SAAU,sBAInB4F,UAAUvC,MAAKvC,SAAU6B,QAAOL,OAAOI,cAAcC,QAAQ7B,KAAM;AAAA,MAAC;AAAA,IAAA;AAAA,EAExE,CACD,GAACgE,OAAAc,WAAAd,OAAAQ,MAAAA,KAAAR,EAAA,CAAA;AAAA,MAAAY;AAAA,SAAAZ,EAAA,CAAA,MAAAxC,UAAAwC,SAAAvC,kBAAAuC,EAAA,CAAA,MAAAtB,uBACFkC,KAAA;AAAA,IAAAzC,OACS;AAAA,MAAAX;AAAAA,MAAAC;AAAAA,MAAAiB;AAAAA,IAAAA;AAAAA,EAIP,GACDsB,OAAAxC,QAAAwC,OAAAvC,gBAAAuC,OAAAtB,qBAAAsB,OAAAY,MAAAA,KAAAZ,EAAA,CAAA,GAlBHiB,YACET,IAWAI,EAOF,GAEO;AAAI;"}
1
+ {"version":3,"file":"index.js","sources":["../src/plugin.sdk-value.tsx"],"sourcesContent":["import {\n useEditor,\n type Editor,\n type PortableTextBlock,\n type Patch as PtePatch,\n} from '@portabletext/editor'\nimport type {\n JSONValue,\n Path,\n PathSegment,\n InsertPatch as PteInsertPatch,\n} from '@portabletext/patches'\nimport {diffValue, type SanityPatchOperations} from '@sanity/diff-patch'\nimport {\n parsePath,\n type ExprNode,\n type PathNode,\n type SegmentNode,\n type ThisNode,\n} from '@sanity/json-match'\nimport {\n editDocument,\n getDocumentState,\n subscribeDocumentEvents,\n useApplyDocumentActions,\n useEditDocument,\n useSanityInstance,\n type DocumentHandle,\n type EditDocumentAction,\n} from '@sanity/sdk-react'\nimport {useActorRef} from '@xstate/react'\nimport {useCallback} from 'react'\nimport {fromCallback, setup, type AnyEventObject} from 'xstate'\n\ntype InsertPatch = Required<Pick<SanityPatchOperations, 'insert'>>\n\nconst ARRAYIFY_ERROR_MESSAGE =\n 'Unexpected path format from diffValue output. Please report this issue.'\n\nfunction* getSegments(\n node: PathNode,\n): Generator<Exclude<SegmentNode, ThisNode>> {\n if (node.base) {\n yield* getSegments(node.base)\n }\n if (node.segment.type !== 'This') {\n yield node.segment\n }\n}\n\nfunction isKeyPath(node: ExprNode): node is PathNode {\n if (node.type !== 'Path') {\n return false\n }\n if (node.base) {\n return false\n }\n if (node.recursive) {\n return false\n }\n if (node.segment.type !== 'Identifier') {\n return false\n }\n return node.segment.name === '_key'\n}\n\nexport function arrayifyPath(pathExpr: string): Path {\n const node = parsePath(pathExpr)\n if (!node) {\n return []\n }\n if (node.type !== 'Path') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n\n return Array.from(getSegments(node)).map((segment): PathSegment => {\n if (segment.type === 'Identifier') {\n return segment.name\n }\n if (segment.type !== 'Subscript') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n if (segment.elements.length !== 1) {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n\n const [element] = segment.elements\n if (element.type === 'Number') {\n return element.value\n }\n\n if (element.type !== 'Comparison') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n if (element.operator !== '==') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n const keyPathNode = [element.left, element.right].find(isKeyPath)\n if (!keyPathNode) {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n const other = element.left === keyPathNode ? element.right : element.left\n if (other.type !== 'String') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n return {_key: other.value}\n })\n}\n\nexport function convertPatches(patches: SanityPatchOperations[]): PtePatch[] {\n return patches.flatMap((p) => {\n return Object.entries(p).flatMap(([type, values]): PtePatch[] => {\n const origin = 'remote'\n\n switch (type) {\n case 'set':\n case 'setIfMissing':\n case 'diffMatchPatch':\n case 'inc':\n case 'dec': {\n return Object.entries(values).map(\n ([pathExpr, value]) =>\n ({type, value, origin, path: arrayifyPath(pathExpr)}) as PtePatch,\n )\n }\n case 'unset': {\n if (!Array.isArray(values)) {\n return []\n }\n return values.map(arrayifyPath).map((path) => ({type, origin, path}))\n }\n case 'insert': {\n const {items, ...rest} = values as InsertPatch['insert']\n type InsertPosition = PteInsertPatch['position']\n const position = Object.keys(rest).at(0) as InsertPosition | undefined\n\n if (!position) {\n return []\n }\n const pathExpr = (rest as {[K in InsertPosition]: string})[position]\n const insertPatch: PteInsertPatch = {\n type,\n origin,\n position,\n path: arrayifyPath(pathExpr),\n items: items as JSONValue[],\n }\n\n return [insertPatch]\n }\n\n default: {\n return []\n }\n }\n })\n })\n}\n\nconst STRINGIFY_ERROR_MESSAGE =\n 'Unable to convert an editor patch path to a Sanity path expression.'\n\n/**\n * Converts a Portable Text Editor patch path (an array of segments) into a\n * Sanity json-match path expression. The inverse of `arrayifyPath`.\n *\n * @internal\n */\nexport function stringifyPatchPath(path: Path): string {\n let result = ''\n for (const segment of path) {\n if (typeof segment === 'string') {\n result = result === '' ? segment : `${result}.${segment}`\n } else if (typeof segment === 'number') {\n result = `${result}[${segment}]`\n } else if (\n typeof segment === 'object' &&\n segment !== null &&\n '_key' in segment\n ) {\n result = `${result}[_key==\"${segment._key}\"]`\n } else {\n throw new Error(STRINGIFY_ERROR_MESSAGE)\n }\n }\n return result\n}\n\nfunction prefixPathExpression(prefix: string, expression: string): string {\n if (expression === '') {\n return prefix\n }\n return expression.startsWith('[')\n ? `${prefix}${expression}`\n : `${prefix}.${expression}`\n}\n\n/**\n * `SanityPatchOperations` from `@sanity/diff-patch` only covers the\n * operations `diffValue` emits; the editor can additionally produce these.\n */\ntype SanityPatchOperationsWithExtras = SanityPatchOperations & {\n setIfMissing?: {[path: string]: unknown}\n inc?: {[path: string]: number}\n dec?: {[path: string]: number}\n}\n\n/**\n * Converts Portable Text Editor patches into Sanity patch operations rooted\n * at the given document field path. Throws if a patch cannot be converted;\n * callers should fall back to pushing the whole value.\n *\n * @internal\n */\nexport function convertPatchesToSanity(\n patches: PtePatch[],\n options: {prefix: string},\n): SanityPatchOperationsWithExtras[] {\n return patches.map((patch): SanityPatchOperationsWithExtras => {\n const pathExpression = prefixPathExpression(\n options.prefix,\n stringifyPatchPath(patch.path),\n )\n\n switch (patch.type) {\n case 'set':\n return {set: {[pathExpression]: patch.value}}\n case 'setIfMissing':\n return {setIfMissing: {[pathExpression]: patch.value}}\n case 'unset':\n // The editor unsets the whole field when it becomes empty. Write an\n // empty array instead: unsetting the field would leave other clients\n // unable to reconcile against it (their remote value disappears).\n if (patch.path.length === 0) {\n return {set: {[pathExpression]: []}}\n }\n return {unset: [pathExpression]}\n case 'diffMatchPatch':\n return {diffMatchPatch: {[pathExpression]: patch.value}}\n case 'inc':\n return {inc: {[pathExpression]: patch.value as number}}\n case 'dec':\n return {dec: {[pathExpression]: patch.value as number}}\n case 'insert':\n return {\n insert: {\n [patch.position]: pathExpression,\n items: patch.items,\n } as SanityPatchOperations['insert'],\n }\n default:\n throw new Error(STRINGIFY_ERROR_MESSAGE)\n }\n })\n}\n\nfunction segmentsEqual(a: PathSegment, b: PathSegment): boolean {\n if (typeof a === 'string' || typeof a === 'number') {\n return a === b\n }\n if (typeof b === 'string' || typeof b === 'number') {\n return false\n }\n if (Array.isArray(a) || Array.isArray(b)) {\n return false\n }\n return a._key === b._key\n}\n\nfunction pathsEqual(a: Path, b: Path): boolean {\n return (\n a.length === b.length &&\n a.every((segment, index) => segmentsEqual(segment, b[index]))\n )\n}\n\n/**\n * The editor engine can only resolve keyed/indexed segments against the\n * root block array and (possibly nested) `children` arrays. Operations\n * that address items of any other array, e.g. a block's `markDefs` or a\n * span's `marks`, misapply. When a patch path enters such a sidecar\n * array, this returns the path of the array itself so callers can fall\n * back to replacing the whole property; returns `null` for paths the\n * engine can apply.\n *\n * @internal\n */\nexport function findSidecarArrayPath(path: Path): Path | null {\n // a keyed/numeric segment is only resolvable first (root block array)\n // or directly after a `children` property\n let expectNode = true\n for (let index = 0; index < path.length; index++) {\n const segment = path[index]\n if (typeof segment === 'string') {\n expectNode = segment === 'children'\n } else {\n if (!expectNode) {\n return path.slice(0, index)\n }\n expectNode = false\n }\n }\n return null\n}\n\nfunction getValueAtPath(value: JSONValue, path: Path): JSONValue | undefined {\n let current: JSONValue | undefined = value\n for (const segment of path) {\n if (current === null || current === undefined) {\n return undefined\n }\n if (typeof segment === 'number') {\n if (!Array.isArray(current)) {\n return undefined\n }\n current = current[segment < 0 ? current.length + segment : segment]\n } else if (typeof segment === 'string') {\n if (typeof current !== 'object' || Array.isArray(current)) {\n return undefined\n }\n current = (current as {[key: string]: JSONValue})[segment]\n } else if (Array.isArray(segment)) {\n // index tuples address ranges, not single values\n return undefined\n } else {\n if (!Array.isArray(current)) {\n return undefined\n }\n current = current.find(\n (item) =>\n typeof item === 'object' &&\n item !== null &&\n !Array.isArray(item) &&\n (item as {_key?: unknown})._key === segment._key,\n )\n }\n }\n return current\n}\n\n/**\n * Converts a target-value diff into patches the editor engine can apply.\n * Patches addressing items inside sidecar arrays are coalesced into whole\n * `set`s (or `unset`s) of the owning property, taken from the target\n * value.\n *\n * @internal\n */\nexport function toEngineSafePatches(\n patches: PtePatch[],\n targetValue: PortableTextBlock[],\n): PtePatch[] {\n const safe: PtePatch[] = []\n const sidecarPaths: Path[] = []\n\n for (const patch of patches) {\n const sidecarPath = findSidecarArrayPath(patch.path)\n if (!sidecarPath) {\n safe.push(patch)\n continue\n }\n if (!sidecarPaths.some((existing) => pathsEqual(existing, sidecarPath))) {\n sidecarPaths.push(sidecarPath)\n }\n }\n\n for (const sidecarPath of sidecarPaths) {\n const value = getValueAtPath(\n targetValue as unknown as JSONValue,\n sidecarPath,\n )\n safe.push(\n value === undefined\n ? {type: 'unset', path: sidecarPath, origin: 'remote'}\n : {type: 'set', path: sidecarPath, value, origin: 'remote'},\n )\n }\n\n return safe\n}\n\n/**\n * The editor writes `markDefs` as whole-array `set`s. Two clients\n * formatting the same block concurrently then overwrite each other's\n * arrays at the server (last writer wins) while both clients' span\n * `marks` references survive, stranding marks without definitions.\n * Outgoing `markDefs` sets are therefore decomposed into item-level\n * operations against the store (server truth): new definitions insert,\n * changed definitions set by key, and removed definitions unset by key,\n * except definitions the store's own spans still reference (a diverged\n * client's normalizer prunes those spuriously; a later, converged flush\n * removes them for real). Item-keyed operations merge at the server\n * instead of overwriting.\n *\n * @internal\n */\nexport function toMergeableMarkDefsPatches(\n patches: PtePatch[],\n getCurrentValue: () => PortableTextBlock[] | null | undefined,\n): PtePatch[] {\n return patches.flatMap((patch): PtePatch[] => {\n if (\n patch.type !== 'set' ||\n patch.path.at(-1) !== 'markDefs' ||\n !Array.isArray(patch.value)\n ) {\n return [patch]\n }\n const currentValue = getCurrentValue()\n if (!currentValue) {\n return [patch]\n }\n const root = currentValue as unknown as JSONValue\n const storeMarkDefs = getValueAtPath(root, patch.path)\n const storeBlock = getValueAtPath(root, patch.path.slice(0, -1))\n if (\n !Array.isArray(storeMarkDefs) ||\n typeof storeBlock !== 'object' ||\n storeBlock === null\n ) {\n return [patch]\n }\n\n const local = patch.value as Array<{_key?: string}>\n const store = storeMarkDefs as Array<{_key?: string}>\n if (local.some((item) => item._key === undefined)) {\n return [patch]\n }\n\n const referencedKeys = new Set(\n (\n (storeBlock as {children?: Array<{marks?: string[]}>}).children ?? []\n ).flatMap((child) => child.marks ?? []),\n )\n const storeByKey = new Map(store.map((item) => [item._key, item]))\n const localKeys = new Set(local.map((item) => item._key))\n const origin = patch.origin\n\n const ops: PtePatch[] = []\n\n const inserted = local.filter((item) => !storeByKey.has(item._key))\n if (inserted.length > 0) {\n ops.push({\n type: 'insert',\n origin,\n position: 'after',\n path: [...patch.path, -1],\n items: inserted as JSONValue[],\n })\n }\n\n for (const item of local) {\n const existing = storeByKey.get(item._key)\n if (existing && JSON.stringify(existing) !== JSON.stringify(item)) {\n ops.push({\n type: 'set',\n origin,\n path: [...patch.path, {_key: item._key as string}],\n value: item as JSONValue,\n })\n }\n }\n\n for (const item of store) {\n if (\n item._key !== undefined &&\n !localKeys.has(item._key) &&\n !referencedKeys.has(item._key)\n ) {\n ops.push({\n type: 'unset',\n origin,\n path: [...patch.path, {_key: item._key}],\n })\n }\n }\n\n return ops\n })\n}\n\n/**\n * Whether a remote patch can resolve against the given editor value.\n * Concurrent edits routinely produce operations addressing nodes another\n * client has already removed or not yet created; sending those into the\n * engine fails loudly (console errors) before being skipped. Callers drop\n * unresolvable patches up front and rely on the follow-up repair sync to\n * converge instead.\n *\n * @internal\n */\nexport function canApplyToValue(\n patch: PtePatch,\n value: PortableTextBlock[] | undefined,\n): boolean {\n if (!value) {\n return true\n }\n const root = value as unknown as JSONValue\n switch (patch.type) {\n // unset needs the node itself; insert needs the sibling at `path`;\n // diffMatchPatch needs the existing string\n case 'unset':\n case 'insert':\n case 'diffMatchPatch':\n return getValueAtPath(root, patch.path) !== undefined\n // set creates its target property, so only the parent must resolve\n case 'set':\n return (\n patch.path.length === 0 ||\n getValueAtPath(root, patch.path.slice(0, -1)) !== undefined\n )\n default:\n return true\n }\n}\n\n/**\n * Scopes document-rooted Sanity patches to the given field path, returning\n * field-relative Portable Text Editor patches. Patches outside the field are\n * dropped. Returns `null` when the field (or an ancestor of it) is replaced\n * wholesale, in which case the caller should fall back to a full value sync.\n * Throws when a path expression cannot be converted.\n *\n * @internal\n */\nexport function scopeRemotePatches(\n patches: SanityPatchOperations[],\n fieldPath: string,\n): PtePatch[] | null {\n const prefix = arrayifyPath(fieldPath)\n const converted = convertPatches(patches)\n const scoped: PtePatch[] = []\n\n for (const patch of converted) {\n const overlap = Math.min(patch.path.length, prefix.length)\n let touchesField = true\n for (let index = 0; index < overlap; index++) {\n if (!segmentsEqual(patch.path[index], prefix[index])) {\n touchesField = false\n break\n }\n }\n if (!touchesField) {\n continue\n }\n if (patch.path.length <= prefix.length) {\n // the patch targets the field itself or an ancestor of it, which\n // cannot be expressed as a field-relative operation\n return null\n }\n scoped.push({...patch, path: patch.path.slice(prefix.length)})\n }\n\n return scoped\n}\n\nfunction applySync({\n editor,\n getRemoteValue,\n}: {\n editor: Editor\n getRemoteValue: () => PortableTextBlock[] | null | undefined\n}) {\n const remoteValue = getRemoteValue()\n\n if (!remoteValue) {\n return\n }\n\n const snapshot = editor.getSnapshot().context.value\n\n let patches: PtePatch[]\n try {\n patches = toEngineSafePatches(\n convertPatches(diffValue(snapshot, remoteValue)),\n remoteValue,\n )\n } catch {\n // diffValue can emit path shapes the converter does not understand\n // (e.g. array slices when multiple items are dropped). Fall back to\n // the full value sync machinery rather than leaving the editor\n // diverged.\n editor.send({type: 'update value', value: remoteValue})\n return\n }\n\n if (patches.length) {\n editor.send({type: 'patches', patches, snapshot})\n\n // Patch application is best-effort: the editor skips operations it\n // cannot resolve against its current tree, and concurrent edits to the\n // same range produce keyed operations whose targets no longer exist\n // locally. When the editor is still diverged after the diff-based\n // repair, escalate to the full value sync machinery, which reconciles\n // arbitrary divergence block by block.\n const valueAfterPatches = editor.getSnapshot().context.value\n if (diffValue(valueAfterPatches, remoteValue).length > 0) {\n editor.send({type: 'update value', value: remoteValue})\n }\n }\n}\n\nconst listenToEditor = fromCallback<AnyEventObject, {editor: Editor}>(\n ({sendBack, input}) => {\n const patchSubscription = input.editor.on('patch', () => {\n sendBack({type: 'patch emitted'})\n })\n\n const mutationSubscription = input.editor.on('mutation', (event) => {\n sendBack({\n type: 'mutation flushed',\n value: event.value,\n patches: event.patches,\n })\n })\n\n return () => {\n patchSubscription.unsubscribe()\n mutationSubscription.unsubscribe()\n }\n },\n)\n\nconst listenToRemote = fromCallback<\n AnyEventObject,\n {onRemoteValueChange: ValueSyncConfig['onRemoteValueChange']}\n>(({sendBack, input}) => {\n return input.onRemoteValueChange(() => {\n sendBack({type: 'remote value changed'})\n })\n})\n\nconst listenToRemotePatches = fromCallback<\n AnyEventObject,\n {onRemotePatches: ValueSyncConfig['onRemotePatches']}\n>(({sendBack, input}) => {\n return input.onRemotePatches?.((patches) => {\n sendBack({type: 'remote patches received', patches})\n })\n})\n\nconst valueSyncMachine = setup({\n types: {\n context: {} as {\n editor: Editor\n getRemoteValue: ValueSyncConfig['getRemoteValue']\n onRemoteValueChange: ValueSyncConfig['onRemoteValueChange']\n onRemotePatches: ValueSyncConfig['onRemotePatches']\n },\n input: {} as {\n editor: Editor\n getRemoteValue: ValueSyncConfig['getRemoteValue']\n onRemoteValueChange: ValueSyncConfig['onRemoteValueChange']\n onRemotePatches: ValueSyncConfig['onRemotePatches']\n },\n events: {} as\n | {type: 'patch emitted'}\n | {\n type: 'mutation flushed'\n value: PortableTextBlock[] | undefined\n patches: PtePatch[]\n }\n | {type: 'remote value changed'}\n | {type: 'remote patches received'; patches: PtePatch[]},\n },\n actions: {\n 'send initial value': ({context}) => {\n context.editor.send({\n type: 'update value',\n value: context.getRemoteValue() ?? [],\n })\n },\n 'push to remote': () => {\n throw new Error('push to remote must be provided via .provide()')\n },\n 'apply sync': ({context}) => {\n applySync({\n editor: context.editor,\n getRemoteValue: context.getRemoteValue,\n })\n },\n 'defer then apply sync': ({context}) => {\n queueMicrotask(() => {\n applySync({\n editor: context.editor,\n getRemoteValue: context.getRemoteValue,\n })\n })\n },\n 'apply remote patches': ({context, event}) => {\n if (event.type !== 'remote patches received') {\n return\n }\n const snapshot = context.editor.getSnapshot().context.value\n // The store already reflects this transaction, so it can serve as\n // the target for coalescing sidecar-array item operations (which\n // the engine misroutes) into whole-property sets.\n const remoteValue = context.getRemoteValue()\n const coalesced = remoteValue\n ? toEngineSafePatches(event.patches, remoteValue)\n : event.patches\n const patches = coalesced.filter((patch) =>\n canApplyToValue(patch, snapshot),\n )\n if (patches.length === 0) {\n return\n }\n context.editor.send({type: 'patches', patches, snapshot})\n },\n },\n actors: {\n 'listen to editor': listenToEditor,\n 'listen to remote': listenToRemote,\n 'listen to remote patches': listenToRemotePatches,\n },\n}).createMachine({\n id: 'value sync',\n context: ({input}) => ({\n editor: input.editor,\n getRemoteValue: input.getRemoteValue,\n onRemoteValueChange: input.onRemoteValueChange,\n onRemotePatches: input.onRemotePatches,\n }),\n entry: ['send initial value'],\n invoke: [\n {\n src: 'listen to editor',\n input: ({context}) => ({editor: context.editor}),\n },\n {\n src: 'listen to remote',\n input: ({context}) => ({\n onRemoteValueChange: context.onRemoteValueChange,\n }),\n },\n {\n src: 'listen to remote patches',\n input: ({context}) => ({\n onRemotePatches: context.onRemotePatches,\n }),\n },\n ],\n // Operational patches from other clients apply immediately in every\n // state; the editor merges them with any in-flight local changes.\n // Application is best-effort, so each state also arranges a follow-up\n // whole-value repair: immediately (deferred a microtask) when no local\n // edits are in flight, or after the next mutation flush when they are.\n initial: 'idle',\n states: {\n 'idle': {\n on: {\n 'patch emitted': {\n target: 'local write',\n },\n 'remote value changed': {\n actions: ['apply sync'],\n },\n 'remote patches received': {\n actions: ['apply remote patches', 'defer then apply sync'],\n },\n },\n },\n 'local write': {\n on: {\n 'patch emitted': {},\n 'mutation flushed': {\n target: 'pushing to remote',\n actions: ['push to remote'],\n },\n 'remote value changed': {\n target: 'pending sync',\n },\n 'remote patches received': {\n target: 'pending sync',\n actions: ['apply remote patches'],\n },\n },\n },\n 'pushing to remote': {\n on: {\n 'patch emitted': {\n target: 'local write',\n },\n 'remote value changed': {\n target: 'idle',\n },\n 'remote patches received': {\n actions: ['apply remote patches', 'defer then apply sync'],\n },\n },\n },\n 'pending sync': {\n on: {\n 'patch emitted': {},\n 'mutation flushed': {\n target: 'pushing to remote',\n actions: ['push to remote', 'defer then apply sync'],\n },\n 'remote value changed': {},\n 'remote patches received': {\n actions: ['apply remote patches'],\n },\n },\n },\n },\n})\n\ninterface SDKValuePluginProps extends DocumentHandle {\n path: string\n}\n\n/**\n * The shape of the `remote-patches` document event emitted by\n * `@sanity/sdk` >= 2.17. Declared structurally so the plugin stays\n * compatible with older SDK typings until its dependency is bumped.\n */\ntype RemotePatchesDocumentEvent = {\n type: 'remote-patches'\n documentId: string\n transactionId: string\n timestamp: string\n previousRev?: string\n patches: SanityPatchOperations[]\n origin: 'local' | 'remote'\n}\n\nfunction isRemotePatchesEvent(event: {\n type: string\n}): event is RemotePatchesDocumentEvent {\n return event.type === 'remote-patches'\n}\n\nfunction getPublishedDocumentId(id: string): string {\n if (id.startsWith('drafts.')) {\n return id.slice('drafts.'.length)\n }\n if (id.startsWith('versions.')) {\n return id.split('.').slice(2).join('.')\n }\n return id\n}\n\n/**\n * @public\n */\nexport function SDKValuePlugin(props: SDKValuePluginProps) {\n const {documentId, documentType, path} = props\n const setSdkValue = useEditDocument(props)\n const instance = useSanityInstance(props)\n const applyActions = useApplyDocumentActions()\n\n const handle = {documentId, documentType, path}\n const {getCurrent, subscribe} = getDocumentState<PortableTextBlock[]>(\n instance,\n handle,\n )\n\n const onRemotePatches = useCallback(\n (callback: (patches: PtePatch[]) => void) => {\n return subscribeDocumentEvents(instance, {\n eventHandler: (event) => {\n // widen before narrowing: `remote-patches` is not part of the\n // `DocumentEvent` union in older SDK typings\n const candidate: {type: string} = event\n if (!isRemotePatchesEvent(candidate)) {\n return\n }\n // our own transactions are already reflected in the editor\n if (candidate.origin !== 'remote') {\n return\n }\n if (\n getPublishedDocumentId(candidate.documentId) !==\n getPublishedDocumentId(documentId)\n ) {\n return\n }\n\n let patches: PtePatch[] | null\n try {\n patches = scopeRemotePatches(candidate.patches, path)\n } catch {\n // unconvertible patch shapes fall back to the whole-value sync\n // driven by `onRemoteValueChange`\n return\n }\n if (patches && patches.length > 0) {\n callback(patches)\n }\n },\n })\n },\n [instance, documentId, path],\n )\n\n const pushPatches = useCallback(\n (patches: PtePatch[]) => {\n const sanityPatches = convertPatchesToSanity(patches, {prefix: path})\n // `preserveOperations` ships in @sanity/sdk >= 2.17; the intersection\n // type keeps the plugin compatible with older SDK typings\n const action: EditDocumentAction & {preserveOperations?: boolean} = {\n ...editDocument(\n {documentId, documentType},\n sanityPatches as Parameters<typeof editDocument>[1],\n ),\n preserveOperations: true,\n }\n applyActions(action)\n },\n [applyActions, documentId, documentType, path],\n )\n\n return (\n <ValueSyncPlugin\n getRemoteValue={getCurrent}\n pushValue={setSdkValue}\n onRemoteValueChange={subscribe}\n onRemotePatches={onRemotePatches}\n pushPatches={pushPatches}\n />\n )\n}\n\n/**\n * @internal\n */\ntype ValueSyncConfig = {\n getRemoteValue: () => PortableTextBlock[] | null | undefined\n pushValue: (value: PortableTextBlock[]) => void\n onRemoteValueChange: (callback: () => void) => () => void\n /**\n * Optional patch channel. When provided, operational patches from other\n * clients are applied directly to the editor (in every state) instead of\n * waiting for a whole-value diff, and local editor patches are pushed\n * through `pushPatches`. The whole-value sync remains as a fallback for\n * anything the patch channel cannot express.\n */\n onRemotePatches?: (\n callback: (patches: PtePatch[]) => void,\n ) => (() => void) | undefined\n /**\n * Pushes the editor's own operational patches to the remote store. May\n * throw when a patch cannot be converted, in which case the plugin falls\n * back to pushing the whole value.\n */\n pushPatches?: (patches: PtePatch[]) => void\n}\n\n/**\n * NOTE: You are probably looking for SDKValuePlugin instead of this.\n * This is a lower-level plugin that only handles syncing the value\n * between the editor and a remote source. It does not know anything\n * about Sanity documents or how to fetch/update them.\n *\n * May be removed in the future, do not rely on this directly.\n *\n * @internal\n */\nexport function ValueSyncPlugin(props: ValueSyncConfig) {\n const {\n getRemoteValue,\n pushValue,\n onRemoteValueChange,\n onRemotePatches,\n pushPatches,\n } = props\n const editor = useEditor()\n\n useActorRef(\n valueSyncMachine.provide({\n actions: {\n 'push to remote': ({context, event}) => {\n if (event.type !== 'mutation flushed') {\n return\n }\n\n if (pushPatches && event.patches.length > 0) {\n try {\n pushPatches(\n toMergeableMarkDefsPatches(\n event.patches,\n context.getRemoteValue,\n ),\n )\n return\n } catch {\n // fall back to pushing the whole value below\n }\n }\n\n pushValue(event.value ?? context.editor.getSnapshot().context.value)\n },\n },\n }),\n {\n input: {\n editor,\n getRemoteValue,\n onRemoteValueChange,\n onRemotePatches,\n },\n },\n )\n\n return null\n}\n"],"names":["ARRAYIFY_ERROR_MESSAGE","getSegments","node","base","segment","type","isKeyPath","recursive","name","arrayifyPath","pathExpr","parsePath","Error","Array","from","map","elements","length","element","value","operator","keyPathNode","left","right","find","other","_key","convertPatches","patches","flatMap","p","Object","entries","values","origin","path","isArray","items","rest","position","keys","at","STRINGIFY_ERROR_MESSAGE","stringifyPatchPath","result","prefixPathExpression","prefix","expression","startsWith","convertPatchesToSanity","options","patch","pathExpression","set","setIfMissing","unset","diffMatchPatch","inc","dec","insert","segmentsEqual","a","b","pathsEqual","every","index","findSidecarArrayPath","expectNode","slice","getValueAtPath","current","item","toEngineSafePatches","targetValue","safe","sidecarPaths","sidecarPath","push","some","existing","undefined","toMergeableMarkDefsPatches","getCurrentValue","currentValue","root","storeMarkDefs","storeBlock","local","store","referencedKeys","Set","children","child","marks","storeByKey","Map","localKeys","ops","inserted","filter","has","get","JSON","stringify","canApplyToValue","scopeRemotePatches","fieldPath","converted","scoped","overlap","Math","min","touchesField","applySync","editor","getRemoteValue","remoteValue","snapshot","getSnapshot","context","diffValue","send","valueAfterPatches","listenToEditor","fromCallback","sendBack","input","patchSubscription","on","mutationSubscription","event","unsubscribe","listenToRemote","onRemoteValueChange","listenToRemotePatches","onRemotePatches","valueSyncMachine","setup","types","events","actions","send initial value","push to remote","apply sync","defer then apply sync","queueMicrotask","apply remote patches","actors","createMachine","id","entry","invoke","src","initial","states","target","isRemotePatchesEvent","getPublishedDocumentId","split","join","SDKValuePlugin","props","$","_c","documentId","documentType","setSdkValue","useEditDocument","instance","useSanityInstance","applyActions","useApplyDocumentActions","t0","getDocumentState","getCurrent","subscribe","t1","callback","subscribeDocumentEvents","eventHandler","candidate","t2","patches_0","sanityPatches","action","editDocument","preserveOperations","pushPatches","t3","ValueSyncPlugin","pushValue","useEditor","provide","useActorRef"],"mappings":";;;;;;;;;AAoCA,MAAMA,yBACJ;AAEF,UAAUC,YACRC,MAC2C;AACvCA,OAAKC,SACP,OAAOF,YAAYC,KAAKC,IAAI,IAE1BD,KAAKE,QAAQC,SAAS,WACxB,MAAMH,KAAKE;AAEf;AAEA,SAASE,UAAUJ,MAAkC;AAUnD,SATIA,KAAKG,SAAS,UAGdH,KAAKC,QAGLD,KAAKK,aA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,OAAAc,MAAAA,KAAAd,EAAA,CAAA,GAjCH+B,YACErB,IAyBAI,EAQF,GAEO;AAAI;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/plugin-sdk-value",
3
- "version": "7.0.35",
3
+ "version": "7.1.0",
4
4
  "description": "Connect a Portable Text Editor with a Sanity document using the SDK",
5
5
  "keywords": [
6
6
  "@sanity/sdk",
@@ -40,6 +40,7 @@
40
40
  "xstate": "^5.32.4"
41
41
  },
42
42
  "devDependencies": {
43
+ "@sanity/diff-match-patch": "^3.2.0",
43
44
  "@sanity/pkg-utils": "^10.8.2",
44
45
  "@sanity/sdk-react": "^2.13.0",
45
46
  "@sanity/tsconfig": "^2.1.0",
@@ -59,8 +60,8 @@
59
60
  "vitest": "^4.1.10",
60
61
  "vitest-browser-react": "^2.2.0",
61
62
  "@portabletext/editor": "^7.10.8",
62
- "@portabletext/patches": "^2.0.5",
63
63
  "@portabletext/schema": "^2.2.3",
64
+ "@portabletext/patches": "^2.0.5",
64
65
  "@portabletext/test": "^1.0.4"
65
66
  },
66
67
  "peerDependencies": {