@portabletext/editor 2.2.0 → 2.3.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,5 +1,5 @@
1
1
  import { Behavior, Editor, EditorEmittedEvent, EditorSchema } from "../_chunks-dts/behavior.types.action.cjs";
2
- import * as react21 from "react";
2
+ import * as react22 from "react";
3
3
  import React from "react";
4
4
  /**
5
5
  * @beta
@@ -181,7 +181,7 @@ type MarkdownPluginConfig = MarkdownBehaviorsConfig & {
181
181
  */
182
182
  declare function MarkdownPlugin(props: {
183
183
  config: MarkdownPluginConfig;
184
- }): react21.JSX.Element;
184
+ }): react22.JSX.Element;
185
185
  /**
186
186
  * @beta
187
187
  * Restrict the editor to one line. The plugin takes care of blocking
@@ -192,5 +192,5 @@ declare function MarkdownPlugin(props: {
192
192
  *
193
193
  * @deprecated Install the plugin from `@portabletext/plugin-one-line`
194
194
  */
195
- declare function OneLinePlugin(): react21.JSX.Element;
195
+ declare function OneLinePlugin(): react22.JSX.Element;
196
196
  export { BehaviorPlugin, DecoratorShortcutPlugin, EditorRefPlugin, EventListenerPlugin, MarkdownPlugin, type MarkdownPluginConfig, OneLinePlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -80,8 +80,8 @@
80
80
  "slate-react": "0.117.4",
81
81
  "xstate": "^5.20.2",
82
82
  "@portabletext/block-tools": "2.0.8",
83
- "@portabletext/keyboard-shortcuts": "1.1.1",
84
- "@portabletext/patches": "1.1.6"
83
+ "@portabletext/patches": "1.1.6",
84
+ "@portabletext/keyboard-shortcuts": "1.1.1"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@portabletext/toolkit": "^2.0.17",
@@ -1,4 +1,4 @@
1
- import {isSpan} from '../internal-utils/parse-blocks'
1
+ import {isSpan, isTextBlock} from '../internal-utils/parse-blocks'
2
2
  import * as selectors from '../selectors'
3
3
  import * as utils from '../utils'
4
4
  import {raise} from './behavior.types.action'
@@ -25,6 +25,57 @@ export const abstractDeleteBehaviors = [
25
25
  ],
26
26
  ],
27
27
  }),
28
+ defineBehavior({
29
+ on: 'delete',
30
+ guard: ({snapshot, event}) => {
31
+ if (event.direction !== 'backward') {
32
+ return false
33
+ }
34
+
35
+ const previousBlock = selectors.getPreviousBlock(snapshot)
36
+ const focusTextBlock = selectors.getFocusTextBlock(snapshot)
37
+
38
+ if (!previousBlock || !focusTextBlock) {
39
+ return false
40
+ }
41
+
42
+ if (!selectors.isAtTheStartOfBlock(focusTextBlock)(snapshot)) {
43
+ return false
44
+ }
45
+
46
+ const previousBlockEndPoint = utils.getBlockEndPoint({
47
+ context: snapshot.context,
48
+ block: previousBlock,
49
+ })
50
+
51
+ if (!isTextBlock(snapshot.context, previousBlock.node)) {
52
+ return false
53
+ }
54
+
55
+ return {previousBlockEndPoint, focusTextBlock}
56
+ },
57
+ actions: [
58
+ (_, {previousBlockEndPoint, focusTextBlock}) => [
59
+ raise({
60
+ type: 'delete.block',
61
+ at: focusTextBlock.path,
62
+ }),
63
+ raise({
64
+ type: 'select',
65
+ at: {
66
+ anchor: previousBlockEndPoint,
67
+ focus: previousBlockEndPoint,
68
+ },
69
+ }),
70
+ raise({
71
+ type: 'insert.block',
72
+ block: focusTextBlock.node,
73
+ placement: 'auto',
74
+ select: 'start',
75
+ }),
76
+ ],
77
+ ],
78
+ }),
28
79
  defineBehavior({
29
80
  on: 'delete.forward',
30
81
  guard: ({snapshot}) => {
@@ -45,6 +96,45 @@ export const abstractDeleteBehaviors = [
45
96
  ],
46
97
  ],
47
98
  }),
