@wordpress/core-data 7.45.0 → 7.45.1-next.v.202605131032.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 (181) hide show
  1. package/build/actions.cjs +8 -6
  2. package/build/actions.cjs.map +2 -2
  3. package/build/awareness/post-editor-awareness.cjs +1 -1
  4. package/build/awareness/post-editor-awareness.cjs.map +2 -2
  5. package/build/resolvers.cjs +2 -1
  6. package/build/resolvers.cjs.map +2 -2
  7. package/build/types.cjs.map +2 -2
  8. package/build/utils/block-selection-history.cjs +4 -1
  9. package/build/utils/block-selection-history.cjs.map +2 -2
  10. package/build/utils/crdt-blocks.cjs +157 -89
  11. package/build/utils/crdt-blocks.cjs.map +2 -2
  12. package/build/utils/crdt-selection.cjs +1 -1
  13. package/build/utils/crdt-selection.cjs.map +2 -2
  14. package/build/utils/crdt-user-selections.cjs +4 -1
  15. package/build/utils/crdt-user-selections.cjs.map +2 -2
  16. package/build/utils/crdt-utils.cjs +18 -6
  17. package/build/utils/crdt-utils.cjs.map +2 -2
  18. package/build/utils/crdt.cjs +12 -2
  19. package/build/utils/crdt.cjs.map +2 -2
  20. package/build-module/actions.mjs +8 -6
  21. package/build-module/actions.mjs.map +2 -2
  22. package/build-module/awareness/post-editor-awareness.mjs +5 -2
  23. package/build-module/awareness/post-editor-awareness.mjs.map +2 -2
  24. package/build-module/resolvers.mjs +2 -1
  25. package/build-module/resolvers.mjs.map +2 -2
  26. package/build-module/types.mjs.map +2 -2
  27. package/build-module/utils/block-selection-history.mjs +5 -1
  28. package/build-module/utils/block-selection-history.mjs.map +2 -2
  29. package/build-module/utils/crdt-blocks.mjs +162 -90
  30. package/build-module/utils/crdt-blocks.mjs.map +2 -2
  31. package/build-module/utils/crdt-selection.mjs +2 -1
  32. package/build-module/utils/crdt-selection.mjs.map +2 -2
  33. package/build-module/utils/crdt-user-selections.mjs +9 -2
  34. package/build-module/utils/crdt-user-selections.mjs.map +2 -2
  35. package/build-module/utils/crdt-utils.mjs +16 -6
  36. package/build-module/utils/crdt-utils.mjs.map +2 -2
  37. package/build-module/utils/crdt.mjs +13 -2
  38. package/build-module/utils/crdt.mjs.map +2 -2
  39. package/build-types/actions.d.ts +177 -64
  40. package/build-types/actions.d.ts.map +1 -1
  41. package/build-types/awareness/awareness-state.d.ts.map +1 -1
  42. package/build-types/awareness/base-awareness.d.ts +0 -3
  43. package/build-types/awareness/base-awareness.d.ts.map +1 -1
  44. package/build-types/awareness/post-editor-awareness.d.ts +1 -8
  45. package/build-types/awareness/post-editor-awareness.d.ts.map +1 -1
  46. package/build-types/awareness/typed-awareness.d.ts.map +1 -1
  47. package/build-types/batch/create-batch.d.ts +1 -1
  48. package/build-types/batch/create-batch.d.ts.map +1 -1
  49. package/build-types/batch/default-processor.d.ts.map +1 -1
  50. package/build-types/batch/index.d.ts +2 -2
  51. package/build-types/batch/index.d.ts.map +1 -1
  52. package/build-types/entities.d.ts +114 -87
  53. package/build-types/entities.d.ts.map +1 -1
  54. package/build-types/entity-context.d.ts +1 -1
  55. package/build-types/entity-context.d.ts.map +1 -1
  56. package/build-types/entity-provider.d.ts +2 -2
  57. package/build-types/entity-provider.d.ts.map +1 -1
  58. package/build-types/entity-types/attachment.d.ts.map +1 -1
  59. package/build-types/entity-types/base-entity-records.d.ts.map +1 -1
  60. package/build-types/entity-types/base.d.ts.map +1 -1
  61. package/build-types/entity-types/comment.d.ts.map +1 -1
  62. package/build-types/entity-types/font-collection.d.ts.map +1 -1
  63. package/build-types/entity-types/font-family.d.ts.map +1 -1
  64. package/build-types/entity-types/global-styles-revision.d.ts.map +1 -1
  65. package/build-types/entity-types/icon.d.ts.map +1 -1
  66. package/build-types/entity-types/menu-location.d.ts.map +1 -1
  67. package/build-types/entity-types/nav-menu-item.d.ts.map +1 -1
  68. package/build-types/entity-types/nav-menu.d.ts.map +1 -1
  69. package/build-types/entity-types/page.d.ts.map +1 -1
  70. package/build-types/entity-types/plugin.d.ts.map +1 -1
  71. package/build-types/entity-types/post-revision.d.ts.map +1 -1
  72. package/build-types/entity-types/post-status.d.ts.map +1 -1
  73. package/build-types/entity-types/post.d.ts.map +1 -1
  74. package/build-types/entity-types/settings.d.ts.map +1 -1
  75. package/build-types/entity-types/sidebar.d.ts.map +1 -1
  76. package/build-types/entity-types/taxonomy.d.ts.map +1 -1
  77. package/build-types/entity-types/term.d.ts.map +1 -1
  78. package/build-types/entity-types/theme.d.ts.map +1 -1
  79. package/build-types/entity-types/type.d.ts.map +1 -1
  80. package/build-types/entity-types/user.d.ts.map +1 -1
  81. package/build-types/entity-types/widget-type.d.ts.map +1 -1
  82. package/build-types/entity-types/widget.d.ts.map +1 -1
  83. package/build-types/entity-types/wp-template-part.d.ts.map +1 -1
  84. package/build-types/entity-types/wp-template.d.ts.map +1 -1
  85. package/build-types/fetch/__experimental-fetch-url-data.d.ts +2 -5
  86. package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -1
  87. package/build-types/fetch/index.d.ts +3 -3
  88. package/build-types/fetch/index.d.ts.map +1 -1
  89. package/build-types/footnotes/get-footnotes-order.d.ts.map +1 -1
  90. package/build-types/footnotes/get-rich-text-values-cached.d.ts.map +1 -1
  91. package/build-types/footnotes/index.d.ts +1 -1
  92. package/build-types/footnotes/index.d.ts.map +1 -1
  93. package/build-types/hooks/use-entity-block-editor.d.ts +1 -1
  94. package/build-types/hooks/use-entity-block-editor.d.ts.map +1 -1
  95. package/build-types/hooks/use-entity-id.d.ts.map +1 -1
  96. package/build-types/hooks/use-entity-prop.d.ts.map +1 -1
  97. package/build-types/hooks/use-resource-permissions.d.ts.map +1 -1
  98. package/build-types/index.d.ts +155 -153
  99. package/build-types/index.d.ts.map +1 -1
  100. package/build-types/locks/actions.d.ts +1 -1
  101. package/build-types/locks/actions.d.ts.map +1 -1
  102. package/build-types/locks/engine.d.ts +1 -1
  103. package/build-types/locks/engine.d.ts.map +1 -1
  104. package/build-types/locks/reducer.d.ts.map +1 -1
  105. package/build-types/locks/selectors.d.ts +2 -2
  106. package/build-types/locks/selectors.d.ts.map +1 -1
  107. package/build-types/locks/utils.d.ts +5 -5
  108. package/build-types/locks/utils.d.ts.map +1 -1
  109. package/build-types/name.d.ts +1 -1
  110. package/build-types/name.d.ts.map +1 -1
  111. package/build-types/private-actions.d.ts +45 -29
  112. package/build-types/private-actions.d.ts.map +1 -1
  113. package/build-types/private-apis.d.ts +1 -1
  114. package/build-types/private-apis.d.ts.map +1 -1
  115. package/build-types/queried-data/actions.d.ts +3 -3
  116. package/build-types/queried-data/actions.d.ts.map +1 -1
  117. package/build-types/queried-data/get-query-parts.d.ts +10 -34
  118. package/build-types/queried-data/get-query-parts.d.ts.map +1 -1
  119. package/build-types/queried-data/index.d.ts +3 -3
  120. package/build-types/queried-data/index.d.ts.map +1 -1
  121. package/build-types/queried-data/reducer.d.ts +7 -23
  122. package/build-types/queried-data/reducer.d.ts.map +1 -1
  123. package/build-types/queried-data/selectors.d.ts +3 -3
  124. package/build-types/queried-data/selectors.d.ts.map +1 -1
  125. package/build-types/reducer.d.ts +40 -32
  126. package/build-types/reducer.d.ts.map +1 -1
  127. package/build-types/resolvers.d.ts +130 -47
  128. package/build-types/resolvers.d.ts.map +1 -1
  129. package/build-types/selectors.d.ts +1 -1
  130. package/build-types/selectors.d.ts.map +1 -1
  131. package/build-types/types.d.ts +61 -6
  132. package/build-types/types.d.ts.map +1 -1
  133. package/build-types/utils/block-selection-history.d.ts.map +1 -1
  134. package/build-types/utils/conservative-map-item.d.ts.map +1 -1
  135. package/build-types/utils/crdt-blocks.d.ts +19 -9
  136. package/build-types/utils/crdt-blocks.d.ts.map +1 -1
  137. package/build-types/utils/crdt-selection.d.ts.map +1 -1
  138. package/build-types/utils/crdt-user-selections.d.ts.map +1 -1
  139. package/build-types/utils/crdt-utils.d.ts +35 -2
  140. package/build-types/utils/crdt-utils.d.ts.map +1 -1
  141. package/build-types/utils/crdt.d.ts.map +1 -1
  142. package/build-types/utils/forward-resolver.d.ts +2 -2
  143. package/build-types/utils/forward-resolver.d.ts.map +1 -1
  144. package/build-types/utils/get-nested-value.d.ts.map +1 -1
  145. package/build-types/utils/get-normalized-comma-separable.d.ts +1 -1
  146. package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -1
  147. package/build-types/utils/if-matching-action.d.ts +3 -3
  148. package/build-types/utils/if-matching-action.d.ts.map +1 -1
  149. package/build-types/utils/index.d.ts +12 -12
  150. package/build-types/utils/index.d.ts.map +1 -1
  151. package/build-types/utils/is-numeric-id.d.ts.map +1 -1
  152. package/build-types/utils/log-entity-deprecation.d.ts +1 -1
  153. package/build-types/utils/log-entity-deprecation.d.ts.map +1 -1
  154. package/build-types/utils/normalize-query-for-resolution.d.ts.map +1 -1
  155. package/build-types/utils/receive-intermediate-results.d.ts +1 -1
  156. package/build-types/utils/receive-intermediate-results.d.ts.map +1 -1
  157. package/build-types/utils/replace-action.d.ts +3 -3
  158. package/build-types/utils/replace-action.d.ts.map +1 -1
  159. package/build-types/utils/set-nested-value.d.ts.map +1 -1
  160. package/build-types/utils/user-permissions.d.ts +3 -3
  161. package/build-types/utils/user-permissions.d.ts.map +1 -1
  162. package/build-types/utils/with-weak-map-cache.d.ts +1 -1
  163. package/build-types/utils/with-weak-map-cache.d.ts.map +1 -1
  164. package/package.json +20 -20
  165. package/src/actions.js +7 -9
  166. package/src/awareness/post-editor-awareness.ts +5 -2
  167. package/src/resolvers.js +2 -1
  168. package/src/test/actions.js +58 -0
  169. package/src/test/resolvers.js +115 -2
  170. package/src/test/rtc-rich-text-offset-space.test.js +204 -0
  171. package/src/types.ts +63 -6
  172. package/src/utils/block-selection-history.ts +5 -1
  173. package/src/utils/crdt-blocks.ts +316 -116
  174. package/src/utils/crdt-selection.ts +2 -1
  175. package/src/utils/crdt-user-selections.ts +9 -2
  176. package/src/utils/crdt-utils.ts +53 -10
  177. package/src/utils/crdt.ts +30 -4
  178. package/src/utils/test/crdt-blocks.ts +74 -18
  179. package/src/utils/test/crdt-utils.ts +18 -2
  180. package/src/utils/test/rtc-rich-text-cursor-scope.test.js +267 -0
  181. package/src/utils/test/rtc-rich-text-offset-space.test.js +469 -0
@@ -73,8 +73,19 @@ function makeBlockAttributesSerializable(blockName, attributes) {
73
73
  }
