@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.
@@ -1,7 +1,11 @@
1
+ function getSelectionEndPoint(selection) {
2
+ return selection ? selection.backward ? selection.anchor : selection.focus : null;
3
+ }
1
4
  function isSelectionCollapsed(selection) {
2
5
  return selection ? selection.anchor.path.join() === selection.focus.path.join() && selection.anchor.offset === selection.focus.offset : !1;
3
6
  }
4
7
  export {
8
+ getSelectionEndPoint,
5
9
  isSelectionCollapsed
6
10
  };
7
11
  //# sourceMappingURL=util.is-selection-collapsed.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.is-selection-collapsed.js","sources":["../../src/utils/util.is-selection-collapsed.ts"],"sourcesContent":["import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n selection.anchor.path.join() === selection.focus.path.join() &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n"],"names":["isSelectionCollapsed","selection","anchor","path","join","focus","offset"],"mappings":"AAKO,SAASA,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHA,UAAUC,OAAOC,KAAKC,KAAAA,MAAWH,UAAUI,MAAMF,KAAKC,KAAAA,KACtDH,UAAUC,OAAOI,WAAWL,UAAUI,MAAMC,SALrC;AAOX;"}
1
+ {"version":3,"file":"util.is-selection-collapsed.js","sources":["../../src/utils/util.get-selection-end-point.ts","../../src/utils/util.is-selection-collapsed.ts"],"sourcesContent":["import type {EditorSelection, EditorSelectionPoint} from '..'\n\n/**\n * @public\n */\nexport function getSelectionEndPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.anchor : selection.focus\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n selection.anchor.path.join() === selection.focus.path.join() &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n"],"names":["getSelectionEndPoint","selection","backward","anchor","focus","isSelectionCollapsed","path","join","offset"],"mappings":"AAKO,SAASA,qBAMdC,WAAoD;AACpD,SAAKA,YAKHA,UAAUC,WAAWD,UAAUE,SAASF,UAAUG,QAJ3C;AAMX;ACdO,SAASC,qBAAqBJ,WAA4B;AAC/D,SAAKA,YAKHA,UAAUE,OAAOG,KAAKC,KAAAA,MAAWN,UAAUG,MAAME,KAAKC,KAAAA,KACtDN,UAAUE,OAAOK,WAAWP,UAAUG,MAAMI,SALrC;AAOX;"}
package/lib/index.cjs CHANGED
@@ -65,6 +65,19 @@ function getDragSelection({
65
65
  }
66
66
  return dragSelection;
67
67
  }
