@portabletext/editor 1.50.2 → 1.50.4

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.
Files changed (33) hide show
  1. package/lib/behaviors/index.d.cts +1 -0
  2. package/lib/behaviors/index.d.ts +1 -0
  3. package/lib/index.cjs +577 -286
  4. package/lib/index.cjs.map +1 -1
  5. package/lib/index.d.cts +15 -2
  6. package/lib/index.d.ts +15 -2
  7. package/lib/index.js +584 -292
  8. package/lib/index.js.map +1 -1
  9. package/lib/plugins/index.d.cts +7 -0
  10. package/lib/plugins/index.d.ts +7 -0
  11. package/lib/selectors/index.d.cts +1 -0
  12. package/lib/selectors/index.d.ts +1 -0
  13. package/lib/utils/index.d.cts +1 -0
  14. package/lib/utils/index.d.ts +1 -0
  15. package/package.json +14 -13
  16. package/src/editor/PortableTextEditor.tsx +22 -22
  17. package/src/editor/create-slate-editor.tsx +9 -1
  18. package/src/editor/editor-selector.ts +1 -5
  19. package/src/editor/editor-snapshot.ts +1 -3
  20. package/src/editor/plugins/createWithPatches.ts +37 -75
  21. package/src/editor/plugins/slate-plugin.update-value.ts +30 -0
  22. package/src/editor/plugins/with-plugins.ts +8 -4
  23. package/src/editor/relay-machine.ts +9 -0
  24. package/src/internal-utils/apply-operation-to-portable-text.test.ts +175 -0
  25. package/src/internal-utils/apply-operation-to-portable-text.ts +435 -0
  26. package/src/internal-utils/create-placeholder-block.ts +20 -0
  27. package/src/internal-utils/{__tests__/operationToPatches.test.ts → operation-to-patches.test.ts} +44 -39
  28. package/src/internal-utils/operation-to-patches.ts +467 -0
  29. package/src/internal-utils/portable-text-node.ts +209 -0
  30. package/src/types/editor.ts +8 -2
  31. package/src/internal-utils/__tests__/patchToOperations.test.ts +0 -312
  32. package/src/internal-utils/operationToPatches.ts +0 -489
  33. package/src/internal-utils/slate-children-to-blocks.ts +0 -49
