@portabletext/editor 2.11.0 → 2.12.1

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
@@ -14,7 +14,7 @@ import isEqual from "lodash/isEqual.js";
14
14
  import { isTextBlock, isSpan, compileSchema } from "@portabletext/schema";
15
15
  import { defineSchema } from "@portabletext/schema";
16
16
  import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getSelectionStartPoint as getSelectionStartPoint$1, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.is-selection-expanded.js";
17
- import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getMarkState, getActiveDecorators, getTrimmedSelection, getActiveAnnotationsMarks, isActiveAnnotation, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, getSelectionEndPoint as getSelectionEndPoint$1, isActiveDecorator, getActiveAnnotations, getSelectedTextBlocks, isActiveListItem, isActiveStyle } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
17
+ import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getMarkState, getActiveDecorators, getTrimmedSelection, isActiveAnnotation, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, getActiveAnnotationsMarks, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, getSelectionEndPoint as getSelectionEndPoint$1, isActiveDecorator, getActiveAnnotations, getSelectedTextBlocks, isActiveListItem, isActiveStyle } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
18
18
  import { defineBehavior, forward, raise, effect } from "./behaviors/index.js";
19
19
  import uniq from "lodash/uniq.js";
20
20
  import { setup, fromCallback, assign, and, enqueueActions, emit, assertEvent, raise as raise$1, not, createActor } from "xstate";
@@ -4791,7 +4791,7 @@ const insertChildOperationImplementation = ({
4791
4791
  }) : Transforms.insertNodes(operation.editor, span, {
4792
4792
  at: [focusBlockIndex, focusChildIndex + 1],
4793
4793
  select: !0
4794
- });
4794
+ }), EDITOR_TO_PENDING_SELECTION.set(operation.editor, operation.editor.selection);
4795
4795
  return;
4796
4796
  }
4797
4797
  const inlineObject = parseInlineObject({
@@ -4826,44 +4826,9 @@ const insertChildOperationImplementation = ({
4826
4826
  }
4827
4827
  throw new Error("Unable to parse child");
4828
4828
  }, insertTextOperationImplementation = ({
4829
- context,
4830
4829
  operation
4831
4830
  }) => {
4832
- const snapshot = {
4833
- blockIndexMap: operation.editor.blockIndexMap,
4834
- context: {
4835
- value: operation.editor.value,
4836
- selection: operation.editor.selection ? slateRangeToSelection({
4837
- schema: context.schema,
4838
- editor: operation.editor,
4839
- range: operation.editor.selection
4840
- }) : null,
4841
- schema: context.schema,
4842
- keyGenerator: context.keyGenerator,
4843
- converters: [],
4844
- readOnly: !1
4845
- },
4846
- decoratorState: operation.editor.decoratorState
4847
- }, markState = getMarkState(snapshot), activeDecorators = getActiveDecorators(snapshot), activeAnnotations = getActiveAnnotationsMarks(snapshot), [focusSpan] = getFocusSpan({
4848
- editor: operation.editor
4849
- });
4850
- if (!focusSpan) {
4851
- Transforms.insertText(operation.editor, operation.text);
4852
- return;
4853
- }
4854
- if (markState && markState.state === "unchanged") {
4855
- const markStateDecorators = (markState.marks ?? []).filter((mark) => context.schema.decorators.map((decorator) => decorator.name).includes(mark));
4856
- if (markStateDecorators.length === activeDecorators.length && markStateDecorators.every((mark) => activeDecorators.includes(mark))) {
4857
- Transforms.insertText(operation.editor, operation.text);
4858
- return;
4859
- }
4860
- }
4861
- Transforms.insertNodes(operation.editor, {
4862
- _type: focusSpan._type,
4863
- _key: context.keyGenerator(),
4864
- text: operation.text,
4865
- marks: [...activeDecorators, ...activeAnnotations]
4866
- }), EDITOR_TO_PENDING_SELECTION.set(operation.editor, operation.editor.selection), operation.editor.decoratorState = {};
4831
+ Transforms.insertText(operation.editor, operation.text);
4867
4832
  }, moveBackwardOperationImplementation = ({
4868
4833
  operation
4869
4834
  }) => {
@@ -7247,7 +7212,39 @@ const coreDndBehaviors = [
7247
7212
  placement: draggingEntireBlocks ? originEvent.position.block === "start" ? "before" : originEvent.position.block === "end" ? "after" : "auto" : "auto"
7248
7213
  })]]
