@portabletext/editor 1.40.4 → 1.41.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
@@ -1,4 +1,4 @@
1
- import { getNodeBlock, toPortableTextRange, fromSlateValue, toSlateRange, debugWithName, EditorActorContext, KEY_TO_VALUE_ELEMENT, usePortableTextEditor, PortableTextEditor, moveRangeByOperation, isEqualToEmptyEditor, getEditorSnapshot, useEditor } from "./_chunks-es/editor-provider.js";
1
+ import { getNodeBlock, getFirstBlock, getLastBlock, toPortableTextRange, fromSlateValue, toSlateRange, debugWithName, EditorActorContext, KEY_TO_VALUE_ELEMENT, usePortableTextEditor, PortableTextEditor, moveRangeByOperation, isEqualToEmptyEditor, getEditorSnapshot, useEditor } from "./_chunks-es/editor-provider.js";
2
2
  import { EditorProvider, defineSchema, defaultKeyGenerator, useEditorSelector, usePortableTextEditorSelection } from "./_chunks-es/editor-provider.js";
3
3
  import { jsxs, jsx, Fragment } from "react/jsx-runtime";
4
4
  import { useSelector } from "@xstate/react";
@@ -11,7 +11,7 @@ import { getBlockEndPoint, getBlockStartPoint, isKeyedSegment } from "./_chunks-
11
11
  import { isSelectionCollapsed, getFocusTextBlock, getFocusSpan, getSelectedBlocks, isSelectionExpanded, getSelectionStartBlock, getSelectionEndBlock, getFocusBlock } from "./_chunks-es/selector.is-at-the-start-of-block.js";
12
12
  import { isOverlappingSelection, isSelectingEntireBlocks } from "./_chunks-es/selector.is-overlapping-selection.js";
13
13
  import { DOMEditor, isDOMNode } from "slate-dom";
14
- import { isSelectionCollapsed as isSelectionCollapsed$1 } from "./_chunks-es/util.is-selection-collapsed.js";
14
+ import { isSelectionCollapsed as isSelectionCollapsed$1, getSelectionEndPoint } from "./_chunks-es/util.is-selection-collapsed.js";
15
15
  import { parseBlocks } from "./_chunks-es/util.selection-point-to-block-offset.js";
16
16
  import { defineBehavior, isHotkey } from "./_chunks-es/behavior.core.js";
17
17
  import { c } from "react-compiler-runtime";
@@ -77,6 +77,19 @@ function getDragSelection({
77
77
  }
78
78
  return dragSelection;
79
79
  }
