@portabletext/editor 1.0.10 → 1.0.11

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.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -53,16 +53,16 @@
53
53
  "@jest/globals": "^29.7.0",
54
54
  "@playwright/test": "1.45.3",
55
55
  "@portabletext/toolkit": "^2.0.15",
56
- "@sanity/block-tools": "^3.52.2",
56
+ "@sanity/block-tools": "^3.52.4",
57
57
  "@sanity/diff-match-patch": "^3.1.1",
58
58
  "@sanity/eslint-config-i18n": "^1.1.0",
59
59
  "@sanity/eslint-config-studio": "^4.0.0",
60
- "@sanity/pkg-utils": "^6.10.6",
61
- "@sanity/schema": "^3.52.2",
60
+ "@sanity/pkg-utils": "^6.10.8",
61
+ "@sanity/schema": "^3.52.4",
62
62
  "@sanity/test": "0.0.1-alpha.1",
63
- "@sanity/types": "^3.52.2",
63
+ "@sanity/types": "^3.52.4",
64
64
  "@sanity/ui": "^2.8.8",
65
- "@sanity/util": "^3.52.2",
65
+ "@sanity/util": "^3.52.4",
66
66
  "@testing-library/react": "^13.4.0",
67
67
  "@types/debug": "^4.1.5",
68
68
  "@types/express": "^4.17.21",
@@ -99,7 +99,7 @@
99
99
  "rxjs": "^7.8.1",
100
100
  "styled-components": "^6.1.12",
101
101
  "tsx": "^4.16.2",
102
- "typescript": "^5.5.3",
102
+ "typescript": "5.5.4",
103
103
  "vite": "^4.5.3"
104
104
  },
