@portabletext/editor 1.52.0 → 1.52.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.52.0",
3
+ "version": "1.52.2",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -80,15 +80,15 @@
80
80
  "slate-react": "0.114.2",
81
81
  "use-effect-event": "^2.0.0",
82
82
  "xstate": "^5.19.4",
83
- "@portabletext/block-tools": "1.1.29",
83
+ "@portabletext/block-tools": "1.1.30",
84
84
  "@portabletext/patches": "1.1.4"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@portabletext/toolkit": "^2.0.17",
88
88
  "@sanity/diff-match-patch": "^3.2.0",
89
- "@sanity/pkg-utils": "^7.2.3",
90
- "@sanity/schema": "^3.91.0",
91
- "@sanity/types": "^3.91.0",
89
+ "@sanity/pkg-utils": "^7.2.4",
90
+ "@sanity/schema": "^3.92.0",
91
+ "@sanity/types": "^3.92.0",
92
92
  "@testing-library/jest-dom": "^6.6.3",
93
93
  "@testing-library/react": "^16.3.0",
94
94
  "@types/debug": "^4.1.12",
@@ -99,8 +99,8 @@
99
99
  "@typescript-eslint/eslint-plugin": "^8.33.0",
100
100
  "@typescript-eslint/parser": "^8.33.0",
101
101
  "@vitejs/plugin-react": "^4.4.1",
102
- "@vitest/browser": "^3.2.1",
103
- "@vitest/coverage-istanbul": "^3.2.1",
102
+ "@vitest/browser": "^3.2.3",
103
+ "@vitest/coverage-istanbul": "^3.2.3",
104
104
  "babel-plugin-react-compiler": "19.1.0-rc.1",
105
105
  "eslint": "8.57.1",
106
106
  "eslint-plugin-react-hooks": "0.0.0-experimental-f9ae0a4c-20250527",
@@ -110,13 +110,13 @@
110
110
  "rxjs": "^7.8.2",
111
111
  "typescript": "5.8.3",
112
112
  "vite": "^6.2.5",
113
- "vitest": "^3.2.1",
113
+ "vitest": "^3.2.3",
114
114
  "vitest-browser-react": "^0.2.0",
115
115
  "racejar": "1.2.5"
116
116
  },
117
117
  "peerDependencies": {
118
- "@sanity/schema": "^3.91.0",
119
- "@sanity/types": "^3.91.0",
118
+ "@sanity/schema": "^3.92.0",
119
+ "@sanity/types": "^3.92.0",
120
120
  "react": "^16.9 || ^17 || ^18 || ^19",
121
121
  "rxjs": "^7.8.2"
122
122
  },
@@ -2,11 +2,7 @@ import {useSelector} from '@xstate/react'
2
2
  import {useContext, type ReactElement} from 'react'
3
3
  import type {Element as SlateElement} from 'slate'
4
4
  import type {RenderElementProps} from 'slate-react'
