@portabletext/editor 2.8.4 → 2.9.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 react12 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
- }): react12.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(): react12.JSX.Element;
195
+ declare function OneLinePlugin(): react22.JSX.Element;
196
196
  export { BehaviorPlugin, DecoratorShortcutPlugin, EditorRefPlugin, EventListenerPlugin, MarkdownPlugin, type MarkdownPluginConfig, OneLinePlugin };
@@ -1,5 +1,5 @@
1
1
  import { Behavior, Editor, EditorEmittedEvent, EditorSchema } from "../_chunks-dts/behavior.types.action.js";
2
- import * as react22 from "react";
2
+ import * as react21 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
- }): react22.JSX.Element;
184
+ }): react21.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(): react22.JSX.Element;
195
+ declare function OneLinePlugin(): react21.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.8.4",
3
+ "version": "2.9.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -81,8 +81,8 @@
81
81
  "xstate": "^5.21.0",
82
82
  "@portabletext/block-tools": "^3.5.5",
83
83
  "@portabletext/keyboard-shortcuts": "^1.1.1",
84
- "@portabletext/schema": "^1.2.0",
85
- "@portabletext/patches": "^1.1.8"
84
+ "@portabletext/patches": "^1.1.8",
85
+ "@portabletext/schema": "^1.2.0"
86
86
  },
87
87
  "devDependencies": {
88
88
  "@sanity/diff-match-patch": "^3.2.0",
@@ -111,8 +111,8 @@
111
111
  "vitest": "^3.2.4",
112
112
  "vitest-browser-react": "^1.0.1",
113
113
  "@portabletext/sanity-bridge": "1.1.9",
114
- "racejar": "1.2.15",
115
- "@portabletext/test": "^0.0.0"
114
+ "@portabletext/test": "^0.0.0",
115
+ "racejar": "1.2.15"
116
116
  },
