@portabletext/editor 4.1.5 → 4.2.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.
@@ -3,7 +3,7 @@ import { AnnotationDefinition, AnnotationSchemaType, BaseDefinition, BlockObject
3
3
  import { BaseRange, Descendant, Operation, Range } from "slate";
4
4
  import * as xstate71 from "xstate";
5
5
  import { ActorRef, ActorRefFrom, EventObject, Snapshot } from "xstate";
6
- import * as react6 from "react";
6
+ import * as react10 from "react";
7
7
  import React$1, { BaseSyntheticEvent, ClipboardEvent, Component, FocusEvent, JSX, KeyboardEvent as KeyboardEvent$1, MutableRefObject, PropsWithChildren, ReactElement, RefObject, TextareaHTMLAttributes } from "react";
8
8
  import { Patch, Patch as Patch$1 } from "@portabletext/patches";
9
9
  import { Observable, Subject } from "rxjs";
@@ -311,7 +311,7 @@ declare class PortableTextEditor extends Component<PortableTextEditorProps<Inter
311
311
  componentDidUpdate(prevProps: PortableTextEditorProps): void;
312
312
  componentWillUnmount(): void;
313
313
  setEditable: (editable: EditableAPI) => void;
314
- render(): react6.JSX.Element;
314
+ render(): react10.JSX.Element;
315
315
  /**
316
316
  * @deprecated
317
317
  * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
@@ -720,7 +720,7 @@ type PortableTextEditableProps = Omit<TextareaHTMLAttributes<HTMLDivElement>, 'o
720
720
  * ```
721
721
  * @group Components
722
722
  */
723
- declare const PortableTextEditable: react6.ForwardRefExoticComponent<Omit<PortableTextEditableProps, "ref"> & react6.RefAttributes<Omit<HTMLDivElement, "as" | "onPaste" | "onBeforeInput">>>;
723
+ declare const PortableTextEditable: react10.ForwardRefExoticComponent<Omit<PortableTextEditableProps, "ref"> & react10.RefAttributes<Omit<HTMLDivElement, "as" | "onPaste" | "onBeforeInput">>>;
724
724
  /** @beta */
725
725
  interface EditableAPIDeleteOptions {
726
726
  mode?: 'blocks' | 'children' | 'selected';
@@ -1511,7 +1511,7 @@ declare const editorMachine: xstate71.StateMachine<{
1511
1511
  initialValue?: Array<PortableTextBlock>;
1512
1512
  }, xstate71.NonReducibleUnknown, InternalPatchEvent | MutationEvent | PatchesEvent | {
1513
1513
  type: "blurred";
1514
- event: react6.FocusEvent<HTMLDivElement, Element>;
1514
+ event: react10.FocusEvent<HTMLDivElement, Element>;
1515
1515
  } | {
1516
1516
  type: "done loading";
1517
1517
  } | {
@@ -1523,7 +1523,7 @@ declare const editorMachine: xstate71.StateMachine<{
1523
1523
  data: unknown;
1524
1524
  } | {
1525
1525
  type: "focused";
1526
- event: react6.FocusEvent<HTMLDivElement, Element>;
1526
+ event: react10.FocusEvent<HTMLDivElement, Element>;
1527
1527
  } | {
1528
1528
  type: "invalid value";
1529
1529
  resolution: InvalidValueResolution | null;
@@ -1804,6 +1804,7 @@ type AbstractBehaviorEvent = {
1804
1804
  blocks: Array<BlockWithOptionalKey>;
1805
1805
  placement: InsertPlacement;
1806
1806
  select?: 'start' | 'end' | 'none';
1807
+ at?: NonNullable<EditorSelection>;
1807
1808
  } | {
1808
1809
  type: StrictExtract<SyntheticBehaviorEventType, 'insert.break'>;
1809
1810
  } | {
package/lib/index.js CHANGED
@@ -8477,11 +8477,11 @@ function insertBlock(options) {
8477
8477
  }
8478
8478
  }
8479
8479
  } else {
8480
- Transforms.insertNodes(editor, [block], {
8481
- select: !1
8482
- });
8483
8480
  const nextPath = [endBlockPath[0] + 1];
8484
- select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
8481
+ Transforms.insertNodes(editor, [block], {
8482
+ select: !1,
8483
+ at: nextPath
8484
+ }), select === "start" ? Transforms.select(editor, Editor.start(editor, nextPath)) : select === "end" && Transforms.select(editor, Editor.end(editor, nextPath));
8485
8485
  }
8486
8486
  }
8487
8487
  }
@@ -9349,104 +9349,70 @@ const abstractInsertBehaviors = [
9349
9349
  type: "insert.block",
9350
9350
  block: onlyBlock,
9351
9351
  placement: event.placement,
9352
- select: event.select ?? "end"
9352
+ select: event.select ?? "end",
9353
+ ...event.at ? {
9354
+ at: event.at
9355
+ } : {}
9353
9356
  })]]