105
105
  "peerDependencies": {
@@ -0,0 +1,229 @@
1
+ import {describe, expect, jest, test} from '@jest/globals'
2
+ import {Schema} from '@sanity/schema'
3
+ import {type PortableTextBlock} from '@sanity/types'
4
+ import {render, waitFor} from '@testing-library/react'
5
+ import {createRef, type RefObject} from 'react'
6
+
7
+ import {type EditorChange, type EditorSelection} from '../../types/editor'
8
+ import {PortableTextEditable} from '../Editable'
9
+ import {PortableTextEditor} from '../PortableTextEditor'
10
+
11
+ const schema = Schema.compile({
12
+ types: [
13
+ {name: 'portable-text', type: 'array', of: [{type: 'block'}, {type: 'image'}]},
14
+ {name: 'image', type: 'object'},
15
+ ],
16
+ }).get('portable-text')
17
+
18
+ describe(PortableTextEditor.insertBlock.name, () => {
19
+ test('Scenario: Inserting a custom block without a selection #1', async () => {
20
+ const editorRef: RefObject<PortableTextEditor> = createRef()
21
+ const emptyTextBlock: PortableTextBlock = {
22
+ _key: 'ba',
23
+ _type: 'block',
24
+ children: [
25
+ {
26
+ _type: 'span',
27
+ _key: 'sa',
28
+ text: '',
29
+ marks: [],
30
+ },
31
+ ],
32
+ style: 'normal',
33
+ }
34
+ const initialValue: Array<PortableTextBlock> = [emptyTextBlock]
35
+ const onChange: (change: EditorChange) => void = jest.fn()
36
+
37
+ render(
38
+ <PortableTextEditor
39
+ ref={editorRef}
40
+ schemaType={schema}
41
+ value={initialValue}
42
+ keyGenerator={() => 'bb'}
43
+ onChange={onChange}
44
+ >
45
+ <PortableTextEditable />
46
+ </PortableTextEditor>,
47
+ )
48
+
49
+ // Given an empty text block
50
+ await waitFor(() => {
51
+ if (editorRef.current) {
52
+ expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
53
+ expect(onChange).toHaveBeenCalledWith({type: 'ready'})
54
+ }
55
+ })
56
+
57
+ // And no selection
58
+ await waitFor(() => {
59
+ if (editorRef.current) {
60
+ expect(PortableTextEditor.getSelection(editorRef.current)).toBeNull()
61
+ }
62
+ })
63
+
64
+ // When a new image is inserted
65
+ await waitFor(() => {
66
+ if (editorRef.current) {
67
+ const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
68
+ (object) => object.name === 'image',
69
+ )!
70
+ PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
71
+ }
72
+ })
73
+
74
+ // Then the empty text block is replaced with the new image
75
+ await waitFor(() => {
76
+ if (editorRef.current) {
77
+ expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
78
+ {_key: 'bb', _type: 'image'},
79
+ ])
80
+ }
81
+ })
82
+ })
83
+
84
+ test('Scenario: Inserting a custom block without a selection #2', async () => {
85
+ const editorRef: RefObject<PortableTextEditor> = createRef()
86
+ const nonEmptyTextBlock: PortableTextBlock = {
87
+ _key: 'ba',
88
+ _type: 'block',
89
+ children: [
90
+ {
91
+ _type: 'span',
92
+ _key: 'xs',
93
+ text: 'foo',
94
+ marks: [],
95
+ },
96
+ ],
97
+ style: 'normal',
98
+ }
99
+ const initialValue: Array<PortableTextBlock> = [nonEmptyTextBlock]
100
+ const onChange: (change: EditorChange) => void = jest.fn()
101
+
102
+ render(
103
+ <PortableTextEditor
104
+ ref={editorRef}
105
+ schemaType={schema}
106
+ value={initialValue}
107
+ keyGenerator={() => 'bb'}
108
+ onChange={onChange}
109
+ >
110
+ <PortableTextEditable />
111
+ </PortableTextEditor>,
112
+ )
113
+
114
+ // Given an non-empty text block
115
+ await waitFor(() => {
116
+ if (editorRef.current) {
117
+ expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
118
+ expect(onChange).toHaveBeenCalledWith({type: 'ready'})
119
+ }
120
+ })
121
+
122
+ // And no selection
123
+ await waitFor(() => {
124
+ if (editorRef.current) {
125
+ expect(PortableTextEditor.getSelection(editorRef.current)).toBeNull()
126
+ }
127
+ })
128
+
129
+ // When a new image is inserted
130
+ await waitFor(() => {
131
+ if (editorRef.current) {
132
+ const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
133
+ (object) => object.name === 'image',
134
+ )!
135
+ PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
136
+ }
137
+ })
138
+
139
+ // Then the empty text block is replaced with the new image
140
+ await waitFor(() => {
141
+ if (editorRef.current) {
142
+ expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
143
+ nonEmptyTextBlock,
144
+ {_key: 'bb', _type: 'image'},
145
+ ])
146
+ }
147
+ })
148
+ })
149
+
150
+ test('Scenario: Replacing an empty text block with a custom block', async () => {
151
+ const editorRef: RefObject<PortableTextEditor> = createRef()
152
+ const emptyTextBlock: PortableTextBlock = {
153
+ _key: 'ba',
154
+ _type: 'block',
155
+ children: [
156
+ {
157
+ _type: 'span',
158
+ _key: 'sa',
159
+ text: '',
160
+ marks: [],
161
+ },
162
+ ],
163
+ style: 'normal',
164
+ }
165
+ const imageBlock: PortableTextBlock = {
166
+ _key: 'bb',
167
+ _type: 'image',
168
+ }
169
+ const initialValue: Array<PortableTextBlock> = [emptyTextBlock, imageBlock]
170
+ const onChange: (change: EditorChange) => void = jest.fn()
171
+
172
+ render(
173
+ <PortableTextEditor
174
+ ref={editorRef}
175
+ schemaType={schema}
176
+ value={initialValue}
177
+ keyGenerator={() => 'bc'}
178
+ onChange={onChange}
179
+ >
180
+ <PortableTextEditable />
181
+ </PortableTextEditor>,
182
+ )
183
+
184
+ // Given an empty text block followed by an image
185
+ await waitFor(() => {
186
+ if (editorRef.current) {
187
+ expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
188
+ expect(onChange).toHaveBeenCalledWith({type: 'ready'})
189
+ }
190
+ })
191
+
192
+ // And a selection in the empty text block
193
+ const initialSelection: EditorSelection = {
194
+ anchor: {path: [{_key: 'ba'}, 'children', {_key: 'sa'}], offset: 0},
195
+ focus: {path: [{_key: 'ba'}, 'children', {_key: 'sa'}], offset: 0},
196
+ backward: false,
197
+ }
198
+ await waitFor(() => {
199
+ if (editorRef.current) {
200
+ PortableTextEditor.select(editorRef.current, initialSelection)
201
+ }
202
+ })
203
+ await waitFor(() => {
204
+ if (editorRef.current) {
205
+ expect(onChange).toHaveBeenCalledWith({type: 'selection', selection: initialSelection})
206
+ }
207
+ })
208
+
209
+ // When a new image is inserted
210
+ await waitFor(() => {
211
+ if (editorRef.current) {
212
+ const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
213
+ (object) => object.name === 'image',
214
+ )!
215
+ PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
216
+ }
217
+ })
218
+
219
+ // Then the empty text block is replaced with the new image
220
+ await waitFor(() => {
221
+ if (editorRef.current) {
222
+ expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
223
+ {_key: 'bc', _type: 'image'},
224
+ {_key: 'bb', _type: 'image'},
225
+ ])
226
+ }
227
+ })
228
+ })
229
+ })
@@ -160,9 +160,6 @@ export function createWithEditableAPI(
160
160
  )
