@portabletext/editor 3.0.9 → 3.1.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
@@ -5,7 +5,7 @@ import noop from "lodash/noop.js";
5
5
  import { createContext, useContext, useEffect, useState, useRef, forwardRef, Component, startTransition } from "react";
6
6
  import { Element as Element$1, Text, Range, Editor, Node, Point, Path, Transforms, Operation, deleteText, createEditor } from "slate";
7
7
  import { useSelected, useSlateSelector, useSlateStatic, ReactEditor, useSlate, Editable, withReact, Slate } from "slate-react";
8
- import debug$j from "debug";
8
+ import debug$h from "debug";
9
9
  import { DOMEditor, isDOMNode, EDITOR_TO_PENDING_SELECTION, IS_FOCUSED, IS_READ_ONLY } from "slate-dom";
10
10
  import { getBlockEndPoint, getBlockStartPoint, getBlockKeyFromSelectionPoint, isSelectionCollapsed, isEqualSelectionPoints, getChildKeyFromSelectionPoint, blockOffsetToSpanSelectionPoint, defaultKeyGenerator, parseBlocks, parseBlock, parseAnnotation, parseMarkDefs, parseSpan, parseInlineObject, isKeyedSegment, isListBlock, isTypedObject, getSelectionStartPoint, getSelectionEndPoint } from "./_chunks-es/util.slice-blocks.js";
11
11
  import isEqual from "lodash/isEqual.js";
@@ -29,10 +29,10 @@ import { EditorContext } from "./_chunks-es/use-editor.js";
29
29
  import { useEditor } from "./_chunks-es/use-editor.js";
30
30
  import { Subject } from "rxjs";
31
31
  const rootName = "sanity-pte:";
32
- debug$j(rootName);
32
+ debug$h(rootName);
33
33
  function debugWithName(name) {
34
34
  const namespace = `${rootName}${name}`;
35
- return debug$j && debug$j.enabled(namespace) ? debug$j(namespace) : debug$j(rootName);
35
+ return debug$h && debug$h.enabled(namespace) ? debug$h(namespace) : debug$h(rootName);
36
36
  }
37
37
  const VOID_CHILD_KEY = "void-child";
38
38
  function keepObjectEquality(object, keyMap) {
@@ -138,8 +138,8 @@ function fromSlateBlock(block, textBlockType, keyMap = {}) {
138
138
  ...typeof blockValue == "object" ? blockValue : {}
139
139
  }, keyMap);
140
140
  }