9354
9357
  }),
9355
9358
  defineBehavior({
9356
9359
  on: "insert.blocks",
9357
9360
  guard: ({
9358
- snapshot,
9359
9361
  event
9360
- }) => {
9361
- if (event.placement !== "before")
9362
- return !1;
9363
- const firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9364
- return {
9365
- firstBlockKey,
9366
- lastBlockKey
9367
- };
9368
- },
9362
+ }) => event.placement === "before" || event.placement === "after",
9369
9363
  actions: [({
9370
9364
  snapshot,
9371
9365
  event
9372
- }, {
9373
- firstBlockKey,
9374
- lastBlockKey
9375
- }) => [...event.blocks.map((block, index) => raise({
9376
- type: "insert.block",
9377
- block: index === 0 ? {
9378
- ...block,
9379
- _key: firstBlockKey
9380
- } : index === event.blocks.length - 1 ? {
9381
- ...block,
9382
- _key: lastBlockKey
9383
- } : block,
9384
- placement: index === 0 ? "before" : "after",
9385
- select: index !== event.blocks.length - 1 ? "end" : "none"
9386
- })), ...event.select === "none" ? [raise({
9387
- type: "select",
9388
- at: snapshot.context.selection
9389
- })] : event.select === "start" ? [raise({
9390
- type: "select.block",
9391
- at: [{
9392
- _key: firstBlockKey
9393
- }],
9394
- select: "start"
9395
- })] : [raise({
9396
- type: "select.block",
9397
- at: [{
9398
- _key: lastBlockKey
9399
- }],
9400
- select: "end"
9401
- })]]]
9402
- }),
9403
- defineBehavior({
9404
- on: "insert.blocks",
9405
- guard: ({
9406
- snapshot,
9407
- event
9408
9366
  }) => {
9409
- if (event.placement !== "after")
9410
- return !1;
9411
- const firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9412
- return {
9413
- firstBlockKey,
9414
- lastBlockKey
9415
- };
9416
- },
9417
- actions: [({
9418
- snapshot,
9419
- event
9420
- }, {
9421
- firstBlockKey,
9422
- lastBlockKey
9423
- }) => [...event.blocks.map((block, index) => raise({
9424
- type: "insert.block",
9425
- block: index === 0 ? {
9426
- ...block,
9427
- _key: firstBlockKey
9428
- } : index === event.blocks.length - 1 ? {
9429
- ...block,
9430
- _key: lastBlockKey
9431
- } : block,
9432
- placement: "after",
9433
- select: index !== event.blocks.length - 1 ? "end" : "none"
9434
- })), ...event.select === "none" ? [raise({
9435
- type: "select",
9436
- at: snapshot.context.selection
9437
- })] : event.select === "start" ? [raise({
9438
- type: "select.block",
9439
- at: [{
9440
- _key: firstBlockKey
9441
- }],
9442
- select: "start"
9443
- })] : [raise({
9444
- type: "select.block",
9445
- at: [{
9446
- _key: lastBlockKey
9447
- }],
9448
- select: "end"
9449
- })]]]
9367
+ let firstBlockKey, lastBlockKey, previousBlockKey;
9368
+ const actions = [];
9369
+ let index = -1;
9370
+ for (const block of event.blocks) {
9371
+ index++;
9372
+ const key = getUniqueBlockKey(block._key)(snapshot);
9373
+ index === 0 && (firstBlockKey = key), index === event.blocks.length - 1 && (lastBlockKey = key), actions.push(raise({
9374
+ type: "insert.block",
9375
+ block: key !== block._key ? {
9376
+ ...block,
9377
+ _key: key
9378
+ } : block,
9379
+ placement: event.placement === "after" ? "after" : index === 0 ? "before" : "after",
9380
+ select: "none",
9381
+ ...previousBlockKey ? {
9382
+ at: {
9383
+ anchor: {
9384
+ path: [{
9385
+ _key: previousBlockKey
9386
+ }],
9387
+ offset: 0
9388
+ },
9389
+ focus: {
9390
+ path: [{
9391
+ _key: previousBlockKey
9392
+ }],
9393
+ offset: 0
9394
+ }
9395
+ }
9396
+ } : event.at ? {
9397
+ at: event.at
9398
+ } : {}
9399
+ })), previousBlockKey = key;
9400
+ }
9401
+ const select = event.select ?? "end";
9402
+ return select === "start" && firstBlockKey && actions.push(raise({
9403
+ type: "select.block",
9404
+ at: [{
9405
+ _key: firstBlockKey
9406
+ }],
9407
+ select: "start"
9408
+ })), select === "end" && lastBlockKey && actions.push(raise({
9409
+ type: "select.block",
9410
+ at: [{
9411
+ _key: lastBlockKey
9412
+ }],
9413
+ select: "end"
9414
+ })), actions;
9415
+ }]
9450
9416
  }),