@@ -0,0 +1,209 @@
1
+ import type {EditorSchema} from '../editor/editor-schema'
2
+ import {isTypedObject} from './asserters'
3
+
4
+ type Path = Array<number>
5
+
6
+ export type PortableTextNode<TEditorSchema extends EditorSchema> =
7
+ | EditorNode<TEditorSchema>
8
+ | TextBlockNode<TEditorSchema>
9
+ | SpanNode<TEditorSchema>
10
+ | PartialSpanNode
11
+ | ObjectNode
12
+
13
+ //////////
14
+
15
+ export type EditorNode<TEditorSchema extends EditorSchema> = {
16
+ children: Array<TextBlockNode<TEditorSchema> | ObjectNode>
17
+ }
18
+
19
+ export function isEditorNode<TEditorSchema extends EditorSchema>(
20
+ node: unknown,
21
+ ): node is EditorNode<TEditorSchema> {
22
+ if (typeof node === 'object' && node !== null) {
23
+ return (
24
+ !('_type' in node) && 'children' in node && Array.isArray(node.children)
25
+ )
26
+ }
27
+
28
+ return false
29
+ }
30
+
31
+ //////////
32
+
33
+ export type TextBlockNode<TEditorSchema extends EditorSchema> = {
34
+ _key: string
35
+ _type: TEditorSchema['block']['name']
36
+ children: Array<SpanNode<TEditorSchema> | ObjectNode>
37
+ [other: string]: unknown
38
+ }
39
+
40
+ export function isTextBlockNode<TEditorSchema extends EditorSchema>(
41
+ context: {schema: TEditorSchema},
42
+ node: unknown,
43
+ ): node is TextBlockNode<TEditorSchema> {
44
+ return isTypedObject(node) && node._type === context.schema.block.name
45
+ }
46
+
47
+ //////////
48
+
49
+ export type SpanNode<TEditorSchema extends EditorSchema> = {
50
+ _key: string
51
+ _type?: TEditorSchema['span']['name']
52
+ text: string
53
+ [other: string]: unknown
54
+ }
55
+
56
+ export function isSpanNode<TEditorSchema extends EditorSchema>(
57
+ context: {schema: TEditorSchema},
58
+ node: unknown,
59
+ ): node is SpanNode<TEditorSchema> {
60
+ if (typeof node !== 'object' || node === null) {
61
+ return false
62
+ }
63
+
64
+ if ('children' in node) {
65
+ return false
66
+ }
67
+
68
+ if ('_type' in node) {
69
+ return node._type === context.schema.span.name
70
+ }
71
+
72
+ return 'text' in node
73
+ }
74
+
75
+ //////////
76
+
77
+ export type PartialSpanNode = {
78
+ text: string
79
+ [other: string]: unknown
80
+ }
81
+
82
+ export function isPartialSpanNode(node: unknown): node is PartialSpanNode {
83
+ return (
84
+ typeof node === 'object' &&
85
+ node !== null &&
86
+ 'text' in node &&
87
+ typeof node.text === 'string'
88
+ )
89
+ }
90
+
91
+ //////////
92
+
93
+ export type ObjectNode = {
94
+ _type: string
95
+ _key: string
96
+ [other: string]: unknown
97
+ }
98
+
99
+ export function isObjectNode(
100
+ context: {schema: EditorSchema},
101
+ node: unknown,
102
+ ): node is ObjectNode {
103
+ return (
104
+ !isEditorNode(node) &&
105
+ !isTextBlockNode(context, node) &&
106
+ !isSpanNode(context, node) &&
107
+ !isPartialSpanNode(node)
108
+ )
109
+ }
110
+
111
+ /**
112
+ *
113
+ */
114
+ export function getBlock<TEditorSchema extends EditorSchema>(
115
+ root: EditorNode<TEditorSchema>,
116
+ path: Path,
117
+ ): TextBlockNode<TEditorSchema> | ObjectNode | undefined {
118
+ const index = path.at(0)
119
+
120
+ if (index === undefined || path.length !== 1) {
121
+ return undefined
122
+ }
123
+
124
+ return root.children.at(index)
125
+ }
126
+
127
+ /**
128
+ * A "node" can either be
129
+ * 1. The root (path length is 0)
130
+ * 2. A block (path length is 1)
131
+ * 3. A span (path length is 2)
132
+ * 4. Or an inline object (path length is 2)
133
+ */
134
+ export function getNode<TEditorSchema extends EditorSchema>(
135
+ context: {schema: TEditorSchema},
136
+ root: EditorNode<TEditorSchema>,
137
+ path: Path,
138
+ ): PortableTextNode<TEditorSchema> | undefined {
139
+ if (path.length === 0) {
140
+ return root
141
+ }
142
+
143
+ if (path.length === 1) {
144
+ return getBlock(root, path)
145
+ }
146
+
147
+ if (path.length === 2) {
148
+ const block = getBlock(root, path.slice(0, 1))
149
+
150
+ if (!block || !isTextBlockNode(context, block)) {
151
+ return undefined
152
+ }
153
+
154
+ const child = block.children.at(path[1])
155
+
156
+ if (!child) {
157
+ return undefined
158
+ }
159
+
160
+ return child
161
+ }
162
+ }
163
+
164
+ export function getSpan<TEditorSchema extends EditorSchema>(
165
+ context: {schema: TEditorSchema},
166
+ root: EditorNode<TEditorSchema>,
167
+ path: Path,
168
+ ) {
169
+ const node = getNode(context, root, path)
170
+
171
+ if (node && isSpanNode(context, node)) {
172
+ return node
173
+ }
174
+
175
+ return undefined
176
+ }
177
+
178
+ /**
179
+ * A parent can either be the root or a text block
180
+ */
181
+ export function getParent<TEditorSchema extends EditorSchema>(
182
+ context: {schema: TEditorSchema},
183
+ root: EditorNode<TEditorSchema>,
184
+ path: Path,
185
+ ) {
186
+ if (path.length === 0) {
187
+ return undefined
188
+ }
189
+
190
+ const parentPath = path.slice(0, -1)
191
+
192
+ if (parentPath.length === 0) {
193
+ return root
194
+ }
195
+
196
+ const blockIndex = parentPath.at(0)
197
+
198
+ if (blockIndex === undefined || parentPath.length !== 1) {
199
+ return undefined
200
+ }
201
+
202
+ const block = root.children.at(blockIndex)
203
+
204
+ if (block && isTextBlockNode(context, block)) {
205
+ return block
206
+ }
207
+
208
+ return undefined
209
+ }
@@ -124,6 +124,7 @@ export interface PortableTextSlateEditor extends ReactEditor {
124
124
  isTextBlock: (value: unknown) => value is PortableTextTextBlock
125
125
  isTextSpan: (value: unknown) => value is PortableTextSpan
126
126
  isListBlock: (value: unknown) => value is PortableTextListBlock
127
+ value: Array<PortableTextBlock>
127
128
 
128
129
  /**
129
130
  * Use hotkeys
@@ -191,7 +192,10 @@ export type FocusChange = {
191
192
  event: FocusEvent<HTMLDivElement, Element>
192
193
  }
193
194
 
194
- /** @beta */
195
+ /**
196
+ * @beta
197
+ * @deprecated Use `'patch'` changes instead
198
+ */
195
199
  export type UnsetChange = {
196
200
  type: 'unset'
197
201
  previousValue: PortableTextBlock[]
@@ -208,7 +212,9 @@ export type BlurChange = {
208
212
  /**
209
213
  * The editor is currently loading something
210
214
  * Could be used to show a spinner etc.
211
- * @beta */
215
+ * @beta
216
+ * @deprecated
217
+ */
212
218
  export type LoadingChange = {
213
219
  type: 'loading'
214
220
  isLoading: boolean
@@ -1,312 +0,0 @@
1
- import type {Patch} from '@portabletext/patches'
2
- import {createEditor, type Descendant} from 'slate'
3
- import {beforeEach, describe, expect, it} from 'vitest'
4
- import {createActor} from 'xstate'
5
- import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
6
- import {editorMachine} from '../../editor/editor-machine'
7
- import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
8
- import {defaultKeyGenerator} from '../../editor/key-generator'
9
- import {createLegacySchema} from '../../editor/legacy-schema'
10
- import {withPlugins} from '../../editor/plugins/with-plugins'
11
- import {relayMachine} from '../../editor/relay-machine'
12
- import {createApplyPatch} from '../applyPatch'
13
- import {VOID_CHILD_KEY} from '../values'
14
-
15
- const legacySchema = createLegacySchema(schemaType)
16
- const schemaTypes = legacySchemaToEditorSchema(legacySchema)
17
-
18
- const patchToOperations = createApplyPatch(schemaTypes)
19
-
20
- const editor = withPlugins(createEditor(), {
21
- editorActor: createActor(editorMachine, {
22
- input: {
23
- schema: schemaTypes,
24
- keyGenerator: defaultKeyGenerator,
25
- getLegacySchema: () => legacySchema,
26
- },
27
- }),
28
- relayActor: createActor(relayMachine),
29
- subscriptions: [],
30
- })
31
-
32
- const createDefaultValue = (): Descendant[] => [
33
- {
34
- _type: 'image',
35
- _key: 'c01739b0d03b',
36
- children: [
37
- {
38
- _key: VOID_CHILD_KEY,
39
- _type: 'span',
40
- text: '',
41
- marks: [],
42
- },
43
- ],
44
- __inline: false,
45
- value: {
46
- asset: {
47
- _ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
48
- _type: 'reference',
49
- },
50
- },
51
- },
52
- ]
53
-
54
- describe('operationToPatches', () => {
55
- beforeEach(() => {
56
- editor.onChange()
57
- })
58
-
59
- it('makes the correct operations for block objects', () => {
60
- editor.children = createDefaultValue()
61
- const patches = [
62
- {
63
- type: 'unset',
64
- path: [{_key: 'c01739b0d03b'}, 'hotspot'],
65
- origin: 'remote',
66
- },
67
- {type: 'unset', path: [{_key: 'c01739b0d03b'}, 'crop'], origin: 'remote'},
68
- {
69
- type: 'set',
70
- path: [{_key: 'c01739b0d03b'}, 'asset'],
71
- value: {
72
- _ref: 'image-b5681d9d0b2b6c922238e7c694500dd7c1349b19-256x256-jpg',
73
- _type: 'reference',
74
- },
75
- origin: 'remote',
76
- },
77
- ] as Patch[]
78
- patches.forEach((p) => {
79
- patchToOperations(editor, p)
80
- })
81
- expect(editor.children).toEqual([
82
- {
83
- __inline: false,
84
- _key: 'c01739b0d03b',
85
- _type: 'image',
86
- children: [
87
- {
88
- _key: VOID_CHILD_KEY,
89
- _type: 'span',
90
- marks: [],
91
- text: '',
92
- },
93
- ],
94
- value: {
95
- asset: {
96
- _ref: 'image-b5681d9d0b2b6c922238e7c694500dd7c1349b19-256x256-jpg',
97
- _type: 'reference',
98
- },
99
- },
100
- },
101
- ])
102
- })
103
- it('will not create operations for insertion inside blocks', () => {
104
- editor.children = [
105
- {
106
- _type: 'someType',
107
- _key: 'c01739b0d03b',
108
- children: [
109
- {
110
- _key: VOID_CHILD_KEY,
111
- _type: 'span',
112
- text: '',
113
- marks: [],
114
- },
115
- ],
116
- __inline: false,
117
- value: {
118
- asset: {
119
- _ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
120
- _type: 'reference',
121
- },
122
- nestedArray: [],
123
- },
124
- },
125
- ]
126
- const patches = [
127
- {
128
- type: 'insert',
129
- path: [{_key: 'c01739b0d03b'}, 'nestedArray'],
130
- origin: 'remote',
131
- },
132
- ] as Patch[]
133
- patches.forEach((p) => {
134
- patchToOperations(editor, p)
135
- })
136
- expect(editor.children).toMatchInlineSnapshot(`
137
- [
138
- {
139
- "__inline": false,
140
- "_key": "c01739b0d03b",
141
- "_type": "someType",
142
- "children": [
143
- {
144
- "_key": "${VOID_CHILD_KEY}",
145
- "_type": "span",
146
- "marks": [],
147
- "text": "",
148
- },
149
- ],
150
- "value": {
151
- "asset": {
152
- "_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
153
- "_type": "reference",
154
- },
155
- "nestedArray": [],
156
- },
157
- },
158
- ]
159
- `)
160
- })
161
- it('will not create operations for removal inside blocks', () => {
162
- editor.children = [
163
- {
164
- _type: 'someType',
165
- _key: 'c01739b0d03b',
166
- children: [
167
- {
168
- _key: VOID_CHILD_KEY,
169
- _type: 'span',
170
- text: '',
171
- marks: [],
172
- },
173
- ],
174
- __inline: false,
175
- value: {
176
- asset: {
177
- _ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
178
- _type: 'reference',
179
- },
180
- nestedArray: [
181
- {
182
- _key: 'foo',
183
- _type: 'nestedValue',
184
- },
185
- ],
186
- },
187
- },
188
- ]
189
- const patches = [
190
- {
191
- type: 'unset',
192
- path: [{_key: 'c01739b0d03b'}, 'nestedArray', 0],
193
- origin: 'remote',
194
- },
195
- ] as Patch[]
196
- patches.forEach((p) => {
197
- patchToOperations(editor, p)
198
- })
199
- expect(editor.children).toMatchInlineSnapshot(`
200
- [
201
- {
202
- "__inline": false,
203
- "_key": "c01739b0d03b",
204
- "_type": "someType",
205
- "children": [
206
- {
207
- "_key": "${VOID_CHILD_KEY}",
208
- "_type": "span",
209
- "marks": [],
210
- "text": "",
211
- },
212
- ],
213
- "value": {
214
- "asset": {
215
- "_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
216
- "_type": "reference",
217
- },
218
- "nestedArray": [
219
- {
220
- "_key": "foo",
221
- "_type": "nestedValue",
222
- },
223
- ],
224
- },
225
- },
226
- ]
227
- `)
228
- })
229
- it('will not create operations for setting data inside blocks', () => {
230
- editor.children = [
231
- {
232
- _key: '1335959d4d03',
233
- _type: 'block',
234
- children: [
235
- {
236
- _key: '9bd868adcd6b',
237
- _type: 'span',
238
- marks: [],
239
- text: '1 ',
240
- },
241
- {
242
- _key: '6f75d593f3fc',
243
- _type: 'span',
244
- marks: ['11de7fcea659'],
245
- text: '2',
246
- },
247
- {
248
- _key: '033618a7f081',
249
- _type: 'span',
250
- marks: [],
251
- text: ' 3',
252
- },
253
- ],
254
- markDefs: [
255
- {
256
- _key: '11de7fcea659',
257
- _type: 'link',
258
- },
259
- ],
260
- style: 'normal',
261
- },
262
- ]
263
- const patches = [
264
- {
265
- type: 'set',
266
- path: [{_key: '1335959d4d03'}, 'markDefs', {_key: '11de7fcea659'}],
267
- origin: 'remote',
268
- value: {href: 'http://www.test.com'},
269
- },
270
- ] as Patch[]
271
- patches.forEach((p) => {
272
- patchToOperations(editor, p)
273
- })
274
- expect(editor.children).toMatchInlineSnapshot(`
275
- [
276
- {
277
- "_key": "1335959d4d03",
278
- "_type": "block",
279
- "children": [
280
- {
281
- "_key": "9bd868adcd6b",
282
- "_type": "span",
283
- "marks": [],
284
- "text": "1 ",
285
- },
286
- {
287
- "_key": "6f75d593f3fc",
288
- "_type": "span",
289
- "marks": [
290
- "11de7fcea659",
291
- ],
292
- "text": "2",
293
- },
294
- {
295
- "_key": "033618a7f081",
296
- "_type": "span",
297
- "marks": [],
298
- "text": " 3",
299
- },
300
- ],
301
- "markDefs": [
302
- {
303
- "_key": "11de7fcea659",
304
- "_type": "link",
305
- },
306
- ],
307
- "style": "normal",
308
- },
309
- ]
310
- `)
311
- })
312
- })