141
- function isEqualToEmptyEditor(blocks, schemaTypes) {
142
- if (blocks.length !== 1)
141
+ function isEqualToEmptyEditor(initialValue, blocks, schemaTypes) {
142
+ if (!blocks || blocks.length !== 1)
143
143
  return !1;
144
144
  const firstBlock = blocks.at(0);
145
145
  if (!firstBlock)
@@ -147,7 +147,7 @@ function isEqualToEmptyEditor(blocks, schemaTypes) {
147
147
  if (!Element$1.isElement(firstBlock) || firstBlock._type !== schemaTypes.block.name || "listItem" in firstBlock || !("style" in firstBlock) || firstBlock.style !== schemaTypes.styles.at(0)?.name || !Array.isArray(firstBlock.children) || firstBlock.children.length !== 1)
148
148
  return !1;
149
149
  const firstChild = firstBlock.children.at(0);
150
- return !(!firstChild || !Text.isText(firstChild) || !("_type" in firstChild) || firstChild._type !== schemaTypes.span.name || firstChild.text !== "" || firstChild.marks?.join(""));
150
+ return !(!firstChild || !Text.isText(firstChild) || !("_type" in firstChild) || firstChild._type !== schemaTypes.span.name || firstChild.text !== "" || firstChild.marks?.join("") || Object.keys(firstBlock).some((key) => key !== "_type" && key !== "_key" && key !== "children" && key !== "markDefs" && key !== "style") || isEqual(initialValue, [firstBlock]));
151
151
  }
152
152
  function getFocusBlock({
153
153
  editor
@@ -1538,7 +1538,7 @@ function toKeyName(name) {
1538
1538
  const keyName = name.toLowerCase();
1539
1539
  return aliases[keyName] ?? keyName;
1540
1540
  }
1541
- const debug$i = debugWithName("plugin:withHotKeys");
1541
+ const debug$g = debugWithName("plugin:withHotKeys");
1542
1542
  function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions) {
1543
1543
  const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions ?? {};
1544
1544
  return function(editor) {
@@ -1553,7 +1553,7 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
1553
1553
  const possibleMark = activeHotkeys[cat];
1554
1554
  if (possibleMark) {
1555
1555
  const mark = possibleMark[hotkey];
1556
- debug$i(`HotKey ${hotkey} to toggle ${mark}`), editorActor.send({
1556
+ debug$g(`HotKey ${hotkey} to toggle ${mark}`), editorActor.send({
1557
1557
  type: "behavior event",
1558
1558
  behaviorEvent: {
1559
1559
  type: "decorator.toggle",
@@ -1853,7 +1853,10 @@ const slateOperationCallback = ({
1853
1853
  });
1854
1854
  function createDecorate(schema, slateEditor) {
1855
1855
  return function([node, path]) {
1856
- if (isEqualToEmptyEditor(slateEditor.value, schema))
1856
+ const defaultStyle = schema.styles.at(0)?.name;
1857
+ if (slateEditor.value.length === 1 && isEmptyTextBlock({
1858
+ schema
1859
+ }, slateEditor.value[0]) && (!slateEditor.value[0].style || slateEditor.value[0].style === defaultStyle) && !slateEditor.value[0].listItem)
1857
1860
  return [{
1858
1861
  anchor: {
1859
1862
  path: [0, 0],
@@ -1882,7 +1885,7 @@ function createDecorate(schema, slateEditor) {
1882
1885
  }) || Range.includes(decoratedRange, path));
1883
1886
  };
1884
1887
  }
1885
- const RelayActorContext = createContext({}), debug$h = debugWithName("validate selection machine"), validateSelectionSetup = setup({
1888
+ const RelayActorContext = createContext({}), debug$f = debugWithName("validate selection machine"), validateSelectionSetup = setup({
1886
1889
  types: {
1887
1890
  context: {},
1888
1891
  input: {},
@@ -1954,12 +1957,12 @@ function validateSelection(slateEditor, editorElement) {
1954
1957
  const existingDOMRange = domSelection.getRangeAt(0);
1955
1958
  try {
1956
1959
  const newDOMRange = ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
1957
- (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug$h("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
1960
+ (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug$f("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
1958
1961
  } catch {
1959
- debug$h("Could not resolve selection, selecting top document"), Transforms.deselect(slateEditor), slateEditor.children.length > 0 && Transforms.select(slateEditor, Editor.start(slateEditor, [])), slateEditor.onChange();
1962
+ debug$f("Could not resolve selection, selecting top document"), Transforms.deselect(slateEditor), slateEditor.children.length > 0 && Transforms.select(slateEditor, Editor.start(slateEditor, [])), slateEditor.onChange();
1960
1963
  }
1961
1964
  }
1962
- const debug$g = debugWithName("component:Editable"), PortableTextEditable = forwardRef(function(props, forwardedRef) {
1965
+ const debug$e = debugWithName("component:Editable"), PortableTextEditable = forwardRef(function(props, forwardedRef) {
1963
1966
  const $ = c(176);
1964
1967
  let hotkeys, onBeforeInput, onBlur, onClick, onCopy, onCut, onDrag, onDragEnd, onDragEnter, onDragLeave, onDragOver, onDragStart, onDrop, onFocus, onPaste, propsSelection, rangeDecorations, renderAnnotation, renderBlock, renderChild, renderDecorator, renderListItem, renderPlaceholder, renderStyle, restProps, scrollSelectionIntoView, spellCheck;
1965
1968
  $[0] !== props ? ({
@@ -2048,10 +2051,10 @@ const debug$g = debugWithName("component:Editable"), PortableTextEditable = forw
2048
2051
  let t14;
2049
2052
  $[63] !== editorActor || $[64] !== propsSelection || $[65] !== slateEditor ? (t14 = () => {
2050
2053
  if (propsSelection) {
2051
- debug$g(`Selection from props ${JSON.stringify(propsSelection)}`);
2054
+ debug$e(`Selection from props ${JSON.stringify(propsSelection)}`);
2052
2055
  const normalizedSelection = normalizeSelection(propsSelection, slateEditor.value);
2053
2056
  if (normalizedSelection !== null) {
2054
- debug$g(`Normalized selection from props ${JSON.stringify(normalizedSelection)}`);
2057
+ debug$e(`Normalized selection from props ${JSON.stringify(normalizedSelection)}`);
2055
2058
  const slateRange = toSlateRange({
2056
2059
  context: {
2057
2060
  schema: editorActor.getSnapshot().context.schema,
@@ -2163,8 +2166,8 @@ const debug$g = debugWithName("component:Editable"), PortableTextEditable = forw
2163
2166
  event_1.preventDefault(), relayActor.send({
2164
2167
  type: "loading"
2165
2168
  }), Promise.resolve(onPasteResult).then((result_1) => {
2166
- if (debug$g("Custom paste function from client resolved", result_1), !result_1 || !result_1.insert) {
2167
- debug$g("No result from custom paste handler, pasting normally");
2169
+ if (debug$e("Custom paste function from client resolved", result_1), !result_1 || !result_1.insert) {
2170
+ debug$e("No result from custom paste handler, pasting normally");
2168
2171
  const selection_1 = editorActor.getSnapshot().context.selection, position_1 = selection_1 ? {
2169
2172
  selection: selection_1
2170
2173
  } : void 0;
@@ -2232,7 +2235,7 @@ const debug$g = debugWithName("component:Editable"), PortableTextEditable = forw
2232
2235
  nativeEvent: event_1
2233
2236
  });
2234
2237
  }
2235
- debug$g("No result from custom paste handler, pasting normally");
2238
+ debug$e("No result from custom paste handler, pasting normally");
2236
2239
  }, $[85] = editorActor, $[86] = onPaste, $[87] = portableTextEditor, $[88] = relayActor, $[89] = slateEditor, $[90] = t21) : t21 = $[90];
2237
2240
  const handlePaste = t21;
2238
2241
  let t22;
@@ -2972,7 +2975,7 @@ function mergeIntoLastStep(steps, lastStep, op) {
2972
2975
  operations: [...lastStep.operations, op]
2973
2976
  }];
2974
2977
  }
2975
- const debug$f = debugWithName("plugin:history"), UNDO_STEP_LIMIT = 1e3;
2978
+ const debug$d = debugWithName("plugin:history"), UNDO_STEP_LIMIT = 1e3;
2976
2979
  function pluginHistory({
2977
2980
  editorActor,
2978
2981
  subscriptions
@@ -2989,7 +2992,7 @@ function pluginHistory({
2989
2992
  for (const patch of patches)
2990
2993
  if (!reset && patch.origin !== "local") {
2991
2994
  if (patch.type === "unset" && patch.path.length === 0) {
2992
- debug$f("Someone else cleared the content, resetting undo/redo history"), editor.history = {
2995
+ debug$d("Someone else cleared the content, resetting undo/redo history"), editor.history = {
2993
2996
  undos: [],
2994
2997
  redos: []
2995
2998
  }, remotePatches.splice(0, remotePatches.length), setWithHistory(editor, !0), reset = !0;
@@ -3074,7 +3077,15 @@ function getNextSpan({
3074
3077
  }
3075
3078
  return nextSpan;
3076
3079
  }
3077
- const debug$e = debugWithName("plugin:withPortableTextMarkModel");
3080
+ const PATCHING = /* @__PURE__ */ new WeakMap();
3081
+ function withoutPatching(editor, fn) {
3082
+ const prev = isPatching(editor);
3083
+ PATCHING.set(editor, !1), fn(), PATCHING.set(editor, prev);
3084
+ }
3085
+ function isPatching(editor) {
3086
+ return PATCHING.get(editor);
3087
+ }
3088
+ const debug$c = debugWithName("plugin:withPortableTextMarkModel");
3078
3089
  function createWithPortableTextMarkModel(editorActor) {
3079
3090
  return function(editor) {
3080
3091
  const {
@@ -3083,12 +3094,19 @@ function createWithPortableTextMarkModel(editorActor) {
3083
3094
  } = editor, decorators = editorActor.getSnapshot().context.schema.decorators.map((t) => t.name), defaultStyle = editorActor.getSnapshot().context.schema.styles.at(0)?.name;
3084
3095
  return editor.normalizeNode = (nodeEntry) => {
3085
3096
  const [node, path] = nodeEntry;
3086
- if (editor.isTextBlock(node)) {
3097
+ if (Editor.isEditor(node) && node.children.length === 0 && withoutPatching(editor, () => {
3098
+ withNormalizeNode(editor, () => {
3099
+ Transforms.insertNodes(editor, createPlaceholderBlock(editorActor.getSnapshot().context), {
3100
+ at: [0],
3101
+ select: !0
3102
+ });
3103
+ });
3104
+ }), editor.isTextBlock(node)) {
3087
3105
  const children = Node.children(editor, path);
3088
3106
  for (const [child, childPath] of children) {
3089
3107
  const nextNode = node.children[childPath[1] + 1];
3090
3108
  if (editor.isTextSpan(child) && editor.isTextSpan(nextNode) && child.marks?.every((mark) => nextNode.marks?.includes(mark)) && nextNode.marks?.every((mark) => child.marks?.includes(mark))) {
3091
- debug$e("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), withNormalizeNode(editor, () => {
3109
+ debug$c("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), withNormalizeNode(editor, () => {
3092
3110
  Transforms.mergeNodes(editor, {
3093
3111
  at: [childPath[0], childPath[1] + 1],
3094
3112
  voids: !0
@@ -3099,7 +3117,7 @@ function createWithPortableTextMarkModel(editorActor) {
3099
3117
  }
3100
3118
  }
3101
3119
  if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
3102
- debug$e("Adding .markDefs to block node"), withNormalizeNode(editor, () => {
3120
+ debug$c("Adding .markDefs to block node"), withNormalizeNode(editor, () => {
3103
3121
  Transforms.setNodes(editor, {
3104
3122
  markDefs: []
3105
3123
  }, {
@@ -3109,7 +3127,7 @@ function createWithPortableTextMarkModel(editorActor) {
3109
3127
  return;
3110
3128
  }
3111
3129
  if (defaultStyle && editor.isTextBlock(node) && typeof node.style > "u") {
3112
- debug$e("Adding .style to block node"), withNormalizeNode(editor, () => {
3130
+ debug$c("Adding .style to block node"), withNormalizeNode(editor, () => {
3113
3131
  Transforms.setNodes(editor, {
3114
3132
  style: defaultStyle
3115
3133
  }, {
@@ -3119,7 +3137,7 @@ function createWithPortableTextMarkModel(editorActor) {
3119
3137
  return;
3120
3138
  }
3121
3139
  if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
3122
- debug$e("Adding .marks to span node"), withNormalizeNode(editor, () => {
3140
+ debug$c("Adding .marks to span node"), withNormalizeNode(editor, () => {
3123
3141
  Transforms.setNodes(editor, {
3124
3142
  marks: []
3125
3143
  }, {
@@ -3131,7 +3149,7 @@ function createWithPortableTextMarkModel(editorActor) {
3131
3149
  if (editor.isTextSpan(node)) {
3132
3150
  const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath), decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
3133
3151
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
3134
- debug$e("Removing annotations from empty span node"), withNormalizeNode(editor, () => {
3152
+ debug$c("Removing annotations from empty span node"), withNormalizeNode(editor, () => {
3135
3153
  Transforms.setNodes(editor, {
3136
3154
  marks: node.marks?.filter((mark) => decorators2.includes(mark))
3137
3155
  }, {
@@ -3147,7 +3165,7 @@ function createWithPortableTextMarkModel(editorActor) {
3147
3165
  if (editor.isTextSpan(child)) {
3148
3166
  const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
3149
3167
  if (orphanedAnnotations.length > 0) {
3150
- debug$e("Removing orphaned annotations from span node"), withNormalizeNode(editor, () => {
3168
+ debug$c("Removing orphaned annotations from span node"), withNormalizeNode(editor, () => {
3151
3169
  Transforms.setNodes(editor, {
3152
3170
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
3153
3171
  }, {
@@ -3163,7 +3181,7 @@ function createWithPortableTextMarkModel(editorActor) {
3163
3181
  if (editor.isTextBlock(block)) {
3164
3182
  const decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
3165
3183
  if (orphanedAnnotations.length > 0) {
3166
- debug$e("Removing orphaned annotations from span node"), withNormalizeNode(editor, () => {
3184
+ debug$c("Removing orphaned annotations from span node"), withNormalizeNode(editor, () => {
3167
3185
  Transforms.setNodes(editor, {
3168
3186
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
3169
3187
  }, {
@@ -3179,7 +3197,7 @@ function createWithPortableTextMarkModel(editorActor) {
3179
3197
  for (const markDef of markDefs)
3180
3198
  markDefKeys.has(markDef._key) || (markDefKeys.add(markDef._key), newMarkDefs.push(markDef));
3181
3199
  if (markDefs.length !== newMarkDefs.length) {
3182
- debug$e("Removing duplicate markDefs"), withNormalizeNode(editor, () => {
3200
+ debug$c("Removing duplicate markDefs"), withNormalizeNode(editor, () => {
3183
3201
  Transforms.setNodes(editor, {
3184
3202
  markDefs: newMarkDefs
3185
3203
  }, {
@@ -3192,7 +3210,7 @@ function createWithPortableTextMarkModel(editorActor) {
3192
3210
  if (editor.isTextBlock(node) && !editor.operations.some((op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1)) {
3193
3211
  const newMarkDefs = (node.markDefs || []).filter((def) => node.children.find((child) => Text.isText(child) && Array.isArray(child.marks) && child.marks.includes(def._key)));
3194
3212
  if (node.markDefs && !isEqual(newMarkDefs, node.markDefs)) {
3195
- debug$e("Removing markDef not in use"), withNormalizeNode(editor, () => {
3213
+ debug$c("Removing markDef not in use"), withNormalizeNode(editor, () => {
3196
3214
  Transforms.setNodes(editor, {
3197
3215
  markDefs: newMarkDefs
3198
3216
  }, {
@@ -3284,7 +3302,7 @@ function createWithPortableTextMarkModel(editorActor) {
3284
3302
  const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
3285
3303
  if (editor.isTextBlock(targetBlock)) {
3286
3304
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
3287
- debug$e("Copying markDefs over to merged block", op), Transforms.setNodes(editor, {
3305
+ debug$c("Copying markDefs over to merged block", op), Transforms.setNodes(editor, {
3288
3306
  markDefs: newMarkDefs
3289
3307
  }, {
3290
3308
  at: targetPath,
@@ -3980,7 +3998,7 @@ function parse(textline) {
3980
3998
  function toInt(num) {
3981
3999
  return parseInt(num, 10);
3982
4000
  }
3983
- const debug$d = debugWithName("transformOperation");
4001
+ const debug$b = debugWithName("transformOperation");
3984
4002
  function transformOperation(editor, patch, operation, snapshot, previousSnapshot) {
3985
4003
  const transformedOperation = {
3986
4004
  ...operation
@@ -3989,16 +4007,16 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
3989
4007
  const insertBlockIndex = (snapshot || []).findIndex((blk) => isEqual({
3990
4008
  _key: blk._key
3991
4009
  }, patch.path[0]));
3992
- return debug$d(`Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`), [adjustBlockPath(transformedOperation, patch.items.length, insertBlockIndex)];
4010
+ return debug$b(`Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`), [adjustBlockPath(transformedOperation, patch.items.length, insertBlockIndex)];
3993
4011
  }
3994
4012
  if (patch.type === "unset" && patch.path.length === 1) {
3995
4013
  const unsetBlockIndex = (previousSnapshot || []).findIndex((blk) => isEqual({
3996
4014
  _key: blk._key
3997
4015
  }, patch.path[0]));
3998
- return "path" in transformedOperation && Array.isArray(transformedOperation.path) && transformedOperation.path[0] === unsetBlockIndex ? (debug$d("Skipping transformation that targeted removed block"), []) : [adjustBlockPath(transformedOperation, -1, unsetBlockIndex)];
4016
+ return "path" in transformedOperation && Array.isArray(transformedOperation.path) && transformedOperation.path[0] === unsetBlockIndex ? (debug$b("Skipping transformation that targeted removed block"), []) : [adjustBlockPath(transformedOperation, -1, unsetBlockIndex)];
3999
4017
  }
4000
4018
  if (patch.type === "unset" && patch.path.length === 0)
4001
- return debug$d(`Adjusting selection for unset everything patch and ${operation.type} operation`), [];
4019
+ return debug$b(`Adjusting selection for unset everything patch and ${operation.type} operation`), [];
4002
4020
  if (patch.type === "diffMatchPatch") {
4003
4021
  const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
4004
4022
  return !operationTargetBlock || !isEqual({
@@ -4069,7 +4087,7 @@ function findOperationTargetBlock(editor, operation) {
4069
4087
  let block;
4070
4088
  return operation.type === "set_selection" && editor.selection ? block = editor.children[editor.selection.focus.path[0]] : "path" in operation && (block = editor.children[operation.path[0]]), block;
4071
4089
  }
4072
- const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOperationImplementation = ({
4090
+ const debug$a = debugWithName("behavior.operation.history.redo"), historyRedoOperationImplementation = ({
4073
4091
  operation
4074
4092
  }) => {
4075
4093
  const editor = operation.editor, {
@@ -4077,7 +4095,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4077
4095
  } = editor.history, remotePatches = getRemotePatches(editor);
4078
4096
  if (redos.length > 0) {
4079
4097
  const step = redos[redos.length - 1];
4080
- if (debug$c("Redoing", step), step.operations.length > 0) {
4098
+ if (debug$a("Redoing", step), step.operations.length > 0) {
4081
4099
  const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
4082
4100
  let transformedOperations = step.operations;
4083
4101
  otherPatches.forEach((item) => {
@@ -4094,7 +4112,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4094
4112
  });
4095
4113
  });
4096
4114
  } catch (err) {
4097
- debug$c("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
4115
+ debug$a("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
4098
4116
  undos: [],
4099
4117
  redos: []
4100
4118
  }, setWithHistory(editor, !0), setIsRedoing(editor, !1), editor.onChange();
@@ -4103,7 +4121,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4103
4121
  editor.history.undos.push(step), editor.history.redos.pop();
4104
4122
  }
4105
4123
  }
4106
- }, debug$b = debugWithName("behavior.operation.history.undo"), historyUndoOperationImplementation = ({
4124
+ }, debug$9 = debugWithName("behavior.operation.history.undo"), historyUndoOperationImplementation = ({
4107
4125
  operation
4108
4126
  }) => {
4109
4127
  const editor = operation.editor, {
@@ -4111,7 +4129,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4111
4129
  } = editor.history, remotePatches = getRemotePatches(editor);
4112
4130
  if (undos.length > 0) {
4113
4131
  const step = undos[undos.length - 1];
4114
- if (debug$b("Undoing", step), step.operations.length > 0) {
4132
+ if (debug$9("Undoing", step), step.operations.length > 0) {
4115
4133
  const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
4116
4134
  let transformedOperations = step.operations;
4117
4135
  otherPatches.forEach((item) => {
@@ -4129,7 +4147,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4129
4147
  });
4130
4148
  });
4131
4149
  } catch (err) {
4132
- debug$b("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
4150
+ debug$9("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
4133
4151
  undos: [],
4134
4152
  redos: []
4135
4153
  }, setWithHistory(editor, !0), setIsUndoing(editor, !1), editor.onChange();
@@ -4583,7 +4601,17 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4583
4601
  }
4584
4602
  },
4585
4603
  mode: "highest"
4586
- }), operation.editor.children.length === 0 && Transforms.insertNodes(operation.editor, createPlaceholderBlock(context));
4604
+ });
4605
+ return;
4606
+ }
4607
+ if (operation.unit === "child") {
4608
+ const range = at ?? operation.editor.selection ?? void 0;
4609
+ if (!range)
4610
+ throw new Error("Unable to delete children without a selection");
4611
+ Transforms.removeNodes(operation.editor, {
4612
+ at: range,
4613
+ match: (node) => isSpan(context, node) && node._key !== VOID_CHILD_KEY || "__inline" in node && node.__inline === !0
4614
+ });
4587
4615
  return;
4588
4616
  }
4589
4617
  if (operation.direction === "backward" && operation.unit === "line") {
@@ -4689,7 +4717,11 @@ function insertBlock(options) {
4689
4717
  selection: options.at
4690
4718
  },
4691
4719
  blockIndexMap: editor.blockIndexMap
4692
- }) : editor.selection, start = at ? Range.start(at) : Editor.start(editor, []), end = at ? Range.end(at) : Editor.end(editor, []), [startBlock, startBlockPath] = Array.from(Editor.nodes(editor, {
4720
+ }) : editor.selection;
4721
+ editor.children.length === 0 && Transforms.insertNodes(editor, createPlaceholderBlock(context), {
4722
+ at: [0]
4723
+ });
4724
+ const start = at ? Range.start(at) : Editor.start(editor, []), end = at ? Range.end(at) : Editor.end(editor, []), [startBlock, startBlockPath] = Array.from(Editor.nodes(editor, {
4693
4725
  at: start,
4694
4726
  mode: "lowest",
4695
4727
  match: (node, path) => Element$1.isElement(node) && path.length <= start.path.length
@@ -5565,13 +5597,13 @@ function createWithObjectKeys(editorActor) {
5565
5597
  }, editor;
5566
5598
  };
5567
5599
  }
5568
- function createApplyPatch(schema) {
5600
+ function createApplyPatch(context) {
5569
5601
  return (editor, patch) => {
5570
5602
  let changed = !1;
5571
5603
  try {
5572
5604
  switch (patch.type) {
5573
5605
  case "insert":
5574
- changed = insertPatch(editor, patch, schema);
5606
+ changed = insertPatch(context, editor, patch);
5575
5607
  break;
5576
5608
  case "unset":
5577
5609
  changed = unsetPatch(editor, patch);
@@ -5614,17 +5646,28 @@ function diffMatchPatch(editor, patch) {
5614
5646
  }) : op === DIFF_EQUAL && (offset += text.length);
5615
5647
  return !0;
5616
5648
  }
5617
- function insertPatch(editor, patch, schema) {
5649
+ function insertPatch(context, editor, patch) {
5618
5650
  const block = findBlock(editor.children, patch.path);
5619
- if (!block || patch.path.length > 1 && patch.path[1] !== "children")
5651
+ if (!block) {
5652
+ if (patch.path.length === 1 && patch.path[0] === 0) {
5653
+ const blocksToInsert = patch.items.map((item) => toSlateBlock(item, {
5654
+ schemaTypes: context.schema
5655
+ }, KEY_TO_SLATE_ELEMENT.get(editor)));
5656
+ return Transforms.insertNodes(editor, blocksToInsert, {
5657
+ at: [0]
5658
+ }), !0;
5659
+ }
5660
+ return !1;
5661
+ }
5662
+ if (patch.path.length > 1 && patch.path[1] !== "children")
5620
5663
  return !1;
5621
5664
  if (patch.path.length === 1) {
5622
5665
  const {
5623
5666
  items: items2,
5624
5667
  position: position2
5625
5668
  } = patch, blocksToInsert = items2.map((item) => toSlateBlock(item, {
5626
- schemaTypes: schema
5627
- }, KEY_TO_SLATE_ELEMENT.get(editor))), targetBlockIndex = block.index, normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex, editorWasEmptyBefore = isEqualToEmptyEditor(editor.value, schema);
5669
+ schemaTypes: context.schema
5670
+ }, KEY_TO_SLATE_ELEMENT.get(editor))), targetBlockIndex = block.index, normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex, editorWasEmptyBefore = isEqualToEmptyEditor(context.initialValue, editor.value, context.schema);
5628
5671
  return Transforms.insertNodes(editor, blocksToInsert, {
5629
5672
  at: [normalizedIdx2]
5630
5673
  }), editorWasEmptyBefore && typeof patch.path[0] == "number" && patch.path[0] === 0 && Transforms.removeNodes(editor, {
@@ -5641,7 +5684,7 @@ function insertPatch(editor, patch, schema) {
5641
5684
  ...block.node,
5642
5685
  children: items
5643
5686
  }, {
5644
- schemaTypes: schema
5687
+ schemaTypes: context.schema
5645
5688
  }, KEY_TO_SLATE_ELEMENT.get(editor)), normalizedIdx = position === "after" ? targetChild.index + 1 : targetChild.index, childInsertPath = [block.index, normalizedIdx];
5646
5689
  return childrenToInsert && Element$1.isElement(childrenToInsert) && Transforms.insertNodes(editor, childrenToInsert.children, {
5647
5690
  at: childInsertPath
@@ -5783,7 +5826,6 @@ function setPatch(editor, patch) {
5783
5826
  }
5784
5827
  function unsetPatch(editor, patch) {
5785
5828
  if (patch.path.length === 0) {
5786
- const previousSelection = editor.selection;
5787
5829
  Transforms.deselect(editor);
5788
5830
  const children = Node.children(editor, [], {
5789
5831
  reverse: !0
@@ -5792,44 +5834,15 @@ function unsetPatch(editor, patch) {
5792
5834
  Transforms.removeNodes(editor, {
5793
5835
  at: path
5794
5836
  });
5795
- return Transforms.insertNodes(editor, editor.pteCreateTextBlock({
5796
- decorators: []
5797
- })), previousSelection && Transforms.select(editor, {
5798
- anchor: {
5799
- path: [0, 0],
5800
- offset: 0
5801
- },
5802
- focus: {
5803
- path: [0, 0],
5804
- offset: 0
5805
- }
5806
- }), editor.onChange(), !0;
5837
+ return !0;
5807
5838
  }
5808
5839
  const block = findBlock(editor.children, patch.path);
5809
5840
  if (!block)
5810
5841
  return !1;
5811
- if (patch.path.length === 1) {
5812
- if (editor.children.length === 1) {
5813
- const previousSelection = editor.selection;
5814
- return Transforms.deselect(editor), Transforms.removeNodes(editor, {
5815
- at: [block.index]
5816
- }), Transforms.insertNodes(editor, editor.pteCreateTextBlock({
5817
- decorators: []
5818
- })), previousSelection && Transforms.select(editor, {
5819
- anchor: {
5820
- path: [0, 0],
5821
- offset: 0
5822
- },
5823
- focus: {
5824
- path: [0, 0],
5825
- offset: 0
5826
- }
5827
- }), editor.onChange(), !0;
5828
- }
5842
+ if (patch.path.length === 1)
5829
5843
  return Transforms.removeNodes(editor, {
5830
5844
  at: [block.index]
5831
5845
  }), !0;
5832
- }
5833
5846
  const child = findBlockChild(block, patch.path);
5834
5847
  if (editor.isTextBlock(block.node) && child && patch.path[1] === "children" && patch.path.length === 3)
5835
5848
  return Transforms.removeNodes(editor, {
@@ -6212,22 +6225,14 @@ function moveNodePatch(schema, beforeValue, operation) {
6212
6225
  }
6213
6226
  return patches;
6214
6227
  }
6215
- const PATCHING = /* @__PURE__ */ new WeakMap();
6216
- function withoutPatching(editor, fn) {
6217
- const prev = isPatching(editor);
6218
- PATCHING.set(editor, !1), fn(), PATCHING.set(editor, prev);
6219
- }
6220
- function isPatching(editor) {
6221
- return PATCHING.get(editor);
6222
- }
6223
- const debug$a = debugWithName("plugin:withPatches");
6228
+ const debug$8 = debugWithName("plugin:withPatches");
6224
6229
  function createWithPatches({
6225
6230
  editorActor,
6226
6231
  relayActor,
6227
6232
  subscriptions
6228
6233
  }) {
6229
6234
  let previousValue;
6230
- const applyPatch = createApplyPatch(editorActor.getSnapshot().context.schema);
6235
+ const applyPatch = createApplyPatch(editorActor.getSnapshot().context);
6231
6236
  return function(editor) {
6232
6237
  IS_PROCESSING_REMOTE_CHANGES.set(editor, !1), PATCHING.set(editor, !0), previousValue = [...editor.value];
6233
6238
  const {
@@ -6245,7 +6250,7 @@ function createWithPatches({
6245
6250
  withoutPatching(editor, () => {
6246
6251
  pluginWithoutHistory(editor, () => {
6247
6252
  for (const patch of patches) {
6248
- debug$a.enabled && debug$a(`Handling remote patch ${JSON.stringify(patch)}`);
6253
+ debug$8.enabled && debug$8(`Handling remote patch ${JSON.stringify(patch)}`);
6249
6254
  try {
6250
6255
  changed = applyPatch(editor, patch);
6251
6256
  } catch (error) {
@@ -6263,17 +6268,17 @@ function createWithPatches({
6263
6268
  remotePatches.length !== 0 && (bufferedPatches = bufferedPatches.concat(remotePatches), handleBufferedRemotePatches());
6264
6269
  };
6265
6270
  return subscriptions.push(() => {
6266
- debug$a("Subscribing to remote patches");
6271
+ debug$8("Subscribing to remote patches");
6267
6272
  const sub = editorActor.on("patches", handlePatches);
6268
6273
  return () => {
6269
- debug$a("Unsubscribing to remote patches"), sub.unsubscribe();
6274
+ debug$8("Unsubscribing to remote patches"), sub.unsubscribe();
6270
6275
  };
6271
6276
  }), editor.apply = (operation) => {
6272
6277
  let patches = [];
6273
6278
  previousValue = editor.value;
6274
- const editorWasEmpty = isEqualToEmptyEditor(previousValue, editorActor.getSnapshot().context.schema);
6279
+ const editorWasEmpty = isEqualToEmptyEditor(editorActor.getSnapshot().context.initialValue, previousValue, editorActor.getSnapshot().context.schema);
6275
6280
  apply2(operation);
6276
- const editorIsEmpty = isEqualToEmptyEditor(editor.value, editorActor.getSnapshot().context.schema);
6281
+ const editorIsEmpty = isEqualToEmptyEditor(editorActor.getSnapshot().context.initialValue, editor.value, editorActor.getSnapshot().context.schema);
6277
6282
  if (!isPatching(editor))
6278
6283
  return editor;
6279
6284
  switch (editorWasEmpty && !editorIsEmpty && operation.type !== "set_selection" && patches.push(insert(previousValue, "before", [0])), operation.type) {
@@ -6320,38 +6325,7 @@ function createWithPatches({
6320
6325
  }, editor;
6321
6326
  };
6322
6327
  }
6323
- const debug$9 = debugWithName("plugin:withPlaceholderBlock");
6324
- function createWithPlaceholderBlock(editorActor) {
6325
- return function(editor) {
6326
- const {
6327
- apply: apply2
6328
- } = editor;
6329
- return editor.apply = (op) => {
6330
- if (editorActor.getSnapshot().matches({
6331
- "edit mode": "read only"
6332
- })) {
6333
- apply2(op);
6334
- return;
6335
- }
6336
- if (isChangingRemotely(editor)) {
6337
- apply2(op);
6338
- return;
6339
- }
6340
- if (isUndoing(editor) || isRedoing(editor)) {
6341
- apply2(op);
6342
- return;
6343
- }
6344
- if (op.type === "remove_node") {
6345
- const blockIndex = op.path.at(0), isLonelyBlock = op.path.length === 1 && blockIndex === 0 && editor.children.length === 1, isBlockObject = op.node._type !== editorActor.getSnapshot().context.schema.block.name;
6346
- isLonelyBlock && isBlockObject && (debug$9("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
6347
- decorators: []
6348
- })));
6349
- }
6350
- apply2(op);
6351
- }, editor;
6352
- };
6353
- }
6354
- const debug$8 = debugWithName("plugin:withSchemaTypes");
6328
+ const debug$7 = debugWithName("plugin:withSchemaTypes");
6355
6329
  function createWithSchemaTypes({
6356
6330
  editorActor
6357
6331
  }) {
@@ -6363,7 +6337,7 @@ function createWithSchemaTypes({
6363
6337
  return editor.normalizeNode = (entry) => {
6364
6338
  const [node, path] = entry;
6365
6339
  if (node._type === void 0 && path.length === 2) {
6366
- debug$8("Setting span type on text node without a type");
6340
+ debug$7("Setting span type on text node without a type");
6367
6341
  const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
6368
6342
  withNormalizeNode(editor, () => {
6369
6343
  Transforms.setNodes(editor, {
@@ -6377,7 +6351,7 @@ function createWithSchemaTypes({
6377
6351
  return;
6378
6352
  }
6379
6353
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
6380
- debug$8("Setting missing key on child node without a key");
6354
+ debug$7("Setting missing key on child node without a key");
6381
6355
  const key = editorActor.getSnapshot().context.keyGenerator();
6382
6356
  withNormalizeNode(editor, () => {
6383
6357
  Transforms.setNodes(editor, {
@@ -6394,34 +6368,6 @@ function createWithSchemaTypes({
6394
6368
  }, editor;
6395
6369
  };
6396
6370
  }
6397
- function createWithUtils({
6398
- editorActor
6399
- }) {
6400
- return function(editor) {
6401
- return editor.pteCreateTextBlock = (options) => toSlateBlock({
6402
- _type: editorActor.getSnapshot().context.schema.block.name,
6403
- _key: editorActor.getSnapshot().context.keyGenerator(),
6404
- style: editorActor.getSnapshot().context.schema.styles[0].name || "normal",
6405
- ...options.listItem ? {
6406
- listItem: options.listItem
6407
- } : {},
6408
- ...options.level ? {
6409
- level: options.level
6410
- } : {},
6411
- markDefs: [],
6412
- children: [{
6413
- _type: "span",
6414
- _key: editorActor.getSnapshot().context.keyGenerator(),
6415
- text: "",
6416
- marks: options.decorators.filter((decorator) => editorActor.getSnapshot().context.schema.decorators.find(({
6417
- name
6418
- }) => name === decorator))
6419
- }]
6420
- }, {
6421
- schemaTypes: editorActor.getSnapshot().context.schema
6422
- }), editor;
6423
- };
6424
- }
6425
6371
  function pluginUpdateSelection({
6426
6372
  editor,
6427
6373
  editorActor
@@ -6762,16 +6708,14 @@ const withPlugins = (editor, options) => {
6762
6708
  }), withUndoRedo = pluginHistory({
6763
6709
  editorActor,
6764
6710
  subscriptions: options.subscriptions
6765
- }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
6766
- editorActor
6767
- });
6768
- return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPlaceholderBlock(withUtils(withUndoRedo(withPatches(pluginUpdateValue(editorActor.getSnapshot().context, pluginUpdateSelection({
6711
+ }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor);
6712
+ return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withUndoRedo(withPatches(pluginUpdateValue(editorActor.getSnapshot().context, pluginUpdateSelection({
6769
6713
  editorActor,
6770
6714
  editor: e
6771
- }))))))))));
6772
- }, debug$7 = debugWithName("setup");
6715
+ }))))))));
6716
+ }, debug$6 = debugWithName("setup");
6773
6717
  function createSlateEditor(config) {
6774
- debug$7("Creating new Slate editor instance");
6718
+ debug$6("Creating new Slate editor instance");
6775
6719
  const placeholderBlock = createPlaceholderBlock(config.editorActor.getSnapshot().context), editor = createEditor();
6776
6720
  editor.decoratedRanges = [], editor.decoratorState = {}, editor.value = [placeholderBlock], editor.blockIndexMap = /* @__PURE__ */ new Map(), editor.listIndexMap = /* @__PURE__ */ new Map();
6777
6721
  const instance = withPlugins(withReact(editor), {
@@ -6779,19 +6723,15 @@ function createSlateEditor(config) {
6779
6723
  relayActor: config.relayActor,
6780
6724
  subscriptions: config.subscriptions
6781
6725
  });
6782
- KEY_TO_SLATE_ELEMENT.set(instance, {}), buildIndexMaps({
6726
+ return KEY_TO_SLATE_ELEMENT.set(instance, {}), buildIndexMaps({
6783
6727
  schema: config.editorActor.getSnapshot().context.schema,
6784
6728
  value: instance.value
6785
6729
  }, {
6786
6730
  blockIndexMap: instance.blockIndexMap,
6787
6731
  listIndexMap: instance.listIndexMap
6788
- });
6789
- const initialValue = [toSlateBlock(placeholderBlock, {
6790
- schemaTypes: config.editorActor.getSnapshot().context.schema
6791
- })];
6792
- return {
6732
+ }), {
6793
6733
  instance,
6794
- initialValue
6734
+ initialValue: [placeholderBlock]
6795
6735
  };
6796
6736
  }
6797
6737
  function createEditorDom(sendBack, slateEditor) {
@@ -9847,7 +9787,7 @@ function isNativeBehaviorEvent(event) {
9847
9787
  function isCustomBehaviorEvent(event) {
9848
9788
  return event.type.startsWith("custom.");
9849
9789
  }
9850
- const debug$6 = debugWithName("behaviors:event");
9790
+ const debug$5 = debugWithName("behaviors:event");
9851
9791
  function eventCategory(event) {
9852
9792
  return isNativeBehaviorEvent(event) ? "native" : isAbstractBehaviorEvent(event) ? "synthetic" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
9853
9793
  }
@@ -9863,7 +9803,7 @@ function performEvent({
9863
9803
  nativeEvent,
9864
9804
  sendBack
9865
9805
  }) {
9866
- mode === "send" && !isNativeBehaviorEvent(event) && createUndoStepId(editor), debug$6(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
9806
+ mode === "send" && !isNativeBehaviorEvent(event) && createUndoStepId(editor), debug$5(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2));
9867
9807
  const eventBehaviors = [...remainingEventBehaviors, ...abstractBehaviors].filter((behavior) => {
9868
9808
  if (behavior.on === "*")
9869
9809
  return !0;
@@ -9872,7 +9812,7 @@ function performEvent({
9872
9812
  });
9873
9813
  if (eventBehaviors.length === 0 && isSyntheticBehaviorEvent(event)) {
9874
9814
  nativeEvent?.preventDefault(), mode === "send" && clearUndoStepId(editor), withPerformingBehaviorOperation(editor, () => {
9875
- debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
9815
+ debug$5(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
9876
9816
  context: {
9877
9817
  keyGenerator,
9878
9818
  schema
@@ -9983,7 +9923,7 @@ function performEvent({
9983
9923
  break;
9984
9924
  }
9985
9925
  !defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event) ? (nativeEvent?.preventDefault(), mode === "send" && clearUndoStepId(editor), withPerformingBehaviorOperation(editor, () => {
9986
- debug$6(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
9926
+ debug$5(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2)), performOperation({
9987
9927
  context: {
9988
9928
  keyGenerator,
9989
9929
  schema
@@ -10063,7 +10003,7 @@ function createEditorSnapshot({
10063
10003
  decoratorState: editor.decoratorState
10064
10004
  };
10065
10005
  }
10066
- const debug$5 = debugWithName("editor machine");
10006
+ const debug$4 = debugWithName("editor machine");
10067
10007
  function rerouteExternalBehaviorEvent({
10068
10008
  event,
10069
10009
  slateEditor
@@ -10320,10 +10260,10 @@ const editorMachine = setup({
10320
10260
  states: {
10321
10261
  "determine initial edit mode": {
10322
10262
  entry: [() => {
10323
- debug$5("entry: edit mode->read only->determine initial edit mode");
10263
+ debug$4("entry: edit mode->read only->determine initial edit mode");
10324
10264
  }],
10325
10265
  exit: [() => {
10326
- debug$5("exit: edit mode->read only->determine initial edit mode");
10266
+ debug$4("exit: edit mode->read only->determine initial edit mode");
10327
10267
  }],
10328
10268
  on: {
10329
10269
  "done syncing value": [{
@@ -10338,10 +10278,10 @@ const editorMachine = setup({
10338
10278
  },
10339
10279
  "read only": {
10340
10280
  entry: [() => {
10341
- debug$5("entry: edit mode->read only->read only");
10281
+ debug$4("entry: edit mode->read only->read only");
10342
10282
  }],
10343
10283
  exit: [() => {
10344
- debug$5("exit: edit mode->read only->read only");
10284
+ debug$4("exit: edit mode->read only->read only");
10345
10285
  }],
10346
10286
  on: {
10347
10287
  "update readOnly": {
@@ -10383,10 +10323,10 @@ const editorMachine = setup({
10383
10323
  states: {
10384
10324
  idle: {
10385
10325
  entry: [() => {
10386
- debug$5("entry: edit mode->editable->idle");
10326
+ debug$4("entry: edit mode->editable->idle");
10387
10327
  }],
10388
10328
  exit: [() => {
10389
- debug$5("exit: edit mode->editable-idle");
10329
+ debug$4("exit: edit mode->editable-idle");
10390
10330
  }],
10391
10331
  on: {
10392
10332
  dragstart: {
@@ -10406,10 +10346,10 @@ const editorMachine = setup({
10406
10346
  states: {
10407
10347
  "checking if busy": {
10408
10348
  entry: [() => {
10409
- debug$5("entry: edit mode->editable->focusing->checking if busy");
10349
+ debug$4("entry: edit mode->editable->focusing->checking if busy");
10410
10350
  }],
10411
10351
  exit: [() => {
10412
- debug$5("exit: edit mode->editable->focusing->checking if busy");
10352
+ debug$4("exit: edit mode->editable->focusing->checking if busy");
10413
10353
  }],
10414
10354
  always: [{
10415
10355
  guard: "slate is busy",
@@ -10421,10 +10361,10 @@ const editorMachine = setup({
10421
10361
  },
10422
10362
  busy: {
10423
10363
  entry: [() => {
10424
- debug$5("entry: edit mode->editable->focusing-busy");
10364
+ debug$4("entry: edit mode->editable->focusing-busy");
10425
10365
  }],
10426
10366
  exit: [() => {
10427
- debug$5("exit: edit mode->editable->focusing->busy");
10367
+ debug$4("exit: edit mode->editable->focusing->busy");
10428
10368
  }],
10429
10369
  after: {
10430
10370
  10: {
@@ -10436,10 +10376,10 @@ const editorMachine = setup({
10436
10376
  },
10437
10377
  "dragging internally": {
10438
10378
  entry: [() => {
10439
- debug$5("entry: edit mode->editable->dragging internally");
10379
+ debug$4("entry: edit mode->editable->dragging internally");
10440
10380
  }],
10441
10381
  exit: [() => {
10442
- debug$5("exit: edit mode->editable->dragging internally");
10382
+ debug$4("exit: edit mode->editable->dragging internally");
10443
10383
  }, ({
10444
10384
  context
10445
10385
  }) => {
@@ -10473,10 +10413,10 @@ const editorMachine = setup({
10473
10413
  states: {
10474
10414
  "setting up": {
10475
10415
  entry: [() => {
10476
- debug$5("entry: setup->setting up");
10416
+ debug$4("entry: setup->setting up");
10477
10417
  }],
10478
10418
  exit: [() => {
10479
- debug$5("exit: setup->setting up");
10419
+ debug$4("exit: setup->setting up");
10480
10420
  }, "emit ready", "emit pending incoming patches", "clear pending incoming patches"],
10481
10421
  on: {
10482
10422
  "internal.patch": {
@@ -10501,10 +10441,10 @@ const editorMachine = setup({
10501
10441
  states: {
10502
10442
  idle: {
10503
10443
  entry: [() => {
10504
- debug$5("entry: setup->set up->value sync->idle");
10444
+ debug$4("entry: setup->set up->value sync->idle");
10505
10445
  }],
10506
10446
  exit: [() => {
10507
- debug$5("exit: setup->set up->value sync->idle");
10447
+ debug$4("exit: setup->set up->value sync->idle");
10508
10448
  }],
10509
10449
  on: {
10510
10450
  patches: {
@@ -10519,10 +10459,10 @@ const editorMachine = setup({
10519
10459
  },
10520
10460
  "syncing value": {
10521
10461
  entry: [() => {
10522
- debug$5("entry: setup->set up->value sync->syncing value");
10462
+ debug$4("entry: setup->set up->value sync->syncing value");
10523
10463
  }],
10524
10464
  exit: [() => {
10525
- debug$5("exit: setup->set up->value sync->syncing value");
10465
+ debug$4("exit: setup->set up->value sync->syncing value");
10526
10466
  }, "emit pending incoming patches", "clear pending incoming patches"],
10527
10467
  on: {
10528
10468
  patches: {
@@ -10543,10 +10483,10 @@ const editorMachine = setup({
10543
10483
  states: {
10544
10484
  idle: {
10545
10485
  entry: [() => {
10546
- debug$5("entry: setup->set up->writing->pristine->idle");
10486
+ debug$4("entry: setup->set up->writing->pristine->idle");
10547
10487
  }],
10548
10488
  exit: [() => {
10549
- debug$5("exit: setup->set up->writing->pristine->idle");
10489
+ debug$4("exit: setup->set up->writing->pristine->idle");
10550
10490
  }],
10551
10491
  on: {
10552
10492
  "internal.patch": [{
@@ -10569,10 +10509,10 @@ const editorMachine = setup({
10569
10509
  },
10570
10510
  dirty: {
10571
10511
  entry: [() => {
10572
- debug$5("entry: setup->set up->writing->dirty");
10512
+ debug$4("entry: setup->set up->writing->dirty");
10573
10513
  }, "emit pending events", "clear pending events"],
10574
10514
  exit: [() => {
10575
- debug$5("exit: setup->set up->writing->dirty");
10515
+ debug$4("exit: setup->set up->writing->dirty");
10576
10516
  }],
10577
10517
  on: {
10578
10518
  "internal.patch": {
@@ -10590,7 +10530,7 @@ const editorMachine = setup({
10590
10530
  }
10591
10531
  }
10592
10532
  }
10593
- }), debug$4 = debugWithName("mutation-machine"), mutationMachine = setup({
10533
+ }), debug$3 = debugWithName("mutation-machine"), mutationMachine = setup({
10594
10534
  types: {
10595
10535
  context: {},
10596
10536
  events: {},
@@ -10738,10 +10678,10 @@ const editorMachine = setup({
10738
10678
  states: {
10739
10679
  idle: {
10740
10680
  entry: [() => {
10741
- debug$4("entry: typing->idle");
10681
+ debug$3("entry: typing->idle");
10742
10682
  }],
10743
10683
  exit: [() => {
10744
- debug$4("exit: typing->idle"), debug$4("entry: typing->typing");
10684
+ debug$3("exit: typing->idle"), debug$3("entry: typing->typing");
10745
10685
  }],
10746
10686
  on: {
10747
10687
  typing: {
@@ -10756,7 +10696,7 @@ const editorMachine = setup({
10756
10696
  actions: [raise$1({
10757
10697
  type: "emit changes"
10758
10698
  }), () => {
10759
- debug$4("exit: typing->typing");
10699
+ debug$3("exit: typing->typing");
10760
10700
  }]
10761
10701
  }
10762
10702
  },
@@ -10780,10 +10720,10 @@ const editorMachine = setup({
10780
10720
  states: {
10781
10721
  idle: {
10782
10722
  entry: [() => {
10783
- debug$4("entry: mutations->idle");
10723
+ debug$3("entry: mutations->idle");
10784
10724
  }],
10785
10725
  exit: [() => {
10786
- debug$4("exit: mutations->idle");
10726
+ debug$3("exit: mutations->idle");
10787
10727
  }],
10788
10728
  on: {
10789
10729
  patch: [{
@@ -10798,10 +10738,10 @@ const editorMachine = setup({
10798
10738
  },
10799
10739
  "has pending mutations": {
10800
10740
  entry: [() => {
10801
- debug$4("entry: mutations->has pending mutations");
10741
+ debug$3("entry: mutations->has pending mutations");
10802
10742
  }, "emit has pending mutations"],
10803
10743
  exit: [() => {
10804
- debug$4("exit: mutations->has pending mutations");
10744
+ debug$3("exit: mutations->has pending mutations");
10805
10745
  }],
10806
10746
  invoke: {
10807
10747
  src: "mutation interval"
@@ -10823,7 +10763,7 @@ const editorMachine = setup({
10823
10763
  }
10824
10764
  }
10825
10765
  }
10826
- }), debug$3 = debugWithName("API:editable");
10766
+ });
10827
10767
  function createEditableAPI(editor, editorActor) {
10828
10768
  const types = editorActor.getSnapshot().context.schema;
10829
10769
  return {
@@ -11083,48 +11023,15 @@ function createEditableAPI(editor, editorActor) {
11083
11023
  };
11084
11024
  },
11085
11025
  delete: (selection, options) => {
11086
- if (selection) {
11087
- const range = toSlateRange({
11088
- context: {
11089
- schema: editorActor.getSnapshot().context.schema,
11090
- value: editor.value,
11091
- selection
11092
- },
11093
- blockIndexMap: editor.blockIndexMap
11094
- });
11095
- if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
11096
- throw new Error("Invalid range");
11097
- if (range) {
11098
- if (!options?.mode || options?.mode === "selected") {
11099
- debug$3("Deleting content in selection"), Transforms.delete(editor, {
11100
- at: range,
11101
- hanging: !0,
11102
- voids: !0
11103
- }), editor.onChange();
11104
- return;
11105
- }
11106
- if (options?.mode === "blocks" && (debug$3("Deleting blocks touched by selection"), Transforms.removeNodes(editor, {
11107
- at: range,
11108
- voids: !0,
11109
- match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && Element$1.isElement(node)
11110
- })), options?.mode === "children" && (debug$3("Deleting children touched by selection"), Transforms.removeNodes(editor, {
11111
- at: range,
11112
- voids: !0,
11113
- match: (node) => node._type === types.span.name || // Text children
11114
- !editor.isTextBlock(node) && Element$1.isElement(node)
11115
- })), editor.children.length === 0) {
11116
- const placeholderBlock = createPlaceholderBlock(editorActor.getSnapshot().context);
11117
- editor.children = [placeholderBlock], editor.value = [placeholderBlock], buildIndexMaps({
11118
- schema: editorActor.getSnapshot().context.schema,
11119
- value: editor.value
11120
- }, {
11121
- blockIndexMap: editor.blockIndexMap,
11122
- listIndexMap: editor.listIndexMap
11123
- });
11124
- }
11125
- editor.onChange();
11126
- }
11127
- }
11026
+ selection && editorActor.send({
11027
+ type: "behavior event",
11028
+ behaviorEvent: {
11029
+ type: "delete",
11030
+ at: selection,
11031
+ unit: options?.mode === "blocks" ? "block" : options?.mode === "children" ? "child" : void 0
11032
+ },
11033
+ editor
11034
+ });
11128
11035
  },
11129
11036
  removeAnnotation: (type) => {
11130
11037
  editorActor.send({
@@ -11826,8 +11733,7 @@ async function updateValue({
11826
11733
  const hadSelection = !!slateEditor.selection;
11827
11734
  if ((!value || value.length === 0) && (debug$2("Value is empty"), clearEditor({
11828
11735
  slateEditor,
11829
- doneSyncing,
11830
- hadSelection
11736
+ doneSyncing
11831
11737
  }), isChanged = !0), value && value.length > 0)
11832
11738
  if (streamBlocks)
11833
11739
  await new Promise((resolve) => {
@@ -11935,8 +11841,7 @@ async function* getStreamedBlocks({
11935
11841
  }
11936
11842
  function clearEditor({
11937
11843
  slateEditor,
11938
- doneSyncing,
11939
- hadSelection
11844
+ doneSyncing
11940
11845
  }) {
11941
11846
  Editor.withoutNormalizing(slateEditor, () => {
11942
11847
  pluginWithoutHistory(slateEditor, () => {
@@ -11944,17 +11849,12 @@ function clearEditor({
11944
11849
  withoutPatching(slateEditor, () => {
11945
11850
  if (doneSyncing)
11946
11851
  return;
11947
- hadSelection && Transforms.deselect(slateEditor);
11948
11852
  const childrenLength = slateEditor.children.length;
11949
11853
  slateEditor.children.forEach((_, index) => {
11950
11854
  Transforms.removeNodes(slateEditor, {
11951
11855
  at: [childrenLength - 1 - index]
11952
11856
  });
11953
- }), Transforms.insertNodes(slateEditor, slateEditor.pteCreateTextBlock({
11954
- decorators: []
11955
- }), {
11956
- at: [0]
11957
- }), hadSelection && Transforms.select(slateEditor, [0, 0]);
11857
+ });
11958
11858
  });
11959
11859
  });
11960
11860
  });
@@ -12537,6 +12437,20 @@ class PortableTextEditor extends Component {
12537
12437
  static blur = (editor) => {
12538
12438
  debug("Host blurred"), editor.editable?.blur();
12539
12439
  };
12440
+ /**
12441
+ * @deprecated
12442
+ * Use `editor.send(...)` instead
12443
+ *
12444
+ * ```
12445
+ * const editor = useEditor()
12446
+ * editor.send({
12447
+ * type: 'delete',
12448
+ * at: {...},
12449
+ * direction: '...',
12450
+ * unit: '...',
12451
+ * })
12452
+ * ```
12453
+ */
12540
12454
  static delete = (editor, selection, options) => editor.editable?.delete(selection, options);
12541
12455
  static findDOMNode = (editor, element) => editor.editable?.findDOMNode(element);
12542
12456
  static findByPath = (editor, path) => editor.editable?.findByPath(path) || [];