9451
9417
  defineBehavior({
9452
9418
  on: "insert.blocks",
@@ -9456,62 +9422,17 @@ const abstractInsertBehaviors = [
9456
9422
  }) => {
9457
9423
  if (event.placement !== "auto")
9458
9424
  return !1;
9459
- const focusTextBlock = getFocusTextBlock(snapshot);
9460
- if (!focusTextBlock || !isEmptyTextBlock(snapshot.context, focusTextBlock.node))
9425
+ const at = event.at ?? snapshot.context.selection;
9426
+ if (!at)
9461
9427
  return !1;
9462
- const firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9463
- return {
9464
- focusTextBlock,
9465
- firstBlockKey,
9466
- lastBlockKey
9467
- };
9468
- },
9469
- actions: [({
9470
- event
9471
- }, {
9472
- firstBlockKey,
9473
- lastBlockKey
9474
- }) => [...event.blocks.map((block, index) => raise({
9475
- type: "insert.block",
9476
- block: index === 0 ? {
9477
- ...block,
9478
- _key: firstBlockKey
9479
- } : index === event.blocks.length - 1 ? {
9480
- ...block,
9481
- _key: lastBlockKey
9482
- } : block,
9483
- placement: index === 0 ? "auto" : "after",
9484
- select: index !== event.blocks.length - 1 ? "end" : "none"
9485
- })), ...event.select === "none" ? [raise({
9486
- type: "select.block",
9487
- at: [{
9488
- _key: firstBlockKey
9489
- }],
9490
- select: "start"
9491
- })] : event.select === "start" ? [raise({
9492
- type: "select.block",
9493
- at: [{
9494
- _key: firstBlockKey
9495
- }],
9496
- select: "start"
9497
- })] : [raise({
9498
- type: "select.block",
9499
- at: [{
9500
- _key: lastBlockKey
9501
- }],
9502
- select: "end"
9503
- })]]]
9504
- }),
9505
- defineBehavior({
9506
- on: "insert.blocks",
9507
- guard: ({
9508
- snapshot,
9509
- event
9510
- }) => {
9511
- if (event.placement !== "auto")
9512
- return !1;
9513
- const focusTextBlock = getFocusTextBlock(snapshot);
9514
- if (!focusTextBlock || !snapshot.context.selection)
9428
+ const adjustedSnapshot = {
9429
+ ...snapshot,
9430
+ context: {
9431
+ ...snapshot.context,
9432
+ selection: at
9433
+ }
9434
+ }, focusTextBlock = getFocusTextBlock(adjustedSnapshot);
9435
+ if (!focusTextBlock || isEmptyTextBlock(snapshot.context, focusTextBlock.node))
9515
9436
  return !1;
9516
9437
  const focusBlockStartPoint = getBlockStartPoint({
9517
9438
  context: snapshot.context,
@@ -9523,19 +9444,19 @@ const abstractInsertBehaviors = [
9523
9444
  context: {
9524
9445
  schema: snapshot.context.schema,
9525
9446
  selection: {
9526
- anchor: snapshot.context.selection.focus,
9447
+ anchor: at.focus,
9527
9448
  focus: focusBlockEndPoint
9528
9449
  }
9529
9450
  },
9530
9451
  block: focusTextBlock.node
9531
- }), firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot), isFirstBlockTextBlock = isTextBlock(snapshot.context, event.blocks.at(0));
9452
+ }), isFirstBlockTextBlock = isTextBlock(snapshot.context, event.blocks.at(0));
9532
9453
  return {
9533
- firstBlockKey,
9534
- lastBlockKey,
9454
+ focusTextBlock,
9535
9455
  focusBlockStartPoint,
9536
9456
  focusBlockEndPoint,
9537
9457
  focusTextBlockAfter,
9538
- selection: snapshot.context.selection,
9458
+ at,
9459
+ originalSelection: snapshot.context.selection,
9539
9460
  isFirstBlockTextBlock
9540
9461
  };
9541
9462
  },
