@portabletext/editor 1.40.3 → 1.40.4

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 { fromSlateValue, toPortableTextRange, toSlateRange, debugWithName, EditorActorContext, KEY_TO_VALUE_ELEMENT, usePortableTextEditor, PortableTextEditor, moveRangeByOperation, isEqualToEmptyEditor, getEditorSnapshot, useEditor } from "./_chunks-es/editor-provider.js";
1
+ import { getNodeBlock, 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";
@@ -7,10 +7,11 @@ import noop from "lodash/noop.js";
7
7
  import { useContext, useRef, useState, useEffect, useMemo, startTransition, useCallback, forwardRef, useImperativeHandle } from "react";
8
8
  import { Editor, Range, Element as Element$2, Text, Transforms, Path } from "slate";
9
9
  import { useSlateStatic, useSelected, ReactEditor, useSlate, Editable } from "slate-react";
10
- import { getBlockEndPoint, getBlockStartPoint } from "./_chunks-es/util.slice-blocks.js";
10
+ import { getBlockEndPoint, getBlockStartPoint, isKeyedSegment } from "./_chunks-es/util.slice-blocks.js";
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
15
  import { parseBlocks } from "./_chunks-es/util.selection-point-to-block-offset.js";
15
16
  import { defineBehavior, isHotkey } from "./_chunks-es/behavior.core.js";
16
17
  import { c } from "react-compiler-runtime";
@@ -55,11 +56,23 @@ function getDragSelection({
55
56
  focus: getBlockEndPoint(focusTextBlock)
56
57
  });
57
58
  const selectedBlocks = getSelectedBlocks(snapshot);
58
- if (snapshot.context.selection && isSelectionExpanded(snapshot) && isOverlappingSelection(eventSelection)(snapshot) && selectedBlocks.length > 1) {
59
+ if (snapshot.context.selection && isSelectionExpanded(snapshot) && selectedBlocks.length > 1) {
59
60
  const selectionStartBlock = getSelectionStartBlock(snapshot), selectionEndBlock = getSelectionEndBlock(snapshot);
60
- selectionStartBlock && selectionEndBlock && (dragSelection = {
61
- anchor: getBlockStartPoint(selectionStartBlock),
62
- focus: getBlockEndPoint(selectionEndBlock)
61
+ if (!selectionStartBlock || !selectionEndBlock)
62
+ return dragSelection;
63
+ const selectionStartPoint = getBlockStartPoint(selectionStartBlock), selectionEndPoint = getBlockEndPoint(selectionEndBlock);
64
+ isOverlappingSelection(eventSelection)({
65
+ ...snapshot,
66
+ context: {
67
+ ...snapshot.context,
68
+ selection: {
69
+ anchor: selectionStartPoint,
70
+ focus: selectionEndPoint
71
+ }
72
+ }
73
+ }) && (dragSelection = {
74
+ anchor: selectionStartPoint,
75
+ focus: selectionEndPoint
63
76
  });
64
77
  }
65
78
  return dragSelection;
@@ -75,7 +88,11 @@ function getEventPosition({
75
88
  });
76
89
  if (!node)
77
90
  return;
78
- const positionBlock = getEventPositionBlock({
91
+ const block = getNodeBlock({
92
+ editor: slateEditor,
93
+ schema,
94
+ node
95
+ }), positionBlock = getEventPositionBlock({
79
96
  node,
80
97
  slateEditor,
81
98
  event
@@ -84,9 +101,8 @@ function getEventPosition({
84
101
  slateEditor,
85
102
  event
86
103
  });
87
- if (positionBlock && !selection && !Editor.isEditor(node)) {
88
- const block = fromSlateValue([node], schema.block.name)?.at(0);
89
- return block ? {
104
+ if (block && positionBlock && !selection && !Editor.isEditor(node))
105
+ return {
90
106
  block: positionBlock,
91
107
  isEditor: !1,
92
108
  selection: {
@@ -103,10 +119,29 @@ function getEventPosition({
103
119
  }]
104
120
  })
105
121
  }
106
- } : void 0;
107
- }
108
- if (!(!positionBlock || !selection))
109
- return {
122
+ };
123
+ if (!positionBlock || !selection)
124
+ return;
125
+ const focusBlockPath = selection.focus.path.at(0), focusBlockKey = isKeyedSegment(focusBlockPath) ? focusBlockPath._key : void 0;
126
+ if (focusBlockKey)
127
+ return isSelectionCollapsed$1(selection) && block && focusBlockKey !== block._key ? {
128
+ block: positionBlock,
129
+ isEditor: !1,
130
+ selection: {
131
+ anchor: getBlockStartPoint({
132
+ node: block,
133
+ path: [{
134
+ _key: block._key
135
+ }]
136
+ }),
137
+ focus: getBlockEndPoint({
138
+ node: block,
139
+ path: [{
140
+ _key: block._key
141
+ }]
142
+ })
143
+ }
144
+ } : {
110
145
  block: positionBlock,
111
146
  isEditor: Editor.isEditor(node),
112
147
  selection