7249
7214
  })
7250
- ], breakingAtTheEndOfTextBlock = defineBehavior({
7215
+ ], coreInsertBehaviors = [defineBehavior({
7216
+ on: "insert.text",
7217
+ guard: ({
7218
+ snapshot
7219
+ }) => {
7220
+ if (!getFocusSpan$1(snapshot))
7221
+ return !1;
7222
+ const markState = getMarkState(snapshot), activeDecorators = getActiveDecorators(snapshot), activeAnnotations = getActiveAnnotationsMarks(snapshot);
7223
+ if (markState && markState.state === "unchanged") {
7224
+ const markStateDecorators = (markState.marks ?? []).filter((mark) => snapshot.context.schema.decorators.map((decorator) => decorator.name).includes(mark));
7225
+ if (markStateDecorators.length === activeDecorators.length && markStateDecorators.every((mark) => activeDecorators.includes(mark)))
7226
+ return !1;
7227
+ }
7228
+ return {
7229
+ activeDecorators,
7230
+ activeAnnotations
7231
+ };
7232
+ },
7233
+ actions: [({
7234
+ snapshot,
7235
+ event
7236
+ }, {
7237
+ activeDecorators,
7238
+ activeAnnotations
7239
+ }) => [raise({
7240
+ type: "insert.child",
7241
+ child: {
7242
+ _type: snapshot.context.schema.span.name,
7243
+ text: event.text,
7244
+ marks: [...activeDecorators, ...activeAnnotations]
7245
+ }
7246
+ })]]
7247
+ })], breakingAtTheEndOfTextBlock = defineBehavior({
7251
7248
  on: "insert.break",
7252
7249
  guard: ({
7253
7250
  snapshot
@@ -7808,7 +7805,7 @@ const MAX_LIST_LEVEL = 10, clearListOnBackspace = defineBehavior({
7808
7805
  inheritListLevel,
7809
7806
  inheritListItem,
7810
7807
  inheritListProperties
7811
- }, coreBehaviorsConfig = [...coreAnnotationBehaviors, coreDecoratorBehaviors.strongShortcut, coreDecoratorBehaviors.emShortcut, coreDecoratorBehaviors.underlineShortcut, coreDecoratorBehaviors.codeShortcut, ...coreDndBehaviors, coreBlockObjectBehaviors.clickingAboveLonelyBlockObject, coreBlockObjectBehaviors.clickingBelowLonelyBlockObject, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace, coreListBehaviors.mergeTextIntoListOnDelete, coreListBehaviors.mergeTextIntoListOnBackspace, coreListBehaviors.deletingListFromStart, coreListBehaviors.clearListOnEnter, coreListBehaviors.indentListOnTab, coreListBehaviors.unindentListOnShiftTab, coreListBehaviors.inheritListLevel, coreListBehaviors.inheritListItem, coreListBehaviors.inheritListProperties, coreInsertBreakBehaviors.breakingAtTheEndOfTextBlock, coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock, coreInsertBreakBehaviors.breakingEntireDocument, coreInsertBreakBehaviors.breakingEntireBlocks, coreInsertBreakBehaviors.breakingInlineObject].map((behavior) => ({
7808
+ }, coreBehaviorsConfig = [...coreAnnotationBehaviors, coreDecoratorBehaviors.strongShortcut, coreDecoratorBehaviors.emShortcut, coreDecoratorBehaviors.underlineShortcut, coreDecoratorBehaviors.codeShortcut, ...coreDndBehaviors, coreBlockObjectBehaviors.clickingAboveLonelyBlockObject, coreBlockObjectBehaviors.clickingBelowLonelyBlockObject, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, ...coreInsertBehaviors, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace, coreListBehaviors.mergeTextIntoListOnDelete, coreListBehaviors.mergeTextIntoListOnBackspace, coreListBehaviors.deletingListFromStart, coreListBehaviors.clearListOnEnter, coreListBehaviors.indentListOnTab, coreListBehaviors.unindentListOnShiftTab, coreListBehaviors.inheritListLevel, coreListBehaviors.inheritListItem, coreListBehaviors.inheritListProperties, coreInsertBreakBehaviors.breakingAtTheEndOfTextBlock, coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock, coreInsertBreakBehaviors.breakingEntireDocument, coreInsertBreakBehaviors.breakingEntireBlocks, coreInsertBreakBehaviors.breakingInlineObject].map((behavior) => ({
7812
7809
  behavior,
7813
7810
  priority: corePriority
7814
7811
  }));
@@ -8719,7 +8716,15 @@ const abstractAnnotationBehaviors = [defineBehavior({
8719
8716
  marks: [...event.decorators ?? [], ...markDefs.map((markDef) => markDef._key)]
8720
8717
  }
8721
8718
  })]]
8722
- })], abstractKeyboardBehaviors = [
8719
+ })], shiftLeft = createKeyboardShortcut({
8720
+ default: [{
8721
+ key: "ArrowLeft",
8722
+ shift: !0,
8723
+ meta: !1,
8724
+ ctrl: !1,
8725
+ alt: !1
8726
+ }]
8727
+ }), abstractKeyboardBehaviors = [
8723
8728
  /**
8724
8729
  * Allow raising an `insert.break` event when pressing Enter on an inline
8725
8730
  * object.
@@ -8771,6 +8776,58 @@ const abstractAnnotationBehaviors = [defineBehavior({
8771
8776
  actions: [() => [raise({
8772
8777
  type: "history.redo"
8773
8778
  })]]
8779
+ }),
8780
+ /**
8781
+ * Fix edge case where Shift+ArrowLeft didn't reduce a selection hanging
8782
+ * onto an empty text block.
8783
+ */
8784
+ defineBehavior({
8785
+ on: "keyboard.keydown",
8786
+ guard: ({
8787
+ snapshot,
8788
+ event
8789
+ }) => {
8790
+ if (!snapshot.context.selection || !shiftLeft.guard(event.originEvent))
8791
+ return !1;
8792
+ const focusBlock = getFocusBlock$1(snapshot);
8793
+ if (!focusBlock)
8794
+ return !1;
8795
+ const previousBlock = getPreviousBlock({
8796
+ ...snapshot,
8797
+ context: {
8798
+ ...snapshot.context,
8799
+ selection: {
8800
+ anchor: {
8801
+ path: focusBlock.path,
8802
+ offset: 0
8803
+ },
8804
+ focus: {
8805
+ path: focusBlock.path,
8806
+ offset: 0
8807
+ }
8808
+ }
8809
+ }
8810
+ });
8811
+ return previousBlock && isTextBlock(snapshot.context, focusBlock.node) && snapshot.context.selection.focus.offset === 0 && isEmptyTextBlock(snapshot.context, focusBlock.node) ? {
8812
+ previousBlock,
8813
+ selection: snapshot.context.selection
8814
+ } : !1;
8815
+ },
8816
+ actions: [({
8817
+ snapshot
8818
+ }, {
8819
+ previousBlock,
8820
+ selection
8821
+ }) => [raise({
8822
+ type: "select",
8823
+ at: {
8824
+ anchor: selection.anchor,
8825
+ focus: getBlockEndPoint({
8826
+ context: snapshot.context,
8827
+ block: previousBlock
8828
+ })
8829
+ }
8830
+ })]]
8774
8831
  })
8775
8832
  ], abstractListItemBehaviors = [defineBehavior({
8776
8833
  on: "list item.add",
@@ -9432,7 +9489,7 @@ function performEvent({
9432
9489
  console.error(new Error(`Evaluating guard for "${event.type}" failed due to: ${error.message}`));
9433
9490
  }
9434
9491
  if (shouldRun) {
9435
- defaultBehaviorOverwritten = !0;
9492
+ defaultBehaviorOverwritten = !0, eventBehavior.actions.length === 0 && (nativeEventPrevented = !0);
9436
9493
  for (const actionSet of eventBehavior.actions) {
9437
9494
  const actionsSnapshot = getSnapshot();
9438
9495
  let actions = [];