80
+ function draggingOnDragOrigin({
81
+ snapshot,
82
+ position
83
+ }) {
84
+ const dragOrigin = snapshot.beta.internalDrag?.origin;
85
+ return dragOrigin ? isOverlappingSelection(position.selection)({
86
+ ...snapshot,
87
+ context: {
88
+ ...snapshot.context,
89
+ selection: dragOrigin.selection
90
+ }
91
+ }) : !1;
92
+ }
80
93
  function getEventPosition({
81
94
  schema,
82
95
  slateEditor,
@@ -158,6 +171,22 @@ function getEventPositionBlock({
158
171
  slateEditor,
159
172
  event
160
173
  }) {
174
+ const [firstBlock] = getFirstBlock({
175
+ editor: slateEditor
176
+ });
177
+ if (!firstBlock)
178
+ return;
179
+ const firstBlockRect = DOMEditor.toDOMNode(slateEditor, firstBlock).getBoundingClientRect();
180
+ if (event.pageY < firstBlockRect.top)
181
+ return "start";
182
+ const [lastBlock] = getLastBlock({
183
+ editor: slateEditor
184
+ });
185
+ if (!lastBlock)
186
+ return;
187
+ const lastBlockRef = DOMEditor.toDOMNode(slateEditor, lastBlock).getBoundingClientRect();
188
+ if (event.pageY > lastBlockRef.bottom)
189
+ return "end";
161
190
  const elementRect = DOMEditor.toDOMNode(slateEditor, node).getBoundingClientRect(), top = elementRect.top, height = elementRect.height;
162
191
  return Math.abs(top - event.pageY) < height / 2 ? "start" : "end";
163
192
  }
@@ -176,7 +205,11 @@ function getSlateRangeFromEvent(editor, event) {
176
205
  let domRange;
177
206
  if (window2.document.caretPositionFromPoint !== void 0) {
178
207
  const position = window2.document.caretPositionFromPoint(event.clientX, event.clientY);
179
- position && (domRange = window2.document.createRange(), domRange.setStart(position.offsetNode, position.offset), domRange.setEnd(position.offsetNode, position.offset));
208
+ if (position)
209
+ try {
210
+ domRange = window2.document.createRange(), domRange.setStart(position.offsetNode, position.offset), domRange.setEnd(position.offsetNode, position.offset);
211
+ } catch {
212
+ }
180
213
  } else if (window2.document.caretRangeFromPoint !== void 0)
181
214
  domRange = window2.document.caretRangeFromPoint(event.clientX, event.clientY) ?? void 0;
182
215
  else {
@@ -304,13 +337,13 @@ function DefaultInlineObject(props) {
304
337
  function DropIndicator() {
305
338
  const $ = c(1);
306
339
  let t0;
307
- return $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = /* @__PURE__ */ jsx("div", { className: "pt-drop-indicator", style: {
340
+ return $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = /* @__PURE__ */ jsx("div", { contentEditable: !1, className: "pt-drop-indicator", style: {
308
341
  position: "absolute",
309
342
  width: "100%",
310
343
  height: 1,
311
344
  borderBottom: "1px solid currentColor",
312
345
  zIndex: 5
313
- } }), $[0] = t0) : t0 = $[0], t0;
346
+ }, children: /* @__PURE__ */ jsx("span", {}) }), $[0] = t0) : t0 = $[0], t0;
314
347
  }
315
348
  debugWithName("components:Element");
316
349
  const EMPTY_ANNOTATIONS = [], inlineBlockStyle = {
@@ -1123,78 +1156,94 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
1123
1156
  };
1124
1157
  }, [slateEditor, editorActor]);
1125
1158
  const handleDragStart = useCallback((event_8) => {
1126
- if (onDragStart?.(event_8), !event_8.isDefaultPrevented() && !event_8.isPropagationStopped()) {
1127
- const position_4 = getEventPosition({
1128
- schema: editorActor.getSnapshot().context.schema,
1129
- slateEditor,
1130
- event: event_8.nativeEvent
1131
- });
1132
- if (!position_4) {
1133
- console.warn("Could not find position for dragstart event");
1134
- return;
1159
+ if (onDragStart?.(event_8), event_8.isDefaultPrevented() || event_8.isPropagationStopped())
1160
+ return;
1161
+ event_8.stopPropagation();
1162
+ const position_4 = getEventPosition({
1163
+ schema: editorActor.getSnapshot().context.schema,
1164
+ slateEditor,
1165
+ event: event_8.nativeEvent
1166
+ });
1167
+ if (!position_4) {
1168
+ console.warn("Could not find position for dragstart event");
1169
+ return;
1170
+ }
1171
+ const snapshot = getEditorSnapshot({
1172
+ editorActorSnapshot: editorActor.getSnapshot(),
1173
+ slateEditorInstance: slateEditor
1174
+ }), dragSelection = getDragSelection({
1175
+ eventSelection: position_4.selection,
1176
+ snapshot
1177
+ }), selectingEntireBlocks = isSelectingEntireBlocks({
1178
+ context: {
1179
+ ...snapshot.context,
1180
+ selection: dragSelection
1135
1181
  }
1136
- const snapshot = getEditorSnapshot({
1137
- editorActorSnapshot: editorActor.getSnapshot(),
1138
- slateEditorInstance: slateEditor
1139
- }), dragSelection = getDragSelection({
1140
- eventSelection: position_4.selection,
1141
- snapshot
1142
- }), selectingEntireBlocks = isSelectingEntireBlocks({
1182
+ }), dragGhost = document.createElement("div"), draggedDomNodes = getSelectionDomNodes({
1183
+ snapshot: {
1143
1184
  context: {
1144
1185
  ...snapshot.context,
1145
1186
  selection: dragSelection
1146
1187
  }
1147
- }), dragGhost = document.createElement("div"), draggedDomNodes = getSelectionDomNodes({
1148
- snapshot: {
1149
- context: {
1150
- ...snapshot.context,
1151
- selection: dragSelection
1152
- }
1153
- },
1154
- slateEditor
1155
- });
1156
- if (selectingEntireBlocks) {
1157
- const clonedBlockNodes = draggedDomNodes.blockNodes.map((node) => node.cloneNode(!0));
1158
- for (const block of clonedBlockNodes)
1159
- block instanceof HTMLElement && (block.style.position = "relative"), dragGhost.appendChild(block);
1160
- const customGhost = dragGhost.querySelector("[data-pt-drag-ghost-element]");
1161
- if (customGhost && dragGhost.replaceChildren(customGhost), dragGhost.setAttribute("data-dragged", ""), dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost), customGhost) {
1162
- const customGhostRect = customGhost.getBoundingClientRect(), x = event_8.clientX - customGhostRect.left, y = event_8.clientY - customGhostRect.top;
1163
- dragGhost.style.width = `${customGhostRect.width}px`, dragGhost.style.height = `${customGhostRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x, y);
1164
- } else {
1165
- const blocksDomRect = getCompoundClientRect(draggedDomNodes.blockNodes), x_0 = event_8.clientX - blocksDomRect.left, y_0 = event_8.clientY - blocksDomRect.top;
1166
- dragGhost.style.width = `${blocksDomRect.width}px`, dragGhost.style.height = `${blocksDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_0, y_0);
1167
- }
1188
+ },
1189
+ slateEditor
1190
+ });
1191
+ if (selectingEntireBlocks) {
1192
+ const clonedBlockNodes = draggedDomNodes.blockNodes.map((node) => node.cloneNode(!0));
1193
+ for (const block of clonedBlockNodes)
1194
+ block instanceof HTMLElement && (block.style.position = "relative"), dragGhost.appendChild(block);
1195
+ const customGhost = dragGhost.querySelector("[data-pt-drag-ghost-element]");
1196
+ if (customGhost && dragGhost.replaceChildren(customGhost), dragGhost.setAttribute("data-dragged", ""), dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost), customGhost) {
1197
+ const customGhostRect = customGhost.getBoundingClientRect(), x = event_8.clientX - customGhostRect.left, y = event_8.clientY - customGhostRect.top;
1198
+ dragGhost.style.width = `${customGhostRect.width}px`, dragGhost.style.height = `${customGhostRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x, y);
1168
1199
  } else {
1169
- const clonedChildNodes = draggedDomNodes.childNodes.map((node_0) => node_0.cloneNode(!0));
1170
- for (const child of clonedChildNodes)
1171
- dragGhost.appendChild(child);
1172
- dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost);
1173
- const childrenDomRect = getCompoundClientRect(draggedDomNodes.childNodes), x_1 = event_8.clientX - childrenDomRect.left, y_1 = event_8.clientY - childrenDomRect.top;
1174
- dragGhost.style.width = `${childrenDomRect.width}px`, dragGhost.style.height = `${childrenDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_1, y_1);
1200
+ const blocksDomRect = getCompoundClientRect(draggedDomNodes.blockNodes), x_0 = event_8.clientX - blocksDomRect.left, y_0 = event_8.clientY - blocksDomRect.top;
1201
+ dragGhost.style.width = `${blocksDomRect.width}px`, dragGhost.style.height = `${blocksDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_0, y_0);
1175
1202
  }
1176
- editorActor.send({
1177
- type: "dragstart",
1178
- origin: {
1179
- selection: dragSelection
1180
- },
1181
- ghost: dragGhost
1182
- }), editorActor.send({
1183
- type: "behavior event",
1184
- behaviorEvent: {
1185
- type: "drag.dragstart",
1186
- originEvent: {
1187
- dataTransfer: event_8.dataTransfer
1188
- },
1189
- position: {
1190
- selection: dragSelection
1191
- }
1192
- },
1193
- editor: slateEditor
1194
- }), event_8.stopPropagation();
1203
+ } else {
1204
+ const clonedChildNodes = draggedDomNodes.childNodes.map((node_0) => node_0.cloneNode(!0));
1205
+ for (const child of clonedChildNodes)
1206
+ dragGhost.appendChild(child);
1207
+ dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost);
1208
+ const childrenDomRect = getCompoundClientRect(draggedDomNodes.childNodes), x_1 = event_8.clientX - childrenDomRect.left, y_1 = event_8.clientY - childrenDomRect.top;
1209
+ dragGhost.style.width = `${childrenDomRect.width}px`, dragGhost.style.height = `${childrenDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_1, y_1);
1195
1210
  }
1211
+ editorActor.send({
1212
+ type: "behavior event",
1213
+ behaviorEvent: {
1214
+ type: "select",
1215
+ selection: isSelectionCollapsed$1(dragSelection) ? dragSelection : {
1216
+ anchor: getSelectionEndPoint(dragSelection),
1217
+ focus: getSelectionEndPoint(dragSelection),
1218
+ backward: !1
1219
+ }
1220
+ },
1221
+ editor: slateEditor
1222
+ }), editorActor.send({
1223
+ type: "dragstart",
1224
+ origin: {
1225
+ selection: dragSelection
1226
+ },
1227
+ ghost: dragGhost
1228
+ }), editorActor.send({
1229
+ type: "behavior event",
1230
+ behaviorEvent: {
1231
+ type: "drag.dragstart",
1232
+ originEvent: {
1233
+ dataTransfer: event_8.dataTransfer
1234
+ },
1235
+ position: {
1236
+ selection: dragSelection
1237
+ }
1238
+ },
1239
+ editor: slateEditor
1240
+ });
1196
1241
  }, [onDragStart, editorActor, slateEditor]), handleDrag = useCallback((event_9) => {
1197
- onDrag?.(event_9), !event_9.isDefaultPrevented() && !event_9.isPropagationStopped() && (editorActor.send({
1242
+ onDrag?.(event_9), !(event_9.isDefaultPrevented() || event_9.isPropagationStopped() || (event_9.stopPropagation(), !getEventPosition({
1243
+ schema: editorActor.getSnapshot().context.schema,
1244
+ slateEditor,
1245
+ event: event_9.nativeEvent
1246
+ }))) && editorActor.send({
1198
1247
  type: "behavior event",
1199
1248
  behaviorEvent: {
1200
1249
  type: "drag.drag",
@@ -1203,9 +1252,9 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
1203
1252
  }
1204
1253
  },
1205
1254
  editor: slateEditor
1206
- }), event_9.stopPropagation());
1255
+ });
1207
1256
  }, [onDrag, editorActor, slateEditor]), handleDragEnd = useCallback((event_10) => {
1208
- onDragEnd?.(event_10), !event_10.isDefaultPrevented() && !event_10.isPropagationStopped() && (editorActor.send({
1257
+ onDragEnd?.(event_10), !(event_10.isDefaultPrevented() || event_10.isPropagationStopped()) && (event_10.stopPropagation(), editorActor.send({
1209
1258
  type: "behavior event",
1210
1259
  behaviorEvent: {
1211
1260
  type: "drag.dragend",
@@ -1214,76 +1263,94 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
1214
1263
  }
1215
1264
  },
1216
1265
  editor: slateEditor
1217
- }), event_10.stopPropagation());
1266
+ }));
1218
1267
  }, [onDragEnd, editorActor, slateEditor]), handleDragEnter = useCallback((event_11) => {
1219
- if (onDragEnter?.(event_11), !event_11.isDefaultPrevented() && !event_11.isPropagationStopped()) {
1220
- const position_5 = getEventPosition({
1221
- schema: editorActor.getSnapshot().context.schema,
1222
- slateEditor,
1223
- event: event_11.nativeEvent
1224
- });
1225
- if (!position_5)
1226
- return;
1227
- editorActor.send({
1228
- type: "behavior event",
1229
- behaviorEvent: {
1230
- type: "drag.dragenter",
1231
- originEvent: {
1232
- dataTransfer: event_11.dataTransfer
1233
- },
1234
- position: position_5
1268
+ if (onDragEnter?.(event_11), event_11.isDefaultPrevented() || event_11.isPropagationStopped())
1269
+ return;
1270
+ event_11.stopPropagation();
1271
+ const position_6 = getEventPosition({
1272
+ schema: editorActor.getSnapshot().context.schema,
1273
+ slateEditor,
1274
+ event: event_11.nativeEvent
1275
+ });
1276
+ position_6 && editorActor.send({
1277
+ type: "behavior event",
1278
+ behaviorEvent: {
1279
+ type: "drag.dragenter",
1280
+ originEvent: {
1281
+ dataTransfer: event_11.dataTransfer
1235
1282
  },
1236
- editor: slateEditor
1237
- }), event_11.stopPropagation();
1238
- }
1283
+ position: position_6
1284
+ },
1285
+ editor: slateEditor
1286
+ });
1239
1287
  }, [onDragEnter, editorActor, slateEditor]), handleDragOver = useCallback((event_12) => {
1240
- if (onDragOver?.(event_12), !event_12.isDefaultPrevented() && !event_12.isPropagationStopped()) {
1241
- const position_6 = getEventPosition({
1242
- schema: editorActor.getSnapshot().context.schema,
1243
- slateEditor,
1244
- event: event_12.nativeEvent
1245
- });
1246
- if (!position_6)
1247
- return;
1248
- editorActor.send({
1249
- type: "behavior event",
1250
- behaviorEvent: {
1251
- type: "drag.dragover",
1252
- originEvent: {
1253
- dataTransfer: event_12.dataTransfer
1254
- },
1255
- position: position_6
1288
+ if (onDragOver?.(event_12), event_12.isDefaultPrevented() || event_12.isPropagationStopped())
1289
+ return;
1290
+ event_12.stopPropagation();
1291
+ const position_7 = getEventPosition({
1292
+ schema: editorActor.getSnapshot().context.schema,
1293
+ slateEditor,
1294
+ event: event_12.nativeEvent
1295
+ });
1296
+ if (!position_7)
1297
+ return;
1298
+ const snapshot_0 = getEditorSnapshot({
1299
+ editorActorSnapshot: editorActor.getSnapshot(),
1300
+ slateEditorInstance: slateEditor
1301
+ });
1302
+ draggingOnDragOrigin({
1303
+ snapshot: snapshot_0,
1304
+ position: position_7
1305
+ }) && event_12.preventDefault(), editorActor.send({
1306
+ type: "behavior event",
1307
+ behaviorEvent: {
1308
+ type: "drag.dragover",
1309
+ originEvent: {
1310
+ dataTransfer: event_12.dataTransfer
1256
1311
  },
1257
- editor: slateEditor,
1258
- nativeEvent: event_12
1259
- }), event_12.stopPropagation();
1260
- }
1312
+ position: position_7
1313
+ },
1314
+ editor: slateEditor,
1315
+ nativeEvent: event_12
1316
+ });
1261
1317
  }, [onDragOver, editorActor, slateEditor]), handleDrop = useCallback((event_13) => {
1262
- if (onDrop?.(event_13), !event_13.isDefaultPrevented() && !event_13.isPropagationStopped()) {
1263
- const position_7 = getEventPosition({
1264
- schema: editorActor.getSnapshot().context.schema,
1265
- slateEditor,
1266
- event: event_13.nativeEvent
1267
- });
1268
- if (!position_7) {
1269
- console.warn("Could not find position for drop event");
1270
- return;
1271
- }
1272
- const range = ReactEditor.findEventRange(slateEditor, event_13);
1273
- slateEditor.select(range), editorActor.send({
1274
- type: "behavior event",
1275
- behaviorEvent: {
1276
- type: "drag.drop",
1277
- originEvent: {
1278
- dataTransfer: event_13.dataTransfer
1279
- },
1280
- position: position_7
1281
- },
1282
- editor: slateEditor
1283
- }), event_13.preventDefault();
1318
+ if (onDrop?.(event_13), event_13.isDefaultPrevented() || event_13.isPropagationStopped())
1319
+ return;
1320
+ event_13.preventDefault();
1321
+ const position_8 = getEventPosition({
1322
+ schema: editorActor.getSnapshot().context.schema,
1323
+ slateEditor,
1324
+ event: event_13.nativeEvent
1325
+ });
1326
+ if (!position_8) {
1327
+ console.warn("Could not find position for drop event");
1328
+ return;
1284
1329
  }
1330
+ editorActor.send({
1331
+ type: "behavior event",
1332
+ behaviorEvent: {
1333
+ type: "select",
1334
+ selection: position_8.selection
1335
+ },
1336
+ editor: slateEditor
1337
+ }), editorActor.send({
1338
+ type: "behavior event",
1339
+ behaviorEvent: {
1340
+ type: "drag.drop",
1341
+ originEvent: {
1342
+ dataTransfer: event_13.dataTransfer
1343
+ },
1344
+ position: position_8
1345
+ },
1346
+ editor: slateEditor
1347
+ });
1285
1348
  }, [onDrop, editorActor, slateEditor]), handleDragLeave = useCallback((event_14) => {
1286
- onDragLeave?.(event_14), !event_14.isDefaultPrevented() && !event_14.isPropagationStopped() && editorActor.send({
1349
+ onDragLeave?.(event_14), !(event_14.isDefaultPrevented() || event_14.isPropagationStopped() || (event_14.stopPropagation(), !getEventPosition({
1350
+ schema: editorActor.getSnapshot().context.schema,
1351
+ slateEditor,
1352
+ event: event_14.nativeEvent
1353
+ }))) && editorActor.send({
1287
1354
  type: "behavior event",
1288
1355
  behaviorEvent: {
1289
1356
  type: "drag.dragleave",