@portabletext/editor 1.0.10 → 1.0.11

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
@@ -1004,9 +1004,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1004
1004
  )) == null ? void 0 : _a.focus.path) || [];
1005
1005
  },
1006
1006
  insertBlock: (type, value) => {
1007
- var _a;
1008
- if (!editor.selection)
1009
- throw new Error("The editor has no selection");
1007
+ var _a, _b, _c;
1010
1008
  const block = toSlateValue(
1011
1009
  [
1012
1010
  {
@@ -1016,17 +1014,32 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1016
1014
  }
1017
1015
  ],
1018
1016
  portableTextEditor
1019
- )[0], [focusBlock] = Array.from(
1017
+ )[0];
1018
+ if (!editor.selection) {
1019
+ const lastBlock = Array.from(
1020
+ slate.Editor.nodes(editor, {
1021
+ match: (n) => !slate.Editor.isEditor(n),
1022
+ at: [],
1023
+ reverse: !0
1024
+ })
1025
+ )[0];
1026
+ return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), (_b = (_a = toPortableTextRange(
1027
+ fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1028
+ editor.selection,
1029
+ types$1
1030
+ )) == null ? void 0 : _a.focus.path) != null ? _b : [];
1031
+ }
1032
+ const focusBlock = Array.from(
1020
1033
  slate.Editor.nodes(editor, {
1021
1034
  at: editor.selection.focus.path.slice(0, 1),
1022
1035
  match: (n) => n._type === types$1.block.name
1023
1036
  })
1024
- )[0] || [void 0];
1025
- return focusBlock && isEqualToEmptyEditor([focusBlock], types$1) && slate.Transforms.removeNodes(editor, { at: editor.selection }), slate.Editor.insertNode(editor, block), editor.onChange(), ((_a = toPortableTextRange(
1037
+ )[0];
1038
+ return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), ((_c = toPortableTextRange(
1026
1039
  fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1027
1040
  editor.selection,
1028
1041
  types$1
1029
- )) == null ? void 0 : _a.focus.path) || [];
1042
+ )) == null ? void 0 : _c.focus.path) || [];
1030
1043
  },
1031
1044
  hasBlockStyle: (style) => {
1032
1045
  try {
@@ -1111,7 +1124,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1111
1124
  match: (node) => slate.Text.isText(node)
1112
1125
  })
1113
1126
  ];
1114
- if (spans.some(
1127
+ if (spans.length === 0 || spans.some(
1115
1128
  ([span]) => {
1116
1129
  var _a;
1117
1130
  return !types.isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;