@portabletext/editor 1.0.9 → 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.d.mts CHANGED
@@ -136,6 +136,9 @@ export declare type ConnectionChange = {
136
136
  value: 'online' | 'offline'
137
137
  }
138
138
 
139
+ /**
140
+ * @internal
141
+ */
139
142
  export declare type createEditorOptions = {
140
143
  keyGenerator: () => string
141
144
  patches$?: PatchObservable
@@ -223,6 +226,9 @@ export declare type EditorChange =
223
226
  | UnsetChange
224
227
  | ValueChange
225
228
 
229
+ /**
230
+ * @beta
231
+ */
226
232
  export declare type EditorChanges = Subject<EditorChange>
227
233
 
228
234
  /** @internal */
@@ -276,6 +282,9 @@ export declare type HistoryItem = {
276
282
  timestamp: Date
277
283
  }
278
284
 
285
+ /**
286
+ * @beta
287
+ */
279
288
  export declare type HotkeyOptions = {
280
289
  marks?: Record<string, string>
281
290
  custom?: Record<string, (event: BaseSyntheticEvent, editor: PortableTextEditor) => void>
@@ -313,6 +322,9 @@ export declare type InvalidValueResolution = {
313
322
  }
314
323
  }
315
324
 
325
+ /**
326
+ * @public
327
+ */
316
328
  export declare const keyGenerator: () => string
317
329
 
318
330
  /**
@@ -357,6 +369,9 @@ export declare type OnPasteResult =
357
369
  }
358
370
  | undefined
359
371
 
372
+ /**
373
+ * @beta
374
+ */
360
375
  export declare type OnPasteResultOrPromise = OnPasteResult | Promise<OnPasteResult>
361
376
 
362
377
  /** @beta */
@@ -545,11 +560,6 @@ export declare class PortableTextEditor extends Component<PortableTextEditorProp
545
560
  ) => boolean | undefined
546
561
  }
547
562
 
548
- /**
549
- * Props for the PortableTextEditor component
550
- *
551
- * @public
552
- */
553
563
  /**
554
564
  * Props for the PortableTextEditor component
555
565
  *
@@ -822,11 +832,13 @@ export declare type UnsetChange = {
822
832
  }
823
833
 
824
834
  /**
835
+ * @public
825
836
  * Get the current editor object from the React context.
826
837
  */
827
838
  export declare const usePortableTextEditor: () => PortableTextEditor
828
839
 
829
840
  /**
841
+ * @public
830
842
  * Get the current editor selection from the React context.
831
843
  */
832
844
  export declare const usePortableTextEditorSelection: () => EditorSelection
package/lib/index.d.ts CHANGED
@@ -136,6 +136,9 @@ export declare type ConnectionChange = {
136
136
  value: 'online' | 'offline'
137
137
  }
138
138
 
139
+ /**
140
+ * @internal
141
+ */
139
142
  export declare type createEditorOptions = {
140
143
  keyGenerator: () => string
141
144
  patches$?: PatchObservable
@@ -223,6 +226,9 @@ export declare type EditorChange =
223
226
  | UnsetChange
224
227
  | ValueChange
225
228
 
229
+ /**
230
+ * @beta
231
+ */
226
232
  export declare type EditorChanges = Subject<EditorChange>
227
233
 
228
234
  /** @internal */
@@ -276,6 +282,9 @@ export declare type HistoryItem = {
276
282
  timestamp: Date
277
283
  }
278
284
 
285
+ /**
286
+ * @beta
287
+ */
279
288
  export declare type HotkeyOptions = {
280
289
  marks?: Record<string, string>
281
290
  custom?: Record<string, (event: BaseSyntheticEvent, editor: PortableTextEditor) => void>
@@ -313,6 +322,9 @@ export declare type InvalidValueResolution = {
313
322
  }
314
323
  }
315
324
 
325
+ /**
326
+ * @public
327
+ */
316
328
  export declare const keyGenerator: () => string
317
329
 
318
330
  /**
@@ -357,6 +369,9 @@ export declare type OnPasteResult =
357
369
  }
358
370
  | undefined
359
371
 
372
+ /**
373
+ * @beta
374
+ */
360
375
  export declare type OnPasteResultOrPromise = OnPasteResult | Promise<OnPasteResult>
361
376
 
362
377
  /** @beta */
@@ -545,11 +560,6 @@ export declare class PortableTextEditor extends Component<PortableTextEditorProp
545
560
  ) => boolean | undefined
546
561
  }
547
562
 
548
- /**
549
- * Props for the PortableTextEditor component
550
- *
551
- * @public
552
- */
553
563
  /**
554
564
  * Props for the PortableTextEditor component
555
565
  *
@@ -822,11 +832,13 @@ export declare type UnsetChange = {
822
832
  }
823
833
 
824
834
  /**
835
+ * @public
825
836
  * Get the current editor object from the React context.
826
837
  */
