@portabletext/editor 2.14.2 → 2.14.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.
- package/lib/_chunks-cjs/use-editor.cjs.map +1 -1
- package/lib/_chunks-dts/behavior.types.action.d.cts +1 -1
- package/lib/_chunks-es/use-editor.js.map +1 -1
- package/lib/index.cjs +388 -315
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +388 -315
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.js.map +1 -1
- package/package.json +6 -6
- package/src/editor/plugins/createWithUndoRedo.ts +78 -61
- package/src/editor/sync-machine.ts +415 -322
- package/src/internal-utils/validateValue.ts +3 -3
- package/src/internal-utils/values.ts +80 -70
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {insert, set, setIfMissing, unset} from '@portabletext/patches'
|
|
2
|
-
import {isTextBlock} from '@portabletext/schema'
|
|
2
|
+
import {isSpan, isTextBlock} from '@portabletext/schema'
|
|
3
3
|
import type {
|
|
4
4
|
PortableTextBlock,
|
|
5
5
|
PortableTextSpan,
|
|
@@ -228,9 +228,9 @@ export function validateValue(
|
|
|
228
228
|
const allUsedMarks = uniq(
|
|
229
229
|
flatten(
|
|
230
230
|
textBlock.children
|
|
231
|
-
.filter((
|
|
231
|
+
.filter((child) => isSpan({schema: types}, child))
|
|
232
232
|
.map((cld) => cld.marks || []),
|
|
233
|
-
)
|
|
233
|
+
),
|
|
234
234
|
)
|
|
235
235
|
|
|
236
236
|
// Test that all markDefs are in use (remove orphaned markDefs)
|
|
@@ -30,82 +30,92 @@ export function toSlateValue(
|
|
|
30
30
|
keyMap: Record<string, any> = {},
|
|
31
31
|
): Descendant[] {
|
|
32
32
|
if (value && Array.isArray(value)) {
|
|
33
|
-
return value.map((block) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
return value.map((block) => toSlateBlock(block, {schemaTypes}, keyMap))
|
|
34
|
+
}
|
|
35
|
+
return []
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function toSlateBlock(
|
|
39
|
+
block: PortableTextBlock,
|
|
40
|
+
{schemaTypes}: {schemaTypes: EditorSchema},
|
|
41
|
+
keyMap: Record<string, any> = {},
|
|
42
|
+
): Descendant {
|
|
43
|
+
const {_type, _key, ...rest} = block
|
|
44
|
+
const isPortableText = block && block._type === schemaTypes.block.name
|
|
45
|
+
if (isPortableText) {
|
|
46
|
+
const textBlock = block as PortableTextTextBlock
|
|
47
|
+
let hasInlines = false
|
|
48
|
+
const hasMissingStyle = typeof textBlock.style === 'undefined'
|
|
49
|
+
const hasMissingMarkDefs = typeof textBlock.markDefs === 'undefined'
|
|
50
|
+
const hasMissingChildren = typeof textBlock.children === 'undefined'
|
|
42
51
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
const children = (textBlock.children || []).map((child) => {
|
|
53
|
+
const {_type: cType, _key: cKey, ...cRest} = child
|
|
54
|
+
// Return 'slate' version of inline object where the actual
|
|
55
|
+
// value is stored in the `value` property.
|
|
56
|
+
// In slate, inline objects are represented as regular
|
|
57
|
+
// children with actual text node in order to be able to
|
|
58
|
+
// be selected the same way as the rest of the (text) content.
|
|
59
|
+
if (cType !== 'span') {
|
|
60
|
+
hasInlines = true
|
|
61
|
+
return keepObjectEquality(
|
|
62
|
+
{
|
|
63
|
+
_type: cType,
|
|
64
|
+
_key: cKey,
|
|
65
|
+
children: [
|
|
53
66
|
{
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
_key: VOID_CHILD_KEY,
|
|
59
|
-
_type: 'span',
|
|
60
|
-
text: '',
|
|
61
|
-
marks: [],
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
value: cRest,
|
|
65
|
-
__inline: true,
|
|
67
|
+
_key: VOID_CHILD_KEY,
|
|
68
|
+
_type: 'span',
|
|
69
|
+
text: '',
|
|
70
|
+
marks: [],
|
|
66
71
|
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
// Return original block
|
|
74
|
-
if (
|
|
75
|
-
!hasMissingStyle &&
|
|
76
|
-
!hasMissingMarkDefs &&
|
|
77
|
-
!hasMissingChildren &&
|
|
78
|
-
!hasInlines &&
|
|
79
|
-
Element.isElement(block)
|
|
80
|
-
) {
|
|
81
|
-
// Original object
|
|
82
|
-
return block
|
|
83
|
-
}
|
|
84
|
-
// TODO: remove this when we have a better way to handle missing style
|
|
85
|
-
if (hasMissingStyle) {
|
|
86
|
-
rest.style = schemaTypes.styles[0].name
|
|
87
|
-
}
|
|
88
|
-
return keepObjectEquality({_type, _key, ...rest, children}, keyMap)
|
|
72
|
+
],
|
|
73
|
+
value: cRest,
|
|
74
|
+
__inline: true,
|
|
75
|
+
},
|
|
76
|
+
keyMap,
|
|
77
|
+
)
|
|
89
78
|
}
|
|
90
|
-
|
|
79
|
+
// Original child object (span)
|
|
80
|
+
return child
|
|
81
|
+
})
|
|
82
|
+
// Return original block
|
|
83
|
+
if (
|
|
84
|
+
!hasMissingStyle &&
|
|
85
|
+
!hasMissingMarkDefs &&
|
|
86
|
+
!hasMissingChildren &&
|
|
87
|
+
!hasInlines &&
|
|
88
|
+
Element.isElement(block)
|
|
89
|
+
) {
|
|
90
|
+
// Original object
|
|
91
|
+
return block
|
|
92
|
+
}
|
|
93
|
+
// TODO: remove this when we have a better way to handle missing style
|
|
94
|
+
if (hasMissingStyle) {
|
|
95
|
+
rest.style = schemaTypes.styles[0].name
|
|
96
|
+
}
|
|
97
|
+
return keepObjectEquality(
|
|
98
|
+
{_type, _key, ...rest, children},
|
|
99
|
+
keyMap,
|
|
100
|
+
) as Descendant
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return keepObjectEquality(
|
|
104
|
+
{
|
|
105
|
+
_type,
|
|
106
|
+
_key,
|
|
107
|
+
children: [
|
|
91
108
|
{
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
_key: VOID_CHILD_KEY,
|
|
97
|
-
_type: 'span',
|
|
98
|
-
text: '',
|
|
99
|
-
marks: [],
|
|
100
|
-
},
|
|
101
|
-
],
|
|
102
|
-
value: rest,
|
|
109
|
+
_key: VOID_CHILD_KEY,
|
|
110
|
+
_type: 'span',
|
|
111
|
+
text: '',
|
|
112
|
+
marks: [],
|
|
103
113
|
},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
114
|
+
],
|
|
115
|
+
value: rest,
|
|
116
|
+
},
|
|
117
|
+
keyMap,
|
|
118
|
+
) as Descendant
|
|
109
119
|
}
|
|
110
120
|
|
|
111
121
|
export function fromSlateValue(
|