@portabletext/editor 1.1.8 → 1.1.10

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.esm.js CHANGED
@@ -11,12 +11,12 @@ import uniq from "lodash/uniq.js";
11
11
  import { Subject } from "rxjs";
12
12
  import { fromCallback, setup, assign, assertEvent, emit, enqueueActions, createActor } from "xstate";
13
13
  import { Schema } from "@sanity/schema";
14
- import { isHotkey } from "is-hotkey-esm";
15
14
  import { diffMatchPatch as diffMatchPatch$1, set, insert, setIfMissing, unset, applyAll } from "@portabletext/patches";
16
15
  import get from "lodash/get.js";
17
16
  import isUndefined from "lodash/isUndefined.js";
18
17
  import omitBy from "lodash/omitBy.js";
19
18
  import flatten from "lodash/flatten.js";
19
+ import { isHotkey } from "is-hotkey-esm";
20
20
  import { htmlToBlocks, normalizeBlock } from "@sanity/block-tools";
21
21
  import isPlainObject from "lodash/isPlainObject.js";
22
22
  import throttle from "lodash/throttle.js";
@@ -816,20 +816,15 @@ function getFocusBlockObject(context) {
816
816
  const focusBlock = getFocusBlock(context);
817
817
  return focusBlock && !isPortableTextTextBlock(focusBlock.node) ? { node: focusBlock.node, path: focusBlock.path } : void 0;
818
818
  }
