@portabletext/editor 1.49.3 → 1.49.4

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.49.3",
3
+ "version": "1.49.4",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -444,48 +444,69 @@ async function updateValue({
444
444
  schemaTypes: context.schema,
445
445
  })
446
446
 
447
- await new Promise<void>((resolve) => {
448
- Editor.withoutNormalizing(slateEditor, () => {
449
- withRemoteChanges(slateEditor, () => {
450
- withoutPatching(slateEditor, async () => {
451
- const childrenLength = slateEditor.children.length
447
+ if (streamBlocks) {
448
+ await new Promise<void>((resolve) => {
449
+ Editor.withoutNormalizing(slateEditor, () => {
450
+ withRemoteChanges(slateEditor, () => {
451
+ withoutPatching(slateEditor, async () => {
452
+ isChanged = removeExtraBlocks({
453
+ slateEditor,
454
+ slateValueFromProps,
455
+ })
452
456
 
453
- // Remove blocks that have become superfluous
454
- if (slateValueFromProps.length < childrenLength) {
455
- for (
456
- let i = childrenLength - 1;
457
- i > slateValueFromProps.length - 1;
458
- i--
459
- ) {
460
- Transforms.removeNodes(slateEditor, {
461
- at: [i],
457
+ for await (const [
458
+ currentBlock,
459
+ currentBlockIndex,
460
+ ] of getStreamedBlocks({
461
+ slateValue: slateValueFromProps,
462
+ })) {
463
+ const {blockChanged, blockValid} = syncBlock({
464
+ context,
465
+ sendBack,
466
+ block: currentBlock,
467
+ index: currentBlockIndex,
468
+ slateEditor,
469
+ value,
462
470
  })
471
+
472
+ isChanged = blockChanged || isChanged
473
+ isValid = isValid && blockValid
463
474
  }
464
- isChanged = true
465
- }
466
475
 
467
- for await (const [currentBlock, currentBlockIndex] of getBlocks({
468
- slateValue: slateValueFromProps,
469
- streamBlocks,
470
- })) {
471
- // Go through all of the blocks and see if they need to be updated
476
+ resolve()
477
+ })
478
+ })
479
+ })
480
+ })
481
+ } else {
482
+ Editor.withoutNormalizing(slateEditor, () => {
483
+ withRemoteChanges(slateEditor, () => {
484
+ withoutPatching(slateEditor, () => {
485
+ isChanged = removeExtraBlocks({
486
+ slateEditor,
487
+ slateValueFromProps,
488
+ })
489
+
490
+ let index = 0
491
+
492
+ for (const currentBlock of slateValueFromProps) {
472
493
  const {blockChanged, blockValid} = syncBlock({
473
494
  context,
474
495
  sendBack,
475
496
  block: currentBlock,
476
- index: currentBlockIndex,
497
+ index,
477
498
  slateEditor,
478
499
  value,
479
500
  })
501
+
480
502
  isChanged = blockChanged || isChanged
481
503
  isValid = isValid && blockValid
504
+ index++
482
505
  }
483
-
484
- resolve()
485
506
  })
486
507
  })
487
508
  })
488
- })
509
+ }
489
510
  }
490
511
 
491
512
  if (!isValid) {
@@ -523,16 +544,36 @@ async function updateValue({
523
544
  sendBack({type: 'done syncing', value})
524
545
  }
525
546
 
526
- async function* getBlocks({
547
+ function removeExtraBlocks({
548
+ slateEditor,
549
+ slateValueFromProps,
550
+ }: {
551
+ slateEditor: PortableTextSlateEditor
552
+ slateValueFromProps: Array<Descendant>
553
+ }) {
554
+ let isChanged = false
555
+ const childrenLength = slateEditor.children.length
556
+
557
+ // Remove blocks that have become superfluous
558
+ if (slateValueFromProps.length < childrenLength) {
559
+ for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--) {
560
+ Transforms.removeNodes(slateEditor, {
561
+ at: [i],
562
+ })
563
+ }
564
+ isChanged = true
565
+ }
566
+ return isChanged
567
+ }
568
+
569
+ async function* getStreamedBlocks({
527
570
  slateValue,
528
- streamBlocks,
529
571
  }: {
530
572
  slateValue: Array<Descendant>
531
- streamBlocks: boolean
532
573
  }) {
533
574
  let index = 0
534
575
  for await (const block of slateValue) {
535
- if (streamBlocks && index % 10 === 0) {
576
+ if (index % 10 === 0) {
536
577
  await new Promise<void>((resolve) => setTimeout(resolve, 0))
537
578
  }
538
579
  yield [block, index] as const