@portabletext/editor 2.13.0 → 2.13.2

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 react22 from "react";
2
+ import * as react12 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
+ }): react12.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(): react12.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 };
@@ -1,5 +1,5 @@
1
1
  import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.cjs";
2
- import * as _sanity_types9 from "@sanity/types";
2
+ import * as _sanity_types8 from "@sanity/types";
3
3
  import { KeyedSegment, PortableTextBlock, PortableTextTextBlock } from "@sanity/types";
4
4
  import { isSpan, isTextBlock } from "@portabletext/schema";
5
5
  /**
@@ -143,7 +143,7 @@ declare function mergeTextBlocks({
143
143
  context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
144
144
  targetBlock: PortableTextTextBlock;
145
145
  incomingBlock: PortableTextTextBlock;
146
- }): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
146
+ }): PortableTextTextBlock<_sanity_types8.PortableTextObject | _sanity_types8.PortableTextSpan>;
147
147
  /**
148
148
  * @public
149
149
  */
@@ -1,5 +1,5 @@
1
1
  import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.js";
2
- import * as _sanity_types8 from "@sanity/types";
2
+ import * as _sanity_types9 from "@sanity/types";
3
3
  import { KeyedSegment, PortableTextBlock, PortableTextTextBlock } from "@sanity/types";
4
4
  import { isSpan, isTextBlock } from "@portabletext/schema";