74
74
  function makeBlocksSerializable(blocks) {
75
75
  return blocks.map((block) => {
76
- const { name, innerBlocks, attributes, ...rest } = block;
77
- delete rest.validationIssues;
76
+ const {
77
+ name,
78
+ innerBlocks,
79
+ attributes,
80
+ /*
81
+ * Any validation issues discovered when loading a block are appended
82
+ * to the block node with a logging function, which cannot be serialized.
83
+ *
84
+ * @see import("@wordpress/blocks/src/api/parser").parseRawBlock()
85
+ */
86
+ validationIssues,
87
+ ...rest
88
+ } = block;
78
89
  return {
79
90
  ...rest,
80
91
  name,
@@ -230,111 +241,138 @@ function createNewYBlock(block) {
230
241
  )
231
242
  );
232
243
  }
233
- function mergeCrdtBlocks(yblocks, incomingBlocks, cursorPosition) {
244
+ function mergeCrdtBlocks(yblocks, incomingBlocks, attributeCursor) {
234
245
  if (!serializableBlocksCache.has(incomingBlocks)) {
235
246
  serializableBlocksCache.set(
236
247
  incomingBlocks,
237
248
  makeBlocksSerializable(incomingBlocks)
238
249
  );
239
250
  }
240
- const blocksToSync = serializableBlocksCache.get(incomingBlocks) ?? [];
251
+ const incomingBlocksToSync = serializableBlocksCache.get(incomingBlocks) ?? [];
241
252
  const numOfCommonEntries = Math.min(
242
- blocksToSync.length ?? 0,
253
+ incomingBlocksToSync.length ?? 0,
243
254
  yblocks.length
244
255
  );
245
256
  let left = 0;
246
257
  let right = 0;
247
- for (; left < numOfCommonEntries && areBlocksEqual(blocksToSync[left], yblocks.get(left)); left++) {
258
+ for (; left < numOfCommonEntries && areBlocksEqual(incomingBlocksToSync[left], yblocks.get(left)); left++) {
248
259
  }
249
260
  for (; right < numOfCommonEntries - left && areBlocksEqual(
250
- blocksToSync[blocksToSync.length - right - 1],
261
+ incomingBlocksToSync[incomingBlocksToSync.length - right - 1],
251
262
  yblocks.get(yblocks.length - right - 1)
252
263
  ); right++) {
253
264
  }
254
265
  const numOfUpdatesNeeded = numOfCommonEntries - left - right;
255
266
  const numOfInsertionsNeeded = Math.max(
256
267
  0,
257
- blocksToSync.length - yblocks.length
268
+ incomingBlocksToSync.length - yblocks.length
258
269
  );
259
270
  const numOfDeletionsNeeded = Math.max(
260
271
  0,
261
- yblocks.length - blocksToSync.length
272
+ yblocks.length - incomingBlocksToSync.length
262
273
  );
263
274
  for (let i = 0; i < numOfUpdatesNeeded; i++, left++) {
264
- const block = blocksToSync[left];
265
- const yblock = yblocks.get(left);
266
- Object.entries(block).forEach(([key, value]) => {
267
- switch (key) {
268
- case "attributes": {
269
- const currentAttributes = yblock.get(key);
270
- if (!currentAttributes) {
271
- yblock.set(
272
- key,
273
- createNewYAttributeMap(block.name, value)
275
+ const incomingYBlock = incomingBlocksToSync[left];
276
+ const localYBlock = yblocks.get(left);
277
+ Object.entries(incomingYBlock).forEach(
278
+ ([incomingBlockProperty, incomingBlockPropertyValue]) => {
279
+ switch (incomingBlockProperty) {
280
+ case "attributes": {
281
+ const localAttributes = localYBlock.get(
282
+ incomingBlockProperty
274
283
  );
275
- break;
276
- }
277
- Object.entries(value).forEach(
278
- ([attributeName, attributeValue]) => {
279
- const currentAttribute = currentAttributes?.get(attributeName);
280
- const isExpectedType = isExpectedAttributeType(
281
- block.name,
282
- attributeName,
283
- currentAttribute
284
- );
285
- const isYType = currentAttribute instanceof import_sync.Y.AbstractType;
286
- const isAttributeChanged = !isExpectedType || isYType || !(0, import_es6.default)(
287
- currentAttribute,
288
- attributeValue
284
+ const incomingAttributes = incomingBlockPropertyValue;
285
+ if (!localAttributes) {
286
+ localYBlock.set(
287
+ incomingBlockProperty,
288
+ createNewYAttributeMap(
289
+ incomingYBlock.name,
290
+ incomingAttributes
291
+ )
289
292
  );
290
- if (isAttributeChanged) {
291
- updateYBlockAttribute(
292
- block.name,
293
- attributeName,
294
- attributeValue,
295
- currentAttributes,
296
- cursorPosition
293
+ break;
294
+ }
295
+ Object.entries(incomingAttributes).forEach(
296
+ ([
297
+ incomingAttributeName,
298
+ incomingAttributeValue
299
+ ]) => {
300
+ const currentAttribute = localAttributes?.get(
301
+ incomingAttributeName
302
+ );
303
+ const isExpectedType = isExpectedAttributeType(
304
+ incomingYBlock.name,
305
+ incomingAttributeName,
306
+ currentAttribute
297
307
  );
308
+ const isYType = currentAttribute instanceof import_sync.Y.AbstractType;
309
+ const isAttributeChanged = !isExpectedType || isYType || !(0, import_es6.default)(
310
+ currentAttribute,
311
+ incomingAttributeValue
312
+ );
313
+ if (isAttributeChanged) {
314
+ updateYBlockAttribute(
315
+ incomingYBlock.name,
316
+ incomingYBlock.clientId,
317
+ incomingAttributeName,
318
+ incomingAttributeValue,
319
+ localAttributes,
320
+ attributeCursor
321
+ );
322
+ }
298
323
  }
299
- }
300
- );
301
- currentAttributes.forEach(
302
- (_attrValue, attrName) => {
303
- if (!value.hasOwnProperty(attrName)) {
304
- currentAttributes.delete(attrName);
324
+ );
325
+ localAttributes.forEach(
326
+ (_attrValue, attrName) => {
327
+ if (!incomingBlockPropertyValue.hasOwnProperty(
328
+ attrName
329
+ )) {
330
+ localAttributes.delete(attrName);
331
+ }
305
332
  }
333
+ );
334
+ break;
335
+ }
336
+ case "innerBlocks": {
337
+ let yInnerBlocks = localYBlock.get(
338
+ incomingBlockProperty
339
+ );
340
+ if (!(yInnerBlocks instanceof import_sync.Y.Array)) {
341
+ yInnerBlocks = new import_sync.Y.Array();
342
+ localYBlock.set(
343
+ incomingBlockProperty,
344
+ yInnerBlocks
345
+ );
306
346
  }
307
- );
308
- break;
309
- }
310
- case "innerBlocks": {
311
- let yInnerBlocks = yblock.get(key);
312
- if (!(yInnerBlocks instanceof import_sync.Y.Array)) {
313
- yInnerBlocks = new import_sync.Y.Array();
314
- yblock.set(key, yInnerBlocks);
347
+ mergeCrdtBlocks(
348
+ yInnerBlocks,
349
+ incomingBlockPropertyValue ?? [],
350
+ attributeCursor
351
+ );
352
+ break;
315
353
  }
316
- mergeCrdtBlocks(
317
- yInnerBlocks,
318
- value ?? [],
319
- cursorPosition
320
- );
321
- break;
354
+ default:
355
+ if (!(0, import_es6.default)(
356
+ incomingYBlock[incomingBlockProperty],
357
+ localYBlock.get(incomingBlockProperty)
358
+ )) {
359
+ localYBlock.set(
360
+ incomingBlockProperty,
361
+ incomingBlockPropertyValue
362
+ );
363
+ }
322
364
  }
323
- default:
324
- if (!(0, import_es6.default)(block[key], yblock.get(key))) {
325
- yblock.set(key, value);
326
- }
327
365
  }
328
- });
329
- yblock.forEach((_v, k) => {
330
- if (!block.hasOwnProperty(k)) {
331
- yblock.delete(k);
366
+ );
367
+ localYBlock.forEach((_v, k) => {
368
+ if (!incomingYBlock.hasOwnProperty(k)) {
369
+ localYBlock.delete(k);
332
370
  }
333
371
  });
334
372
  }
335
373
  yblocks.delete(left, numOfDeletionsNeeded);
336
374
  for (let i = 0; i < numOfInsertionsNeeded; i++, left++) {
337
- const newBlock = [createNewYBlock(blocksToSync[left])];
375
+ const newBlock = [createNewYBlock(incomingBlocksToSync[left])];
338
376
  yblocks.insert(left, newBlock);
339
377
  }
340
378
  const knownClientIds = /* @__PURE__ */ new Set();
@@ -357,7 +395,7 @@ function areArrayElementsEqual(newElement, yElement) {
357
395
  }
358
396
  return (0, import_es6.default)(newElement, yElement);
359
397
  }
360
- function mergeYArray(yArray, newValue, schema, cursorPosition) {
398
+ function mergeYArray(yArray, newValue, schema, cursorPosition, cursorScope) {
361
399
  if (!schema.query) {
362
400
  return;
363
401
  }
@@ -381,7 +419,8 @@ function mergeYArray(yArray, newValue, schema, cursorPosition) {
381
419
  currentElement,
382
420
  newElement,
383
421
  query,
384
- cursorPosition
422
+ cursorPosition,
423
+ cursorScope
385
424
  );
386
425
  } else {
387
426
  yArray.delete(0, yArray.length);
@@ -414,14 +453,24 @@ function mergeYArray(yArray, newValue, schema, cursorPosition) {
414
453
  yArray.insert(insertAt, itemsToInsert);
415
454
  }
416
455
  }
417
- function mergeYValue(schema, newVal, yMap, key, cursorPosition) {
456
+ function mergeYValue(schema, newVal, yMap, key, cursorPosition, cursorScope) {
418
457
  const currentVal = yMap.get(key);
419
458
  if (schema?.type === "rich-text" && typeof newVal === "string" && currentVal instanceof import_sync.Y.Text) {
420
- mergeRichTextUpdate(currentVal, newVal, cursorPosition);
459
+ mergeRichTextUpdate(
460
+ currentVal,
461
+ newVal,
462
+ resolveRichTextCursorPosition(cursorPosition, cursorScope, newVal)
463
+ );
421
464
  } else if (schema?.type === "array" && schema.query && Array.isArray(newVal) && currentVal instanceof import_sync.Y.Array) {
422
- mergeYArray(currentVal, newVal, schema, cursorPosition);
465
+ mergeYArray(currentVal, newVal, schema, cursorPosition, cursorScope);
423
466
  } else if (schema?.type === "object" && schema.query && isRecord(newVal) && currentVal instanceof import_sync.Y.Map) {
424
- mergeYMapValues(currentVal, newVal, schema.query, cursorPosition);
467
+ mergeYMapValues(
468
+ currentVal,
469
+ newVal,
470
+ schema.query,
471
+ cursorPosition,
472
+ cursorScope
473
+ );
425
474
  } else {
426
475
  const newYValue = createYValueFromSchema(schema, newVal);
427
476
  if (newYValue !== newVal || !(0, import_es6.default)(currentVal, newVal)) {
@@ -429,9 +478,16 @@ function mergeYValue(schema, newVal, yMap, key, cursorPosition) {
429
478
  }
430
479
  }
431
480
  }
432
- function mergeYMapValues(yMap, newObj, query, cursorPosition) {
481
+ function mergeYMapValues(yMap, newObj, query, cursorPosition, cursorScope) {
433
482
  for (const [key, newVal] of Object.entries(newObj)) {
434
- mergeYValue(query[key], newVal, yMap, key, cursorPosition);
483
+ mergeYValue(
484
+ query[key],
485
+ newVal,
486
+ yMap,
487
+ key,
488
+ cursorPosition,
489
+ cursorScope
490
+ );
435
491
  }
436
492
  for (const key of yMap.keys()) {
437
493
  if (!Object.hasOwn(newObj, key)) {
@@ -439,16 +495,23 @@ function mergeYMapValues(yMap, newObj, query, cursorPosition) {
439
495
  }
440
496
  }
441
497
  }
442
- function updateYBlockAttribute(blockName, attributeName, attributeValue, currentAttributes, cursorPosition) {
498
+ function updateYBlockAttribute(blockName, clientId, attributeName, attributeValue, currentAttributes, newCursorPosition) {
443
499
  const schema = getBlockAttributeSchema(blockName, attributeName);
444
500
  mergeYValue(
445
501
  schema,
446
502
  attributeValue,
447
503
  currentAttributes,
448
504
  attributeName,
449
- cursorPosition
505
+ newCursorPosition,
506
+ { attributeKey: attributeName, clientId }
450
507
  );
451
508
  }
509
+ function resolveRichTextCursorPosition(cursorPosition, cursorScope, updatedValue) {
510
+ return cursorPosition && cursorPosition.clientId === cursorScope.clientId && cursorPosition.attributeKey === cursorScope.attributeKey && "number" === typeof cursorPosition.offset && Number.isInteger(cursorPosition.offset) ? (0, import_crdt_utils.richTextOffsetToHtmlIndex)(
511
+ updatedValue,
512
+ (0, import_crdt_utils.asRichTextOffset)(cursorPosition.offset)
513
+ ) : null;
514
+ }
452
515
  var cachedBlockAttributeSchemas;
453
516
  function getBlockAttributeSchema(blockName, attributeName) {
454
517
  if (!cachedBlockAttributeSchemas) {
@@ -489,20 +552,25 @@ function isLocalAttribute(blockName, attributeName) {
489
552
  return "local" === getBlockAttributeSchema(blockName, attributeName)?.role;
490
553
  }
491
554
  var localDoc;
492
- function mergeRichTextUpdate(blockYText, updatedValue, cursorPosition = null) {
493
- if (!localDoc) {
494
- localDoc = new import_sync.Y.Doc();
495
- }
496
- const localYText = localDoc.getText("temporary-text");
497
- localYText.delete(0, localYText.length);
498
- localYText.insert(0, updatedValue);
555
+ function mergeRichTextUpdate(blockYText, updatedValue, htmlCursorIndex = null) {
499
556
  const currentValueAsDelta = new import_sync2.Delta(blockYText.toDelta());
500
- const updatedValueAsDelta = new import_sync2.Delta(localYText.toDelta());
557
+ const updatedValueAsDelta = new import_sync2.Delta([{ insert: updatedValue }]);
501
558
  const deltaDiff = currentValueAsDelta.diffWithCursor(
502
559
  updatedValueAsDelta,
503
- cursorPosition
560
+ htmlCursorIndex
504
561
  );
505
- blockYText.applyDelta(deltaDiff.ops);
562
+ const safeDiff = htmlCursorIndex === null || isDeltaVerificationMatch(blockYText, deltaDiff, updatedValue) ? deltaDiff : currentValueAsDelta.diff(updatedValueAsDelta);
563
+ blockYText.applyDelta(safeDiff.ops);
564
+ }
565
+ function isDeltaVerificationMatch(blockYText, delta, expectedValue) {
566
+ if (!localDoc) {
567
+ localDoc = new import_sync.Y.Doc();
568
+ }
569
+ const verificationYText = localDoc.getText("verification-text");
570
+ verificationYText.delete(0, verificationYText.length);
571
+ verificationYText.insert(0, blockYText.toString());
572
+ verificationYText.applyDelta(delta.ops);
573
+ return verificationYText.toString() === expectedValue;
506
574
  }
507
575
  // Annotate the CommonJS export names for ESM import in node:
508
576
  0 && (module.exports = {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/utils/crdt-blocks.ts"],
4
- "sourcesContent": ["/**\n * External dependencies\n */\nimport { v4 as uuidv4 } from 'uuid';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * WordPress dependencies\n */\nimport { getBlockTypes } from '@wordpress/blocks';\nimport { RichTextData } from '@wordpress/rich-text';\nimport { Y } from '@wordpress/sync';\n\n/**\n * Internal dependencies\n */\nimport { createYMap, type YMapRecord, type YMapWrap } from './crdt-utils';\nimport { getCachedRichTextData } from './crdt-text';\nimport { Delta } from '../sync';\n\ninterface BlockAttributes {\n\t[ key: string ]: unknown;\n}\n\ninterface BlockAttributeSchema {\n\trole?: string;\n\ttype?: string;\n\tquery?: Record< string, BlockAttributeSchema >;\n}\n\ninterface BlockType {\n\tattributes?: Record< string, BlockAttributeSchema >;\n\tname: string;\n}\n\n// A block as represented in Gutenberg's data store.\nexport interface Block {\n\tattributes: BlockAttributes;\n\tclientId?: string;\n\tinnerBlocks: Block[];\n\tisValid?: boolean;\n\tname: string;\n\toriginalContent?: string;\n\tvalidationIssues?: string[]; // unserializable\n}\n\n// A block as represented in the CRDT document (Y.Map).\nexport interface YBlockRecord extends YMapRecord {\n\tattributes: YBlockAttributes;\n\tclientId: string;\n\tinnerBlocks: YBlocks;\n\tisValid?: boolean;\n\toriginalContent?: string;\n\tname: string;\n}\n\nexport type YBlock = YMapWrap< YBlockRecord >;\nexport type YBlocks = Y.Array< YBlock >;\n\n// Block attribute schema cannot be known at compile time, so we use Y.Map.\n// Attribute values will be typed as the union of `Y.Text` and `unknown`.\nexport type YBlockAttributes = Y.Map< Y.Text | unknown >;\n\nconst serializableBlocksCache = new WeakMap< WeakKey, Block[] >();\n\n/**\n * Recursively walk an attribute value and convert any RichTextData instances\n * to their string (HTML) representation. This is necessary for array-type and\n * object-type attributes, which can contain nested RichTextData.\n *\n * @param value The attribute value to serialize.\n * @return The value with all RichTextData instances replaced by strings.\n */\nfunction serializeAttributeValue( value: unknown ): unknown {\n\tif ( value instanceof RichTextData ) {\n\t\treturn value.valueOf();\n\t}\n\n\t// e.g. core/table `body`: [ { cells: [ { content: RichTextData } ] } ]\n\tif ( Array.isArray( value ) ) {\n\t\treturn value.map( serializeAttributeValue );\n\t}\n\n\t// e.g. a single row inside core/table `body`: { cells: [ ... ] }\n\tif ( value && typeof value === 'object' ) {\n\t\tconst result: Record< string, unknown > = {};\n\n\t\tfor ( const [ k, v ] of Object.entries( value ) ) {\n\t\t\tresult[ k ] = serializeAttributeValue( v );\n\t\t}\n\t\treturn result;\n\t}\n\n\treturn value;\n}\n\nfunction makeBlockAttributesSerializable(\n\tblockName: string,\n\tattributes: BlockAttributes\n): BlockAttributes {\n\tconst newAttributes = { ...attributes };\n\tfor ( const [ key, value ] of Object.entries( attributes ) ) {\n\t\tif ( isLocalAttribute( blockName, key ) ) {\n\t\t\tdelete newAttributes[ key ];\n\t\t\tcontinue;\n\t\t}\n\n\t\tnewAttributes[ key ] = serializeAttributeValue( value );\n\t}\n\treturn newAttributes;\n}\n\nfunction makeBlocksSerializable( blocks: Block[] ): Block[] {\n\treturn blocks.map( ( block: Block ) => {\n\t\tconst { name, innerBlocks, attributes, ...rest } = block;\n\t\tdelete rest.validationIssues;\n\t\treturn {\n\t\t\t...rest,\n\t\t\tname,\n\t\t\tattributes: makeBlockAttributesSerializable( name, attributes ),\n\t\t\tinnerBlocks: makeBlocksSerializable( innerBlocks ),\n\t\t};\n\t} );\n}\n\n/**\n * Recursively walk an attribute value and convert any strings that correspond\n * to rich-text schema nodes into RichTextData instances. This is the inverse\n * of serializeAttributeValue and handles nested structures like table cells.\n *\n * @param schema The attribute type definition for this value.\n * @param value The attribute value from CRDT (toJSON).\n * @return The value with rich-text strings replaced by RichTextData.\n */\nfunction deserializeAttributeValue(\n\tschema: BlockAttributeSchema | undefined,\n\tvalue: unknown\n): unknown {\n\tif ( schema?.type === 'rich-text' && typeof value === 'string' ) {\n\t\treturn getCachedRichTextData( value );\n\t}\n\n\t// e.g. core/table `body`: [ { cells: [ { content: RichTextData } ] } ]\n\tif ( Array.isArray( value ) ) {\n\t\treturn value.map( ( item ) =>\n\t\t\tdeserializeAttributeValue( schema, item )\n\t\t);\n\t}\n\n\t// e.g. a single row inside core/table `body`: { cells: [ ... ] }\n\tif ( value && typeof value === 'object' ) {\n\t\tconst result: Record< string, unknown > = {};\n\n\t\tfor ( const [ key, innerValue ] of Object.entries(\n\t\t\tvalue as Record< string, unknown >\n\t\t) ) {\n\t\t\tresult[ key ] = deserializeAttributeValue(\n\t\t\t\tschema?.query?.[ key ],\n\t\t\t\tinnerValue\n\t\t\t);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\treturn value;\n}\n\n/**\n * Convert blocks from their CRDT-serialized form back to the runtime form\n * expected by the block editor. Rich-text attributes are stored as Y.Text in\n * the CRDT document, which serializes to plain strings via toJSON(). This\n * function restores them to RichTextData instances so that block edit\n * components that rely on RichTextData methods (e.g. `.text`) work correctly.\n *\n * @param blocks Blocks as extracted from the CRDT document via toJSON().\n * @return Blocks with rich-text attributes restored to RichTextData.\n */\nexport function deserializeBlockAttributes( blocks: Block[] ): Block[] {\n\treturn blocks.map( ( block: Block ) => {\n\t\tconst { name, innerBlocks, attributes, ...rest } = block;\n\n\t\tconst newAttributes = { ...attributes };\n\n\t\tfor ( const [ key, value ] of Object.entries( attributes ) ) {\n\t\t\tconst schema = getBlockAttributeSchema( name, key );\n\n\t\t\tif ( schema ) {\n\t\t\t\tnewAttributes[ key ] = deserializeAttributeValue(\n\t\t\t\t\tschema,\n\t\t\t\t\tvalue\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t...rest,\n\t\t\tname,\n\t\t\tattributes: newAttributes,\n\t\t\tinnerBlocks: deserializeBlockAttributes( innerBlocks ?? [] ),\n\t\t};\n\t} );\n}\n\n/**\n * @param {any} gblock\n * @param {Y.Map} yblock\n */\nfunction areBlocksEqual( gblock: Block, yblock: YBlock ): boolean {\n\tconst yblockAsJson = yblock.toJSON();\n\n\t// we must not sync clientId, as this can't be generated consistently and\n\t// hence will lead to merge conflicts.\n\tconst overwrites = {\n\t\tinnerBlocks: null,\n\t\tclientId: null,\n\t};\n\tconst res = fastDeepEqual(\n\t\tObject.assign( {}, gblock, overwrites ),\n\t\tObject.assign( {}, yblockAsJson, overwrites )\n\t);\n\tconst inners = gblock.innerBlocks || [];\n\tconst yinners = yblock.get( 'innerBlocks' );\n\treturn (\n\t\tres &&\n\t\tinners.length === yinners?.length &&\n\t\tinners.every( ( block: Block, i: number ) =>\n\t\t\tareBlocksEqual( block, yinners.get( i ) )\n\t\t)\n\t);\n}\n\nfunction createNewYAttributeMap(\n\tblockName: string,\n\tattributes: BlockAttributes\n): YBlockAttributes {\n\treturn new Y.Map(\n\t\tObject.entries( attributes ).map(\n\t\t\t( [ attributeName, attributeValue ] ) => {\n\t\t\t\treturn [\n\t\t\t\t\tattributeName,\n\t\t\t\t\tcreateNewYAttributeValue(\n\t\t\t\t\t\tblockName,\n\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\tattributeValue\n\t\t\t\t\t),\n\t\t\t\t];\n\t\t\t}\n\t\t)\n\t);\n}\n\nfunction createNewYAttributeValue(\n\tblockName: string,\n\tattributeName: string,\n\tattributeValue: unknown\n): Y.Text | Y.Array< unknown > | Y.Map< unknown > | unknown {\n\tconst schema = getBlockAttributeSchema( blockName, attributeName );\n\treturn createYValueFromSchema( schema, attributeValue );\n}\n\n/**\n * Recursively create the appropriate Y.js type for a value based on its\n * block-attribute schema.\n *\n * - `rich-text` -> Y.Text\n * - `array` with query -> Y.Array of Y.Maps\n * - `object` with query -> Y.Map\n * - anything else -> plain value (unchanged)\n *\n * @param schema The attribute type definition.\n * @param value The plain JS value to convert.\n * @return A Y.js type or the original value.\n */\nfunction createYValueFromSchema(\n\tschema: BlockAttributeSchema | undefined,\n\tvalue: unknown\n): Y.Text | Y.Array< unknown > | Y.Map< unknown > | unknown {\n\tif ( ! schema ) {\n\t\treturn value;\n\t}\n\n\tif ( schema.type === 'rich-text' ) {\n\t\treturn new Y.Text( value?.toString() ?? '' );\n\t}\n\n\tif ( schema.type === 'array' && schema.query && Array.isArray( value ) ) {\n\t\tconst query = schema.query;\n\t\tconst yArray = new Y.Array< Y.Map< unknown > >();\n\n\t\tyArray.insert(\n\t\t\t0,\n\t\t\tvalue.map( ( item ) => createYMapFromQuery( query, item ) )\n\t\t);\n\n\t\treturn yArray;\n\t}\n\n\tif ( schema.type === 'object' && schema.query && isRecord( value ) ) {\n\t\treturn createYMapFromQuery( schema.query, value );\n\t}\n\n\treturn value;\n}\n\n/**\n * Type guard that narrows `unknown` to `Record< string, unknown >`.\n *\n * @param value Value to check.\n * @return True if `value` is a non-null, non-array object.\n */\nfunction isRecord( value: unknown ): value is Record< string, unknown > {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n\n/**\n * Create a Y.Map from a plain object, using a query schema to decide which\n * properties should become nested Y.js types (Y.Text, Y.Array, Y.Map).\n *\n * @param query The query schema defining the properties.\n * @param obj The plain object to convert.\n * @return A Y.Map with typed values.\n */\nfunction createYMapFromQuery(\n\tquery: Record< string, BlockAttributeSchema >,\n\tobj: unknown\n): Y.Map< unknown > {\n\tif ( ! isRecord( obj ) ) {\n\t\treturn new Y.Map();\n\t}\n\n\tconst entries: [ string, unknown ][] = Object.entries( obj ).map(\n\t\t( [ key, val ] ): [ string, unknown ] => {\n\t\t\tconst subSchema = query[ key ];\n\t\t\treturn [ key, createYValueFromSchema( subSchema, val ) ];\n\t\t}\n\t);\n\n\treturn new Y.Map( entries );\n}\n\nfunction createNewYBlock( block: Block ): YBlock {\n\treturn createYMap< YBlockRecord >(\n\t\tObject.fromEntries(\n\t\t\tObject.entries( block ).map( ( [ key, value ] ) => {\n\t\t\t\tswitch ( key ) {\n\t\t\t\t\tcase 'attributes': {\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\tcreateNewYAttributeMap( block.name, value ),\n\t\t\t\t\t\t];\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'innerBlocks': {\n\t\t\t\t\t\tconst innerBlocks = new Y.Array();\n\n\t\t\t\t\t\t// If not an array, set to empty Y.Array.\n\t\t\t\t\t\tif ( ! Array.isArray( value ) ) {\n\t\t\t\t\t\t\treturn [ key, innerBlocks ];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tinnerBlocks.insert(\n\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\tvalue.map( ( innerBlock: Block ) =>\n\t\t\t\t\t\t\t\tcreateNewYBlock( innerBlock )\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn [ key, innerBlocks ];\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\treturn [ key, value ];\n\t\t\t\t}\n\t\t\t} )\n\t\t)\n\t);\n}\n\n/**\n * Merge incoming block data into the local Y.Doc.\n * This function is called to sync local block changes to a shared Y.Doc.\n *\n * @param yblocks The blocks in the local Y.Doc.\n * @param incomingBlocks Gutenberg blocks being synced.\n * @param cursorPosition The position of the cursor after the change occurs.\n */\nexport function mergeCrdtBlocks(\n\tyblocks: YBlocks,\n\tincomingBlocks: Block[],\n\tcursorPosition: number | null\n): void {\n\t// Ensure we are working with serializable block data.\n\tif ( ! serializableBlocksCache.has( incomingBlocks ) ) {\n\t\tserializableBlocksCache.set(\n\t\t\tincomingBlocks,\n\t\t\tmakeBlocksSerializable( incomingBlocks )\n\t\t);\n\t}\n\tconst blocksToSync = serializableBlocksCache.get( incomingBlocks ) ?? [];\n\n\t// This is a rudimentary diff implementation similar to the y-prosemirror diffing\n\t// approach.\n\t// A better implementation would also diff the textual content and represent it\n\t// using a Y.Text type.\n\t// However, at this time it makes more sense to keep this algorithm generic to\n\t// support all kinds of block types.\n\t// Ideally, we ensure that block data structure have a consistent data format.\n\t// E.g.:\n\t// - textual content (using rich-text formatting?) may always be stored under `block.text`\n\t// - local information that shouldn't be shared (e.g. clientId or isDragging) is stored under `block.private`\n\t//\n\t// @credit Kevin Jahns (dmonad)\n\t// @link https://github.com/WordPress/gutenberg/pull/68483\n\tconst numOfCommonEntries = Math.min(\n\t\tblocksToSync.length ?? 0,\n\t\tyblocks.length\n\t);\n\n\tlet left = 0;\n\tlet right = 0;\n\n\t// skip equal blocks from left\n\tfor (\n\t\t;\n\t\tleft < numOfCommonEntries &&\n\t\tareBlocksEqual( blocksToSync[ left ], yblocks.get( left ) );\n\t\tleft++\n\t) {\n\t\t/* nop */\n\t}\n\n\t// skip equal blocks from right\n\tfor (\n\t\t;\n\t\tright < numOfCommonEntries - left &&\n\t\tareBlocksEqual(\n\t\t\tblocksToSync[ blocksToSync.length - right - 1 ],\n\t\t\tyblocks.get( yblocks.length - right - 1 )\n\t\t);\n\t\tright++\n\t) {\n\t\t/* nop */\n\t}\n\n\tconst numOfUpdatesNeeded = numOfCommonEntries - left - right;\n\tconst numOfInsertionsNeeded = Math.max(\n\t\t0,\n\t\tblocksToSync.length - yblocks.length\n\t);\n\tconst numOfDeletionsNeeded = Math.max(\n\t\t0,\n\t\tyblocks.length - blocksToSync.length\n\t);\n\n\t// updates\n\tfor ( let i = 0; i < numOfUpdatesNeeded; i++, left++ ) {\n\t\tconst block = blocksToSync[ left ];\n\t\tconst yblock = yblocks.get( left );\n\n\t\tObject.entries( block ).forEach( ( [ key, value ] ) => {\n\t\t\tswitch ( key ) {\n\t\t\t\tcase 'attributes': {\n\t\t\t\t\tconst currentAttributes = yblock.get( key );\n\n\t\t\t\t\t// If attributes are not set on the yblock, use the new values.\n\t\t\t\t\tif ( ! currentAttributes ) {\n\t\t\t\t\t\tyblock.set(\n\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\tcreateNewYAttributeMap( block.name, value )\n\t\t\t\t\t\t);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tObject.entries( value ).forEach(\n\t\t\t\t\t\t( [ attributeName, attributeValue ] ) => {\n\t\t\t\t\t\t\tconst currentAttribute =\n\t\t\t\t\t\t\t\tcurrentAttributes?.get( attributeName );\n\n\t\t\t\t\t\t\tconst isExpectedType = isExpectedAttributeType(\n\t\t\t\t\t\t\t\tblock.name,\n\t\t\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\t\t\tcurrentAttribute\n\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t// Y types (Y.Text, Y.Array, Y.Map) cannot be\n\t\t\t\t\t\t\t// compared with fastDeepEqual against plain values.\n\t\t\t\t\t\t\t// Delegate to mergeYValue which handles no-op\n\t\t\t\t\t\t\t// detection at the edges.\n\t\t\t\t\t\t\tconst isYType =\n\t\t\t\t\t\t\t\tcurrentAttribute instanceof Y.AbstractType;\n\n\t\t\t\t\t\t\tconst isAttributeChanged =\n\t\t\t\t\t\t\t\t! isExpectedType ||\n\t\t\t\t\t\t\t\tisYType ||\n\t\t\t\t\t\t\t\t! fastDeepEqual(\n\t\t\t\t\t\t\t\t\tcurrentAttribute,\n\t\t\t\t\t\t\t\t\tattributeValue\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\tif ( isAttributeChanged ) {\n\t\t\t\t\t\t\t\tupdateYBlockAttribute(\n\t\t\t\t\t\t\t\t\tblock.name,\n\t\t\t\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\t\t\t\tattributeValue,\n\t\t\t\t\t\t\t\t\tcurrentAttributes,\n\t\t\t\t\t\t\t\t\tcursorPosition\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\n\t\t\t\t\t// Delete any attributes that are no longer present.\n\t\t\t\t\tcurrentAttributes.forEach(\n\t\t\t\t\t\t( _attrValue: unknown, attrName: string ) => {\n\t\t\t\t\t\t\tif ( ! value.hasOwnProperty( attrName ) ) {\n\t\t\t\t\t\t\t\tcurrentAttributes.delete( attrName );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tcase 'innerBlocks': {\n\t\t\t\t\t// Recursively merge innerBlocks\n\t\t\t\t\tlet yInnerBlocks = yblock.get( key );\n\n\t\t\t\t\tif ( ! ( yInnerBlocks instanceof Y.Array ) ) {\n\t\t\t\t\t\tyInnerBlocks = new Y.Array< YBlock >();\n\t\t\t\t\t\tyblock.set( key, yInnerBlocks );\n\t\t\t\t\t}\n\n\t\t\t\t\tmergeCrdtBlocks(\n\t\t\t\t\t\tyInnerBlocks,\n\t\t\t\t\t\tvalue ?? [],\n\t\t\t\t\t\tcursorPosition\n\t\t\t\t\t);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tdefault:\n\t\t\t\t\tif ( ! fastDeepEqual( block[ key ], yblock.get( key ) ) ) {\n\t\t\t\t\t\tyblock.set( key, value );\n\t\t\t\t\t}\n\t\t\t}\n\t\t} );\n\t\tyblock.forEach( ( _v, k ) => {\n\t\t\tif ( ! block.hasOwnProperty( k ) ) {\n\t\t\t\tyblock.delete( k );\n\t\t\t}\n\t\t} );\n\t}\n\n\t// deletes\n\tyblocks.delete( left, numOfDeletionsNeeded );\n\n\t// inserts\n\tfor ( let i = 0; i < numOfInsertionsNeeded; i++, left++ ) {\n\t\tconst newBlock = [ createNewYBlock( blocksToSync[ left ] ) ];\n\n\t\tyblocks.insert( left, newBlock );\n\t}\n\n\t// remove duplicate clientids\n\tconst knownClientIds = new Set< string >();\n\tfor ( let j = 0; j < yblocks.length; j++ ) {\n\t\tconst yblock: YBlock = yblocks.get( j );\n\n\t\tlet clientId = yblock.get( 'clientId' );\n\n\t\tif ( ! clientId ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif ( knownClientIds.has( clientId ) ) {\n\t\t\tclientId = uuidv4();\n\t\t\tyblock.set( 'clientId', clientId );\n\t\t}\n\t\tknownClientIds.add( clientId );\n\t}\n}\n\n/**\n * Compare a plain array element against a Y.Map element for equality.\n * Used by the left-right sweep diff in mergeYArray.\n *\n * @param newElement The plain object from the incoming array.\n * @param yElement The Y.Map element from the existing Y.Array.\n * @return True if the elements are deeply equal.\n */\nfunction areArrayElementsEqual(\n\tnewElement: unknown,\n\tyElement: unknown\n): boolean {\n\tif ( yElement instanceof Y.Map && isRecord( newElement ) ) {\n\t\treturn fastDeepEqual( newElement, yElement.toJSON() );\n\t}\n\n\treturn fastDeepEqual( newElement, yElement );\n}\n\n/**\n * Merge an incoming plain array into an existing Y.Array in-place.\n *\n * Uses the same left-right sweep diff approach as mergeCrdtBlocks:\n * equal elements are skipped from both ends, then the middle section\n * is updated, deleted, or inserted as needed. This preserves existing\n * Y.Map/Y.Text objects for unchanged elements, so concurrent edits\n * to those elements are not lost.\n *\n * @param yArray The existing Y.Array to update.\n * @param newValue The new plain array to merge into the Y.Array.\n * @param schema The attribute schema (must have `query`).\n * @param cursorPosition The local cursor position for rich-text delta merges.\n */\nfunction mergeYArray(\n\tyArray: Y.Array< unknown >,\n\tnewValue: unknown[],\n\tschema: BlockAttributeSchema,\n\tcursorPosition: number | null\n): void {\n\tif ( ! schema.query ) {\n\t\treturn;\n\t}\n\n\tconst query = schema.query;\n\tconst numOfCommonEntries = Math.min( newValue.length, yArray.length );\n\n\tlet left = 0;\n\tlet right = 0;\n\n\t// Skip equal elements from left.\n\tfor (\n\t\t;\n\t\tleft < numOfCommonEntries &&\n\t\tareArrayElementsEqual( newValue[ left ], yArray.get( left ) );\n\t\tleft++\n\t) {\n\t\t/* nop */\n\t}\n\n\t// Skip equal elements from right.\n\tfor (\n\t\t;\n\t\tright < numOfCommonEntries - left &&\n\t\tareArrayElementsEqual(\n\t\t\tnewValue[ newValue.length - right - 1 ],\n\t\t\tyArray.get( yArray.length - right - 1 )\n\t\t);\n\t\tright++\n\t) {\n\t\t/* nop */\n\t}\n\n\t// Updates: merge changed elements in-place.\n\tconst numOfUpdatesNeeded = numOfCommonEntries - left - right;\n\n\tfor ( let i = 0; i < numOfUpdatesNeeded; i++ ) {\n\t\tconst currentElement = yArray.get( left + i );\n\t\tconst newElement = newValue[ left + i ];\n\n\t\tif ( currentElement instanceof Y.Map && isRecord( newElement ) ) {\n\t\t\tmergeYMapValues(\n\t\t\t\tcurrentElement,\n\t\t\t\tnewElement,\n\t\t\t\tquery,\n\t\t\t\tcursorPosition\n\t\t\t);\n\t\t} else {\n\t\t\t// Element is the wrong type (e.g. partial migration) or the\n\t\t\t// incoming value is not an object. Rebuild the entire array.\n\t\t\tyArray.delete( 0, yArray.length );\n\t\t\tyArray.insert(\n\t\t\t\t0,\n\t\t\t\tnewValue.map( ( item ) => createYMapFromQuery( query, item ) )\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\t}\n\n\t// Deletes.\n\tconst numOfDeletionsNeeded = Math.max( 0, yArray.length - newValue.length );\n\n\tif ( numOfDeletionsNeeded > 0 ) {\n\t\tyArray.delete( left + numOfUpdatesNeeded, numOfDeletionsNeeded );\n\t}\n\n\t// Inserts.\n\tconst numOfInsertionsNeeded = Math.max(\n\t\t0,\n\t\tnewValue.length - yArray.length\n\t);\n\n\tif ( numOfInsertionsNeeded > 0 ) {\n\t\tconst insertAt = left + numOfUpdatesNeeded;\n\t\tconst itemsToInsert: Y.Map< unknown >[] = new Array(\n\t\t\tnumOfInsertionsNeeded\n\t\t);\n\n\t\tfor ( let i = 0; i < numOfInsertionsNeeded; i++ ) {\n\t\t\titemsToInsert[ i ] = createYMapFromQuery(\n\t\t\t\tquery,\n\t\t\t\tnewValue[ insertAt + i ]\n\t\t\t);\n\t\t}\n\n\t\tyArray.insert( insertAt, itemsToInsert );\n\t}\n}\n\n/**\n * Merge a single value into a Y.Map entry, using the attribute schema to\n * decide how to merge.\n *\n * If the current value is already a matching Y.js type (Y.Text, Y.Array,\n * Y.Map), the update is merged in-place so concurrent edits are preserved.\n * Otherwise the value is replaced wholesale.\n *\n * @param schema The attribute type definition for this value.\n * @param newVal The new value to merge into the Y.Map entry.\n * @param yMap The Y.Map that owns this entry.\n * @param key The key of this entry in the Y.Map.\n * @param cursorPosition The local cursor position for rich-text delta merges.\n */\nfunction mergeYValue(\n\tschema: BlockAttributeSchema | undefined,\n\tnewVal: unknown,\n\tyMap: Y.Map< unknown >,\n\tkey: string,\n\tcursorPosition: number | null\n): void {\n\tconst currentVal = yMap.get( key );\n\tif (\n\t\tschema?.type === 'rich-text' &&\n\t\ttypeof newVal === 'string' &&\n\t\tcurrentVal instanceof Y.Text\n\t) {\n\t\tmergeRichTextUpdate( currentVal, newVal, cursorPosition );\n\t} else if (\n\t\tschema?.type === 'array' &&\n\t\tschema.query &&\n\t\tArray.isArray( newVal ) &&\n\t\tcurrentVal instanceof Y.Array\n\t) {\n\t\tmergeYArray( currentVal, newVal, schema, cursorPosition );\n\t} else if (\n\t\tschema?.type === 'object' &&\n\t\tschema.query &&\n\t\tisRecord( newVal ) &&\n\t\tcurrentVal instanceof Y.Map\n\t) {\n\t\tmergeYMapValues( currentVal, newVal, schema.query, cursorPosition );\n\t} else {\n\t\tconst newYValue = createYValueFromSchema( schema, newVal );\n\n\t\t// If createYValueFromSchema wrapped the value into a Y type, the\n\t\t// current value is the wrong type and needs upgrading. Otherwise,\n\t\t// only replace if the raw value actually changed.\n\t\tif ( newYValue !== newVal || ! fastDeepEqual( currentVal, newVal ) ) {\n\t\t\tyMap.set( key, newYValue );\n\t\t}\n\t}\n}\n\n/**\n * Merge an incoming plain object into an existing Y.Map in-place, using\n * the query schema to decide how each property should be merged.\n *\n * Properties present in the Y.Map but absent from `newObj` are deleted.\n *\n * @param yMap The existing Y.Map to update.\n * @param newObj The new plain object to merge into the Y.Map.\n * @param query The query schema defining property types.\n * @param cursorPosition The local cursor position for rich-text delta merges.\n */\nfunction mergeYMapValues(\n\tyMap: Y.Map< unknown >,\n\tnewObj: Record< string, unknown >,\n\tquery: Record< string, BlockAttributeSchema >,\n\tcursorPosition: number | null\n): void {\n\tfor ( const [ key, newVal ] of Object.entries( newObj ) ) {\n\t\tmergeYValue( query[ key ], newVal, yMap, key, cursorPosition );\n\t}\n\n\t// Delete properties absent from the incoming object.\n\tfor ( const key of yMap.keys() ) {\n\t\tif ( ! Object.hasOwn( newObj, key ) ) {\n\t\t\tyMap.delete( key );\n\t\t}\n\t}\n}\n\n/**\n * Update a single attribute on a Yjs block attributes map (currentAttributes).\n *\n * @param blockName The block type name, e.g. 'core/paragraph'.\n * @param attributeName The name of the attribute to update, e.g. 'content'.\n * @param attributeValue The new value for the attribute.\n * @param currentAttributes The Y.Map holding the block's current attributes.\n * @param cursorPosition The local cursor position, used when merging rich-text deltas.\n */\nfunction updateYBlockAttribute(\n\tblockName: string,\n\tattributeName: string,\n\tattributeValue: unknown,\n\tcurrentAttributes: YBlockAttributes,\n\tcursorPosition: number | null\n): void {\n\tconst schema = getBlockAttributeSchema( blockName, attributeName );\n\n\tmergeYValue(\n\t\tschema,\n\t\tattributeValue,\n\t\tcurrentAttributes,\n\t\tattributeName,\n\t\tcursorPosition\n\t);\n}\n\n// Cached block attribute types, populated once from getBlockTypes().\nlet cachedBlockAttributeSchemas: Map<\n\tstring,\n\tMap< string, BlockAttributeSchema >\n>;\n\n/**\n * Get the attribute type definition for a block attribute.\n *\n * @param blockName The name of the block, e.g. 'core/paragraph'.\n * @param attributeName The name of the attribute, e.g. 'content'.\n * @return The type definition of the attribute.\n */\nfunction getBlockAttributeSchema(\n\tblockName: string,\n\tattributeName: string\n): BlockAttributeSchema | undefined {\n\tif ( ! cachedBlockAttributeSchemas ) {\n\t\t// Parse the attributes for all blocks once.\n\t\tcachedBlockAttributeSchemas = new Map();\n\n\t\tfor ( const blockType of getBlockTypes() as BlockType[] ) {\n\t\t\tcachedBlockAttributeSchemas.set(\n\t\t\t\tblockType.name,\n\t\t\t\tnew Map< string, BlockAttributeSchema >(\n\t\t\t\t\tObject.entries( blockType.attributes ?? {} ).map(\n\t\t\t\t\t\t( [ name, definition ] ) => {\n\t\t\t\t\t\t\tconst { role, type, query } = definition;\n\t\t\t\t\t\t\treturn [ name, { role, type, query } ];\n\t\t\t\t\t\t}\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\t}\n\n\treturn cachedBlockAttributeSchemas.get( blockName )?.get( attributeName );\n}\n\n/**\n * Check if an attribute value is the expected type.\n *\n * @param blockName The name of the block, e.g. 'core/paragraph'.\n * @param attributeName The name of the attribute, e.g. 'content'.\n * @param attributeValue The current attribute value.\n * @return True if the attribute type is expected, false otherwise.\n */\nfunction isExpectedAttributeType(\n\tblockName: string,\n\tattributeName: string,\n\tattributeValue: unknown\n): boolean {\n\tconst schema = getBlockAttributeSchema( blockName, attributeName );\n\n\tif ( schema?.type === 'rich-text' ) {\n\t\treturn attributeValue instanceof Y.Text;\n\t}\n\n\tif ( schema?.type === 'string' ) {\n\t\treturn typeof attributeValue === 'string';\n\t}\n\n\tif ( schema?.type === 'array' && schema.query ) {\n\t\treturn attributeValue instanceof Y.Array;\n\t}\n\n\tif ( schema?.type === 'object' && schema.query ) {\n\t\treturn attributeValue instanceof Y.Map;\n\t}\n\n\treturn true;\n}\n\n/**\n * Given a block name and attribute key, return true if the attribute is local\n * and should not be synced.\n *\n * @param blockName The name of the block, e.g. 'core/image'.\n * @param attributeName The name of the attribute to check, e.g. 'blob'.\n * @return True if the attribute is local, false otherwise.\n */\nfunction isLocalAttribute( blockName: string, attributeName: string ): boolean {\n\treturn (\n\t\t'local' === getBlockAttributeSchema( blockName, attributeName )?.role\n\t);\n}\n\nlet localDoc: Y.Doc;\n\n/**\n * Given a Y.Text object and an updated string value, diff the new value and\n * apply the delta to the Y.Text.\n *\n * @param blockYText The Y.Text to update.\n * @param updatedValue The updated value.\n * @param cursorPosition The position of the cursor after the change occurs.\n */\nexport function mergeRichTextUpdate(\n\tblockYText: Y.Text,\n\tupdatedValue: string,\n\tcursorPosition: number | null = null\n): void {\n\t// Gutenberg does not use Yjs shared types natively, so we can only subscribe\n\t// to changes from store and apply them to Yjs types that we create and\n\t// manage. Crucially, for rich-text attributes, we do not receive granular\n\t// string updates; we get the new full string value on each change, even when\n\t// only a single character changed.\n\t//\n\t// The code below allows us to compute a delta between the current and new\n\t// value, then apply it to the Y.Text.\n\n\tif ( ! localDoc ) {\n\t\t// Y.Text must be attached to a Y.Doc to be able to do operations on it.\n\t\t// Create a temporary Y.Text attached to a local Y.Doc for delta computation.\n\t\tlocalDoc = new Y.Doc();\n\t}\n\n\tconst localYText = localDoc.getText( 'temporary-text' );\n\tlocalYText.delete( 0, localYText.length );\n\tlocalYText.insert( 0, updatedValue );\n\n\tconst currentValueAsDelta = new Delta( blockYText.toDelta() );\n\tconst updatedValueAsDelta = new Delta( localYText.toDelta() );\n\tconst deltaDiff = currentValueAsDelta.diffWithCursor(\n\t\tupdatedValueAsDelta,\n\t\tcursorPosition\n\t);\n\n\tblockYText.applyDelta( deltaDiff.ops );\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA6B;AAC7B,iBAA0B;AAK1B,oBAA8B;AAC9B,uBAA6B;AAC7B,kBAAkB;AAKlB,wBAA2D;AAC3D,uBAAsC;AACtC,IAAAA,eAAsB;AA6CtB,IAAM,0BAA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K,CAAE,UAAW;AAGjB,eAAW,IAAI,cAAE,IAAI;AAAA,EACtB;AAEA,QAAM,aAAa,SAAS,QAAS,gBAAiB;AACtD,aAAW,OAAQ,GAAG,WAAW,MAAO;AACxC,aAAW,OAAQ,GAAG,YAAa;AAEnC,QAAM,sBAAsB,IAAI,mBAAO,WAAW,QAAQ,CAAE;AAC5D,QAAM,sBAAsB,IAAI,mBAAO,WAAW,QAAQ,CAAE;AAC5D,QAAM,YAAY,oBAAoB;AAAA,IACrC;AAAA,IACA;AAAA,EACD;AAEA,aAAW,WAAY,UAAU,GAAI;AACtC;",
4
+ "sourcesContent": ["/**\n * External dependencies\n */\nimport { v4 as uuidv4 } from 'uuid';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\n\n/**\n * WordPress dependencies\n */\nimport { getBlockTypes } from '@wordpress/blocks';\nimport { RichTextData } from '@wordpress/rich-text';\nimport { Y } from '@wordpress/sync';\n\n/**\n * Internal dependencies\n */\nimport {\n\tasRichTextOffset,\n\tcreateYMap,\n\trichTextOffsetToHtmlIndex,\n\ttype HtmlStringIndex,\n\ttype YMapRecord,\n\ttype YMapWrap,\n} from './crdt-utils';\nimport { getCachedRichTextData } from './crdt-text';\nimport { Delta } from '../sync';\nimport { type WPBlockSelection } from '../types';\n\ninterface BlockAttributes {\n\t[ key: string ]: unknown;\n}\n\ninterface BlockAttributeSchema {\n\trole?: string;\n\ttype?: string;\n\tquery?: Record< string, BlockAttributeSchema >;\n}\n\ninterface BlockType {\n\tattributes?: Record< string, BlockAttributeSchema >;\n\tname: string;\n}\n\n// A block as represented in Gutenberg's data store.\nexport interface Block {\n\tattributes: BlockAttributes;\n\tclientId?: string;\n\tinnerBlocks: Block[];\n\tisValid?: boolean;\n\tname: string;\n\toriginalContent?: string;\n\tvalidationIssues?: string[]; // unserializable\n}\n\n// A block as represented in the CRDT document (Y.Map).\nexport interface YBlockRecord extends YMapRecord {\n\tattributes: YBlockAttributes;\n\tclientId: string;\n\tinnerBlocks: YBlocks;\n\tisValid?: boolean;\n\toriginalContent?: string;\n\tname: string;\n}\n\nexport type YBlock = YMapWrap< YBlockRecord >;\nexport type YBlocks = Y.Array< YBlock >;\n\n// Block attribute schema cannot be known at compile time, so we use Y.Map.\n// Attribute values will be typed as the union of `Y.Text` and `unknown`.\nexport type YBlockAttributes = Y.Map< Y.Text | unknown >;\n\n/**\n * Optional description of where a cursor falls.\n *\n * Used to coordinate shifting of cursor when applying changes\n * to a Y.Doc with RichText instances.\n */\nexport type MergeCursorPosition = WPBlockSelection | null;\n\nconst serializableBlocksCache = new WeakMap< WeakKey, Block[] >();\n\n/**\n * Recursively walk an attribute value and convert any RichTextData instances\n * to their string (HTML) representation. This is necessary for array-type and\n * object-type attributes, which can contain nested RichTextData.\n *\n * @param value The attribute value to serialize.\n * @return The value with all RichTextData instances replaced by strings.\n */\nfunction serializeAttributeValue( value: unknown ): unknown {\n\tif ( value instanceof RichTextData ) {\n\t\treturn value.valueOf();\n\t}\n\n\t// e.g. core/table `body`: [ { cells: [ { content: RichTextData } ] } ]\n\tif ( Array.isArray( value ) ) {\n\t\treturn value.map( serializeAttributeValue );\n\t}\n\n\t// e.g. a single row inside core/table `body`: { cells: [ ... ] }\n\tif ( value && typeof value === 'object' ) {\n\t\tconst result: Record< string, unknown > = {};\n\n\t\tfor ( const [ k, v ] of Object.entries( value ) ) {\n\t\t\tresult[ k ] = serializeAttributeValue( v );\n\t\t}\n\t\treturn result;\n\t}\n\n\treturn value;\n}\n\nfunction makeBlockAttributesSerializable(\n\tblockName: string,\n\tattributes: BlockAttributes\n): BlockAttributes {\n\tconst newAttributes = { ...attributes };\n\tfor ( const [ key, value ] of Object.entries( attributes ) ) {\n\t\tif ( isLocalAttribute( blockName, key ) ) {\n\t\t\tdelete newAttributes[ key ];\n\t\t\tcontinue;\n\t\t}\n\n\t\tnewAttributes[ key ] = serializeAttributeValue( value );\n\t}\n\treturn newAttributes;\n}\n\n/**\n * Recursively removes properties which cannot be serialized from a list of block objects.\n *\n * @param blocks Eemove unserializable properties from each block object in this set.\n * @return Copies of the provided blocks without the unserializable properties.\n */\nfunction makeBlocksSerializable( blocks: Block[] ): Block[] {\n\treturn blocks.map( ( block: Block ) => {\n\t\tconst {\n\t\t\tname,\n\t\t\tinnerBlocks,\n\t\t\tattributes,\n\t\t\t/*\n\t\t\t * Any validation issues discovered when loading a block are appended\n\t\t\t * to the block node with a logging function, which cannot be serialized.\n\t\t\t *\n\t\t\t * @see import(\"@wordpress/blocks/src/api/parser\").parseRawBlock()\n\t\t\t */\n\t\t\tvalidationIssues,\n\t\t\t...rest\n\t\t} = block;\n\n\t\treturn {\n\t\t\t...rest,\n\t\t\tname,\n\t\t\tattributes: makeBlockAttributesSerializable( name, attributes ),\n\t\t\tinnerBlocks: makeBlocksSerializable( innerBlocks ),\n\t\t};\n\t} );\n}\n\n/**\n * Recursively walk an attribute value and convert any strings that correspond\n * to rich-text schema nodes into RichTextData instances. This is the inverse\n * of serializeAttributeValue and handles nested structures like table cells.\n *\n * @param schema The attribute type definition for this value.\n * @param value The attribute value from CRDT (toJSON).\n * @return The value with rich-text strings replaced by RichTextData.\n */\nfunction deserializeAttributeValue(\n\tschema: BlockAttributeSchema | undefined,\n\tvalue: unknown\n): unknown {\n\tif ( schema?.type === 'rich-text' && typeof value === 'string' ) {\n\t\treturn getCachedRichTextData( value );\n\t}\n\n\t// e.g. core/table `body`: [ { cells: [ { content: RichTextData } ] } ]\n\tif ( Array.isArray( value ) ) {\n\t\treturn value.map( ( item ) =>\n\t\t\tdeserializeAttributeValue( schema, item )\n\t\t);\n\t}\n\n\t// e.g. a single row inside core/table `body`: { cells: [ ... ] }\n\tif ( value && typeof value === 'object' ) {\n\t\tconst result: Record< string, unknown > = {};\n\n\t\tfor ( const [ key, innerValue ] of Object.entries(\n\t\t\tvalue as Record< string, unknown >\n\t\t) ) {\n\t\t\tresult[ key ] = deserializeAttributeValue(\n\t\t\t\tschema?.query?.[ key ],\n\t\t\t\tinnerValue\n\t\t\t);\n\t\t}\n\n\t\treturn result;\n\t}\n\n\treturn value;\n}\n\n/**\n * Convert blocks from their CRDT-serialized form back to the runtime form\n * expected by the block editor. Rich-text attributes are stored as Y.Text in\n * the CRDT document, which serializes to plain strings via toJSON(). This\n * function restores them to RichTextData instances so that block edit\n * components that rely on RichTextData methods (e.g. `.text`) work correctly.\n *\n * @param blocks Blocks as extracted from the CRDT document via toJSON().\n * @return Blocks with rich-text attributes restored to RichTextData.\n */\nexport function deserializeBlockAttributes( blocks: Block[] ): Block[] {\n\treturn blocks.map( ( block: Block ) => {\n\t\tconst { name, innerBlocks, attributes, ...rest } = block;\n\n\t\tconst newAttributes = { ...attributes };\n\n\t\tfor ( const [ key, value ] of Object.entries( attributes ) ) {\n\t\t\tconst schema = getBlockAttributeSchema( name, key );\n\n\t\t\tif ( schema ) {\n\t\t\t\tnewAttributes[ key ] = deserializeAttributeValue(\n\t\t\t\t\tschema,\n\t\t\t\t\tvalue\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t...rest,\n\t\t\tname,\n\t\t\tattributes: newAttributes,\n\t\t\tinnerBlocks: deserializeBlockAttributes( innerBlocks ?? [] ),\n\t\t};\n\t} );\n}\n\n/**\n * @param {any} gblock\n * @param {Y.Map} yblock\n */\nfunction areBlocksEqual( gblock: Block, yblock: YBlock ): boolean {\n\tconst yblockAsJson = yblock.toJSON();\n\n\t// we must not sync clientId, as this can't be generated consistently and\n\t// hence will lead to merge conflicts.\n\tconst overwrites = {\n\t\tinnerBlocks: null,\n\t\tclientId: null,\n\t};\n\tconst res = fastDeepEqual(\n\t\tObject.assign( {}, gblock, overwrites ),\n\t\tObject.assign( {}, yblockAsJson, overwrites )\n\t);\n\tconst inners = gblock.innerBlocks || [];\n\tconst yinners = yblock.get( 'innerBlocks' );\n\treturn (\n\t\tres &&\n\t\tinners.length === yinners?.length &&\n\t\tinners.every( ( block: Block, i: number ) =>\n\t\t\tareBlocksEqual( block, yinners.get( i ) )\n\t\t)\n\t);\n}\n\nfunction createNewYAttributeMap(\n\tblockName: string,\n\tattributes: BlockAttributes\n): YBlockAttributes {\n\treturn new Y.Map(\n\t\tObject.entries( attributes ).map(\n\t\t\t( [ attributeName, attributeValue ] ) => {\n\t\t\t\treturn [\n\t\t\t\t\tattributeName,\n\t\t\t\t\tcreateNewYAttributeValue(\n\t\t\t\t\t\tblockName,\n\t\t\t\t\t\tattributeName,\n\t\t\t\t\t\tattributeValue\n\t\t\t\t\t),\n\t\t\t\t];\n\t\t\t}\n\t\t)\n\t);\n}\n\nfunction createNewYAttributeValue(\n\tblockName: string,\n\tattributeName: string,\n\tattributeValue: unknown\n): Y.Text | Y.Array< unknown > | Y.Map< unknown > | unknown {\n\tconst schema = getBlockAttributeSchema( blockName, attributeName );\n\treturn createYValueFromSchema( schema, attributeValue );\n}\n\n/**\n * Recursively create the appropriate Y.js type for a value based on its\n * block-attribute schema.\n *\n * - `rich-text` -> Y.Text\n * - `array` with query -> Y.Array of Y.Maps\n * - `object` with query -> Y.Map\n * - anything else -> plain value (unchanged)\n *\n * @param schema The attribute type definition.\n * @param value The plain JS value to convert.\n * @return A Y.js type or the original value.\n */\nfunction createYValueFromSchema(\n\tschema: BlockAttributeSchema | undefined,\n\tvalue: unknown\n): Y.Text | Y.Array< unknown > | Y.Map< unknown > | unknown {\n\tif ( ! schema ) {\n\t\treturn value;\n\t}\n\n\tif ( schema.type === 'rich-text' ) {\n\t\treturn new Y.Text( value?.toString() ?? '' );\n\t}\n\n\tif ( schema.type === 'array' && schema.query && Array.isArray( value ) ) {\n\t\tconst query = schema.query;\n\t\tconst yArray = new Y.Array< Y.Map< unknown > >();\n\n\t\tyArray.insert(\n\t\t\t0,\n\t\t\tvalue.map( ( item ) => createYMapFromQuery( query, item ) )\n\t\t);\n\n\t\treturn yArray;\n\t}\n\n\tif ( schema.type === 'object' && schema.query && isRecord( value ) ) {\n\t\treturn createYMapFromQuery( schema.query, value );\n\t}\n\n\treturn value;\n}\n\n/**\n * Type guard that narrows `unknown` to `Record< string, unknown >`.\n *\n * @param value Value to check.\n * @return True if `value` is a non-null, non-array object.\n */\nfunction isRecord( value: unknown ): value is Record< string, unknown > {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n\n/**\n * Create a Y.Map from a plain object, using a query schema to decide which\n * properties should become nested Y.js types (Y.Text, Y.Array, Y.Map).\n *\n * @param query The query schema defining the properties.\n * @param obj The plain object to convert.\n * @return A Y.Map with typed values.\n */\nfunction createYMapFromQuery(\n\tquery: Record< string, BlockAttributeSchema >,\n\tobj: unknown\n): Y.Map< unknown > {\n\tif ( ! isRecord( obj ) ) {\n\t\treturn new Y.Map();\n\t}\n\n\tconst entries: [ string, unknown ][] = Object.entries( obj ).map(\n\t\t( [ key, val ] ): [ string, unknown ] => {\n\t\t\tconst subSchema = query[ key ];\n\t\t\treturn [ key, createYValueFromSchema( subSchema, val ) ];\n\t\t}\n\t);\n\n\treturn new Y.Map( entries );\n}\n\nfunction createNewYBlock( block: Block ): YBlock {\n\treturn createYMap< YBlockRecord >(\n\t\tObject.fromEntries(\n\t\t\tObject.entries( block ).map( ( [ key, value ] ) => {\n\t\t\t\tswitch ( key ) {\n\t\t\t\t\tcase 'attributes': {\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\tcreateNewYAttributeMap( block.name, value ),\n\t\t\t\t\t\t];\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'innerBlocks': {\n\t\t\t\t\t\tconst innerBlocks = new Y.Array();\n\n\t\t\t\t\t\t// If not an array, set to empty Y.Array.\n\t\t\t\t\t\tif ( ! Array.isArray( value ) ) {\n\t\t\t\t\t\t\treturn [ key, innerBlocks ];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tinnerBlocks.insert(\n\t\t\t\t\t\t\t0,\n\t\t\t\t\t\t\tvalue.map( ( innerBlock: Block ) =>\n\t\t\t\t\t\t\t\tcreateNewYBlock( innerBlock )\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn [ key, innerBlocks ];\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\treturn [ key, value ];\n\t\t\t\t}\n\t\t\t} )\n\t\t)\n\t);\n}\n\n/**\n * Merge incoming block data into the local Y.Doc.\n * This function is called to sync local block changes to a shared Y.Doc.\n *\n * @param yblocks The blocks in the local Y.Doc.\n * @param incomingBlocks Gutenberg blocks being synced.\n * @param attributeCursor When provided, describes a selection cursor falling within a\n * RichText field associated with a specific block and attribute.\n * Derived from the changes that produced the blocks.\n */\nexport function mergeCrdtBlocks(\n\tyblocks: YBlocks,\n\tincomingBlocks: Block[],\n\tattributeCursor: MergeCursorPosition\n): void {\n\t// Ensure we are working with serializable block data.\n\tif ( ! serializableBlocksCache.has( incomingBlocks ) ) {\n\t\tserializableBlocksCache.set(\n\t\t\tincomingBlocks,\n\t\t\tmakeBlocksSerializable( incomingBlocks )\n\t\t);\n\t}\n\n\tconst incomingBlocksToSync =\n\t\tserializableBlocksCache.get( incomingBlocks ) ?? [];\n\n\t// This is a rudimentary diff implementation similar to the y-prosemirror diffing\n\t// approach.\n\t// A better implementation would also diff the textual content and represent it\n\t// using a Y.Text type.\n\t// However, at this time it makes more sense to keep this algorithm generic to\n\t// support all kinds of block types.\n\t// Ideally, we ensure that block data structure have a consistent data format.\n\t// E.g.:\n\t// - textual content (using rich-text formatting?) may always be stored under `block.text`\n\t// - local information that shouldn't be shared (e.g. clientId or isDragging) is stored under `block.private`\n\t//\n\t// @credit Kevin Jahns (dmonad)\n\t// @link https://github.com/WordPress/gutenberg/pull/68483\n\tconst numOfCommonEntries = Math.min(\n\t\tincomingBlocksToSync.length ?? 0,\n\t\tyblocks.length\n\t);\n\n\tlet left = 0;\n\tlet right = 0;\n\n\t// skip equal blocks from left\n\tfor (\n\t\t;\n\t\tleft < numOfCommonEntries &&\n\t\tareBlocksEqual( incomingBlocksToSync[ left ], yblocks.get( left ) );\n\t\tleft++\n\t) {\n\t\t/* nop */\n\t}\n\n\t// skip equal blocks from right\n\tfor (\n\t\t;\n\t\tright < numOfCommonEntries - left &&\n\t\tareBlocksEqual(\n\t\t\tincomingBlocksToSync[ incomingBlocksToSync.length - right - 1 ],\n\t\t\tyblocks.get( yblocks.length - right - 1 )\n\t\t);\n\t\tright++\n\t) {\n\t\t/* nop */\n\t}\n\n\tconst numOfUpdatesNeeded = numOfCommonEntries - left - right;\n\tconst numOfInsertionsNeeded = Math.max(\n\t\t0,\n\t\tincomingBlocksToSync.length - yblocks.length\n\t);\n\tconst numOfDeletionsNeeded = Math.max(\n\t\t0,\n\t\tyblocks.length - incomingBlocksToSync.length\n\t);\n\n\t// updates\n\tfor ( let i = 0; i < numOfUpdatesNeeded; i++, left++ ) {\n\t\tconst incomingYBlock = incomingBlocksToSync[ left ];\n\t\tconst localYBlock = yblocks.get( left );\n\n\t\tObject.entries( incomingYBlock ).forEach(\n\t\t\t( [ incomingBlockProperty, incomingBlockPropertyValue ] ) => {\n\t\t\t\tswitch ( incomingBlockProperty ) {\n\t\t\t\t\tcase 'attributes': {\n\t\t\t\t\t\tconst localAttributes = localYBlock.get(\n\t\t\t\t\t\t\tincomingBlockProperty\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst incomingAttributes = incomingBlockPropertyValue;\n\n\t\t\t\t\t\t// When the local block has no attributes, adopt the incoming set.\n\t\t\t\t\t\tif ( ! localAttributes ) {\n\t\t\t\t\t\t\tlocalYBlock.set(\n\t\t\t\t\t\t\t\tincomingBlockProperty,\n\t\t\t\t\t\t\t\tcreateNewYAttributeMap(\n\t\t\t\t\t\t\t\t\tincomingYBlock.name,\n\t\t\t\t\t\t\t\t\tincomingAttributes\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Otherwise the attributes need to be merged.\n\t\t\t\t\t\tObject.entries( incomingAttributes ).forEach(\n\t\t\t\t\t\t\t( [\n\t\t\t\t\t\t\t\tincomingAttributeName,\n\t\t\t\t\t\t\t\tincomingAttributeValue,\n\t\t\t\t\t\t\t] ) => {\n\t\t\t\t\t\t\t\tconst currentAttribute = localAttributes?.get(\n\t\t\t\t\t\t\t\t\tincomingAttributeName\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\tconst isExpectedType = isExpectedAttributeType(\n\t\t\t\t\t\t\t\t\tincomingYBlock.name,\n\t\t\t\t\t\t\t\t\tincomingAttributeName,\n\t\t\t\t\t\t\t\t\tcurrentAttribute\n\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\t// Y types (Y.Text, Y.Array, Y.Map) cannot be\n\t\t\t\t\t\t\t\t// compared with fastDeepEqual against plain values.\n\t\t\t\t\t\t\t\t// Delegate to mergeYValue which handles no-op\n\t\t\t\t\t\t\t\t// detection at the edges.\n\t\t\t\t\t\t\t\tconst isYType =\n\t\t\t\t\t\t\t\t\tcurrentAttribute instanceof Y.AbstractType;\n\n\t\t\t\t\t\t\t\tconst isAttributeChanged =\n\t\t\t\t\t\t\t\t\t! isExpectedType ||\n\t\t\t\t\t\t\t\t\tisYType ||\n\t\t\t\t\t\t\t\t\t! fastDeepEqual(\n\t\t\t\t\t\t\t\t\t\tcurrentAttribute,\n\t\t\t\t\t\t\t\t\t\tincomingAttributeValue\n\t\t\t\t\t\t\t\t\t);\n\n\t\t\t\t\t\t\t\tif ( isAttributeChanged ) {\n\t\t\t\t\t\t\t\t\tupdateYBlockAttribute(\n\t\t\t\t\t\t\t\t\t\tincomingYBlock.name,\n\t\t\t\t\t\t\t\t\t\tincomingYBlock.clientId,\n\t\t\t\t\t\t\t\t\t\tincomingAttributeName,\n\t\t\t\t\t\t\t\t\t\tincomingAttributeValue,\n\t\t\t\t\t\t\t\t\t\tlocalAttributes,\n\t\t\t\t\t\t\t\t\t\tattributeCursor\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\t// Delete any attributes that are no longer present.\n\t\t\t\t\t\tlocalAttributes.forEach(\n\t\t\t\t\t\t\t( _attrValue: unknown, attrName: string ) => {\n\t\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t\t! incomingBlockPropertyValue.hasOwnProperty(\n\t\t\t\t\t\t\t\t\t\tattrName\n\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\t\tlocalAttributes.delete( attrName );\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase 'innerBlocks': {\n\t\t\t\t\t\t// Recursively merge innerBlocks\n\t\t\t\t\t\tlet yInnerBlocks = localYBlock.get(\n\t\t\t\t\t\t\tincomingBlockProperty\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif ( ! ( yInnerBlocks instanceof Y.Array ) ) {\n\t\t\t\t\t\t\tyInnerBlocks = new Y.Array< YBlock >();\n\t\t\t\t\t\t\tlocalYBlock.set(\n\t\t\t\t\t\t\t\tincomingBlockProperty,\n\t\t\t\t\t\t\t\tyInnerBlocks\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tmergeCrdtBlocks(\n\t\t\t\t\t\t\tyInnerBlocks,\n\t\t\t\t\t\t\tincomingBlockPropertyValue ?? [],\n\t\t\t\t\t\t\tattributeCursor\n\t\t\t\t\t\t);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t! fastDeepEqual(\n\t\t\t\t\t\t\t\tincomingYBlock[ incomingBlockProperty ],\n\t\t\t\t\t\t\t\tlocalYBlock.get( incomingBlockProperty )\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tlocalYBlock.set(\n\t\t\t\t\t\t\t\tincomingBlockProperty,\n\t\t\t\t\t\t\t\tincomingBlockPropertyValue\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t);\n\t\tlocalYBlock.forEach( ( _v, k ) => {\n\t\t\tif ( ! incomingYBlock.hasOwnProperty( k ) ) {\n\t\t\t\tlocalYBlock.delete( k );\n\t\t\t}\n\t\t} );\n\t}\n\n\t// deletes\n\tyblocks.delete( left, numOfDeletionsNeeded );\n\n\t// inserts\n\tfor ( let i = 0; i < numOfInsertionsNeeded; i++, left++ ) {\n\t\tconst newBlock = [ createNewYBlock( incomingBlocksToSync[ left ] ) ];\n\n\t\tyblocks.insert( left, newBlock );\n\t}\n\n\t// remove duplicate clientids\n\tconst knownClientIds = new Set< string >();\n\tfor ( let j = 0; j < yblocks.length; j++ ) {\n\t\tconst yblock: YBlock = yblocks.get( j );\n\n\t\tlet clientId = yblock.get( 'clientId' );\n\n\t\tif ( ! clientId ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif ( knownClientIds.has( clientId ) ) {\n\t\t\tclientId = uuidv4();\n\t\t\tyblock.set( 'clientId', clientId );\n\t\t}\n\t\tknownClientIds.add( clientId );\n\t}\n}\n\n/**\n * Compare a plain array element against a Y.Map element for equality.\n * Used by the left-right sweep diff in mergeYArray.\n *\n * @param newElement The plain object from the incoming array.\n * @param yElement The Y.Map element from the existing Y.Array.\n * @return True if the elements are deeply equal.\n */\nfunction areArrayElementsEqual(\n\tnewElement: unknown,\n\tyElement: unknown\n): boolean {\n\tif ( yElement instanceof Y.Map && isRecord( newElement ) ) {\n\t\treturn fastDeepEqual( newElement, yElement.toJSON() );\n\t}\n\n\treturn fastDeepEqual( newElement, yElement );\n}\n\n/**\n * Merge an incoming plain array into an existing Y.Array in-place.\n *\n * Uses the same left-right sweep diff approach as mergeCrdtBlocks:\n * equal elements are skipped from both ends, then the middle section\n * is updated, deleted, or inserted as needed. This preserves existing\n * Y.Map/Y.Text objects for unchanged elements, so concurrent edits\n * to those elements are not lost.\n *\n * @param yArray The existing Y.Array to update.\n * @param newValue The new plain array to merge into the Y.Array.\n * @param schema The attribute schema (must have `query`).\n * @param cursorPosition The local cursor position for rich-text delta merges.\n * @param cursorScope The selected block attribute scope for rich-text cursor hints.\n */\nfunction mergeYArray(\n\tyArray: Y.Array< unknown >,\n\tnewValue: unknown[],\n\tschema: BlockAttributeSchema,\n\tcursorPosition: MergeCursorPosition,\n\tcursorScope: RichTextCursorScope\n): void {\n\tif ( ! schema.query ) {\n\t\treturn;\n\t}\n\n\tconst query = schema.query;\n\tconst numOfCommonEntries = Math.min( newValue.length, yArray.length );\n\n\tlet left = 0;\n\tlet right = 0;\n\n\t// Skip equal elements from left.\n\tfor (\n\t\t;\n\t\tleft < numOfCommonEntries &&\n\t\tareArrayElementsEqual( newValue[ left ], yArray.get( left ) );\n\t\tleft++\n\t) {\n\t\t/* nop */\n\t}\n\n\t// Skip equal elements from right.\n\tfor (\n\t\t;\n\t\tright < numOfCommonEntries - left &&\n\t\tareArrayElementsEqual(\n\t\t\tnewValue[ newValue.length - right - 1 ],\n\t\t\tyArray.get( yArray.length - right - 1 )\n\t\t);\n\t\tright++\n\t) {\n\t\t/* nop */\n\t}\n\n\t// Updates: merge changed elements in-place.\n\tconst numOfUpdatesNeeded = numOfCommonEntries - left - right;\n\n\tfor ( let i = 0; i < numOfUpdatesNeeded; i++ ) {\n\t\tconst currentElement = yArray.get( left + i );\n\t\tconst newElement = newValue[ left + i ];\n\n\t\tif ( currentElement instanceof Y.Map && isRecord( newElement ) ) {\n\t\t\tmergeYMapValues(\n\t\t\t\tcurrentElement,\n\t\t\t\tnewElement,\n\t\t\t\tquery,\n\t\t\t\tcursorPosition,\n\t\t\t\tcursorScope\n\t\t\t);\n\t\t} else {\n\t\t\t// Element is the wrong type (e.g. partial migration) or the\n\t\t\t// incoming value is not an object. Rebuild the entire array.\n\t\t\tyArray.delete( 0, yArray.length );\n\t\t\tyArray.insert(\n\t\t\t\t0,\n\t\t\t\tnewValue.map( ( item ) => createYMapFromQuery( query, item ) )\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\t}\n\n\t// Deletes.\n\tconst numOfDeletionsNeeded = Math.max( 0, yArray.length - newValue.length );\n\n\tif ( numOfDeletionsNeeded > 0 ) {\n\t\tyArray.delete( left + numOfUpdatesNeeded, numOfDeletionsNeeded );\n\t}\n\n\t// Inserts.\n\tconst numOfInsertionsNeeded = Math.max(\n\t\t0,\n\t\tnewValue.length - yArray.length\n\t);\n\n\tif ( numOfInsertionsNeeded > 0 ) {\n\t\tconst insertAt = left + numOfUpdatesNeeded;\n\t\tconst itemsToInsert: Y.Map< unknown >[] = new Array(\n\t\t\tnumOfInsertionsNeeded\n\t\t);\n\n\t\tfor ( let i = 0; i < numOfInsertionsNeeded; i++ ) {\n\t\t\titemsToInsert[ i ] = createYMapFromQuery(\n\t\t\t\tquery,\n\t\t\t\tnewValue[ insertAt + i ]\n\t\t\t);\n\t\t}\n\n\t\tyArray.insert( insertAt, itemsToInsert );\n\t}\n}\n\n/**\n * Merge a single value into a Y.Map entry, using the attribute schema to\n * decide how to merge.\n *\n * If the current value is already a matching Y.js type (Y.Text, Y.Array,\n * Y.Map), the update is merged in-place so concurrent edits are preserved.\n * Otherwise the value is replaced wholesale.\n *\n * @param schema The attribute type definition for this value.\n * @param newVal The new value to merge into the Y.Map entry.\n * @param yMap The Y.Map that owns this entry.\n * @param key The key of this entry in the Y.Map.\n * @param cursorPosition The cursor position for rich-text delta merges from the updated value.\n * @param cursorScope Indicates a specific block and attribute associated with the editor;\n * determines whether the cursor should be updated based on the change.\n */\nfunction mergeYValue(\n\tschema: BlockAttributeSchema | undefined,\n\tnewVal: unknown,\n\tyMap: Y.Map< unknown >,\n\tkey: string,\n\tcursorPosition: MergeCursorPosition,\n\tcursorScope: RichTextCursorScope\n): void {\n\tconst currentVal = yMap.get( key );\n\tif (\n\t\tschema?.type === 'rich-text' &&\n\t\ttypeof newVal === 'string' &&\n\t\tcurrentVal instanceof Y.Text\n\t) {\n\t\tmergeRichTextUpdate(\n\t\t\tcurrentVal,\n\t\t\tnewVal,\n\t\t\tresolveRichTextCursorPosition( cursorPosition, cursorScope, newVal )\n\t\t);\n\t} else if (\n\t\tschema?.type === 'array' &&\n\t\tschema.query &&\n\t\tArray.isArray( newVal ) &&\n\t\tcurrentVal instanceof Y.Array\n\t) {\n\t\tmergeYArray( currentVal, newVal, schema, cursorPosition, cursorScope );\n\t} else if (\n\t\tschema?.type === 'object' &&\n\t\tschema.query &&\n\t\tisRecord( newVal ) &&\n\t\tcurrentVal instanceof Y.Map\n\t) {\n\t\tmergeYMapValues(\n\t\t\tcurrentVal,\n\t\t\tnewVal,\n\t\t\tschema.query,\n\t\t\tcursorPosition,\n\t\t\tcursorScope\n\t\t);\n\t} else {\n\t\tconst newYValue = createYValueFromSchema( schema, newVal );\n\n\t\t// If createYValueFromSchema wrapped the value into a Y type, the\n\t\t// current value is the wrong type and needs upgrading. Otherwise,\n\t\t// only replace if the raw value actually changed.\n\t\tif ( newYValue !== newVal || ! fastDeepEqual( currentVal, newVal ) ) {\n\t\t\tyMap.set( key, newYValue );\n\t\t}\n\t}\n}\n\n/**\n * Merge an incoming plain object into an existing Y.Map in-place, using\n * the query schema to decide how each property should be merged.\n *\n * Properties present in the Y.Map but absent from `newObj` are deleted.\n *\n * @param yMap The existing Y.Map to update.\n * @param newObj The new plain object to merge into the Y.Map.\n * @param query The query schema defining property types.\n * @param cursorPosition The local cursor position for rich-text delta merges.\n * @param cursorScope The selected block attribute scope for rich-text cursor hints.\n */\nfunction mergeYMapValues(\n\tyMap: Y.Map< unknown >,\n\tnewObj: Record< string, unknown >,\n\tquery: Record< string, BlockAttributeSchema >,\n\tcursorPosition: MergeCursorPosition,\n\tcursorScope: RichTextCursorScope\n): void {\n\tfor ( const [ key, newVal ] of Object.entries( newObj ) ) {\n\t\tmergeYValue(\n\t\t\tquery[ key ],\n\t\t\tnewVal,\n\t\t\tyMap,\n\t\t\tkey,\n\t\t\tcursorPosition,\n\t\t\tcursorScope\n\t\t);\n\t}\n\n\t// Delete properties absent from the incoming object.\n\tfor ( const key of yMap.keys() ) {\n\t\tif ( ! Object.hasOwn( newObj, key ) ) {\n\t\t\tyMap.delete( key );\n\t\t}\n\t}\n}\n\n/**\n * Update a single attribute on a Yjs block attributes map (currentAttributes).\n *\n * @param blockName The block type name, e.g. 'core/paragraph'.\n * @param clientId The local clientId for the block being merged.\n * @param attributeName The name of the attribute to update, e.g. 'content'.\n * @param attributeValue The new value for the attribute.\n * @param currentAttributes The Y.Map holding the block's current attributes.\n * @param newCursorPosition The cursor position for rich-text delta merges from the updated value.\n * Notably, this may not correspond to the attribute being edited and is\n * used to determine if any cursors need shifting in response to the change.\n */\nfunction updateYBlockAttribute(\n\tblockName: string,\n\tclientId: string | undefined,\n\tattributeName: string,\n\tattributeValue: unknown,\n\tcurrentAttributes: YBlockAttributes,\n\tnewCursorPosition: MergeCursorPosition\n): void {\n\tconst schema = getBlockAttributeSchema( blockName, attributeName );\n\n\t/*\n\t * @todo There is a slight discrepancy between the attribute name and key, which might\n\t * show up when working with multiline RichText instances (of which there are no\n\t * more within Core). For those instances, a cursor might never be updated in\n\t * response to changes because its `attributeKey` won\u2019t match any of the block\u2019s\n\t * attribute names, and since updating this attribute is based on the block names,\n\t * no suitable key for the cursor scope will be created. To fix, the updating code\n\t * would need to parse multiline attributes and infer the `attributeKey` being updated.\n\t */\n\tmergeYValue(\n\t\tschema,\n\t\tattributeValue,\n\t\tcurrentAttributes,\n\t\tattributeName,\n\t\tnewCursorPosition,\n\t\t{ attributeKey: attributeName, clientId }\n\t);\n}\n\n/**\n * References the specific block and attribute associated with a RichText component.\n *\n * This is used to associate a cursor with the attribute it\u2019s editing.\n *\n * @see WPBlockSelection\n */\ninterface RichTextCursorScope {\n\tattributeKey: string;\n\tclientId: string | undefined;\n}\n\ninterface DeltaWithOps {\n\tops: Parameters< Y.Text[ 'applyDelta' ] >[ 0 ];\n}\n\n/**\n * When the provided cursor falls within the given block and attribute\u2019s scope,\n * returns an index into the RichText\u2019s serialized HTML where the cursor falls.\n *\n * The cursor scope constrains resolution to ensure that indices are only reported\n * when a cursor falls within the block and attribute being updated, since the\n * attributes being updated may not always be the ones where a cursor presently falls.\n *\n * Returned index measures JS string lengths, thus is counted in UTF-16 code units\n * and contains the syntax characters making up HTML tags, comments, character\n * references, and other non-plaintext content.\n *\n * @param cursorPosition Description of the cursor in the new value.\n * @param cursorScope Cursors should only be updated if they fall within this\n * specific block and attribute.\n * @param updatedValue New RichText value potentially containing cursor.\n * @return String length into serialized HTML for RichText instance where cursor falls.\n */\nfunction resolveRichTextCursorPosition(\n\tcursorPosition: MergeCursorPosition,\n\tcursorScope: RichTextCursorScope,\n\tupdatedValue: string\n): HtmlStringIndex | null {\n\treturn cursorPosition &&\n\t\tcursorPosition.clientId === cursorScope.clientId &&\n\t\tcursorPosition.attributeKey === cursorScope.attributeKey &&\n\t\t'number' === typeof cursorPosition.offset &&\n\t\tNumber.isInteger( cursorPosition.offset )\n\t\t? richTextOffsetToHtmlIndex(\n\t\t\t\tupdatedValue,\n\t\t\t\tasRichTextOffset( cursorPosition.offset )\n\t\t )\n\t\t: null;\n}\n\n// Cached block attribute types, populated once from getBlockTypes().\nlet cachedBlockAttributeSchemas: Map<\n\tstring,\n\tMap< string, BlockAttributeSchema >\n>;\n\n/**\n * Get the attribute type definition for a block attribute.\n *\n * @param blockName The name of the block, e.g. 'core/paragraph'.\n * @param attributeName The name of the attribute, e.g. 'content'.\n * @return The type definition of the attribute.\n */\nfunction getBlockAttributeSchema(\n\tblockName: string,\n\tattributeName: string\n): BlockAttributeSchema | undefined {\n\tif ( ! cachedBlockAttributeSchemas ) {\n\t\t// Parse the attributes for all blocks once.\n\t\tcachedBlockAttributeSchemas = new Map();\n\n\t\tfor ( const blockType of getBlockTypes() as BlockType[] ) {\n\t\t\tcachedBlockAttributeSchemas.set(\n\t\t\t\tblockType.name,\n\t\t\t\tnew Map< string, BlockAttributeSchema >(\n\t\t\t\t\tObject.entries( blockType.attributes ?? {} ).map(\n\t\t\t\t\t\t( [ name, definition ] ) => {\n\t\t\t\t\t\t\tconst { role, type, query } = definition;\n\t\t\t\t\t\t\treturn [ name, { role, type, query } ];\n\t\t\t\t\t\t}\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\t}\n\n\treturn cachedBlockAttributeSchemas.get( blockName )?.get( attributeName );\n}\n\n/**\n * Check if an attribute value is the expected type.\n *\n * @param blockName The name of the block, e.g. 'core/paragraph'.\n * @param attributeName The name of the attribute, e.g. 'content'.\n * @param attributeValue The current attribute value.\n * @return True if the attribute type is expected, false otherwise.\n */\nfunction isExpectedAttributeType(\n\tblockName: string,\n\tattributeName: string,\n\tattributeValue: unknown\n): boolean {\n\tconst schema = getBlockAttributeSchema( blockName, attributeName );\n\n\tif ( schema?.type === 'rich-text' ) {\n\t\treturn attributeValue instanceof Y.Text;\n\t}\n\n\tif ( schema?.type === 'string' ) {\n\t\treturn typeof attributeValue === 'string';\n\t}\n\n\tif ( schema?.type === 'array' && schema.query ) {\n\t\treturn attributeValue instanceof Y.Array;\n\t}\n\n\tif ( schema?.type === 'object' && schema.query ) {\n\t\treturn attributeValue instanceof Y.Map;\n\t}\n\n\treturn true;\n}\n\n/**\n * Given a block name and attribute key, return true if the attribute is local\n * and should not be synced.\n *\n * @param blockName The name of the block, e.g. 'core/image'.\n * @param attributeName The name of the attribute to check, e.g. 'blob'.\n * @return True if the attribute is local, false otherwise.\n */\nfunction isLocalAttribute( blockName: string, attributeName: string ): boolean {\n\treturn (\n\t\t'local' === getBlockAttributeSchema( blockName, attributeName )?.role\n\t);\n}\n\nlet localDoc: Y.Doc;\n\n/**\n * Given a Y.Text object and an updated string value, diff the new value and\n * apply the delta to the Y.Text.\n *\n * @param blockYText The Y.Text to update.\n * @param updatedValue The updated value.\n * @param htmlCursorIndex The cursor index in the updated HTML string.\n */\nexport function mergeRichTextUpdate(\n\tblockYText: Y.Text,\n\tupdatedValue: string,\n\thtmlCursorIndex: HtmlStringIndex | null = null\n): void {\n\t// Gutenberg does not use Yjs shared types natively, so we can only subscribe\n\t// to changes from store and apply them to Yjs types that we create and\n\t// manage. Crucially, for rich-text attributes, we do not receive granular\n\t// string updates; we get the new full string value on each change, even when\n\t// only a single character changed.\n\t//\n\t// The code below allows us to compute a delta between the current and new\n\t// value, then apply it to the Y.Text.\n\n\tconst currentValueAsDelta = new Delta( blockYText.toDelta() );\n\tconst updatedValueAsDelta = new Delta( [ { insert: updatedValue } ] );\n\tconst deltaDiff = currentValueAsDelta.diffWithCursor(\n\t\tupdatedValueAsDelta,\n\t\thtmlCursorIndex\n\t);\n\n\t/**\n\t * When there is no cursor involved, or when the diff is able to shuffle properly\n\t * around the cursor then apply that already-computed diff.\n\t *\n\t * However, `diffWithCursor()` currently fails in certain cases, producing corrupted\n\t * output. In these cases, fall back to the raw diff as that will apply cleanly,\n\t * even if it provides a less meaningful diff.\n\t *\n\t * @see Delta.diffWithCursor()\n\t */\n\tconst safeDiff =\n\t\thtmlCursorIndex === null ||\n\t\tisDeltaVerificationMatch( blockYText, deltaDiff, updatedValue )\n\t\t\t? deltaDiff\n\t\t\t: currentValueAsDelta.diff( updatedValueAsDelta );\n\n\tblockYText.applyDelta( safeDiff.ops );\n}\n\n/**\n * Verify that applying a delta to an existing Y.Text object produces the expected\n * output string.\n *\n * A stale, mis-scoped, or corrupted Delta will mutate a text value to the wrong\n * output string. This function applies the given Delta and indicates whether it\n * produces the given expected output string value.\n *\n * @param blockYText The current Y.Text before applying the candidate delta.\n * @param delta The candidate delta.\n * @param expectedValue The exact string expected after applying the delta.\n * @return Whether the candidate delta produces the expected value.\n */\nfunction isDeltaVerificationMatch(\n\tblockYText: Y.Text,\n\tdelta: DeltaWithOps,\n\texpectedValue: string\n): boolean {\n\tif ( ! localDoc ) {\n\t\t// Y.Text must be attached to a Y.Doc to be able to do operations on it.\n\t\t// Create a temporary Y.Text attached to a local Y.Doc for delta computation.\n\t\t// This is an optimization to avoid creating a new Y.Doc on every update.\n\t\tlocalDoc = new Y.Doc();\n\t}\n\n\tconst verificationYText = localDoc.getText( 'verification-text' );\n\n\t// Because this is global, it must be cleared before using.\n\tverificationYText.delete( 0, verificationYText.length );\n\tverificationYText.insert( 0, blockYText.toString() );\n\tverificationYText.applyDelta( delta.ops );\n\n\treturn verificationYText.toString() === expectedValue;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA6B;AAC7B,iBAA0B;AAK1B,oBAA8B;AAC9B,uBAA6B;AAC7B,kBAAkB;AAKlB,wBAOO;AACP,uBAAsC;AACtC,IAAAA,eAAsB;AAsDtB,IAAM,0BAA0B,oBAAI,QAA4B;AAUhE,SAAS,wBAAyB,OAA0B;AAC3D,MAAK,iBAAiB,+BAAe;AACpC,WAAO,MAAM,QAAQ;AAAA,EACtB;AAGA,MAAK,MAAM,QAAS,KAAM,GAAI;AAC7B,WAAO,MAAM,IAAK,uBAAwB;AAAA,EAC3C;AAGA,MAAK,SAAS,OAAO,UAAU,UAAW;AACzC,UAAM,SAAoC,CAAC;AAE3C,eAAY,CAAE,GAAG,CAAE,KAAK,OAAO,QAAS,KAAM,GAAI;AACjD,aAAQ,CAAE,IAAI,wBAAyB,CAAE;AAAA,IAC1C;AACA,WAAO;AAAA,EACR;AAEA,SAAO;AACR;AAEA,SAAS,gCACR,WACA,YACkB;AAClB,QAAM,gBAAgB,EAAE,GAAG,WAAW;AACtC,aAAY,CAAE,KAAK,KAAM,KAAK,OAAO,QAAS,UAAW,GAAI;AAC5D,QAAK,iBAAkB,WAAW,GAAI,GAAI;AACzC,aAAO,cAAe,GAAI;AAC1B;AAAA,IACD;AAEA,kBAAe,GAAI,IAAI,wBAAyB,KAAM;AAAA,EACvD;AACA,SAAO;AACR;AAQA,S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sBAAsB,IAAI,mBAAO,WAAW,QAAQ,CAAE;AAC5D,QAAM,sBAAsB,IAAI,mBAAO,CAAE,EAAE,QAAQ,aAAa,CAAE,CAAE;AACpE,QAAM,YAAY,oBAAoB;AAAA,IACrC;AAAA,IACA;AAAA,EACD;AAYA,QAAM,WACL,oBAAoB,QACpB,yBAA0B,YAAY,WAAW,YAAa,IAC3D,YACA,oBAAoB,KAAM,mBAAoB;AAElD,aAAW,WAAY,SAAS,GAAI;AACrC;AAeA,SAAS,yBACR,YACA,OACA,eACU;AACV,MAAK,CAAE,UAAW;AAIjB,eAAW,IAAI,cAAE,IAAI;AAAA,EACtB;AAEA,QAAM,oBAAoB,SAAS,QAAS,mBAAoB;AAGhE,oBAAkB,OAAQ,GAAG,kBAAkB,MAAO;AACtD,oBAAkB,OAAQ,GAAG,WAAW,SAAS,CAAE;AACnD,oBAAkB,WAAY,MAAM,GAAI;AAExC,SAAO,kBAAkB,SAAS,MAAM;AACzC;",
6
6
  "names": ["import_sync", "fastDeepEqual", "uuidv4"]
7
7
  }
@@ -60,7 +60,7 @@ function convertYSelectionToBlockSelection(ySelection, ydoc) {
60
60
  attributeKey,
61
61
  offset: (0, import_crdt_utils.htmlIndexToRichTextOffset)(
62
62
  absolutePosition.type.toString(),
63
- absolutePosition.index
63
+ (0, import_crdt_utils.asHtmlStringIndex)(absolutePosition.index)
64
64
  )
65
65
  };
66
66
  }