827
838
  export declare const usePortableTextEditor: () => PortableTextEditor
828
839
 
829
840
  /**
841
+ * @public
830
842
  * Get the current editor selection from the React context.
831
843
  */
832
844
  export declare const usePortableTextEditorSelection: () => EditorSelection
package/lib/index.esm.js CHANGED
@@ -1020,9 +1020,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1020
1020
  )) == null ? void 0 : _a.focus.path) || [];
1021
1021
  },
1022
1022
  insertBlock: (type, value) => {
1023
- var _a;
1024
- if (!editor.selection)
1025
- throw new Error("The editor has no selection");
1023
+ var _a, _b, _c;
1026
1024
  const block = toSlateValue(
1027
1025
  [
1028
1026
  {
@@ -1032,17 +1030,32 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1032
1030
  }
1033
1031
  ],
1034
1032
  portableTextEditor
1035
- )[0], [focusBlock] = Array.from(
1033
+ )[0];
1034
+ if (!editor.selection) {
1035
+ const lastBlock = Array.from(
1036
+ Editor.nodes(editor, {
1037
+ match: (n) => !Editor.isEditor(n),
1038
+ at: [],
1039
+ reverse: !0
1040
+ })
1041
+ )[0];
1042
+ return Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types) && Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), (_b = (_a = toPortableTextRange(
1043
+ fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1044
+ editor.selection,
1045
+ types
1046
+ )) == null ? void 0 : _a.focus.path) != null ? _b : [];
1047
+ }
1048
+ const focusBlock = Array.from(
1036
1049
  Editor.nodes(editor, {
1037
1050
  at: editor.selection.focus.path.slice(0, 1),
1038
1051
  match: (n) => n._type === types.block.name
1039
1052
  })
1040
- )[0] || [void 0];
1041
- return focusBlock && isEqualToEmptyEditor([focusBlock], types) && Transforms.removeNodes(editor, { at: editor.selection }), Editor.insertNode(editor, block), editor.onChange(), ((_a = toPortableTextRange(
1053
+ )[0];
1054
+ return Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types) && Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), ((_c = toPortableTextRange(
1042
1055
  fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1043
1056
  editor.selection,
1044
1057
  types
1045
- )) == null ? void 0 : _a.focus.path) || [];
1058
+ )) == null ? void 0 : _c.focus.path) || [];
1046
1059
  },
1047
1060
  hasBlockStyle: (style) => {
1048
1061
  try {
@@ -1127,7 +1140,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1127
1140
  match: (node) => Text.isText(node)
1128
1141
  })
1129
1142
  ];
1130
- if (spans.some(
1143
+ if (spans.length === 0 || spans.some(
1131
1144
  ([span]) => {
1132
1145
  var _a;
1133
1146
  return !isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
@@ -3047,7 +3060,7 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
3047
3060
  const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions || DEFAULT_HOTKEYS;
3048
3061
  return function(editor) {
3049
3062
  return editor.pteWithHotKeys = (event) => {
3050
- var _a, _b, _c, _d;
3063
+ var _a, _b, _c, _d, _e, _f;
3051
3064
  Object.keys(activeHotkeys).forEach((cat) => {
3052
3065
  if (cat === "marks")
3053
3066
  for (const hotkey in activeHotkeys[cat]) {
@@ -3120,8 +3133,8 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
3120
3133
  const nextBlock = Node.descendant(
3121
3134
  editor,
3122
3135
  Path.next(editor.selection.focus.path.slice(0, 1))
3123
- ), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
3124
- if (nextBlock && focusBlock && !Editor.isVoid(editor, focusBlock) && Editor.isVoid(editor, nextBlock)) {
3136
+ ), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 && ((_f = (_e = focusBlock.children) == null ? void 0 : _e[0]) == null ? void 0 : _f.text) === "";
3137
+ if (nextBlock && focusBlock && !Editor.isVoid(editor, focusBlock) && Editor.isVoid(editor, nextBlock) && isEmptyFocusBlock) {
3125
3138
  debug$8("Preventing deleting void block below"), event.preventDefault(), event.stopPropagation(), Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), Transforms.select(editor, focusBlockPath), editor.onChange();
3126
3139
  return;
3127
3140
  }
@@ -4000,7 +4013,7 @@ class PortableTextEditor extends Component {
4000
4013
  if (this.editable)
4001
4014
  return this.editable.getValue();
4002
4015
  }), !props.schemaType)
4003
- throw new Error('PortableTextEditor: missing "type" property');
4016
+ throw new Error('PortableTextEditor: missing "schemaType" property');
4004
4017
  props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
4005
4018
  props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
4006
4019
  );