819
- const overwriteSoftReturn = {
820
- on: "key down",
821
- guard: ({ event }) => isHotkey("shift+enter", event.nativeEvent),
822
- actions: [
823
- ({ event }) => (event.nativeEvent.preventDefault(), { type: "insert text", text: `
824
- ` })
825
- ]
826
- }, enterOnVoidBlock = {
827
- on: "key down",
828
- guard: ({ context, event }) => isHotkey("enter", event.nativeEvent) ? !!getFocusBlockObject(context) : !1,
829
- actions: [
830
- ({ event }) => (event.nativeEvent.preventDefault(), { type: "insert text block", decorators: [] })
831
- ]
832
- }, coreBehaviors = [overwriteSoftReturn, enterOnVoidBlock], debug$k = debugWithName("operationToPatches");
819
+ const softReturn = {
820
+ on: "insert soft break",
821
+ actions: [() => ({ type: "insert text", text: `
822
+ ` })]
823
+ }, breakingVoidBlock = {
824
+ on: "insert break",
825
+ guard: ({ context }) => !!getFocusBlockObject(context),
826
+ actions: [() => ({ type: "insert text block", decorators: [] })]
827
+ }, coreBehaviors = [softReturn, breakingVoidBlock], debug$k = debugWithName("operationToPatches");
833
828
  function createOperationToPatches(types) {
834
829
  const textBlockName = types.block.name;
835
830
  function insertTextPatch(editor, operation, beforeValue) {
@@ -1104,6 +1099,50 @@ function createOperationToPatches(types) {
1104
1099
  splitNodePatch
1105
1100
  };
1106
1101
  }
1102
+ function createWithEventListeners(editorActor) {
1103
+ return function(editor) {
1104
+ const { deleteBackward, insertBreak, insertSoftBreak, insertText } = editor;
1105
+ return editor.deleteBackward = (unit) => {
1106
+ editorActor.send({
1107
+ type: "behavior event",
1108
+ behaviorEvent: {
1109
+ type: "delete backward",
1110
+ unit,
1111
+ default: () => deleteBackward(unit)
1112
+ },
1113
+ editor
1114
+ });
1115
+ }, editor.insertBreak = () => {
1116
+ editorActor.send({
1117
+ type: "behavior event",
1118
+ behaviorEvent: {
1119
+ type: "insert break",
1120
+ default: insertBreak
1121
+ },
1122
+ editor
1123
+ });
1124
+ }, editor.insertSoftBreak = () => {
1125
+ editorActor.send({
1126
+ type: "behavior event",
1127
+ behaviorEvent: {
1128
+ type: "insert soft break",
1129
+ default: insertSoftBreak
1130
+ },
1131
+ editor
1132
+ });
1133
+ }, editor.insertText = (text, options) => {
1134
+ editorActor.send({
1135
+ type: "behavior event",
1136
+ behaviorEvent: {
1137
+ type: "insert text",
1138
+ text,
1139
+ default: () => insertText(text, options)
1140
+ },
1141
+ editor
1142
+ });
1143
+ }, editor;
1144
+ };
1145
+ }
1107
1146
  const debug$j = debugWithName("API:editable");
1108
1147
  function createWithEditableAPI(editorActor, portableTextEditor, types) {
1109
1148
  return function(editor) {
@@ -1619,7 +1658,9 @@ function createWithInsertBreak(editorActor, types) {
1619
1658
  Editor.insertNode(
1620
1659
  editor,
1621
1660
  editor.pteCreateTextBlock({
1622
- decorators: focusAnnotations.length === 0 ? focusDecorators : []
1661
+ decorators: focusAnnotations.length === 0 ? focusDecorators : [],
1662
+ listItem: focusBlock.listItem,
1663
+ level: focusBlock.level
1623
1664
  })
1624
1665
  );
1625
1666
  const [nextBlockPath] = Path.next(focusBlockPath);
@@ -1637,7 +1678,9 @@ function createWithInsertBreak(editorActor, types) {
1637
1678
  Editor.insertNode(
1638
1679
  editor,
1639
1680
  editor.pteCreateTextBlock({
1640
- decorators: []
1681
+ decorators: [],
1682
+ listItem: focusBlock.listItem,
1683
+ level: focusBlock.level
1641
1684
  })
1642
1685
  );
1643
1686
  const [nextBlockPath] = Path.next(focusBlockPath);
@@ -3507,6 +3550,17 @@ function createWithPortableTextMarkModel(editorActor, types) {
3507
3550
  }
3508
3551
  }
3509
3552
  }
3553
+ if (atTheBeginningOfSpan && !spanIsEmpty && previousSpan) {
3554
+ Transforms.insertNodes(editor, {
3555
+ _type: "span",
3556
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3557
+ text: op.text,
3558
+ marks: (previousSpan.marks ?? []).filter(
3559
+ (mark) => decorators.includes(mark)
3560
+ )
3561
+ });
3562
+ return;
3563
+ }
3510
3564
  }
3511
3565
  }
3512
3566
  if (op.type === "remove_text") {
@@ -3784,6 +3838,8 @@ function createWithUtils({
3784
3838
  _type: schemaTypes.block.name,
3785
3839
  _key: editorActor.getSnapshot().context.keyGenerator(),
3786
3840
  style: schemaTypes.styles[0].value || "normal",
3841
+ ...options.listItem ? { listItem: options.listItem } : {},
3842
+ ...options.level ? { level: options.level } : {},
3787
3843
  markDefs: [],
3788
3844
  children: [
3789
3845
  {
@@ -4410,7 +4466,7 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4410
4466
  }), withPortableTextSelections = createWithPortableTextSelections(
4411
4467
  editorActor,
4412
4468
  schemaTypes
4413
- );
4469
+ ), withEventListeners = createWithEventListeners(editorActor);
4414
4470
  return e.destroy = () => {
4415
4471
  const originalFunctions = originalFnMap.get(e);
4416
4472
  if (!originalFunctions)
@@ -4436,18 +4492,20 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4436
4492
  ),
4437
4493
  subscribe: () => noop
4438
4494
  } : {
4439
- editor: withSchemaTypes(
4440
- withObjectKeys(
4441
- withPortableTextMarkModel(
4442
- withPortableTextBlockStyle(
4443
- withPortableTextLists(
4444
- withPlaceholderBlock(
4445
- withUtils(
4446
- withMaxBlocks(
4447
- withUndoRedo(
4448
- withPatches(
4449
- withPortableTextSelections(
4450
- withEditableAPI(withInsertBreak(e))
4495
+ editor: withEventListeners(
4496
+ withSchemaTypes(
4497
+ withObjectKeys(
4498
+ withPortableTextMarkModel(
4499
+ withPortableTextBlockStyle(
4500
+ withPortableTextLists(
4501
+ withPlaceholderBlock(
4502
+ withUtils(
4503
+ withMaxBlocks(
4504
+ withUndoRedo(
4505
+ withPatches(
4506
+ withPortableTextSelections(
4507
+ withEditableAPI(withInsertBreak(e))
4508
+ )
4451
4509
  )
4452
4510
  )
4453
4511
  )
@@ -4828,11 +4886,20 @@ const EditorActorContext = createContext({}), behaviorActionImplementations = {
4828
4886
  Transforms.setNodes(event.editor, { style: event.style }, { at });
4829
4887
  }
4830
4888
  },
4889
+ "delete backward": ({ event }) => {
4890
+ Editor.deleteBackward(event.editor, { unit: event.unit });
4891
+ },
4831
4892
  "delete text": ({ event }) => {
4832
4893
  Transforms.delete(event.editor, {
4833
4894
  at: toSlateRange(event.selection, event.editor)
4834
4895
  });
4835
4896
  },
4897
+ "insert break": ({ event }) => {
4898
+ Editor.insertBreak(event.editor);
4899
+ },
4900
+ "insert soft break": ({ event }) => {
4901
+ Editor.insertSoftBreak(event.editor);
4902
+ },
4836
4903
  "insert text": ({ event }) => {
4837
4904
  Editor.insertText(event.editor, event.text);
4838
4905
  },
@@ -4884,12 +4951,14 @@ const EditorActorContext = createContext({}), behaviorActionImplementations = {
4884
4951
  pendingEvents: []
4885
4952
  }),
4886
4953
  "handle behavior event": enqueueActions(({ context, event, enqueue }) => {
4887
- assertEvent(event, ["key down", "before insert text"]);
4954
+ assertEvent(event, ["behavior event"]);
4888
4955
  const eventBehaviors = context.behaviors.filter(
4889
- (behavior) => behavior.on === event.type
4956
+ (behavior) => behavior.on === event.behaviorEvent.type
4890
4957
  );
4891
- if (eventBehaviors.length === 0)
4958
+ if (eventBehaviors.length === 0) {
4959
+ event.behaviorEvent.default();
4892
4960
  return;
4961
+ }
4893
4962
  const value = fromSlateValue(
4894
4963
  event.editor.children,
4895
4964
  context.schema.block.name,
@@ -4902,7 +4971,7 @@ const EditorActorContext = createContext({}), behaviorActionImplementations = {
4902
4971
  if (!selection) {
4903
4972
  console.warn(
4904
4973
  `Unable to handle event ${event.type} due to missing selection`
4905
- );
4974
+ ), event.behaviorEvent.default();
4906
4975
  return;
4907
4976
  }
4908
4977
  const behaviorContext = {
@@ -4910,22 +4979,27 @@ const EditorActorContext = createContext({}), behaviorActionImplementations = {
4910
4979
  value,
4911
4980
  selection
4912
4981
  };
4982
+ let behaviorOverwritten = !1;
4913
4983
  for (const eventBehavior of eventBehaviors) {
4914
4984
  const shouldRun = eventBehavior.guard?.({
4915
4985
  context: behaviorContext,
4916
- event
4986
+ event: event.behaviorEvent
4917
4987
  }) ?? !0;
4918
4988
  if (!shouldRun)
4919
4989
  continue;
4920
4990
  const actions = eventBehavior.actions.map(
4921
- (action) => action({ context: behaviorContext, event }, shouldRun)
4991
+ (action) => action(
4992
+ { context: behaviorContext, event: event.behaviorEvent },
4993
+ shouldRun
4994
+ )
4922
4995
  );
4923
4996
  for (const action of actions)
4924
- typeof action == "object" && enqueue.raise({
4997
+ typeof action == "object" && (behaviorOverwritten = !0, enqueue.raise({
4925
4998
  ...action,
4926
4999
  editor: event.editor
4927
- });
5000
+ }));
4928
5001
  }
5002
+ behaviorOverwritten || event.behaviorEvent.default();
4929
5003
  })
4930
5004
  },
4931
5005
  actors: {
@@ -4957,18 +5031,22 @@ const EditorActorContext = createContext({}), behaviorActionImplementations = {
4957
5031
  loading: { actions: emit({ type: "loading" }) },
4958
5032
  "done loading": { actions: emit({ type: "done loading" }) },
4959
5033
  "update schema": { actions: "assign schema" },
4960
- "key down": {
4961
- actions: ["handle behavior event"]
4962
- },
4963
- "before insert text": {
4964
- actions: ["handle behavior event"]
4965
- },
5034
+ "behavior event": { actions: "handle behavior event" },
4966
5035
  "apply block style": {
4967
5036
  actions: [behaviorActionImplementations["apply block style"]]
4968
5037
  },
5038
+ "delete backward": {
5039
+ actions: [behaviorActionImplementations["delete backward"]]
5040
+ },
4969
5041
  "delete text": {
4970
5042
  actions: [behaviorActionImplementations["delete text"]]
4971
5043
  },
5044
+ "insert break": {
5045
+ actions: [behaviorActionImplementations["insert break"]]
5046
+ },
5047
+ "insert soft break": {
5048
+ actions: [behaviorActionImplementations["insert soft break"]]
5049
+ },
4972
5050
  "insert text": {
4973
5051
  actions: [behaviorActionImplementations["insert text"]]
4974
5052
  },
@@ -5605,11 +5683,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5605
5683
  [editorActor, onBlur]
5606
5684
  ), handleOnBeforeInput = useCallback(
5607
5685
  (event) => {
5608
- onBeforeInput && onBeforeInput(event), !event.defaultPrevented && event.inputType === "insertText" && editorActor.send({
5609
- type: "before insert text",
5610
- nativeEvent: event,
5611
- editor: slateEditor
5612
- });
5686
+ onBeforeInput && onBeforeInput(event);
5613
5687
  },
5614
5688
  [onBeforeInput]
5615
5689
  ), validateSelection = useCallback(() => {
@@ -5648,11 +5722,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5648
5722
  }, [validateSelection, editableElement]);
5649
5723
  const handleKeyDown = useCallback(
5650
5724
  (event) => {
5651
- props.onKeyDown && props.onKeyDown(event), event.isDefaultPrevented() || (editorActor.send({
5652
- type: "key down",
5653
- nativeEvent: event.nativeEvent,
5654
- editor: slateEditor
5655
- }), slateEditor.pteWithHotKeys(event));
5725
+ props.onKeyDown && props.onKeyDown(event), event.isDefaultPrevented() || slateEditor.pteWithHotKeys(event);
5656
5726
  },
5657
5727
  [props, slateEditor]
5658
5728
  ), scrollSelectionIntoViewToSlate = useMemo(() => {