@portabletext/editor 2.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -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,