99
+ defineBehavior({
100
+ on: 'delete',
101
+ guard: ({snapshot, event}) => {
102
+ if (event.direction !== 'forward') {
103
+ return false
104
+ }
105
+
106
+ const nextBlock = selectors.getNextBlock(snapshot)
107
+ const focusTextBlock = selectors.getFocusTextBlock(snapshot)
108
+
109
+ if (!nextBlock || !focusTextBlock) {
110
+ return false
111
+ }
112
+
113
+ if (!selectors.isAtTheEndOfBlock(focusTextBlock)(snapshot)) {
114
+ return false
115
+ }
116
+
117
+ if (!isTextBlock(snapshot.context, nextBlock.node)) {
118
+ return false
119
+ }
120
+
121
+ return {nextBlock}
122
+ },
123
+ actions: [
124
+ (_, {nextBlock}) => [
125
+ raise({
126
+ type: 'delete.block',
127
+ at: nextBlock.path,
128
+ }),
129
+ raise({
130
+ type: 'insert.block',
131
+ block: nextBlock.node,
132
+ placement: 'auto',
133
+ select: 'none',
134
+ }),
135
+ ],
136
+ ],
137
+ }),
48
138
  defineBehavior({
49
139
  on: 'delete.block',
50
140
  actions: [
@@ -1,9 +1,14 @@
1
1
  import {isListBlock, isTextBlock} from '../internal-utils/parse-blocks'
2
2
  import {defaultKeyboardShortcuts} from '../keyboard-shortcuts/default-keyboard-shortcuts'
3
3
  import * as selectors from '../selectors'
4
- import {getBlockEndPoint} from '../utils'
4
+ import {
5
+ getBlockEndPoint,
6
+ getBlockStartPoint,
7
+ isEqualSelectionPoints,
8
+ } from '../utils'
5
9
  import {isAtTheBeginningOfBlock} from '../utils/util.at-the-beginning-of-block'
6
10
  import {isEmptyTextBlock} from '../utils/util.is-empty-text-block'
11
+ import {sliceTextBlock} from '../utils/util.slice-text-block'
7
12
  import {raise} from './behavior.types.action'
8
13
  import {defineBehavior} from './behavior.types.behavior'
9
14
 
@@ -183,6 +188,148 @@ const mergeTextIntoListOnBackspace = defineBehavior({
183
188
  ],
184
189
  })
185
190
 
191
+ /**
192
+ * When performing a delete operation where the start point of the operation is
193
+ * at the start of a list item and the end point of the operation is in another
194
+ * list item, we make sure the preserve the first list item. Otherwise, the
195
+ * default behavior would be to preserve the last item.
196
+ */
197
+ const deletingListFromStart = defineBehavior({
198
+ on: 'delete',
199
+ guard: ({snapshot, event}) => {
200
+ const blocksToDelete = selectors.getSelectedBlocks({
201
+ ...snapshot,
202
+ context: {
203
+ ...snapshot.context,
204
+ selection: event.at,
205
+ },
206
+ })
207
+
208
+ if (blocksToDelete.length < 2) {
209
+ return false
210
+ }
211
+
212
+ const startBlock = blocksToDelete.at(0)?.node
213
+ const middleBlocks = blocksToDelete.slice(1, -1)
214
+ const endBlock = blocksToDelete.at(-1)?.node
215
+
216
+ if (
217
+ !isListBlock(snapshot.context, startBlock) ||
218
+ !isListBlock(snapshot.context, endBlock)
219
+ ) {
220
+ // It's that any block in between isn't a list item, but the first and
221
+ // last blocks have to be list items for this Behavior to take effect.
222
+ return false
223
+ }
224
+
225
+ const deleteStartPoint = selectors.getSelectionStartPoint({
226
+ ...snapshot,
227
+ context: {
228
+ ...snapshot.context,
229
+ selection: event.at,
230
+ },
231
+ })
232
+ const deleteEndPoint = selectors.getSelectionEndPoint({
233
+ ...snapshot,
234
+ context: {
235
+ ...snapshot.context,
236
+ selection: event.at,
237
+ },
238
+ })
239
+
240
+ if (!deleteStartPoint || !deleteEndPoint) {
241
+ return false
242
+ }
243
+
244
+ const startBlockStartPoint = getBlockStartPoint({
245
+ context: snapshot.context,
246
+ block: {
247
+ node: startBlock,
248
+ path: [{_key: startBlock._key}],
249
+ },
250
+ })
251
+
252
+ if (!isEqualSelectionPoints(deleteStartPoint, startBlockStartPoint)) {
253
+ // If we aren't deleting from the beginning of the first list item, then
254
+ // there is no need to proceed. The default delete Behavior will suffice.
255
+ return false
256
+ }
257
+
258
+ const startBlockEndPoint = getBlockEndPoint({
259
+ context: snapshot.context,
260
+ block: {
261
+ node: startBlock,
262
+ path: [{_key: startBlock._key}],
263
+ },
264
+ })
265
+ const endBlockEndPoint = getBlockEndPoint({
266
+ context: snapshot.context,
267
+ block: {
268
+ node: endBlock,
269
+ path: [{_key: endBlock._key}],
270
+ },
271
+ })
272
+ const slicedEndBlock = sliceTextBlock({
273
+ context: {
274
+ schema: snapshot.context.schema,
275
+ selection: {
276
+ anchor: deleteEndPoint,
277
+ focus: endBlockEndPoint,
278
+ },
279
+ },
280
+ block: endBlock,
281
+ })
282
+
283
+ return {
284
+ startBlockStartPoint,
285
+ startBlockEndPoint,
286
+ middleBlocks,
287
+ endBlock,
288
+ slicedEndBlock,
289
+ }
290
+ },
291
+ actions: [
292
+ (
293
+ _,
294
+ {
295
+ startBlockStartPoint,
296
+ startBlockEndPoint,
297
+ middleBlocks,
298
+ endBlock,
299
+ slicedEndBlock,
300
+ },
301
+ ) => [
302
+ // All block in between can safely be deleted.
303
+ ...middleBlocks.map((block) =>
304
+ raise({type: 'delete.block', at: block.path}),
305
+ ),
306
+ // The last block is deleted as well.
307
+ raise({type: 'delete.block', at: [{_key: endBlock._key}]}),
308
+ // But in case the delete operation didn't reach all the way to the end
309
+ // of it, we first place the caret at the end of the start block...
310
+ raise({
311
+ type: 'select',
312
+ at: {
313
+ anchor: startBlockEndPoint,
314
+ focus: startBlockEndPoint,
315
+ },
316
+ }),
317
+ // ...and insert the rest of the end block at the end of it.
318
+ raise({
319
+ type: 'insert.block',
320
+ block: slicedEndBlock,
321
+ placement: 'auto',
322
+ select: 'none',
323
+ }),
324
+ // And finally, we delete the original text of the start block.
325
+ raise({
326
+ type: 'delete',
327
+ at: {anchor: startBlockStartPoint, focus: startBlockEndPoint},
328
+ }),
329
+ ],
330
+ ],
331
+ })
332
+
186
333
  /**
187
334
  * Hitting Enter in an empty list item would create a new list item below by
188
335
  * default. Instead, the list properties should be cleared.
@@ -521,6 +668,7 @@ export const coreListBehaviors = {
521
668
  unindentListOnBackspace,
522
669
  mergeTextIntoListOnDelete,
523
670
  mergeTextIntoListOnBackspace,
671
+ deletingListFromStart,
524
672
  clearListOnEnter,
525
673
  indentListOnTab,
526
674
  unindentListOnShiftTab,
@@ -24,6 +24,7 @@ export const coreBehaviorsConfig = [
24
24
  coreListBehaviors.unindentListOnBackspace,
25
25
  coreListBehaviors.mergeTextIntoListOnDelete,
26
26
  coreListBehaviors.mergeTextIntoListOnBackspace,
27
+ coreListBehaviors.deletingListFromStart,
27
28
  coreListBehaviors.clearListOnEnter,
28
29
  coreListBehaviors.indentListOnTab,
29
30
  coreListBehaviors.unindentListOnShiftTab,