@portabletext/editor 1.0.16 → 1.0.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -51,6 +51,7 @@
51
51
  },
52
52
  "devDependencies": {
53
53
  "@jest/globals": "^29.7.0",
54
+ "@jest/types": "^29.6.3",
54
55
  "@playwright/test": "1.46.1",
55
56
  "@portabletext/toolkit": "^2.0.15",
56
57
  "@sanity/block-tools": "^3.55.0",
@@ -419,16 +419,7 @@ export function createWithEditableAPI(
419
419
  )
420
420
  }
421
421
  editor.onChange()
422
- if (editor.selection) {
423
- // Insert an empty string to continue writing non-annotated text
424
- Transforms.insertNodes(
425
- editor,
426
- [{_type: 'span', text: '', marks: [], _key: keyGenerator()}],
427
- {
428
- at: Range.end(editor.selection),
429
- },
430
- )
431
- }
422
+
432
423
  const newPortableTextEditorSelection = toPortableTextRange(
433
424
  fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
434
425
  editor.selection,
@@ -264,9 +264,19 @@ export function createOperationToPatches(types: PortableTextMemberSchemaTypes):
264
264
  }
265
265
  throw new Error('Block not found')
266
266
  } else if (editor.isTextBlock(block) && operation.path.length === 2) {
267
- const spanToRemove =
268
- editor.isTextBlock(block) && block.children && block.children[operation.path[1]]
267
+ const spanToRemove = block.children[operation.path[1]]
268
+
269
269
  if (spanToRemove) {
270
+ const spansMatchingKey = block.children.filter((span) => span._key === operation.node._key)
271
+
272
+ if (spansMatchingKey.length > 1) {
273
+ console.warn(
274
+ `Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
275
+ JSON.stringify(block, null, 2),
276
+ )
277
+ return []
278
+ }
279
+
270
280
  return [unset([{_key: block._key}, 'children', {_key: spanToRemove._key}])]
271
281
  }
272
282
  debug('Span not found in editor trying to remove node')
@@ -285,7 +295,7 @@ export function createOperationToPatches(types: PortableTextMemberSchemaTypes):
285
295
  const patches: Patch[] = []
286
296
 
287
297
  const block = beforeValue[operation.path[0]]
288
- const targetBlock = editor.children[operation.path[0]]
298
+ const updatedBlock = editor.children[operation.path[0]]
289
299
 
290
300
  if (operation.path.length === 1) {
291
301
  if (block?._key) {
@@ -295,17 +305,51 @@ export function createOperationToPatches(types: PortableTextMemberSchemaTypes):
295
305
  } else {
296
306
  throw new Error('Target key not found!')
297
307
  }
298
- } else if (operation.path.length === 2 && editor.isTextBlock(targetBlock)) {
299
- const mergedSpan =
300
- (editor.isTextBlock(block) && block.children[operation.path[1]]) || undefined
301
- const targetSpan = targetBlock.children[operation.path[1] - 1]
302
- if (editor.isTextSpan(targetSpan)) {
303
- // Set the merged span with it's new value
304
- patches.push(
305
- set(targetSpan.text, [{_key: block._key}, 'children', {_key: targetSpan._key}, 'text']),
306
- )
307
- if (mergedSpan) {
308
- patches.push(unset([{_key: block._key}, 'children', {_key: mergedSpan._key}]))
308
+ } else if (
309
+ editor.isTextBlock(block) &&
310
+ editor.isTextBlock(updatedBlock) &&
311
+ operation.path.length === 2
312
+ ) {
313
+ const updatedSpan =
314
+ updatedBlock.children[operation.path[1] - 1] &&
315
+ editor.isTextSpan(updatedBlock.children[operation.path[1] - 1])
316
+ ? updatedBlock.children[operation.path[1] - 1]
317
+ : undefined
318
+ const removedSpan =
319
+ block.children[operation.path[1]] && editor.isTextSpan(block.children[operation.path[1]])
320
+ ? block.children[operation.path[1]]
321
+ : undefined
322
+
323
+ if (updatedSpan) {
324
+ const spansMatchingKey = block.children.filter((span) => span._key === updatedSpan._key)
325
+
326
+ if (spansMatchingKey.length === 1) {
327
+ patches.push(
328
+ set(updatedSpan.text, [
329
+ {_key: block._key},
330
+ 'children',
331
+ {_key: updatedSpan._key},
332
+ 'text',
333
+ ]),
334
+ )
335
+ } else {
336
+ console.warn(
337
+ `Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`,
338
+ JSON.stringify(block, null, 2),
339
+ )
340
+ }
341
+ }
342
+
343
+ if (removedSpan) {
344
+ const spansMatchingKey = block.children.filter((span) => span._key === removedSpan._key)
345
+
346
+ if (spansMatchingKey.length === 1) {
347
+ patches.push(unset([{_key: block._key}, 'children', {_key: removedSpan._key}]))
348
+ } else {
349
+ console.warn(
350
+ `Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`,
351
+ JSON.stringify(block, null, 2),
352
+ )
309
353
  }
310
354
  }
311
355
  } else {