161
161
  },
162
162
  insertBlock: (type: SchemaType, value?: {[prop: string]: any}): Path => {
163
- if (!editor.selection) {
164
- throw new Error('The editor has no selection')
165
- }
166
163
  const block = toSlateValue(
167
164
  [
168
165
  {
@@ -173,22 +170,52 @@ export function createWithEditableAPI(
173
170
  ],
174
171
  portableTextEditor,
175
172
  )[0] as unknown as Node
176
- const [focusBlock] = Array.from(
173
+
174
+ if (!editor.selection) {
175
+ const lastBlock = Array.from(
176
+ Editor.nodes(editor, {
177
+ match: (n) => !Editor.isEditor(n),
178
+ at: [],
179
+ reverse: true,
180
+ }),
181
+ )[0]
182
+
183
+ // If there is no selection, let's just insert the new block at the
184
+ // end of the document
185
+ Editor.insertNode(editor, block)
186
+
187
+ if (lastBlock && isEqualToEmptyEditor([lastBlock[0]], types)) {
188
+ // And if the last block was an empty text block, let's remove
189
+ // that too
190
+ Transforms.removeNodes(editor, {at: lastBlock[1]})
191
+ }
192
+
193
+ editor.onChange()
194
+
195
+ return (
196
+ toPortableTextRange(
197
+ fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
198
+ editor.selection,
199
+ types,
200
+ )?.focus.path ?? []
201
+ )
202
+ }
203
+
204
+ const focusBlock = Array.from(
177
205
  Editor.nodes(editor, {
178
206
  at: editor.selection.focus.path.slice(0, 1),
179
207
  match: (n) => n._type === types.block.name,
180
208
  }),
181
- )[0] || [undefined]
209
+ )[0]
182
210
 
183
- const isEmptyTextBlock = focusBlock && isEqualToEmptyEditor([focusBlock], types)
211
+ Editor.insertNode(editor, block)
184
212
 
185
- if (isEmptyTextBlock) {
186
- // If the text block is empty, remove it before inserting the new block.
187
- Transforms.removeNodes(editor, {at: editor.selection})
213
+ if (focusBlock && isEqualToEmptyEditor([focusBlock[0]], types)) {
214
+ Transforms.removeNodes(editor, {at: focusBlock[1]})
188
215
  }
189
216
 
190
- Editor.insertNode(editor, block)
191
217
  editor.onChange()
218
+
192
219
  return (
193
220
  toPortableTextRange(
194
221
  fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
@@ -308,6 +335,10 @@ export function createWithEditableAPI(
308
335
  }),
309
336
  ]
310
337
 
338
+ if (spans.length === 0) {
339
+ return false
340
+ }
341
+
311
342
  if (
312
343
  spans.some(
313
344
  ([span]) => !isPortableTextSpan(span) || !span.marks || span.marks?.length === 0,