117
117
  "peerDependencies": {
118
118
  "@portabletext/sanity-bridge": "^1.1.9",
@@ -1,37 +1,134 @@
1
1
  import {getFocusTextBlock} from '../selectors'
2
- import {isEmptyTextBlock} from '../utils'
2
+ import {
3
+ getBlockEndPoint,
4
+ getBlockStartPoint,
5
+ isEmptyTextBlock,
6
+ isEqualSelectionPoints,
7
+ } from '../utils'
8
+ import {sliceTextBlock} from '../utils/util.slice-text-block'
3
9
  import {execute, raise} from './behavior.types.action'
4
10
  import {defineBehavior} from './behavior.types.behavior'
5
11
 
6
12
  export const abstractInsertBehaviors = [
7
13
  defineBehavior({
8
14
  on: 'insert.blocks',
9
- guard: ({event}) => event.placement === 'before',
15
+ guard: ({event}) => {
16
+ const onlyBlock =
17
+ event.blocks.length === 1 ? event.blocks.at(0) : undefined
18
+
19
+ if (!onlyBlock) {
20
+ return false
21
+ }
22
+
23
+ return {onlyBlock}
24
+ },
10
25
  actions: [
11
- ({event}) =>
12
- event.blocks.map((block, index) =>
26
+ ({event}, {onlyBlock}) => [
27
+ raise({
28
+ type: 'insert.block',
29
+ block: onlyBlock,
30
+ placement: event.placement,
31
+ select: event.select ?? 'end',
32
+ }),
33
+ ],
34
+ ],
35
+ }),
36
+ defineBehavior({
37
+ on: 'insert.blocks',
38
+ guard: ({snapshot, event}) => {
39
+ if (event.placement !== 'before') {
40
+ return false
41
+ }
42
+
43
+ const firstBlockKey =
44
+ event.blocks.at(0)?._key ?? snapshot.context.keyGenerator()
45
+ const lastBlockKey =
46
+ event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator()
47
+
48
+ return {firstBlockKey, lastBlockKey}
49
+ },
50
+ actions: [
51
+ ({snapshot, event}, {firstBlockKey, lastBlockKey}) => [
52
+ ...event.blocks.map((block, index) =>
13
53
  raise({
14
54
  type: 'insert.block',
15
55
  block,
16
56
  placement: index === 0 ? 'before' : 'after',
17
- select: event.select ?? 'end',
57
+ select: index !== event.blocks.length - 1 ? 'end' : 'none',
18
58
  }),
19
59
  ),
60
+ ...(event.select === 'none'
61
+ ? [
62
+ raise({
63
+ type: 'select',
64
+ at: snapshot.context.selection,
65
+ }),
66
+ ]
67
+ : event.select === 'start'
68
+ ? [
69
+ raise({
70
+ type: 'select.block',
71
+ at: [{_key: firstBlockKey}],
72
+ select: 'start',
73
+ }),
74
+ ]
75
+ : [
76
+ raise({
77
+ type: 'select.block',
78
+ at: [{_key: lastBlockKey}],
79
+ select: 'end',
80
+ }),
81
+ ]),
82
+ ],
20
83
  ],
21
84
  }),
22
85
  defineBehavior({
23
86
  on: 'insert.blocks',
24
- guard: ({event}) => event.placement === 'after',
87
+ guard: ({snapshot, event}) => {
88
+ if (event.placement !== 'after') {
89
+ return false
90
+ }
91
+
92
+ const firstBlockKey =
93
+ event.blocks.at(0)?._key ?? snapshot.context.keyGenerator()
94
+ const lastBlockKey =
95
+ event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator()
96
+
97
+ return {firstBlockKey, lastBlockKey}
98
+ },
25
99
  actions: [
26
- ({event}) =>
27
- event.blocks.map((block) =>
100
+ ({snapshot, event}, {firstBlockKey, lastBlockKey}) => [
101
+ ...event.blocks.map((block, index) =>
28
102
  raise({
29
103
  type: 'insert.block',
30
104
  block,
31
105
  placement: 'after',
32
- select: event.select ?? 'end',
106
+ select: index !== event.blocks.length - 1 ? 'end' : 'none',
33
107
  }),
34
108
  ),
109
+ ...(event.select === 'none'
110
+ ? [
111
+ raise({
112
+ type: 'select',
113
+ at: snapshot.context.selection,
114
+ }),
115
+ ]
116
+ : event.select === 'start'
117
+ ? [
118
+ raise({
119
+ type: 'select.block',
120
+ at: [{_key: firstBlockKey}],
121
+ select: 'start',
122
+ }),
123
+ ]
124
+ : [
125
+ raise({
126
+ type: 'select.block',
127
+ at: [{_key: lastBlockKey}],
128
+ select: 'end',
129
+ }),
130
+ ]),
131
+ ],
35
132
  ],
36
133
  }),
37
134
  defineBehavior({
@@ -47,67 +144,168 @@ export const abstractInsertBehaviors = [
47
144
  return false
48
145
  }
49
146
 
50
- return {focusTextBlock}
147
+ if (!isEmptyTextBlock(snapshot.context, focusTextBlock.node)) {
148
+ return false
149
+ }
150
+
151
+ const firstBlockKey =
152
+ event.blocks.at(0)?._key ?? snapshot.context.keyGenerator()
153
+ const lastBlockKey =
154
+ event.blocks.at(-1)?._key ?? snapshot.context.keyGenerator()
155
+
156
+ return {focusTextBlock, firstBlockKey, lastBlockKey}
51
157
  },
52
158
  actions: [
53
- ({snapshot, event}, {focusTextBlock}) =>
54
- event.blocks.length === 1
159
+ ({event}, {firstBlockKey, lastBlockKey}) => [
160
+ ...event.blocks.map((block, index) =>
161
+ raise({
162
+ type: 'insert.block',
163
+ block,
164
+ placement: index === 0 ? 'auto' : 'after',
165
+ select: index !== event.blocks.length - 1 ? 'end' : 'none',
166
+ }),
167
+ ),
168
+ ...(event.select === 'none' || event.select === 'start'
55
169
  ? [
56
170
  raise({
57
- type: 'insert.block',
58
- block: event.blocks[0],
59
- placement: 'auto',
60
- select: event.select ?? 'end',
171
+ type: 'select.block',
172
+ at: [{_key: firstBlockKey}],
173
+ select: 'start',
61
174
  }),
62
175
  ]
63
- : isEmptyTextBlock(snapshot.context, focusTextBlock.node)
64
- ? event.blocks.map((block, index) =>
176
+ : [
177
+ raise({
178
+ type: 'select.block',
179
+ at: [{_key: lastBlockKey}],
180
+ select: 'end',
181
+ }),
182
+ ]),
183
+ ],
184
+ ],
185
+ }),
186
+ defineBehavior({
187
+ on: 'insert.blocks',
188
+ guard: ({snapshot, event}) => {
189
+ if (event.placement !== 'auto') {
190
+ return false
191
+ }
192
+
193
+ const focusTextBlock = getFocusTextBlock(snapshot)
194
+
195
+ if (!focusTextBlock || !snapshot.context.selection) {
196
+ return false
197
+ }
198
+
199
+ const focusBlockStartPoint = getBlockStartPoint({
200
+ context: snapshot.context,
201
+ block: focusTextBlock,
202
+ })
203
+ const focusBlockEndPoint = getBlockEndPoint({
204
+ context: snapshot.context,
205
+ block: focusTextBlock,
206
+ })
207
+ const focusTextBlockAfter = sliceTextBlock({
208
+ context: {
209
+ schema: snapshot.context.schema,
210
+ selection: {
211
+ anchor: snapshot.context.selection.focus,
212
+ focus: focusBlockEndPoint,
213
+ },
214
+ },
215
+ block: focusTextBlock.node,
216
+ })
217
+ const firstBlockKey =
218
+ event.blocks.at(0)?._key ?? snapshot.context.keyGenerator()
219
+
220
+ return {
221
+ firstBlockKey,
222
+ focusBlockStartPoint,
223
+ focusBlockEndPoint,
224
+ focusTextBlockAfter,
225
+ selection: snapshot.context.selection,
226
+ }
227
+ },
228
+ actions: [
229
+ (
230
+ {event},
231
+ {
232
+ focusBlockEndPoint,
233
+ focusTextBlockAfter,
234
+ selection,
235
+ firstBlockKey,
236
+ focusBlockStartPoint,
237
+ },
238
+ ) => [
239
+ ...event.blocks.flatMap((block, index) =>
240
+ index === 0
241
+ ? [
242
+ ...(isEqualSelectionPoints(selection.focus, focusBlockEndPoint)
243
+ ? []
244
+ : [
245
+ raise({
246
+ type: 'delete',
247
+ at: {
248
+ anchor: selection.focus,
249
+ focus: focusBlockEndPoint,
250
+ },
251
+ }),
252
+ ]),
65
253
  raise({
66
254
  type: 'insert.block',
67
255
  block,
68
- placement: index === 0 ? 'auto' : 'after',
69
- select: event.select ?? 'end',
256
+ placement: 'auto',
257
+ select: 'end',
70
258
  }),
71
- )
72
- : event.blocks.flatMap((block, index) =>
73
- index === 0
74
- ? [
75
- raise({
76
- type: 'split',
77
- }),
78
- raise({
79
- type: 'select.previous block',
80
- select: 'end',
81
- }),
82
- raise({
83
- type: 'insert.block',
84
- block,
85
- placement: 'auto',
86
- select: event.select ?? 'end',
87
- }),
88
- ]
89
- : index === event.blocks.length - 1
90
- ? [
91
- raise({
92
- type: 'select.next block',
93
- select: 'start',
94
- }),
95
- raise({
96
- type: 'insert.block',
97
- block,
98
- placement: 'auto',
99
- select: event.select ?? 'end',
100
- }),
101
- ]
102
- : [
103
- raise({
104
- type: 'insert.block',
105
- block,
106
- placement: 'after',
107
- select: event.select ?? 'end',
108
- }),
109
- ],
110
- ),
259
+ ]
260
+ : index === event.blocks.length - 1
261
+ ? [
262
+ raise({
263
+ type: 'insert.block',
264
+ block,
265
+ placement: 'after',
266
+ select: 'end',
267
+ }),
268
+ raise({
269
+ type: 'insert.block',
270
+ block: focusTextBlockAfter,
271
+ placement: 'auto',
272
+ select: event.select === 'end' ? 'none' : 'end',
273
+ }),
274
+ ]
275
+ : [
276
+ raise({
277
+ type: 'insert.block',
278
+ block,
279
+ placement: 'after',
280
+ select: 'end',
281
+ }),
282
+ ],
283
+ ),
284
+ ...(event.select === 'none'
285
+ ? [
286
+ raise({
287
+ type: 'select',
288
+ at: selection,
289
+ }),
290
+ ]
291
+ : event.select === 'start'
292
+ ? [
293
+ isEqualSelectionPoints(selection.focus, focusBlockStartPoint)
294
+ ? raise({
295
+ type: 'select.block',
296
+ at: [{_key: firstBlockKey}],
297
+ select: 'start',
298
+ })
299
+ : raise({
300
+ type: 'select',
301
+ at: {
302
+ anchor: selection.focus,
303
+ focus: selection.focus,
304
+ },
305
+ }),
306
+ ]
307
+ : []),
308
+ ],
111
309
  ],
112
310
  }),
113
311
  defineBehavior({
@@ -1,72 +1,114 @@
1
- import {getNextBlock, getPreviousBlock} from '../selectors'
2
- import {getBlockEndPoint, getBlockStartPoint} from '../utils'
1
+ import {getFocusBlock, getNextBlock, getPreviousBlock} from '../selectors'
2
+ import {getBlockEndPoint} from '../utils'
3
3
  import {raise} from './behavior.types.action'
4
4
  import {defineBehavior} from './behavior.types.behavior'
5
5
 
6
6
  export const abstractSelectBehaviors = [
7
7
  defineBehavior({
8
- on: 'select.previous block',
8
+ on: 'select.block',
9
9
  guard: ({snapshot, event}) => {
10
- const previousBlock = getPreviousBlock(snapshot)
11
-
12
- if (!previousBlock) {
10
+ if (event.select !== 'end') {
13
11
  return false
14
12
  }
15
13
 
16
- const point =
17
- event.select === 'end'
18
- ? getBlockEndPoint({
19
- context: snapshot.context,
20
- block: previousBlock,
21
- })
22
- : getBlockStartPoint({
23
- context: snapshot.context,
24
- block: previousBlock,
25
- })
26
-
27
- return {
28
- selection: {
29
- anchor: point,
30
- focus: point,
14
+ const block = getFocusBlock({
15
+ ...snapshot,
16
+ context: {
17
+ ...snapshot.context,
18
+ selection: {
19
+ anchor: {
20
+ path: event.at,
21
+ offset: 0,
22
+ },
23
+ focus: {
24
+ path: event.at,
25
+ offset: 0,
26
+ },
27
+ },
31
28
  },
29
+ })
30
+
31
+ if (!block) {
32
+ return false
32
33
  }
34
+
35
+ const blockEndPoint = getBlockEndPoint({
36
+ context: snapshot.context,
37
+ block,
38
+ })
39
+
40
+ return {blockEndPoint}
33
41
  },
34
42
  actions: [
35
- (_, {selection}) => [
43
+ (_, {blockEndPoint}) => [
36
44
  raise({
37
45
  type: 'select',
38
- at: selection,
46
+ at: {
47
+ anchor: blockEndPoint,
48
+ focus: blockEndPoint,
49
+ },
50
+ }),
51
+ ],
52
+ ],
53
+ }),
54
+ defineBehavior({
55
+ on: 'select.block',
56
+ actions: [
57
+ ({event}) => [
58
+ raise({
59
+ type: 'select',
60
+ at: {
61
+ anchor: {
62
+ path: event.at,
63
+ offset: 0,
64
+ },
65
+ focus: {
66
+ path: event.at,
67
+ offset: 0,
68
+ },
69
+ },
70
+ }),
71
+ ],
72
+ ],
73
+ }),
74
+ defineBehavior({
75
+ on: 'select.previous block',
76
+ guard: ({snapshot}) => {
77
+ const previousBlock = getPreviousBlock(snapshot)
78
+
79
+ if (!previousBlock) {
80
+ return false
81
+ }
82
+
83
+ return {previousBlock}
84
+ },
85
+ actions: [
86
+ ({event}, {previousBlock}) => [
87
+ raise({
88
+ type: 'select.block',
89
+ at: previousBlock.path,
90
+ select: event.select,
39
91
  }),
40
92
  ],
41
93
  ],
42
94
  }),
43
95
  defineBehavior({
44
96
  on: 'select.next block',
45
- guard: ({snapshot, event}) => {
97
+ guard: ({snapshot}) => {
46
98
  const nextBlock = getNextBlock(snapshot)
47
99
 
48
100
  if (!nextBlock) {
49
101
  return false
50
102
  }
51
103
 
52
- const point =
53
- event.select === 'end'
54
- ? getBlockEndPoint({
55
- context: snapshot.context,
56
- block: nextBlock,
57
- })
58
- : getBlockStartPoint({
59
- context: snapshot.context,
60
- block: nextBlock,
61
- })
62
-
63
- return {selection: {anchor: point, focus: point}}
104
+ return {nextBlock}
64
105
  },
65
106
  actions: [
66
- (_, {selection}) => [
107
+ ({event}, {nextBlock}) => [
67
108
  raise({
68
- type: 'select',
69
- at: selection,
109
+ type: 'select.block',
110
+ at: nextBlock.path,
111
+ select: event.select,
70
112
  }),
71
113
  ],
72
114
  ],
@@ -228,6 +228,7 @@ const abstractBehaviorEventTypes = [
228
228
  'list item.toggle',
229
229
  'move.block down',
230
230
  'move.block up',
231
+ 'select.block',
231
232
  'select.previous block',
232
233
  'select.next block',
233
234
  'serialize',
@@ -415,6 +416,11 @@ type AbstractBehaviorEvent =
415
416
  type: StrictExtract<SyntheticBehaviorEventType, 'move.block up'>
416
417
  at: BlockPath
417
418
  }
419
+ | {
420
+ type: StrictExtract<SyntheticBehaviorEventType, 'select.block'>
421
+ at: BlockPath
422
+ select?: 'start' | 'end'
423
+ }
418
424
  | {
419
425
  type: StrictExtract<SyntheticBehaviorEventType, 'select.previous block'>
420
426
  select?: 'start' | 'end'
@@ -1027,7 +1027,18 @@ function validateSelection(slateEditor: Editor, activeElement: HTMLDivElement) {
1027
1027
  if (!slateEditor.selection) {
1028
1028
  return
1029
1029
  }
1030
- const root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
1030
+
1031
+ let root: Document | ShadowRoot | undefined
1032
+
1033
+ try {
1034
+ root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
1035
+ } catch {}
1036
+
1037
+ if (!root) {
1038
+ // The editor has most likely been unmounted
1039
+ return
1040
+ }
1041
+
1031
1042
  // Return if the editor isn't the active element
1032
1043
  if (activeElement !== root.activeElement) {
1033
1044
  return