@portabletext/editor 1.47.4 → 1.47.6

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": "1.47.4",
3
+ "version": "1.47.6",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -16,7 +16,7 @@ import {
16
16
  type MutableRefObject,
17
17
  type TextareaHTMLAttributes,
18
18
  } from 'react'
19
- import {Transforms, type Text} from 'slate'
19
+ import {Editor, Transforms, type Text} from 'slate'
20
20
  import {
21
21
  ReactEditor,
22
22
  Editable as SlateEditable,
@@ -59,6 +59,7 @@ import {EditorActorContext} from './editor-actor-context'
59
59
  import {getEditorSnapshot} from './editor-selector'
60
60
  import {usePortableTextEditor} from './hooks/usePortableTextEditor'
61
61
  import {createWithHotkeys} from './plugins/createWithHotKeys'
62
+ import {PortableTextEditor} from './PortableTextEditor'
62
63
  import {
63
64
  createDecorate,
64
65
  rangeDecorationsMachine,
@@ -570,17 +571,16 @@ export const PortableTextEditable = forwardRef<
570
571
  onFocus(event)
571
572
  }
572
573
  if (!event.isDefaultPrevented()) {
574
+ const selection = PortableTextEditor.getSelection(portableTextEditor)
575
+ // Create an editor selection if it does'nt exist
576
+ if (selection === null) {
577
+ Transforms.select(slateEditor, Editor.start(slateEditor, []))
578
+ slateEditor.onChange()
579
+ }
573
580
  editorActor.send({type: 'notify.focused', event})
574
-
575
- const selection = slateEditor.selection
576
- ? slateRangeToSelection({
577
- schema: editorActor.getSnapshot().context.schema,
578
- editor: slateEditor,
579
- range: slateEditor.selection,
580
- })
581
- : null
582
-
583
- if (selection) {
581
+ const newSelection = PortableTextEditor.getSelection(portableTextEditor)
582
+ // If the selection is the same, emit it explicitly here as there is no actual onChange event triggered.
583
+ if (selection === newSelection) {
584
584
  editorActor.send({
585
585
  type: 'notify.selection',
586
586
  selection,
@@ -588,7 +588,7 @@ export const PortableTextEditable = forwardRef<
588
588
  }
589
589
  }
590
590
  },
591
- [editorActor, onFocus, slateEditor],
591
+ [editorActor, onFocus, slateEditor, portableTextEditor],
592
592
  )
593
593
 
594
594
  const handleClick = useCallback(
@@ -379,7 +379,6 @@ describe(parseSpan.name, () => {
379
379
  _type: 'span',
380
380
  text: '',
381
381
  marks: [],
382
- foo: 'bar',
383
382
  })
384
383
  })
385
384
 
@@ -169,8 +169,7 @@ function parseTextBlock({
169
169
  .filter((child) => child !== undefined)
170
170
 
171
171
  const parsedBlock: PortableTextTextBlock = {
172
- // Spread the entire block to allow custom properties on it
173
- ...block,
172
+ _type: context.schema.block.name,
174
173
  _key,
175
174
  children:
176
175
  children.length > 0
@@ -186,37 +185,30 @@ function parseTextBlock({
186
185
  markDefs,
187
186
  }
188
187
 
189
- /**
190
- * Reset text block .style if it's somehow set to an invalid type
191
- */
192
188
  if (
193
- typeof parsedBlock.style !== 'string' ||
194
- !context.schema.styles.find((style) => style.name === block.style)
189
+ typeof block.style === 'string' &&
190
+ context.schema.styles.find((style) => style.name === block.style)
195
191
  ) {
192
+ parsedBlock.style = block.style
193
+ } else {
196
194
  const defaultStyle = context.schema.styles.at(0)?.name
197
195
 
198
196
  if (defaultStyle !== undefined) {
199
197
  parsedBlock.style = defaultStyle
200
198
  } else {
201
- delete parsedBlock.style
199
+ console.error('Expected default style')
202
200
  }
203
201
  }
204
202
 
205
- /**
206
- * Reset text block .listItem if it's somehow set to an invalid type
207
- */
208
203
  if (
209
- typeof parsedBlock.listItem !== 'string' ||
210
- !context.schema.lists.find((list) => list.name === block.listItem)
204
+ typeof block.listItem === 'string' &&
205
+ context.schema.lists.find((list) => list.name === block.listItem)
211
206
  ) {
212
- delete parsedBlock.listItem
207
+ parsedBlock.listItem = block.listItem
213
208
  }
214
209
 
215
- /**
216
- * Reset text block .level if it's somehow set to an invalid type
217
- */
218
- if (typeof parsedBlock.level !== 'number') {
219
- delete parsedBlock.level
210
+ if (typeof block.level === 'number') {
211
+ parsedBlock.level = block.level
220
212
  }
221
213
 
222
214
  return parsedBlock
@@ -280,8 +272,6 @@ export function parseSpan({
280
272
  })
281
273
 
282
274
  return {
283
- // Spread the entire span to allow custom properties on it
284
- ...span,
285
275
  _type: 'span',
286
276
  _key: options.refreshKeys
287
277
  ? context.keyGenerator()