-
Notifications
You must be signed in to change notification settings - Fork 29
/
react-portable-text.tsx
254 lines (218 loc) · 7.21 KB
/
react-portable-text.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import React, {ReactNode, useMemo} from 'react'
import {
LIST_NEST_MODE_HTML,
ToolkitNestedPortableTextSpan,
ToolkitTextNode,
} from '@portabletext/toolkit'
import type {
MissingComponentHandler,
NodeRenderer,
PortableTextProps,
PortableTextReactComponents,
ReactPortableTextList,
Serializable,
SerializedBlock,
} from './types'
import {
isPortableTextBlock,
isPortableTextListItemBlock,
isPortableTextToolkitList,
isPortableTextToolkitSpan,
isPortableTextToolkitTextNode,
nestLists,
spanToPlainText,
buildMarksTree,
} from '@portabletext/toolkit'
import type {
PortableTextBlock,
PortableTextListItemBlock,
PortableTextMarkDefinition,
PortableTextSpan,
TypedObject,
} from '@portabletext/types'
import {mergeComponents} from './components/merge'
import {defaultComponents} from './components/defaults'
import {
printWarning,
unknownBlockStyleWarning,
unknownListItemStyleWarning,
unknownListStyleWarning,
unknownMarkWarning,
unknownTypeWarning,
} from './warnings'
export function PortableText<B extends TypedObject = PortableTextBlock>({
value: input,
components: componentOverrides,
listNestingMode,
onMissingComponent: missingComponentHandler = printWarning,
}: PortableTextProps<B>) {
const handleMissingComponent = missingComponentHandler || noop
const blocks = Array.isArray(input) ? input : [input]
const nested = nestLists(blocks, listNestingMode || LIST_NEST_MODE_HTML)
const components = useMemo(() => {
return componentOverrides
? mergeComponents(defaultComponents, componentOverrides)
: defaultComponents
}, [componentOverrides])
const renderNode = useMemo(
() => getNodeRenderer(components, handleMissingComponent),
[components, handleMissingComponent]
)
const rendered = nested.map((node, index) =>
renderNode({node: node, index, isInline: false, renderNode})
)
return <>{rendered}</>
}
const getNodeRenderer = (
components: PortableTextReactComponents,
handleMissingComponent: MissingComponentHandler
): NodeRenderer => {
function renderNode<N extends TypedObject>(options: Serializable<N>): ReactNode {
const {node, index, isInline} = options
const key = node._key || `node-${index}`
if (isPortableTextToolkitList(node)) {
return renderList(node, index, key)
}
if (isPortableTextListItemBlock(node)) {
return renderListItem(node, index, key)
}
if (isPortableTextToolkitSpan(node)) {
return renderSpan(node, index, key)
}
if (isPortableTextBlock(node)) {
return renderBlock(node, index, key, isInline)
}
if (isPortableTextToolkitTextNode(node)) {
return renderText(node, key)
}
return renderCustomBlock(node, index, key, isInline)
}
/* eslint-disable react/jsx-no-bind */
function renderListItem(
node: PortableTextListItemBlock<PortableTextMarkDefinition, PortableTextSpan>,
index: number,
key: string
) {
const tree = serializeBlock({node, index, isInline: false, renderNode})
const renderer = components.listItem
const handler = typeof renderer === 'function' ? renderer : renderer[node.listItem]
const Li = handler || components.unknownListItem
if (Li === components.unknownListItem) {
const style = node.listItem || 'bullet'
handleMissingComponent(unknownListItemStyleWarning(style), {
type: style,
nodeType: 'listItemStyle',
})
}
let children = tree.children
if (node.style && node.style !== 'normal') {
// Wrap any other style in whatever the block serializer says to use
const {listItem, ...blockNode} = node
children = renderNode({node: blockNode, index, isInline: false, renderNode})
}
return (
<Li key={key} value={node} index={index} isInline={false} renderNode={renderNode}>
{children}
</Li>
)
}
function renderList(node: ReactPortableTextList, index: number, key: string) {
const children = node.children.map((child, childIndex) =>
renderNode({
node: child._key ? child : {...child, _key: `li-${index}-${childIndex}`},
index: index,
isInline: false,
renderNode,
})
)
const component = components.list
const handler = typeof component === 'function' ? component : component[node.listItem]
const List = handler || components.unknownList
if (List === components.unknownList) {
const style = node.listItem || 'bullet'
handleMissingComponent(unknownListStyleWarning(style), {nodeType: 'listStyle', type: style})
}
return (
<List key={key} value={node} index={index} isInline={false} renderNode={renderNode}>
{children}
</List>
)
}
function renderSpan(node: ToolkitNestedPortableTextSpan, _index: number, key: string) {
const {markDef, markType, markKey} = node
const Span = components.marks[markType] || components.unknownMark
const children = node.children.map((child, childIndex) =>
renderNode({node: child, index: childIndex, isInline: true, renderNode})
)
if (Span === components.unknownMark) {
handleMissingComponent(unknownMarkWarning(markType), {nodeType: 'mark', type: markType})
}
return (
<Span
key={key}
text={spanToPlainText(node)}
value={markDef}
markType={markType}
markKey={markKey}
renderNode={renderNode}
>
{children}
</Span>
)
}
function renderBlock(node: PortableTextBlock, index: number, key: string, isInline: boolean) {
const {_key, ...props} = serializeBlock({node, index, isInline, renderNode})
const style = props.node.style || 'normal'
const handler =
typeof components.block === 'function' ? components.block : components.block[style]
const Block = handler || components.unknownBlockStyle
if (Block === components.unknownBlockStyle) {
handleMissingComponent(unknownBlockStyleWarning(style), {
nodeType: 'blockStyle',
type: style,
})
}
return <Block key={key} {...props} value={props.node} renderNode={renderNode} />
}
function renderText(node: ToolkitTextNode, key: string) {
if (node.text === '\n') {
const HardBreak = components.hardBreak
return HardBreak ? <HardBreak key={key} /> : '\n'
}
return node.text
}
function renderCustomBlock(node: TypedObject, index: number, key: string, isInline: boolean) {
const Node = components.types[node._type]
const nodeOptions = {
value: node,
isInline,
index,
renderNode,
}
if (Node) {
return <Node key={key} {...nodeOptions} />
}
handleMissingComponent(unknownTypeWarning(node._type), {nodeType: 'block', type: node._type})
const UnknownType = components.unknownType
return <UnknownType key={key} {...nodeOptions} />
}
/* eslint-enable react/jsx-no-bind */
return renderNode
}
function serializeBlock(options: Serializable<PortableTextBlock>): SerializedBlock {
const {node, index, isInline, renderNode} = options
const tree = buildMarksTree(node)
const children = tree.map((child, i) =>
renderNode({node: child, isInline: true, index: i, renderNode})
)
return {
_key: node._key || `block-${index}`,
children,
index,
isInline,
node,
}
}
function noop() {
// Intentional noop
}