5
5
  /**
@@ -143,7 +143,7 @@ declare function mergeTextBlocks({
143
143
  context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
144
144
  targetBlock: PortableTextTextBlock;
145
145
  incomingBlock: PortableTextTextBlock;
146
- }): PortableTextTextBlock<_sanity_types8.PortableTextObject | _sanity_types8.PortableTextSpan>;
146
+ }): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
147
147
  /**
148
148
  * @public
149
149
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.13.0",
3
+ "version": "2.13.2",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -86,9 +86,9 @@
86
86
  "slate-react": "0.117.4",
87
87
  "xstate": "^5.22.0",
88
88
  "@portabletext/block-tools": "^3.5.7",
89
- "@portabletext/patches": "^1.1.8",
89
+ "@portabletext/keyboard-shortcuts": "^1.1.1",
90
90
  "@portabletext/schema": "^1.2.0",
91
- "@portabletext/keyboard-shortcuts": "^1.1.1"
91
+ "@portabletext/patches": "^1.1.8"
92
92
  },
93
93
  "devDependencies": {
94
94
  "@sanity/diff-match-patch": "^3.2.0",
@@ -117,8 +117,8 @@
117
117
  "vitest": "^3.2.4",
118
118
  "vitest-browser-react": "^1.0.1",
119
119
  "@portabletext/sanity-bridge": "1.1.11",
120
- "racejar": "1.3.0",
121
- "@portabletext/test": "^0.0.0"
120
+ "@portabletext/test": "^0.0.0",
121
+ "racejar": "1.3.0"
122
122
  },
123
123
  "peerDependencies": {
124
124
  "@portabletext/sanity-bridge": "^1.1.11",
@@ -942,8 +942,28 @@ export const PortableTextEditable = forwardRef<
942
942
  } else if (forwardedRef) {
943
943
  forwardedRef.current = node
944
944
  }
945
+
946
+ if (node) {
947
+ // Observe mutations (child list and subtree) to this component's DOM,
948
+ // and make sure the editor selection is valid when that happens.
949
+ const mutationObserver = new MutationObserver(() => {
950
+ validateSelection(slateEditor, node)
951
+ })
952
+
953
+ mutationObserver.observe(node, {
954
+ attributeOldValue: false,
955
+ attributes: false,
956
+ characterData: false,
957
+ childList: true,
958
+ subtree: true,
959
+ })
960
+
961
+ return () => {
962
+ mutationObserver.disconnect()
963
+ }
964
+ }
945
965
  },
946
- [forwardedRef],
966
+ [forwardedRef, slateEditor],
947
967
  )
948
968
 
949
969
  if (!portableTextEditor) {
@@ -987,3 +1007,72 @@ export const PortableTextEditable = forwardRef<
987
1007
  })
988
1008
 
989
1009
  PortableTextEditable.displayName = 'ForwardRef(PortableTextEditable)'
1010
+
1011
+ // This function will handle unexpected DOM changes inside the Editable rendering,
1012
+ // and make sure that we can maintain a stable slateEditor.selection when that happens.
1013
+ //
1014
+ // For example, if this Editable is rendered inside something that might re-render
1015
+ // this component (hidden contexts) while the user is still actively changing the
1016
+ // contentEditable, this could interfere with the intermediate DOM selection,
1017
+ // which again could be picked up by ReactEditor's event listeners.
1018
+ // If that range is invalid at that point, the slate.editorSelection could be
1019
+ // set either wrong, or invalid, to which slateEditor will throw exceptions
1020
+ // that are impossible to recover properly from or result in a wrong selection.
1021
+ //
1022
+ // Also the other way around, when the ReactEditor will try to create a DOM Range
1023
+ // from the current slateEditor.selection, it may throw unrecoverable errors
1024
+ // if the current editor.selection is invalid according to the DOM.
1025
+ // If this is the case, default to selecting the top of the document, if the
1026
+ // user already had a selection.
1027
+ function validateSelection(slateEditor: Editor, activeElement: HTMLDivElement) {
1028
+ if (!slateEditor.selection) {
1029
+ return
1030
+ }
1031
+
1032
+ let root: Document | ShadowRoot | undefined
1033
+
1034
+ try {
1035
+ root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
1036
+ } catch {}
1037
+
1038
+ if (!root) {
1039
+ // The editor has most likely been unmounted
1040
+ return
1041
+ }
1042
+
1043
+ // Return if the editor isn't the active element
1044
+ if (activeElement !== root.activeElement) {
1045
+ return
1046
+ }
1047
+ const window = ReactEditor.getWindow(slateEditor)
1048
+ const domSelection = window.getSelection()
1049
+ if (!domSelection || domSelection.rangeCount === 0) {
1050
+ return
1051
+ }
1052
+ const existingDOMRange = domSelection.getRangeAt(0)
1053
+ try {
1054
+ const newDOMRange = ReactEditor.toDOMRange(
1055
+ slateEditor,
1056
+ slateEditor.selection,
1057
+ )
1058
+ if (
1059
+ newDOMRange.startOffset !== existingDOMRange.startOffset ||
1060
+ newDOMRange.endOffset !== existingDOMRange.endOffset
1061
+ ) {
1062
+ debug('DOM range out of sync, validating selection')
1063
+ // Remove all ranges temporary
1064
+ domSelection?.removeAllRanges()
1065
+ // Set the correct range
1066
+ domSelection.addRange(newDOMRange)
1067
+ }
1068
+ } catch {
1069
+ debug(`Could not resolve selection, selecting top document`)
1070
+ // Deselect the editor
1071
+ Transforms.deselect(slateEditor)
1072
+ // Select top document if there is a top block to select
1073
+ if (slateEditor.children.length > 0) {
1074
+ Transforms.select(slateEditor, [0, 0])
1075
+ }
1076
+ slateEditor.onChange()
1077
+ }
1078
+ }
@@ -8,6 +8,7 @@ import {
8
8
  } from 'slate-react'
9
9
  import type {EventPositionBlock} from '../../internal-utils/event-position'
10
10
  import type {
11
+ BlockRenderProps,
11
12
  PortableTextMemberSchemaTypes,
12
13
  RenderBlockFunction,
13
14
  } from '../../types/editor'
@@ -73,7 +74,8 @@ export function RenderBlockObject(props: {
73
74
  draggable={!props.readOnly}
74
75
  >
75
76
  {props.renderBlock && legacySchemaType ? (
76
- <props.renderBlock
77
+ <RenderBlock
78
+ renderBlock={props.renderBlock}
77
79
  editorElementRef={blockObjectRef}
78
80
  focused={focused}
79
81
  path={[{_key: props.element._key}]}
@@ -83,7 +85,7 @@ export function RenderBlockObject(props: {
83
85
  value={blockObject}
84
86
  >
85
87
  <RenderDefaultBlockObject blockObject={blockObject} />
86
- </props.renderBlock>
88
+ </RenderBlock>
87
89
  ) : (
88
90
  <RenderDefaultBlockObject blockObject={blockObject} />
89
91
  )}
@@ -92,3 +94,28 @@ export function RenderBlockObject(props: {
92
94
  </div>
93
95
  )
94
96
  }
97
+
98
+ function RenderBlock({
99
+ renderBlock,
100
+ children,
101
+ editorElementRef,
102
+ focused,
103
+ path,
104
+ schemaType,
105
+ selected,
106
+ type,
107
+ value,
108
+ }: {
109
+ renderBlock: RenderBlockFunction
110
+ } & BlockRenderProps) {
111
+ return renderBlock({
112
+ children,
113
+ editorElementRef,
114
+ focused,
115
+ path,
116
+ schemaType,
117
+ selected,
118
+ type,
119
+ value,
120
+ })
121
+ }
@@ -9,6 +9,7 @@ import {
9
9
  } from 'slate-react'
10
10
  import {getPointBlock} from '../../internal-utils/slate-utils'
11
11
  import type {
12
+ BlockChildRenderProps,
12
13
  PortableTextMemberSchemaTypes,
13
14
  RenderChildFunction,
14
15
  } from '../../types/editor'
@@ -79,7 +80,8 @@ export function RenderInlineObject(props: {
79
80
  {props.children}
80
81
  <span ref={inlineObjectRef} style={{display: 'inline-block'}}>
81
82
  {props.renderChild && block && legacySchemaType ? (
82
- <props.renderChild
83
+ <RenderChild
84
+ renderChild={props.renderChild}
83
85
  annotations={[]}
84
86
  editorElementRef={inlineObjectRef}
85
87
  selected={selected}
@@ -90,7 +92,7 @@ export function RenderInlineObject(props: {
90
92
  type={legacySchemaType}
91
93
  >
92
94
  <RenderDefaultInlineObject inlineObject={inlineObject} />
93
- </props.renderChild>
95
+ </RenderChild>
94
96
  ) : (
95
97
  <RenderDefaultInlineObject inlineObject={inlineObject} />
96
98
  )}
@@ -98,3 +100,30 @@ export function RenderInlineObject(props: {
98
100
  </span>
99
101
  )
100
102
  }
103
+
104
+ function RenderChild({
105
+ renderChild,
106
+ annotations,
107
+ children,
108
+ editorElementRef,
109
+ focused,
110
+ path,
111
+ schemaType,
112
+ selected,
113
+ value,
114
+ type,
115
+ }: {
116
+ renderChild: RenderChildFunction
117
+ } & BlockChildRenderProps) {
118
+ return renderChild({
119
+ annotations,
120
+ children,
121
+ editorElementRef,
122
+ focused,
123
+ path,
124
+ schemaType,
125
+ selected,
126
+ value,
127
+ type,
128
+ })
129
+ }
@@ -9,6 +9,9 @@ import {
9
9
  isSelectionCollapsed,
10
10
  } from '../../selectors'
11
11
  import type {
12
+ BlockAnnotationRenderProps,
13
+ BlockChildRenderProps,
14
+ BlockDecoratorRenderProps,
12
15
  EditorSelection,
13
16
  RenderAnnotationFunction,
14
17
  RenderChildFunction,
@@ -140,7 +143,8 @@ export function RenderSpan(props: RenderSpanProps) {
140
143
 
141
144
  if (path && legacyDecoratorSchemaType && props.renderDecorator) {
142
145
  children = (
143
- <props.renderDecorator
146
+ <RenderDecorator
147
+ renderDecorator={props.renderDecorator}
144
148
  editorElementRef={spanRef}
145
149
  focused={focused}
146
150
  path={path}
@@ -150,7 +154,7 @@ export function RenderSpan(props: RenderSpanProps) {
150
154
  type={legacyDecoratorSchemaType}
151
155
  >
152
156
  {children}
153
- </props.renderDecorator>
157
+ </RenderDecorator>
154
158
  )
155
159
  }
156
160
  }
@@ -166,7 +170,8 @@ export function RenderSpan(props: RenderSpanProps) {
166
170
  if (block && path && props.renderAnnotation) {
167
171
  children = (
168
172
  <span ref={spanRef}>
169
- <props.renderAnnotation
173
+ <RenderAnnotation
174
+ renderAnnotation={props.renderAnnotation}
170
175
  block={block}
171
176
  editorElementRef={spanRef}
172
177
  focused={focused}
@@ -177,7 +182,7 @@ export function RenderSpan(props: RenderSpanProps) {
177
182
  type={legacyAnnotationSchemaType}
178
183
  >
179
184
  {children}
180
- </props.renderAnnotation>
185
+ </RenderAnnotation>
181
186
  </span>
182
187
  )
183
188
  } else {
@@ -196,7 +201,8 @@ export function RenderSpan(props: RenderSpanProps) {
196
201
 
197
202
  if (child) {
198
203
  children = (
199
- <props.renderChild
204
+ <RenderChild
205
+ renderChild={props.renderChild}
200
206
  annotations={annotationMarkDefs}
201
207
  editorElementRef={spanRef}
202
208
  focused={focused}
@@ -207,7 +213,7 @@ export function RenderSpan(props: RenderSpanProps) {
207
213
  type={legacySchema.span}
208
214
  >
209
215
  {children}
210
- </props.renderChild>
216
+ </RenderChild>
211
217
  )
212
218
  }
213
219
  }
@@ -218,3 +224,82 @@ export function RenderSpan(props: RenderSpanProps) {
218
224
  </span>
219
225
  )
220
226
  }
227
+
228
+ function RenderAnnotation({
229
+ renderAnnotation,
230
+ block,
231
+ children,
232
+ editorElementRef,
233
+ focused,
234
+ path,
235
+ schemaType,
236
+ selected,
237
+ value,
238
+ type,
239
+ }: {
240
+ renderAnnotation: RenderAnnotationFunction
241
+ } & BlockAnnotationRenderProps) {
242
+ return renderAnnotation({
243
+ block,
244
+ children,
245
+ editorElementRef,
246
+ focused,
247
+ path,
248
+ schemaType,
249
+ selected,
250
+ value,
251
+ type,
252
+ })
253
+ }
254
+
255
+ function RenderDecorator({
256
+ renderDecorator,
257
+ children,
258
+ editorElementRef,
259
+ focused,
260
+ path,
261
+ schemaType,
262
+ selected,
263
+ value,
264
+ type,
265
+ }: {
266
+ renderDecorator: RenderDecoratorFunction
267
+ } & BlockDecoratorRenderProps) {
268
+ return renderDecorator({
269
+ children,
270
+ editorElementRef,
271
+ focused,
272
+ path,
273
+ schemaType,
274
+ selected,
275
+ value,
276
+ type,
277
+ })
278
+ }
279
+
280
+ function RenderChild({
281
+ renderChild,
282
+ annotations,
283
+ children,
284
+ editorElementRef,
285
+ focused,
286
+ path,
287
+ schemaType,
288
+ selected,
289
+ value,
290
+ type,
291
+ }: {
292
+ renderChild: RenderChildFunction
293
+ } & BlockChildRenderProps) {
294
+ return renderChild({
295
+ annotations,
296
+ children,
297
+ editorElementRef,
298
+ focused,
299
+ path,
300
+ schemaType,
301
+ selected,
302
+ value,
303
+ type,
304
+ })
305
+ }
@@ -8,6 +8,9 @@ import {
8
8
  } from 'slate-react'
9
9
  import type {EventPositionBlock} from '../../internal-utils/event-position'
10
10
  import type {
11
+ BlockListItemRenderProps,
12
+ BlockRenderProps,
13
+ BlockStyleRenderProps,
11
14
  PortableTextMemberSchemaTypes,
12
15
  RenderBlockFunction,
13
16
  RenderListItemFunction,
@@ -61,7 +64,8 @@ export function RenderTextBlock(props: {
61
64
 
62
65
  if (legacyStyleSchemaType) {
63
66
  children = (
64
- <props.renderStyle
67
+ <RenderStyle
68
+ renderStyle={props.renderStyle}
65
69
  block={props.textBlock}
66
70
  editorElementRef={blockRef}
67
71
  focused={focused}
@@ -71,7 +75,7 @@ export function RenderTextBlock(props: {
71
75
  value={props.textBlock.style}
72
76
  >
73
77
  {children}
74
- </props.renderStyle>
78
+ </RenderStyle>
75
79
  )
76
80
  } else {
77
81
  console.error(
@@ -87,7 +91,8 @@ export function RenderTextBlock(props: {
87
91
 
88
92
  if (legacyListItemSchemaType) {
89
93
  children = (
90
- <props.renderListItem
94
+ <RenderListItem
95
+ renderListItem={props.renderListItem}
91
96
  block={props.textBlock}
92
97
  editorElementRef={blockRef}
93
98
  focused={focused}
@@ -98,7 +103,7 @@ export function RenderTextBlock(props: {
98
103
  schemaType={legacyListItemSchemaType}
99
104
  >
100
105
  {children}
101
- </props.renderListItem>
106
+ </RenderListItem>
102
107
  )
103
108
  } else {
104
109
  console.error(
@@ -152,7 +157,8 @@ export function RenderTextBlock(props: {
152
157
  {dragPositionBlock === 'start' ? <DropIndicator /> : null}
153
158
  <div ref={blockRef}>
154
159
  {props.renderBlock ? (
155
- <props.renderBlock
160
+ <RenderBlock
161
+ renderBlock={props.renderBlock}
156
162
  editorElementRef={blockRef}
157
163
  focused={focused}
158
164
  level={props.textBlock.level}
@@ -165,7 +171,7 @@ export function RenderTextBlock(props: {
165
171
  value={props.textBlock}
166
172
  >
167
173
  {children}
168
- </props.renderBlock>
174
+ </RenderBlock>
169
175
  ) : (
170
176
  children
171
177
  )}
@@ -174,3 +180,86 @@ export function RenderTextBlock(props: {
174
180
  </div>
175
181
  )
176
182
  }
183
+
184
+ function RenderBlock({
185
+ renderBlock,
186
+ children,
187
+ editorElementRef,
188
+ focused,
189
+ level,
190
+ listItem,
191
+ path,
192
+ selected,
193
+ style,
194
+ schemaType,
195
+ type,
196
+ value,
197
+ }: {
198
+ renderBlock: RenderBlockFunction
199
+ } & BlockRenderProps) {
200
+ return renderBlock({
201
+ children,
202
+ editorElementRef,
203
+ focused,
204
+ level,
205
+ listItem,
206
+ path,
207
+ selected,
208
+ style,
209
+ schemaType,
210
+ type,
211
+ value,
212
+ })
213
+ }
214
+
215
+ function RenderListItem({
216
+ renderListItem,
217
+ block,
218
+ children,
219
+ editorElementRef,
220
+ focused,
221
+ level,
222
+ path,
223
+ schemaType,
224
+ selected,
225
+ value,
226
+ }: {
227
+ renderListItem: RenderListItemFunction
228
+ } & BlockListItemRenderProps) {
229
+ return renderListItem({
230
+ block,
231
+ children,
232
+ editorElementRef,
233
+ focused,
234
+ level,
235
+ path,
236
+ schemaType,
237
+ selected,
238
+ value,
239
+ })
240
+ }
241
+
242
+ function RenderStyle({
243
+ renderStyle,
244
+ block,
245
+ children,
246
+ editorElementRef,
247
+ focused,
248
+ path,
249
+ schemaType,
250
+ selected,
251
+ value,
252
+ }: {
253
+ renderStyle: RenderStyleFunction
254
+ } & BlockStyleRenderProps) {
255
+ return renderStyle({
256
+ block,
257
+ children,
258
+ editorElementRef,
259
+ focused,
260
+ path,
261
+ schemaType,
262
+ selected,
263
+ value,
264
+ })
265
+ }