-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathlayout.ts
299 lines (273 loc) · 8.29 KB
/
layout.ts
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* This module is used to calculate the layout of the current sub-tree.
*/
import type { ReactNode } from 'react'
import type { Node as YogaNode } from 'yoga-wasm-web'
import getYoga from './yoga/index.js'
import {
isReactElement,
isClass,
buildXMLString,
normalizeChildren,
hasDangerouslySetInnerHTMLProp,
} from './utils.js'
import { SVGNodeToImage } from './handler/preprocess.js'
import computeStyle from './handler/compute.js'
import FontLoader from './font.js'
import buildTextNodes from './text/index.js'
import rect from './builder/rect.js'
import { Locale, normalizeLocale } from './language.js'
import { SerializedStyle } from './handler/expand.js'
export interface LayoutContext {
id: string
parentStyle: SerializedStyle
inheritedStyle: SerializedStyle
isInheritingTransform?: boolean
parent: YogaNode
font: FontLoader
embedFont: boolean
debug?: boolean
graphemeImages?: Record<string, string>
canLoadAdditionalAssets: boolean
locale?: Locale
getTwStyles: (tw: string, style: any) => any
onNodeDetected?: (node: SatoriNode) => void
}
export interface SatoriNode {
// Layout information.
left: number
top: number
width: number
height: number
type: string
key?: string | number
props: Record<string, any>
textContent?: string
}
export default async function* layout(
element: ReactNode,
context: LayoutContext
): AsyncGenerator<
{ word: string; locale?: string }[],
string,
[number, number]
> {
const Yoga = await getYoga()
const {
id,
inheritedStyle,
parent,
font,
debug,
locale,
embedFont = true,
graphemeImages,
canLoadAdditionalAssets,
getTwStyles,
} = context
// 1. Pre-process the node.
if (element === null || typeof element === 'undefined') {
yield
yield
return ''
}
// Not a regular element.
if (!isReactElement(element) || typeof element.type === 'function') {
let iter: ReturnType<typeof layout>
if (!isReactElement(element)) {
// Process as text node.
iter = buildTextNodes(String(element), context)
yield (await iter.next()).value as { word: string; locale?: Locale }[]
} else {
if (isClass(element.type as Function)) {
throw new Error('Class component is not supported.')
}
// If it's a custom component, Satori strictly requires it to be pure,
// stateless, and not relying on any React APIs such as hooks or suspense.
// So we can safely evaluate it to render. Otherwise, an error will be
// thrown by React.
iter = layout((element.type as Function)(element.props), context)
yield (await iter.next()).value as { word: string; locale?: string }[]
}
await iter.next()
const offset = yield
return (await iter.next(offset)).value as string
}
// Process as element.
const { type, props } = element
if (props && hasDangerouslySetInnerHTMLProp(props)) {
throw new Error(
'dangerouslySetInnerHTML property is not supported. See documentation for more information https://github.com/vercel/satori#jsx.'
)
}
let { style, children, tw, lang: _newLocale = locale } = props || {}
const newLocale = normalizeLocale(_newLocale)
// Extend Tailwind styles.
if (tw) {
const twStyles = getTwStyles(tw, style)
style = Object.assign(twStyles, style)
}
const node = Yoga.Node.create()
parent.insertChild(node, parent.getChildCount())
const [computedStyle, newInheritableStyle] = await computeStyle(
node,
type,
inheritedStyle,
style,
props
)
// Post-process styles to attach inheritable properties for Satori.
// If the element is inheriting the parent `transform`, or applying its own.
// This affects the coordinate system.
const isInheritingTransform =
computedStyle.transform === inheritedStyle.transform
if (!isInheritingTransform) {
;(computedStyle.transform as any).__parent = inheritedStyle.transform
}
// If the element has `overflow` set to `hidden` or clip-path is set, we need to create a clip
// path and use it in all its children.
if (
computedStyle.overflow === 'hidden' ||
(computedStyle.clipPath && computedStyle.clipPath !== 'none')
) {
newInheritableStyle._inheritedClipPathId = `satori_cp-${id}`
newInheritableStyle._inheritedMaskId = `satori_om-${id}`
}
if (computedStyle.maskImage) {
newInheritableStyle._inheritedMaskId = `satori_mi-${id}`
}
// If the element has `background-clip: text` set, we need to create a clip
// path and use it in all its children.
if (computedStyle.backgroundClip === 'text') {
const mutateRefValue = { value: '' } as any
newInheritableStyle._inheritedBackgroundClipTextPath = mutateRefValue
computedStyle._inheritedBackgroundClipTextPath = mutateRefValue
}
// 2. Do layout recursively for its children.
const normalizedChildren = normalizeChildren(children)
const iterators: ReturnType<typeof layout>[] = []
let i = 0
const segmentsMissingFont: { word: string; locale?: string }[] = []
for (const child of normalizedChildren) {
const iter = layout(child, {
id: id + '-' + i++,
parentStyle: computedStyle,
inheritedStyle: newInheritableStyle,
isInheritingTransform: true,
parent: node,
font,
embedFont,
debug,
graphemeImages,
canLoadAdditionalAssets,
locale: newLocale,
getTwStyles,
onNodeDetected: context.onNodeDetected,
})
if (canLoadAdditionalAssets) {
segmentsMissingFont.push(...(((await iter.next()).value as any) || []))
} else {
await iter.next()
}
iterators.push(iter)
}
yield segmentsMissingFont
for (const iter of iterators) await iter.next()
// 3. Post-process the node.
const [x, y] = yield
let { left, top, width, height } = node.getComputedLayout()
// Attach offset to the current node.
left += x
top += y
let childrenRenderResult = ''
let baseRenderResult = ''
let depsRenderResult = ''
// Emit event for the current node. We don't pass the children prop to the
// event handler because everything is already flattened, unless it's a text
// node.
const { children: childrenNode, ...restProps } = props
context.onNodeDetected?.({
left,
top,
width,
height,
type,
props: restProps,
key: element.key,
textContent: isReactElement(childrenNode) ? undefined : childrenNode,
})
// Generate the rendered markup for the current node.
if (type === 'img') {
const src = computedStyle.__src as string
baseRenderResult = await rect(
{
id,
left,
top,
width,
height,
src,
isInheritingTransform,
debug,
},
computedStyle,
newInheritableStyle
)
} else if (type === 'svg') {
// When entering a <svg> node, we need to convert it to a <img> with the
// SVG data URL embedded.
const currentColor = computedStyle.color
const src = await SVGNodeToImage(element, currentColor)
baseRenderResult = await rect(
{
id,
left,
top,
width,
height,
src,
isInheritingTransform,
debug,
},
computedStyle,
newInheritableStyle
)
} else {
const display = style?.display
if (
type === 'div' &&
children &&
typeof children !== 'string' &&
display !== 'flex' &&
display !== 'none'
) {
throw new Error(
`Expected <div> to have explicit "display: flex" or "display: none" if it has more than one child node.`
)
}
baseRenderResult = await rect(
{ id, left, top, width, height, isInheritingTransform, debug },
computedStyle,
newInheritableStyle
)
}
// Generate the rendered markup for the children.
for (const iter of iterators) {
childrenRenderResult += (await iter.next([left, top])).value
}
// An extra pass to generate the special background-clip shape collected from
// children.
if (computedStyle._inheritedBackgroundClipTextPath) {
depsRenderResult += buildXMLString(
'clipPath',
{
id: `satori_bct-${id}`,
'clip-path': computedStyle._inheritedClipPathId
? `url(#${computedStyle._inheritedClipPathId})`
: undefined,
},
(computedStyle._inheritedBackgroundClipTextPath as any).value
)
}
return depsRenderResult + baseRenderResult + childrenRenderResult
}