68
+ function draggingOnDragOrigin({
69
+ snapshot,
70
+ position
71
+ }) {
72
+ const dragOrigin = snapshot.beta.internalDrag?.origin;
73
+ return dragOrigin ? selector_isOverlappingSelection.isOverlappingSelection(position.selection)({
74
+ ...snapshot,
75
+ context: {
76
+ ...snapshot.context,
77
+ selection: dragOrigin.selection
78
+ }
79
+ }) : !1;
80
+ }
68
81
  function getEventPosition({
69
82
  schema,
70
83
  slateEditor,
@@ -146,6 +159,22 @@ function getEventPositionBlock({
146
159
  slateEditor,
147
160
  event
148
161
  }) {
162
+ const [firstBlock] = editorProvider.getFirstBlock({
163
+ editor: slateEditor
164
+ });
165
+ if (!firstBlock)
166
+ return;
167
+ const firstBlockRect = slateDom.DOMEditor.toDOMNode(slateEditor, firstBlock).getBoundingClientRect();
168
+ if (event.pageY < firstBlockRect.top)
169
+ return "start";
170
+ const [lastBlock] = editorProvider.getLastBlock({
171
+ editor: slateEditor
172
+ });
173
+ if (!lastBlock)
174
+ return;
175
+ const lastBlockRef = slateDom.DOMEditor.toDOMNode(slateEditor, lastBlock).getBoundingClientRect();
176
+ if (event.pageY > lastBlockRef.bottom)
177
+ return "end";
149
178
  const elementRect = slateDom.DOMEditor.toDOMNode(slateEditor, node).getBoundingClientRect(), top = elementRect.top, height = elementRect.height;
150
179
  return Math.abs(top - event.pageY) < height / 2 ? "start" : "end";
151
180
  }
@@ -164,7 +193,11 @@ function getSlateRangeFromEvent(editor, event) {
164
193
  let domRange;
165
194
  if (window2.document.caretPositionFromPoint !== void 0) {
166
195
  const position = window2.document.caretPositionFromPoint(event.clientX, event.clientY);
167
- position && (domRange = window2.document.createRange(), domRange.setStart(position.offsetNode, position.offset), domRange.setEnd(position.offsetNode, position.offset));
196
+ if (position)
197
+ try {
198
+ domRange = window2.document.createRange(), domRange.setStart(position.offsetNode, position.offset), domRange.setEnd(position.offsetNode, position.offset);
199
+ } catch {
200
+ }
168
201
  } else if (window2.document.caretRangeFromPoint !== void 0)
169
202
  domRange = window2.document.caretRangeFromPoint(event.clientX, event.clientY) ?? void 0;
170
203
  else {
@@ -292,13 +325,13 @@ function DefaultInlineObject(props) {
292
325
  function DropIndicator() {
293
326
  const $ = reactCompilerRuntime.c(1);
294
327
  let t0;
295
- return $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = /* @__PURE__ */ jsxRuntime.jsx("div", { className: "pt-drop-indicator", style: {
328
+ return $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = /* @__PURE__ */ jsxRuntime.jsx("div", { contentEditable: !1, className: "pt-drop-indicator", style: {
296
329
  position: "absolute",
297
330
  width: "100%",
298
331
  height: 1,
299
332
  borderBottom: "1px solid currentColor",
300
333
  zIndex: 5
301
- } }), $[0] = t0) : t0 = $[0], t0;
334
+ }, children: /* @__PURE__ */ jsxRuntime.jsx("span", {}) }), $[0] = t0) : t0 = $[0], t0;
302
335
  }
303
336
  editorProvider.debugWithName("components:Element");
304
337
  const EMPTY_ANNOTATIONS = [], inlineBlockStyle = {
@@ -1111,78 +1144,94 @@ const debug = editorProvider.debugWithName("component:Editable"), PLACEHOLDER_ST
1111
1144
  };
1112
1145
  }, [slateEditor, editorActor]);
1113
1146
  const handleDragStart = React.useCallback((event_8) => {
1114
- if (onDragStart?.(event_8), !event_8.isDefaultPrevented() && !event_8.isPropagationStopped()) {
1115
- const position_4 = getEventPosition({
1116
- schema: editorActor.getSnapshot().context.schema,
1117
- slateEditor,
1118
- event: event_8.nativeEvent
1119
- });
1120
- if (!position_4) {
1121
- console.warn("Could not find position for dragstart event");
1122
- return;
1147
+ if (onDragStart?.(event_8), event_8.isDefaultPrevented() || event_8.isPropagationStopped())
1148
+ return;
1149
+ event_8.stopPropagation();
1150
+ const position_4 = getEventPosition({
1151
+ schema: editorActor.getSnapshot().context.schema,
1152
+ slateEditor,
1153
+ event: event_8.nativeEvent
1154
+ });
1155
+ if (!position_4) {
1156
+ console.warn("Could not find position for dragstart event");
1157
+ return;
1158
+ }
1159
+ const snapshot = editorProvider.getEditorSnapshot({
1160
+ editorActorSnapshot: editorActor.getSnapshot(),
1161
+ slateEditorInstance: slateEditor
1162
+ }), dragSelection = getDragSelection({
1163
+ eventSelection: position_4.selection,
1164
+ snapshot
1165
+ }), selectingEntireBlocks = selector_isOverlappingSelection.isSelectingEntireBlocks({
1166
+ context: {
1167
+ ...snapshot.context,
1168
+ selection: dragSelection
1123
1169
  }
1124
- const snapshot = editorProvider.getEditorSnapshot({
1125
- editorActorSnapshot: editorActor.getSnapshot(),
1126
- slateEditorInstance: slateEditor
1127
- }), dragSelection = getDragSelection({
1128
- eventSelection: position_4.selection,
1129
- snapshot
1130
- }), selectingEntireBlocks = selector_isOverlappingSelection.isSelectingEntireBlocks({
1170
+ }), dragGhost = document.createElement("div"), draggedDomNodes = getSelectionDomNodes({
1171
+ snapshot: {
1131
1172
  context: {
1132
1173
  ...snapshot.context,
1133
1174
  selection: dragSelection
1134
1175
  }
1135
- }), dragGhost = document.createElement("div"), draggedDomNodes = getSelectionDomNodes({
1136
- snapshot: {
1137
- context: {
1138
- ...snapshot.context,
1139
- selection: dragSelection
1140
- }
1141
- },
1142
- slateEditor
1143
- });
1144
- if (selectingEntireBlocks) {
1145
- const clonedBlockNodes = draggedDomNodes.blockNodes.map((node) => node.cloneNode(!0));
1146
- for (const block of clonedBlockNodes)
1147
- block instanceof HTMLElement && (block.style.position = "relative"), dragGhost.appendChild(block);
1148
- const customGhost = dragGhost.querySelector("[data-pt-drag-ghost-element]");
1149
- 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) {
1150
- const customGhostRect = customGhost.getBoundingClientRect(), x = event_8.clientX - customGhostRect.left, y = event_8.clientY - customGhostRect.top;
1151
- dragGhost.style.width = `${customGhostRect.width}px`, dragGhost.style.height = `${customGhostRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x, y);
1152
- } else {
1153
- const blocksDomRect = getCompoundClientRect(draggedDomNodes.blockNodes), x_0 = event_8.clientX - blocksDomRect.left, y_0 = event_8.clientY - blocksDomRect.top;
1154
- dragGhost.style.width = `${blocksDomRect.width}px`, dragGhost.style.height = `${blocksDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_0, y_0);
1155
- }
1176
+ },
1177
+ slateEditor
1178
+ });
1179
+ if (selectingEntireBlocks) {
1180
+ const clonedBlockNodes = draggedDomNodes.blockNodes.map((node) => node.cloneNode(!0));
1181
+ for (const block of clonedBlockNodes)
1182
+ block instanceof HTMLElement && (block.style.position = "relative"), dragGhost.appendChild(block);
1183
+ const customGhost = dragGhost.querySelector("[data-pt-drag-ghost-element]");
1184
+ 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) {
1185
+ const customGhostRect = customGhost.getBoundingClientRect(), x = event_8.clientX - customGhostRect.left, y = event_8.clientY - customGhostRect.top;
1186
+ dragGhost.style.width = `${customGhostRect.width}px`, dragGhost.style.height = `${customGhostRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x, y);
1156
1187
  } else {
1157
- const clonedChildNodes = draggedDomNodes.childNodes.map((node_0) => node_0.cloneNode(!0));
1158
- for (const child of clonedChildNodes)
1159
- dragGhost.appendChild(child);
1160
- dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost);
1161
- const childrenDomRect = getCompoundClientRect(draggedDomNodes.childNodes), x_1 = event_8.clientX - childrenDomRect.left, y_1 = event_8.clientY - childrenDomRect.top;
1162
- dragGhost.style.width = `${childrenDomRect.width}px`, dragGhost.style.height = `${childrenDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_1, y_1);
1188
+ const blocksDomRect = getCompoundClientRect(draggedDomNodes.blockNodes), x_0 = event_8.clientX - blocksDomRect.left, y_0 = event_8.clientY - blocksDomRect.top;
1189
+ dragGhost.style.width = `${blocksDomRect.width}px`, dragGhost.style.height = `${blocksDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_0, y_0);
1163
1190
  }
1164
- editorActor.send({
1165
- type: "dragstart",
1166
- origin: {
1167
- selection: dragSelection
1168
- },
1169
- ghost: dragGhost
1170
- }), editorActor.send({
1171
- type: "behavior event",
1172
- behaviorEvent: {
1173
- type: "drag.dragstart",
1174
- originEvent: {
1175
- dataTransfer: event_8.dataTransfer
1176
- },
1177
- position: {
1178
- selection: dragSelection
1179
- }
1180
- },
1181
- editor: slateEditor
1182
- }), event_8.stopPropagation();
1191
+ } else {
1192
+ const clonedChildNodes = draggedDomNodes.childNodes.map((node_0) => node_0.cloneNode(!0));
1193
+ for (const child of clonedChildNodes)
1194
+ dragGhost.appendChild(child);
1195
+ dragGhost.style.position = "absolute", dragGhost.style.left = "-99999px", dragGhost.style.boxSizing = "border-box", document.body.appendChild(dragGhost);
1196
+ const childrenDomRect = getCompoundClientRect(draggedDomNodes.childNodes), x_1 = event_8.clientX - childrenDomRect.left, y_1 = event_8.clientY - childrenDomRect.top;
1197
+ dragGhost.style.width = `${childrenDomRect.width}px`, dragGhost.style.height = `${childrenDomRect.height}px`, event_8.dataTransfer.setDragImage(dragGhost, x_1, y_1);
1183
1198
  }
1199
+ editorActor.send({
1200
+ type: "behavior event",
1201
+ behaviorEvent: {
1202
+ type: "select",
1203
+ selection: util_isSelectionCollapsed.isSelectionCollapsed(dragSelection) ? dragSelection : {
1204
+ anchor: util_isSelectionCollapsed.getSelectionEndPoint(dragSelection),
1205
+ focus: util_isSelectionCollapsed.getSelectionEndPoint(dragSelection),
1206
+ backward: !1
1207
+ }
1208
+ },
1209
+ editor: slateEditor
1210
+ }), editorActor.send({
1211
+ type: "dragstart",
1212
+ origin: {
1213
+ selection: dragSelection
1214
+ },
1215
+ ghost: dragGhost
1216
+ }), editorActor.send({
1217
+ type: "behavior event",
1218
+ behaviorEvent: {
1219
+ type: "drag.dragstart",
1220
+ originEvent: {
1221
+ dataTransfer: event_8.dataTransfer
1222
+ },
1223
+ position: {
1224
+ selection: dragSelection
1225
+ }
1226
+ },
1227
+ editor: slateEditor
1228
+ });
1184
1229
  }, [onDragStart, editorActor, slateEditor]), handleDrag = React.useCallback((event_9) => {
1185
- onDrag?.(event_9), !event_9.isDefaultPrevented() && !event_9.isPropagationStopped() && (editorActor.send({
1230
+ onDrag?.(event_9), !(event_9.isDefaultPrevented() || event_9.isPropagationStopped() || (event_9.stopPropagation(), !getEventPosition({
1231
+ schema: editorActor.getSnapshot().context.schema,
1232
+ slateEditor,
1233
+ event: event_9.nativeEvent
1234
+ }))) && editorActor.send({
1186
1235
  type: "behavior event",
1187
1236
  behaviorEvent: {
1188
1237
  type: "drag.drag",
@@ -1191,9 +1240,9 @@ const debug = editorProvider.debugWithName("component:Editable"), PLACEHOLDER_ST
1191
1240
  }
1192
1241
  },
1193
1242
  editor: slateEditor
1194
- }), event_9.stopPropagation());
1243
+ });
1195
1244
  }, [onDrag, editorActor, slateEditor]), handleDragEnd = React.useCallback((event_10) => {
1196
- onDragEnd?.(event_10), !event_10.isDefaultPrevented() && !event_10.isPropagationStopped() && (editorActor.send({
1245
+ onDragEnd?.(event_10), !(event_10.isDefaultPrevented() || event_10.isPropagationStopped()) && (event_10.stopPropagation(), editorActor.send({
1197
1246
  type: "behavior event",
1198
1247
  behaviorEvent: {
1199
1248
  type: "drag.dragend",
@@ -1202,76 +1251,94 @@ const debug = editorProvider.debugWithName("component:Editable"), PLACEHOLDER_ST
1202
1251
  }
1203
1252
  },
1204
1253
  editor: slateEditor
1205
- }), event_10.stopPropagation());
1254
+ }));
1206
1255
  }, [onDragEnd, editorActor, slateEditor]), handleDragEnter = React.useCallback((event_11) => {
1207
- if (onDragEnter?.(event_11), !event_11.isDefaultPrevented() && !event_11.isPropagationStopped()) {
1208
- const position_5 = getEventPosition({
1209
- schema: editorActor.getSnapshot().context.schema,
1210
- slateEditor,
1211
- event: event_11.nativeEvent
1212
- });
1213
- if (!position_5)
1214
- return;
1215
- editorActor.send({
1216
- type: "behavior event",
1217
- behaviorEvent: {
1218
- type: "drag.dragenter",
1219
- originEvent: {
1220
- dataTransfer: event_11.dataTransfer
1221
- },
1222
- position: position_5
1256
+ if (onDragEnter?.(event_11), event_11.isDefaultPrevented() || event_11.isPropagationStopped())
1257
+ return;
1258
+ event_11.stopPropagation();
1259
+ const position_6 = getEventPosition({
1260
+ schema: editorActor.getSnapshot().context.schema,
1261
+ slateEditor,
1262
+ event: event_11.nativeEvent
1263
+ });
1264
+ position_6 && editorActor.send({
1265
+ type: "behavior event",
1266
+ behaviorEvent: {
1267
+ type: "drag.dragenter",
1268
+ originEvent: {
1269
+ dataTransfer: event_11.dataTransfer
1223
1270
  },
1224
- editor: slateEditor
1225
- }), event_11.stopPropagation();
1226
- }
1271
+ position: position_6
1272
+ },
1273
+ editor: slateEditor
1274
+ });
1227
1275
  }, [onDragEnter, editorActor, slateEditor]), handleDragOver = React.useCallback((event_12) => {
1228
- if (onDragOver?.(event_12), !event_12.isDefaultPrevented() && !event_12.isPropagationStopped()) {
1229
- const position_6 = getEventPosition({
1230
- schema: editorActor.getSnapshot().context.schema,
1231
- slateEditor,
1232
- event: event_12.nativeEvent
1233
- });
1234
- if (!position_6)
1235
- return;
1236
- editorActor.send({
1237
- type: "behavior event",
1238
- behaviorEvent: {
1239
- type: "drag.dragover",
1240
- originEvent: {
1241
- dataTransfer: event_12.dataTransfer
1242
- },
1243
- position: position_6
1276
+ if (onDragOver?.(event_12), event_12.isDefaultPrevented() || event_12.isPropagationStopped())
1277
+ return;
1278
+ event_12.stopPropagation();
1279
+ const position_7 = getEventPosition({
1280
+ schema: editorActor.getSnapshot().context.schema,
1281
+ slateEditor,
1282
+ event: event_12.nativeEvent
1283
+ });
1284
+ if (!position_7)
1285
+ return;
1286
+ const snapshot_0 = editorProvider.getEditorSnapshot({
1287
+ editorActorSnapshot: editorActor.getSnapshot(),
1288
+ slateEditorInstance: slateEditor
1289
+ });
1290
+ draggingOnDragOrigin({
1291
+ snapshot: snapshot_0,
1292
+ position: position_7
1293
+ }) && event_12.preventDefault(), editorActor.send({
1294
+ type: "behavior event",
1295
+ behaviorEvent: {
1296
+ type: "drag.dragover",
1297
+ originEvent: {
1298
+ dataTransfer: event_12.dataTransfer
1244
1299
  },
1245
- editor: slateEditor,
1246
- nativeEvent: event_12
1247
- }), event_12.stopPropagation();
1248
- }
1300
+ position: position_7
1301
+ },
1302
+ editor: slateEditor,
1303
+ nativeEvent: event_12
1304
+ });
1249
1305
  }, [onDragOver, editorActor, slateEditor]), handleDrop = React.useCallback((event_13) => {
1250
- if (onDrop?.(event_13), !event_13.isDefaultPrevented() && !event_13.isPropagationStopped()) {
1251
- const position_7 = getEventPosition({
1252
- schema: editorActor.getSnapshot().context.schema,
1253
- slateEditor,
1254
- event: event_13.nativeEvent
1255
- });
1256
- if (!position_7) {
1257
- console.warn("Could not find position for drop event");
1258
- return;
1259
- }
1260
- const range = slateReact.ReactEditor.findEventRange(slateEditor, event_13);
1261
- slateEditor.select(range), editorActor.send({
1262
- type: "behavior event",
1263
- behaviorEvent: {
1264
- type: "drag.drop",
1265
- originEvent: {
1266
- dataTransfer: event_13.dataTransfer
1267
- },
1268
- position: position_7
1269
- },
1270
- editor: slateEditor
1271
- }), event_13.preventDefault();
1306
+ if (onDrop?.(event_13), event_13.isDefaultPrevented() || event_13.isPropagationStopped())
1307
+ return;
1308
+ event_13.preventDefault();
1309
+ const position_8 = getEventPosition({
1310
+ schema: editorActor.getSnapshot().context.schema,
1311
+ slateEditor,
1312
+ event: event_13.nativeEvent
1313
+ });
1314
+ if (!position_8) {
1315
+ console.warn("Could not find position for drop event");
1316
+ return;
1272
1317
  }
1318
+ editorActor.send({
1319
+ type: "behavior event",
1320
+ behaviorEvent: {
1321
+ type: "select",
1322
+ selection: position_8.selection
1323
+ },
1324
+ editor: slateEditor
1325
+ }), editorActor.send({
1326
+ type: "behavior event",
1327
+ behaviorEvent: {
1328
+ type: "drag.drop",
1329
+ originEvent: {
1330
+ dataTransfer: event_13.dataTransfer
1331
+ },
1332
+ position: position_8
1333
+ },
1334
+ editor: slateEditor
1335
+ });
1273
1336
  }, [onDrop, editorActor, slateEditor]), handleDragLeave = React.useCallback((event_14) => {
1274
- onDragLeave?.(event_14), !event_14.isDefaultPrevented() && !event_14.isPropagationStopped() && editorActor.send({
1337
+ onDragLeave?.(event_14), !(event_14.isDefaultPrevented() || event_14.isPropagationStopped() || (event_14.stopPropagation(), !getEventPosition({
1338
+ schema: editorActor.getSnapshot().context.schema,
1339
+ slateEditor,
1340
+ event: event_14.nativeEvent
1341
+ }))) && editorActor.send({
1275
1342
  type: "behavior event",
1276
1343
  behaviorEvent: {
1277
1344
  type: "drag.dragleave",