@@ -9543,110 +9464,178 @@ const abstractInsertBehaviors = [
9543
9464
  snapshot,
9544
9465
  event
9545
9466
  }, {
9467
+ focusTextBlock,
9546
9468
  focusBlockEndPoint,
9547
9469
  focusTextBlockAfter,
9548
- selection,
9549
- firstBlockKey,
9550
- lastBlockKey,
9470
+ at,
9551
9471
  focusBlockStartPoint,
9552
- isFirstBlockTextBlock
9553
- }) => [...event.blocks.flatMap((block, index) => index === 0 ? [...isEqualSelectionPoints(selection.focus, focusBlockEndPoint) ? [] : [raise({
9554
- type: "delete",
9555
- at: {
9556
- anchor: selection.focus,
9557
- focus: focusBlockEndPoint
9558
- }
9559
- })], raise({
9560
- type: "insert.block",
9561
- block: {
9562
- ...block,
9563
- _key: firstBlockKey
9564
- },
9565
- placement: "auto",
9566
- select: "end"
9567
- })] : index === event.blocks.length - 1 ? [raise({
9568
- type: "insert.block",
9569
- block: {
9570
- ...block,
9571
- _key: lastBlockKey
9572
- },
9573
- placement: "after",
9574
- select: "end"
9575
- }), ...isEmptyTextBlock(snapshot.context, focusTextBlockAfter) ? [] : [raise({
9576
- type: "insert.block",
9577
- block: focusTextBlockAfter,
9578
- placement: "auto",
9579
- select: event.select === "end" ? "none" : "end"
9580
- })]] : [raise({
9581
- type: "insert.block",
9582
- block,
9583
- placement: "after",
9584
- select: "end"
9585
- })]), ...event.select === "none" ? [raise({
9586
- type: "select",
9587
- at: selection
9588
- })] : event.select === "start" ? [isEqualSelectionPoints(selection.focus, focusBlockStartPoint) || !isFirstBlockTextBlock ? raise({
9589
- type: "select.block",
9590
- at: [{
9591
- _key: firstBlockKey
9592
- }],
9593
- select: "start"
9594
- }) : raise({
9595
- type: "select",
9596
- at: {
9597
- anchor: selection.focus,
9598
- focus: selection.focus
9472
+ isFirstBlockTextBlock,
9473
+ originalSelection
9474
+ }) => {
9475
+ let previousBlockKey, firstBlockKey;
9476
+ const actions = [];
9477
+ let index = -1;
9478
+ for (const block of event.blocks) {
9479
+ if (index++, index === 0) {
9480
+ isEqualSelectionPoints(at.focus, focusBlockEndPoint) || actions.push(raise({
9481
+ type: "delete",
9482
+ at: {
9483
+ anchor: at.focus,
9484
+ focus: focusBlockEndPoint
9485
+ }
9486
+ }));
9487
+ const key2 = getUniqueBlockKey(block._key)(snapshot), deletingEndToEnd = isEqualSelectionPoints(at.focus, focusBlockStartPoint);
9488
+ isTextBlock(snapshot.context, block) && !deletingEndToEnd ? (firstBlockKey = focusTextBlock.node._key, previousBlockKey = focusTextBlock.node._key) : (firstBlockKey = key2, previousBlockKey = key2), actions.push(raise({
9489
+ type: "insert.block",
9490
+ block: key2 !== block._key ? {
9491
+ ...block,
9492
+ _key: key2
9493
+ } : block,
9494
+ placement: "auto",
9495
+ select: "end",
9496
+ ...event.at ? {
9497
+ at: event.at
9498
+ } : {}
9499
+ }));
9500
+ continue;
9501
+ }
9502
+ if (index === event.blocks.length - 1) {
9503
+ actions.push(raise({
9504
+ type: "insert.block",
9505
+ block,
9506
+ placement: "after",
9507
+ select: "end",
9508
+ at: previousBlockKey ? {
9509
+ anchor: {
9510
+ path: [{
9511
+ _key: previousBlockKey
9512
+ }],
9513
+ offset: 0
9514
+ },
9515
+ focus: {
9516
+ path: [{
9517
+ _key: previousBlockKey
9518
+ }],
9519
+ offset: 0
9520
+ }
9521
+ } : void 0
9522
+ }));
9523
+ continue;
9524
+ }
9525
+ const key = getUniqueBlockKey(block._key)(snapshot);
9526
+ actions.push(raise({
9527
+ type: "insert.block",
9528
+ block: key !== block._key ? {
9529
+ ...block,
9530
+ _key: key
9531
+ } : block,
9532
+ placement: "after",
9533
+ select: previousBlockKey ? "none" : "end",
9534
+ at: previousBlockKey ? {
9535
+ anchor: {
9536
+ path: [{
9537
+ _key: previousBlockKey
9538
+ }],
9539
+ offset: 0
9540
+ },
9541
+ focus: {
9542
+ path: [{
9543
+ _key: previousBlockKey
9544
+ }],
9545
+ offset: 0
9546
+ }
9547
+ } : void 0
9548
+ })), previousBlockKey = key;
9599
9549
  }
9600
- })] : []]]
9550
+ return isEmptyTextBlock(snapshot.context, focusTextBlockAfter) || actions.push(raise({
9551
+ type: "insert.block",
9552
+ block: focusTextBlockAfter,
9553
+ placement: "auto",
9554
+ select: event.select === "end" ? "none" : "end"
9555
+ })), event.select === "none" && actions.push(raise({
9556
+ type: "select",
9557
+ at: originalSelection
9558
+ })), event.select === "start" && ((isEqualSelectionPoints(at.focus, focusBlockStartPoint) || !isFirstBlockTextBlock) && firstBlockKey ? actions.push(raise({
9559
+ type: "select.block",
9560
+ at: [{
9561
+ _key: firstBlockKey
9562
+ }],
9563
+ select: "start"
9564
+ })) : actions.push(raise({
9565
+ type: "select",
9566
+ at: {
9567
+ anchor: at.focus,
9568
+ focus: at.focus
9569
+ }
9570
+ }))), actions;
9571
+ }]
9601
9572
  }),
