@portabletext/editor 1.1.9 → 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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), isHotkeyEsm = require("is-hotkey-esm"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
3
+ var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -799,20 +799,15 @@ function getFocusBlockObject(context) {
799
799
  const focusBlock = getFocusBlock(context);
800
800
  return focusBlock && !types.isPortableTextTextBlock(focusBlock.node) ? { node: focusBlock.node, path: focusBlock.path } : void 0;
801
801
  }
802
- const overwriteSoftReturn = {
803
- on: "key down",
804
- guard: ({ event }) => isHotkeyEsm.isHotkey("shift+enter", event.nativeEvent),
805
- actions: [
806
- ({ event }) => (event.nativeEvent.preventDefault(), { type: "insert text", text: `
807
- ` })
808
- ]
809
- }, enterOnVoidBlock = {
810
- on: "key down",
811
- guard: ({ context, event }) => isHotkeyEsm.isHotkey("enter", event.nativeEvent) ? !!getFocusBlockObject(context) : !1,
812
- actions: [
813
- ({ event }) => (event.nativeEvent.preventDefault(), { type: "insert text block", decorators: [] })
814
- ]
815
- }, coreBehaviors = [overwriteSoftReturn, enterOnVoidBlock], debug$k = debugWithName("operationToPatches");
802
+ const softReturn = {
803
+ on: "insert soft break",
804
+ actions: [() => ({ type: "insert text", text: `
805
+ ` })]
806
+ }, breakingVoidBlock = {
807
+ on: "insert break",
808
+ guard: ({ context }) => !!getFocusBlockObject(context),
809
+ actions: [() => ({ type: "insert text block", decorators: [] })]
810
+ }, coreBehaviors = [softReturn, breakingVoidBlock], debug$k = debugWithName("operationToPatches");
816
811
  function createOperationToPatches(types2) {
817
812
  const textBlockName = types2.block.name;
818
813
  function insertTextPatch(editor, operation, beforeValue) {
@@ -1087,6 +1082,50 @@ function createOperationToPatches(types2) {
1087
1082
  splitNodePatch
1088
1083
  };
1089
1084
  }
1085
+ function createWithEventListeners(editorActor) {
1086
+ return function(editor) {
1087
+ const { deleteBackward, insertBreak, insertSoftBreak, insertText } = editor;
1088
+ return editor.deleteBackward = (unit) => {
1089
+ editorActor.send({
1090
+ type: "behavior event",
1091
+ behaviorEvent: {
1092
+ type: "delete backward",
1093
+ unit,
1094
+ default: () => deleteBackward(unit)
1095
+ },
1096
+ editor
1097
+ });
1098
+ }, editor.insertBreak = () => {
1099
+ editorActor.send({
1100
+ type: "behavior event",
1101
+ behaviorEvent: {
1102
+ type: "insert break",
1103
+ default: insertBreak
1104
+ },
1105
+ editor
1106
+ });
1107
+ }, editor.insertSoftBreak = () => {
1108
+ editorActor.send({
1109
+ type: "behavior event",
1110
+ behaviorEvent: {
1111
+ type: "insert soft break",
1112
+ default: insertSoftBreak
1113
+ },
1114
+ editor
1115
+ });
1116
+ }, editor.insertText = (text, options) => {
1117
+ editorActor.send({
1118
+ type: "behavior event",
1119
+ behaviorEvent: {
1120
+ type: "insert text",
1121
+ text,
1122
+ default: () => insertText(text, options)
1123
+ },
1124
+ editor
1125
+ });
1126
+ }, editor;
1127
+ };
1128
+ }
1090
1129
  const debug$j = debugWithName("API:editable");
1091
1130
  function createWithEditableAPI(editorActor, portableTextEditor, types$1) {
1092
1131
  return function(editor) {
@@ -1602,7 +1641,9 @@ function createWithInsertBreak(editorActor, types2) {
1602
1641
  slate.Editor.insertNode(
1603
1642
  editor,
1604
1643
  editor.pteCreateTextBlock({
1605
- decorators: focusAnnotations.length === 0 ? focusDecorators : []
1644
+ decorators: focusAnnotations.length === 0 ? focusDecorators : [],
1645
+ listItem: focusBlock.listItem,
1646
+ level: focusBlock.level
1606
1647
  })
1607
1648
  );
1608
1649
  const [nextBlockPath] = slate.Path.next(focusBlockPath);
@@ -1620,7 +1661,9 @@ function createWithInsertBreak(editorActor, types2) {
1620
1661
  slate.Editor.insertNode(
1621
1662
  editor,
1622
1663
  editor.pteCreateTextBlock({
1623
- decorators: []
1664
+ decorators: [],
1665
+ listItem: focusBlock.listItem,
1666
+ level: focusBlock.level
1624
1667
  })
1625
1668
  );
1626
1669
  const [nextBlockPath] = slate.Path.next(focusBlockPath);
@@ -3778,6 +3821,8 @@ function createWithUtils({
3778
3821
  _type: schemaTypes.block.name,
3779
3822
  _key: editorActor.getSnapshot().context.keyGenerator(),
3780
3823
  style: schemaTypes.styles[0].value || "normal",
3824
+ ...options.listItem ? { listItem: options.listItem } : {},
3825
+ ...options.level ? { level: options.level } : {},
3781
3826
  markDefs: [],
3782
3827
  children: [
3783
3828
  {
@@ -4404,7 +4449,7 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4404
4449
  }), withPortableTextSelections = createWithPortableTextSelections(
4405
4450
  editorActor,
4406
4451
  schemaTypes
4407
- );
4452
+ ), withEventListeners = createWithEventListeners(editorActor);
4408
4453
  return e.destroy = () => {
4409
4454
  const originalFunctions = originalFnMap.get(e);
4410
4455
  if (!originalFunctions)
@@ -4430,18 +4475,20 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4430
4475
  ),
4431
4476
  subscribe: () => noop__default.default
4432
4477
  } : {
4433
- editor: withSchemaTypes(
4434
- withObjectKeys(
4435
- withPortableTextMarkModel(
4436
- withPortableTextBlockStyle(
4437
- withPortableTextLists(
4438
- withPlaceholderBlock(
4439
- withUtils(
4440
- withMaxBlocks(
4441
- withUndoRedo(
4442
- withPatches(
4443
- withPortableTextSelections(
4444
- withEditableAPI(withInsertBreak(e))
4478
+ editor: withEventListeners(
4479
+ withSchemaTypes(
4480
+ withObjectKeys(
4481
+ withPortableTextMarkModel(
4482
+ withPortableTextBlockStyle(
4483
+ withPortableTextLists(
4484
+ withPlaceholderBlock(
4485
+ withUtils(
4486
+ withMaxBlocks(
4487
+ withUndoRedo(
4488
+ withPatches(
4489
+ withPortableTextSelections(
4490
+ withEditableAPI(withInsertBreak(e))
4491
+ )
4445
4492
  )
4446
4493
  )
4447
4494
  )
@@ -4822,11 +4869,20 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4822
4869
  slate.Transforms.setNodes(event.editor, { style: event.style }, { at });
4823
4870
  }
4824
4871
  },
4872
+ "delete backward": ({ event }) => {
4873
+ slate.Editor.deleteBackward(event.editor, { unit: event.unit });
4874
+ },
4825
4875
  "delete text": ({ event }) => {
4826
4876
  slate.Transforms.delete(event.editor, {
4827
4877
  at: toSlateRange(event.selection, event.editor)
4828
4878
  });
4829
4879
  },
4880
+ "insert break": ({ event }) => {
4881
+ slate.Editor.insertBreak(event.editor);
4882
+ },
4883
+ "insert soft break": ({ event }) => {
4884
+ slate.Editor.insertSoftBreak(event.editor);
4885
+ },
4830
4886
  "insert text": ({ event }) => {
4831
4887
  slate.Editor.insertText(event.editor, event.text);
4832
4888
  },
@@ -4878,12 +4934,14 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4878
4934
  pendingEvents: []
4879
4935
  }),
4880
4936
  "handle behavior event": xstate.enqueueActions(({ context, event, enqueue }) => {
4881
- xstate.assertEvent(event, ["key down", "before insert text"]);
4937
+ xstate.assertEvent(event, ["behavior event"]);
4882
4938
  const eventBehaviors = context.behaviors.filter(
4883
- (behavior) => behavior.on === event.type
4939
+ (behavior) => behavior.on === event.behaviorEvent.type
4884
4940
  );
4885
- if (eventBehaviors.length === 0)
4941
+ if (eventBehaviors.length === 0) {
4942
+ event.behaviorEvent.default();
4886
4943
  return;
4944
+ }
4887
4945
  const value = fromSlateValue(
4888
4946
  event.editor.children,
4889
4947
  context.schema.block.name,
@@ -4896,7 +4954,7 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4896
4954
  if (!selection) {
4897
4955
  console.warn(
4898
4956
  `Unable to handle event ${event.type} due to missing selection`
4899
- );
4957
+ ), event.behaviorEvent.default();
4900
4958
  return;
4901
4959
  }
4902
4960
  const behaviorContext = {
@@ -4904,22 +4962,27 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4904
4962
  value,
4905
4963
  selection
4906
4964
  };
4965
+ let behaviorOverwritten = !1;
4907
4966
  for (const eventBehavior of eventBehaviors) {
4908
4967
  const shouldRun = eventBehavior.guard?.({
4909
4968
  context: behaviorContext,
4910
- event
4969
+ event: event.behaviorEvent
4911
4970
  }) ?? !0;
4912
4971
  if (!shouldRun)
4913
4972
  continue;
4914
4973
  const actions = eventBehavior.actions.map(
4915
- (action) => action({ context: behaviorContext, event }, shouldRun)
4974
+ (action) => action(
4975
+ { context: behaviorContext, event: event.behaviorEvent },
4976
+ shouldRun
4977
+ )
4916
4978
  );
4917
4979
  for (const action of actions)
4918
- typeof action == "object" && enqueue.raise({
4980
+ typeof action == "object" && (behaviorOverwritten = !0, enqueue.raise({
4919
4981
  ...action,
4920
4982
  editor: event.editor
4921
- });
4983
+ }));
4922
4984
  }
4985
+ behaviorOverwritten || event.behaviorEvent.default();
4923
4986
  })
4924
4987
  },
4925
4988
  actors: {
@@ -4951,18 +5014,22 @@ const EditorActorContext = react.createContext({}), behaviorActionImplementation
4951
5014
  loading: { actions: xstate.emit({ type: "loading" }) },
4952
5015
  "done loading": { actions: xstate.emit({ type: "done loading" }) },
4953
5016
  "update schema": { actions: "assign schema" },
4954
- "key down": {
4955
- actions: ["handle behavior event"]
4956
- },
4957
- "before insert text": {
4958
- actions: ["handle behavior event"]
4959
- },
5017
+ "behavior event": { actions: "handle behavior event" },
4960
5018
  "apply block style": {
4961
5019
  actions: [behaviorActionImplementations["apply block style"]]
4962
5020
  },
5021
+ "delete backward": {
5022
+ actions: [behaviorActionImplementations["delete backward"]]
5023
+ },
4963
5024
  "delete text": {
4964
5025
  actions: [behaviorActionImplementations["delete text"]]
4965
5026
  },
5027
+ "insert break": {
5028
+ actions: [behaviorActionImplementations["insert break"]]
5029
+ },
5030
+ "insert soft break": {
5031
+ actions: [behaviorActionImplementations["insert soft break"]]
5032
+ },
4966
5033
  "insert text": {
4967
5034
  actions: [behaviorActionImplementations["insert text"]]
4968
5035
  },
@@ -5599,11 +5666,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5599
5666
  [editorActor, onBlur]
5600
5667
  ), handleOnBeforeInput = react.useCallback(
5601
5668
  (event) => {
5602
- onBeforeInput && onBeforeInput(event), !event.defaultPrevented && event.inputType === "insertText" && editorActor.send({
5603
- type: "before insert text",
5604
- nativeEvent: event,
5605
- editor: slateEditor
5606
- });
5669
+ onBeforeInput && onBeforeInput(event);
5607
5670
  },
5608
5671
  [onBeforeInput]
5609
5672
  ), validateSelection = react.useCallback(() => {
@@ -5642,11 +5705,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5642
5705
  }, [validateSelection, editableElement]);
5643
5706
  const handleKeyDown = react.useCallback(
5644
5707
  (event) => {
5645
- props.onKeyDown && props.onKeyDown(event), event.isDefaultPrevented() || (editorActor.send({
5646
- type: "key down",
5647
- nativeEvent: event.nativeEvent,
5648
- editor: slateEditor
5649
- }), slateEditor.pteWithHotKeys(event));
5708
+ props.onKeyDown && props.onKeyDown(event), event.isDefaultPrevented() || slateEditor.pteWithHotKeys(event);
5650
5709
  },
5651
5710
  [props, slateEditor]
5652
5711
  ), scrollSelectionIntoViewToSlate = react.useMemo(() => {