5
- import {
6
- parseBlockObject,
7
- parseInlineObject,
8
- parseTextBlock,
9
- } from '../../internal-utils/parse-blocks'
5
+ import {isTextBlock} from '../../internal-utils/parse-blocks'
10
6
  import type {
11
7
  RenderBlockFunction,
12
8
  RenderChildFunction,
@@ -35,22 +31,19 @@ export function RenderElement(props: {
35
31
  '__inline' in props.element && props.element.__inline === true
36
32
 
37
33
  if (isInline) {
38
- const inlineObject = parseInlineObject({
39
- context: {
40
- keyGenerator: () => '',
41
- schema,
42
- },
43
- options: {refreshKeys: false, validateFields: false},
44
- inlineObject: {
45
- _key: props.element._key,
46
- _type: props.element._type,
47
- ...('value' in props.element && typeof props.element.value === 'object'
48
- ? props.element.value
49
- : {}),
50
- },
51
- })
34
+ const inlineObject = {
35
+ _key: props.element._key,
36
+ _type: props.element._type,
37
+ ...('value' in props.element && typeof props.element.value === 'object'
38
+ ? props.element.value
39
+ : {}),
40
+ }
52
41
 
53
- if (!inlineObject) {
42
+ if (
43
+ !schema.inlineObjects.find(
44
+ (inlineObject) => inlineObject.name === props.element._type,
45
+ )
46
+ ) {
54
47
  console.error(
55
48
  `Unable to find Inline Object "${props.element._type}" in Schema`,
56
49
  )
@@ -74,16 +67,7 @@ export function RenderElement(props: {
74
67
  )
75
68
  }
76
69
 
77
- const textBlock = parseTextBlock({
78
- context: {
79
- keyGenerator: () => '',
80
- schema,
81
- },
82
- options: {refreshKeys: false, validateFields: false},
83
- block: props.element,
84
- })
85
-
86
- if (textBlock) {
70
+ if (isTextBlock({schema}, props.element)) {
87
71
  return (
88
72
  <RenderTextBlock
89
73
  attributes={props.attributes}
@@ -93,29 +77,26 @@ export function RenderElement(props: {
93
77
  renderListItem={props.renderListItem}
94
78
  renderStyle={props.renderStyle}
95
79
  spellCheck={props.spellCheck}
96
- textBlock={textBlock}
80
+ textBlock={props.element}
97
81
  >
98
82
  {props.children}
99
83
  </RenderTextBlock>
100
84
  )
101
85
  }
102
86
 
103
- const blockObject = parseBlockObject({
104
- context: {
105
- keyGenerator: () => '',
106
- schema,
107
- },
108
- options: {refreshKeys: false, validateFields: false},
109
- blockObject: {
110
- _key: props.element._key,
111
- _type: props.element._type,
112
- ...('value' in props.element && typeof props.element.value === 'object'
113
- ? props.element.value
114
- : {}),
115
- },
116
- })
87
+ const blockObject = {
88
+ _key: props.element._key,
89
+ _type: props.element._type,
90
+ ...('value' in props.element && typeof props.element.value === 'object'
91
+ ? props.element.value
92
+ : {}),
93
+ }
117
94
 
118
- if (!blockObject) {
95
+ if (
96
+ !schema.blockObjects.find(
97
+ (blockObject) => blockObject.name === props.element._type,
98
+ )
99
+ ) {
119
100
  console.error(
120
101
  `Unable to find Block Object "${props.element._type}" in Schema`,
121
102
  )
@@ -3,7 +3,7 @@ import type {
3
3
  PortableTextSpan,
4
4
  PortableTextTextBlock,
5
5
  } from '@sanity/types'
6
- import {Transforms, type Element} from 'slate'
6
+ import {Editor, Transforms, type Element} from 'slate'
7
7
  import {debugWithName} from '../../internal-utils/debug'
8
8
  import {
9
9
  isListBlock,
@@ -27,15 +27,31 @@ export function createWithSchemaTypes({
27
27
  editor: PortableTextSlateEditor,
28
28
  ): PortableTextSlateEditor {
29
29
  editor.isTextBlock = (value: unknown): value is PortableTextTextBlock => {
30
+ if (Editor.isEditor(value)) {
31
+ return false
32
+ }
33
+
30
34
  return isTextBlock(editorActor.getSnapshot().context, value)
31
35
  }
32
36
  editor.isTextSpan = (value: unknown): value is PortableTextSpan => {
37
+ if (Editor.isEditor(value)) {
38
+ return false
39
+ }
40
+
33
41
  return isSpan(editorActor.getSnapshot().context, value)
34
42
  }
35
43
  editor.isListBlock = (value: unknown): value is PortableTextListBlock => {
44
+ if (Editor.isEditor(value)) {
45
+ return false
46
+ }
47
+
36
48
  return isListBlock(editorActor.getSnapshot().context, value)
37
49
  }
38
50
  editor.isVoid = (element: Element): boolean => {
51
+ if (Editor.isEditor(element)) {
52
+ return false
53
+ }
54
+
39
55
  return (
40
56
  editorActor.getSnapshot().context.schema.block.name !== element._type &&
41
57
  (editorActor
@@ -49,6 +65,10 @@ export function createWithSchemaTypes({
49
65
  )
50
66
  }
51
67
  editor.isInline = (element: Element): boolean => {
68
+ if (Editor.isEditor(element)) {
69
+ return false
70
+ }
71
+
52
72
  const inlineSchemaTypes = editorActor
53
73
  .getSnapshot()
54
74
  .context.schema.inlineObjects.map((obj) => obj.name)
@@ -7,7 +7,6 @@ import {createWithMaxBlocks} from './createWithMaxBlocks'
7
7
  import {createWithObjectKeys} from './createWithObjectKeys'
8
8
  import {createWithPatches} from './createWithPatches'
9
9
  import {createWithPlaceholderBlock} from './createWithPlaceholderBlock'
10
- import {createWithPortableTextBlockStyle} from './createWithPortableTextBlockStyle'
11
10
  import {createWithPortableTextMarkModel} from './createWithPortableTextMarkModel'
12
11
  import {createWithPortableTextSelections} from './createWithPortableTextSelections'
13
12
  import {createWithSchemaTypes} from './createWithSchemaTypes'
@@ -49,8 +48,6 @@ export const withPlugins = <T extends Editor>(
49
48
  subscriptions: options.subscriptions,
50
49
  })
51
50
  const withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor)
52
- const withPortableTextBlockStyle =
53
- createWithPortableTextBlockStyle(editorActor)
54
51
 
55
52
  const withPlaceholderBlock = createWithPlaceholderBlock(editorActor)
56
53
 
@@ -66,19 +63,17 @@ export const withPlugins = <T extends Editor>(
66
63
  withSchemaTypes(
67
64
  withObjectKeys(
68
65
  withPortableTextMarkModel(
69
- withPortableTextBlockStyle(
70
- withPlaceholderBlock(
71
- withUtils(
72
- withMaxBlocks(
73
- withUndoRedo(
74
- withPatches(
75
- withPortableTextSelections(
76
- pluginUpdateValue(
66
+ withPlaceholderBlock(
67
+ withUtils(
68
+ withMaxBlocks(
69
+ withUndoRedo(
70
+ withPatches(
71
+ withPortableTextSelections(
72
+ pluginUpdateValue(
73
+ editorActor.getSnapshot().context,
74
+ pluginUpdateMarkState(
77
75
  editorActor.getSnapshot().context,
78
- pluginUpdateMarkState(
79
- editorActor.getSnapshot().context,
80
- e,
81
- ),
76
+ e,
82
77
  ),
83
78
  ),
84
79
  ),
@@ -1,3 +1,4 @@
1
+ import type {PortableTextSpan} from '@sanity/types'
1
2
  import type {EditorContext} from '../editor/editor-snapshot'
2
3
 
3
4
  export function createPlaceholderBlock(
@@ -14,7 +15,7 @@ export function createPlaceholderBlock(
14
15
  _key: context.keyGenerator(),
15
16
  text: '',
16
17
  marks: [],
17
- },
18
+ } as PortableTextSpan,
18
19
  ],
19
20
  }
20
21
  }
@@ -97,13 +97,19 @@ export function isTextBlock(
97
97
  context: Pick<EditorContext, 'schema'>,
98
98
  block: unknown,
99
99
  ): block is PortableTextTextBlock {
100
- return (
101
- parseTextBlock({
102
- block,
103
- context: {schema: context.schema, keyGenerator: () => ''},
104
- options: {refreshKeys: false, validateFields: false},
105
- }) !== undefined
106
- )
100
+ if (!isTypedObject(block)) {
101
+ return false
102
+ }
103
+
104
+ if (block._type !== context.schema.block.name) {
105
+ return false
106
+ }
107
+
108
+ if (!Array.isArray(block.children)) {
109
+ return false
110
+ }
111
+
112
+ return true
107
113
  }
108
114
 
109
115
  export function parseTextBlock({
@@ -249,14 +255,19 @@ export function isSpan(
249
255
  context: Pick<EditorContext, 'schema'>,
250
256
  child: unknown,
251
257
  ): child is PortableTextSpan {
252
- return (
253
- parseSpan({
254
- span: child,
255
- markDefKeyMap: new Map(),
256
- context: {schema: context.schema, keyGenerator: () => ''},
257
- options: {refreshKeys: false, validateFields: false},
258
- }) !== undefined
259
- )
258
+ if (!isTypedObject(child)) {
259
+ return false
260
+ }
261
+
262
+ if (child._type !== context.schema.span.name) {
263
+ return false
264
+ }
265
+
266
+ if (typeof child.text !== 'string') {
267
+ return false
268
+ }
269
+
270
+ return true
260
271
  }
261
272
 
262
273
  export function parseSpan({
@@ -1,4 +1,5 @@
1
1
  import {Transforms} from 'slate'
2
+ import {createPlaceholderBlock} from '../internal-utils/create-placeholder-block'
2
3
  import {toSlateRange} from '../internal-utils/ranges'
3
4
  import {getBlockPath} from '../internal-utils/slate-utils'
4
5
  import {getBlockKeyFromSelectionPoint} from '../selection/selection-point'
@@ -6,7 +7,7 @@ import type {BehaviorOperationImplementation} from './behavior.operations'
6
7
 
7
8
  export const deleteOperationImplementation: BehaviorOperationImplementation<
8
9
  'delete'
9
- > = ({operation}) => {
10
+ > = ({context, operation}) => {
10
11
  const anchorBlockKey = getBlockKeyFromSelectionPoint(operation.at.anchor)
11
12
  const focusBlockKey = getBlockKeyFromSelectionPoint(operation.at.focus)
12
13
 
@@ -36,6 +37,10 @@ export const deleteOperationImplementation: BehaviorOperationImplementation<
36
37
  at: [anchorBlockPath[0]],
37
38
  })
38
39
 
40
+ if (operation.editor.children.length === 0) {
41
+ Transforms.insertNodes(operation.editor, createPlaceholderBlock(context))
42
+ }
43
+
39
44
  return
40
45
  }
41
46
 
@@ -1,51 +0,0 @@
1
- import {Editor, Path, Text as SlateText, Transforms} from 'slate'
2
- import {debugWithName} from '../../internal-utils/debug'
3
- import type {PortableTextSlateEditor} from '../../types/editor'
4
- import type {EditorActor} from '../editor-machine'
5
-
6
- const debug = debugWithName('plugin:withPortableTextBlockStyle')
7
-
8
- export function createWithPortableTextBlockStyle(
9
- editorActor: EditorActor,
10
- ): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
11
- const defaultStyle = editorActor.getSnapshot().context.schema.styles[0].name
12
- return function withPortableTextBlockStyle(
13
- editor: PortableTextSlateEditor,
14
- ): PortableTextSlateEditor {
15
- // Extend Slate's default normalization to reset split node to normal style
16
- // if there is no text at the right end of the split.
17
- const {normalizeNode} = editor
18
-
19
- editor.normalizeNode = (nodeEntry) => {
20
- const [, path] = nodeEntry
21
-
22
- for (const op of editor.operations) {
23
- if (
24
- op.type === 'split_node' &&
25
- op.path.length === 1 &&
26
- editor.isTextBlock(op.properties) &&
27
- op.properties.style !== defaultStyle &&
28
- op.path[0] === path[0] &&
29
- !Path.equals(path, op.path)
30
- ) {
31
- const [child] = Editor.node(editor, [op.path[0] + 1, 0])
32
- if (SlateText.isText(child) && child.text === '') {
33
- debug(`Normalizing split node to ${defaultStyle} style`, op)
34
- editorActor.send({type: 'normalizing'})
35
- Transforms.setNodes(
36
- editor,
37
- {style: defaultStyle},
38
- {at: [op.path[0] + 1], voids: false},
39
- )
40
- editorActor.send({type: 'done normalizing'})
41
- return
42
- }
43
- }
44
- }
45
-
46
- normalizeNode(nodeEntry)
47
- }
48
-
49
- return editor
50
- }
51
- }