9602
9573
  defineBehavior({
9603
9574
  on: "insert.blocks",
9604
9575
  guard: ({
9605
9576
  snapshot,
9606
9577
  event
9607
- }) => {
9608
- if (event.placement !== "auto")
9609
- return !1;
9610
- const selection = snapshot.context.selection, firstBlockKey = getUniqueBlockKey(event.blocks.at(0)?._key)(snapshot), lastBlockKey = getUniqueBlockKey(event.blocks.at(-1)?._key)(snapshot);
9611
- return {
9612
- firstBlockKey,
9613
- lastBlockKey,
9614
- selection
9615
- };
9578
+ }) => event.placement !== "auto" ? !1 : {
9579
+ originalSelection: snapshot.context.selection
9616
9580
  },
9617
9581
  actions: [({
9582
+ snapshot,
9618
9583
  event
9619
9584
  }, {
9620
- firstBlockKey,
9621
- lastBlockKey,
9622
- selection
9623
- }) => [...event.blocks.map((block, index) => raise({
9624
- type: "insert.block",
9625
- block: index === 0 ? {
9626
- ...block,
9627
- _key: firstBlockKey
9628
- } : index === event.blocks.length - 1 ? {
9629
- ...block,
9630
- _key: lastBlockKey
9631
- } : block,
9632
- placement: index === 0 ? "auto" : "after",
9633
- select: index !== event.blocks.length - 1 ? "end" : event.select ?? "end"
9634
- })), ...event.select === "none" ? [raise({
9635
- type: "select",
9636
- at: selection
9637
- })] : event.select === "start" ? [raise({
9638
- type: "select.block",
9639
- at: [{
9640
- _key: firstBlockKey
9641
- }],
9642
- select: "start"
9643
- })] : [raise({
9644
- type: "select.block",
9645
- at: [{
9646
- _key: lastBlockKey
9647
- }],
9648
- select: "end"
9649
- })]]]
9585
+ originalSelection
9586
+ }) => {
9587
+ let firstBlockKey, lastBlockKey, previousBlockKey;
9588
+ const actions = [];
9589
+ let index = -1;
9590
+ for (const block of event.blocks) {
9591
+ index++;
9592
+ const key = getUniqueBlockKey(block._key)(snapshot);
9593
+ index === 0 && (firstBlockKey = key), index === event.blocks.length - 1 && (lastBlockKey = key), actions.push(raise({
9594
+ type: "insert.block",
9595
+ block: key !== block._key ? {
9596
+ ...block,
9597
+ _key: key
9598
+ } : block,
9599
+ placement: index === 0 ? "auto" : "after",
9600
+ select: "none",
9601
+ ...previousBlockKey ? {
9602
+ at: {
9603
+ anchor: {
9604
+ path: [{
9605
+ _key: previousBlockKey
9606
+ }],
9607
+ offset: 0
9608
+ },
9609
+ focus: {
9610
+ path: [{
9611
+ _key: previousBlockKey
9612
+ }],
9613
+ offset: 0
9614
+ }
9615
+ }
9616
+ } : event.at ? {
9617
+ at: event.at
9618
+ } : {}
9619
+ })), previousBlockKey = key;
9620
+ }
9621
+ const select = event.select ?? "end";
9622
+ return select === "none" ? actions.push(raise({
9623
+ type: "select",
9624
+ at: originalSelection
9625
+ })) : select === "start" && firstBlockKey ? actions.push(raise({
9626
+ type: "select.block",
9627
+ at: [{
9628
+ _key: firstBlockKey
9629
+ }],
9630
+ select: "start"
9631
+ })) : lastBlockKey && actions.push(raise({
9632
+ type: "select.block",
9633
+ at: [{
9634
+ _key: lastBlockKey
9635
+ }],
9636
+ select: "end"
9637
+ })), actions;
9638
+ }]
9650
9639
  }),
9651
9640
  defineBehavior({
9652
9641
  on: "insert.break",