@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.
@@ -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((cld) => cld._type === types.span.name)
231
+ .filter((child) => isSpan({schema: types}, child))
232
232
  .map((cld) => cld.marks || []),
233
- ) as string[],
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
- const {_type, _key, ...rest} = block
35
- const isPortableText = block && block._type === schemaTypes.block.name
36
- if (isPortableText) {
37
- const textBlock = block as PortableTextTextBlock
38
- let hasInlines = false
39
- const hasMissingStyle = typeof textBlock.style === 'undefined'
40
- const hasMissingMarkDefs = typeof textBlock.markDefs === 'undefined'
41
- const hasMissingChildren = typeof textBlock.children === 'undefined'
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
- const children = (textBlock.children || []).map((child) => {
44
- const {_type: cType, _key: cKey, ...cRest} = child
45
- // Return 'slate' version of inline object where the actual
46
- // value is stored in the `value` property.
47
- // In slate, inline objects are represented as regular
48
- // children with actual text node in order to be able to
49
- // be selected the same way as the rest of the (text) content.
50
- if (cType !== 'span') {
51
- hasInlines = true
52
- return keepObjectEquality(
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
- _type: cType,
55
- _key: cKey,
56
- children: [
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
- keyMap,
68
- )
69
- }
70
- // Original child object (span)
71
- return child
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
- return keepObjectEquality(
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
- _type,
93
- _key,
94
- children: [
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
- keyMap,
105
- )
106
- }) as Descendant[]
107
- }
108
- return []
114
+ ],
115
+ value: rest,
116
+ },
117
+ keyMap,
118
+ ) as Descendant
109
119
  }
110
120
 
111
121
  export function fromSlateValue(