@portabletext/editor 2.12.0 → 2.12.2

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/lib/index.js CHANGED
@@ -8716,7 +8716,15 @@ const abstractAnnotationBehaviors = [defineBehavior({
8716
8716
  marks: [...event.decorators ?? [], ...markDefs.map((markDef) => markDef._key)]
8717
8717
  }
8718
8718
  })]]
8719
- })], abstractKeyboardBehaviors = [
8719
+ })], shiftLeft = createKeyboardShortcut({
8720
+ default: [{
8721
+ key: "ArrowLeft",
8722
+ shift: !0,
8723
+ meta: !1,
8724
+ ctrl: !1,
8725
+ alt: !1
8726
+ }]
8727
+ }), abstractKeyboardBehaviors = [
8720
8728
  /**
8721
8729
  * Allow raising an `insert.break` event when pressing Enter on an inline
8722
8730
  * object.
@@ -8731,6 +8739,20 @@ const abstractAnnotationBehaviors = [defineBehavior({
8731
8739
  type: "insert.break"
8732
8740
  })]]
8733
8741
  }),
8742
+ /**
8743
+ * On Firefox, Enter might collapse the selection. To mitigate this, we
8744
+ * `raise` an `insert.break` event manually.
8745
+ */
8746
+ defineBehavior({
8747
+ on: "keyboard.keydown",
8748
+ guard: ({
8749
+ snapshot,
8750
+ event
8751
+ }) => defaultKeyboardShortcuts.break.guard(event.originEvent) && isSelectionExpanded(snapshot),
8752
+ actions: [() => [raise({
8753
+ type: "insert.break"
8754
+ })]]
8755
+ }),
8734
8756
  /**
8735
8757
  * On WebKit, Shift+Enter results in an `insertParagraph` input event rather
8736
8758
  * than an `insertLineBreak` input event. This Behavior makes sure we catch
@@ -8768,6 +8790,58 @@ const abstractAnnotationBehaviors = [defineBehavior({
8768
8790
  actions: [() => [raise({
8769
8791
  type: "history.redo"
8770
8792
  })]]
8793
+ }),
8794
+ /**
8795
+ * Fix edge case where Shift+ArrowLeft didn't reduce a selection hanging
8796
+ * onto an empty text block.
8797
+ */
8798
+ defineBehavior({
8799
+ on: "keyboard.keydown",
8800
+ guard: ({
8801
+ snapshot,
8802
+ event
8803
+ }) => {
8804
+ if (!snapshot.context.selection || !shiftLeft.guard(event.originEvent))
8805
+ return !1;
8806
+ const focusBlock = getFocusBlock$1(snapshot);
8807
+ if (!focusBlock)
8808
+ return !1;
8809
+ const previousBlock = getPreviousBlock({
8810
+ ...snapshot,
8811
+ context: {
8812
+ ...snapshot.context,
8813
+ selection: {
8814
+ anchor: {
8815
+ path: focusBlock.path,
8816
+ offset: 0
8817
+ },
8818
+ focus: {
8819
+ path: focusBlock.path,
8820
+ offset: 0
8821
+ }
8822
+ }
8823
+ }
8824
+ });
8825
+ return previousBlock && isTextBlock(snapshot.context, focusBlock.node) && snapshot.context.selection.focus.offset === 0 && isEmptyTextBlock(snapshot.context, focusBlock.node) ? {
8826
+ previousBlock,
8827
+ selection: snapshot.context.selection
8828
+ } : !1;
8829
+ },
8830
+ actions: [({
8831
+ snapshot
8832
+ }, {
8833
+ previousBlock,
8834
+ selection
8835
+ }) => [raise({
8836
+ type: "select",
8837
+ at: {
8838
+ anchor: selection.anchor,
8839
+ focus: getBlockEndPoint({
8840
+ context: snapshot.context,
8841
+ block: previousBlock
8842
+ })
8843
+ }
8844
+ })]]
8771
8845
  })
8772
8846
  ], abstractListItemBehaviors = [defineBehavior({
8773
8847
  on: "list item.add",
@@ -9428,86 +9502,88 @@ function performEvent({
9428
9502
  } catch (error) {
9429
9503
  console.error(new Error(`Evaluating guard for "${event.type}" failed due to: ${error.message}`));
9430
9504
  }
9431
- if (shouldRun) {
9432
- defaultBehaviorOverwritten = !0;
9433
- for (const actionSet of eventBehavior.actions) {
9434
- const actionsSnapshot = getSnapshot();
9435
- let actions = [];
9436
- try {
9437
- actions = actionSet({
9438
- snapshot: actionsSnapshot,
9439
- event,
9440
- dom: createEditorDom(sendBack, editor)
9441
- }, shouldRun);
9442
- } catch (error) {
9443
- console.error(new Error(`Evaluating actions for "${event.type}" failed due to: ${error.message}`));
9444
- }
9445
- if (actions.length === 0)
9446
- continue;
9447
- nativeEventPrevented = actions.some((action) => action.type === "raise" || action.type === "execute") || !actions.some((action) => action.type === "forward");
9448
- let undoStepCreated = !1;
9449
- actions.some((action) => action.type === "execute") && (createUndoStep(editor), undoStepCreated = !0);
9450
- const actionTypes = actions.map((action) => action.type), uniqueActionTypes = new Set(actionTypes), raiseGroup = actionTypes.length > 1 && uniqueActionTypes.size === 1 && uniqueActionTypes.has("raise"), executeGroup = actionTypes.length > 1 && uniqueActionTypes.size === 1 && uniqueActionTypes.has("execute");
9451
- withoutNormalizingConditional(editor, () => raiseGroup || executeGroup, () => {
9452
- for (const action of actions) {
9453
- if (action.type === "effect") {
9454
- try {
9455
- action.effect({
9456
- send: sendBack
9457
- });
9458
- } catch (error) {
9459
- console.error(new Error(`Executing effect as a result of "${event.type}" failed due to: ${error.message}`));
9460
- }
9461
- continue;
9462
- }
9463
- if (action.type === "forward") {
9464
- const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
9465
- performEvent({
9466
- mode: mode === "execute" ? "execute" : "forward",
9467
- behaviors,
9468
- remainingEventBehaviors: remainingEventBehaviors2,
9469
- event: action.event,
9470
- editor,
9471
- keyGenerator,
9472
- schema,
9473
- getSnapshot,
9474
- nativeEvent,
9475
- sendBack
9476
- });
9477
- continue;
9478
- }
9479
- if (action.type === "raise") {
9480
- performEvent({
9481
- mode: mode === "execute" ? "execute" : "raise",
9482
- behaviors,
9483
- remainingEventBehaviors: mode === "execute" ? remainingEventBehaviors : behaviors,
9484
- event: action.event,
9485
- editor,
9486
- keyGenerator,
9487
- schema,
9488
- getSnapshot,
9489
- nativeEvent,
9490
- sendBack
9505
+ if (!shouldRun)
9506
+ continue;
9507
+ defaultBehaviorOverwritten = !0, eventBehavior.actions.length === 0 && (nativeEventPrevented = !0);
9508
+ let actionSetIndex = -1;
9509
+ for (const actionSet of eventBehavior.actions) {
9510
+ actionSetIndex++;
9511
+ const actionsSnapshot = getSnapshot();
9512
+ let actions = [];
9513
+ try {
9514
+ actions = actionSet({
9515
+ snapshot: actionsSnapshot,
9516
+ event,
9517
+ dom: createEditorDom(sendBack, editor)
9518
+ }, shouldRun);
9519
+ } catch (error) {
9520
+ console.error(new Error(`Evaluating actions for "${event.type}" failed due to: ${error.message}`));
9521
+ }
9522
+ if (actions.length === 0)
9523
+ continue;
9524
+ nativeEventPrevented = actions.some((action) => action.type === "raise" || action.type === "execute") || !actions.some((action) => action.type === "forward");
9525
+ let undoStepCreated = !1;
9526
+ actionSetIndex > 0 && (createUndoStep(editor), undoStepCreated = !0), !undoStepCreated && actions.some((action) => action.type === "execute") && (createUndoStep(editor), undoStepCreated = !0);
9527
+ const actionTypes = actions.map((action) => action.type), uniqueActionTypes = new Set(actionTypes), raiseGroup = actionTypes.length > 1 && uniqueActionTypes.size === 1 && uniqueActionTypes.has("raise"), executeGroup = actionTypes.length > 1 && uniqueActionTypes.size === 1 && uniqueActionTypes.has("execute");
9528
+ withoutNormalizingConditional(editor, () => raiseGroup || executeGroup, () => {
9529
+ for (const action of actions) {
9530
+ if (action.type === "effect") {
9531
+ try {
9532
+ action.effect({
9533
+ send: sendBack
9491
9534
  });
9492
- continue;
9535
+ } catch (error) {
9536
+ console.error(new Error(`Executing effect as a result of "${event.type}" failed due to: ${error.message}`));
9493
9537
  }
9538
+ continue;
9539
+ }
9540
+ if (action.type === "forward") {
9541
+ const remainingEventBehaviors2 = eventBehaviors.slice(eventBehaviorIndex + 1);
9494
9542
  performEvent({
9495
- mode: "execute",
9543
+ mode: mode === "execute" ? "execute" : "forward",
9496
9544
  behaviors,
9497
- remainingEventBehaviors: [],
9545
+ remainingEventBehaviors: remainingEventBehaviors2,
9498
9546
  event: action.event,
9499
9547
  editor,
9500
9548
  keyGenerator,
9501
9549
  schema,
9502
9550
  getSnapshot,
9503
- nativeEvent: void 0,
9551
+ nativeEvent,
9504
9552
  sendBack
9505
9553
  });
9554
+ continue;
9506
9555
  }
9507
- }), undoStepCreated && clearUndoStep(editor);
9508
- }
9509
- break;
9556
+ if (action.type === "raise") {
9557
+ performEvent({
9558
+ mode: mode === "execute" ? "execute" : "raise",
9559
+ behaviors,
9560
+ remainingEventBehaviors: mode === "execute" ? remainingEventBehaviors : behaviors,
9561
+ event: action.event,
9562
+ editor,
9563
+ keyGenerator,
9564
+ schema,
9565
+ getSnapshot,
9566
+ nativeEvent,
9567
+ sendBack
9568
+ });
9569
+ continue;
9570
+ }
9571
+ performEvent({
9572
+ mode: "execute",
9573
+ behaviors,
9574
+ remainingEventBehaviors: [],
9575
+ event: action.event,
9576
+ editor,
9577
+ keyGenerator,
9578
+ schema,
9579
+ getSnapshot,
9580
+ nativeEvent: void 0,
9581
+ sendBack
9582
+ });
9583
+ }
9584
+ }), undoStepCreated && clearUndoStep(editor);
9510
9585
  }
9586
+ break;
9511
9587
  }
9512
9588
  !defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event) ? (nativeEvent?.preventDefault(), mode === "send" && clearUndoStep(editor), withPerformingBehaviorOperation(editor, () => {
9513
9589
  debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({