/
FlexLayoutProcessor.js
305 lines (277 loc) · 9.91 KB
/
FlexLayoutProcessor.js
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
300
301
302
303
304
import { defineWorkerModule, ThenableWorkerModule } from 'troika-worker-utils'
import { fontProcessorWorkerModule } from 'troika-three-text'
import yogaFactory from '../libs/yoga.factory.js'
/**
* Main entry point. This issues a request to the web worker to perform flexbox layout
* on the given `styleTree`, calling the `callback` function with the results when finished.
*
* @param {FlexLayoutStyleNode} styleTree
* @param {function(FlexLayoutResult)} callback
*/
export function requestFlexLayout(styleTree, callback) {
flexLayoutProcessorWorkerModule(styleTree).then(callback)
}
/**
* @typedef {object<FlexLayoutResultNode>} FlexLayoutResult
* Mapping of node ids to layout results.
*/
/**
* @typedef {object} FlexLayoutResultNode - layout result for a single flex node.
* @property {number} left - the node's computed left position
* @property {number} top - the node's computed top position
* @property {number} width - the node's computed width
* @property {number} height - the node's computed height
*/
/**
* @typedef {object} MeasureFunctionParams - Parameters for the `measureFunction`.
* @property {string} text
* @property {string} font
* @property {number} fontSize
* @property {number} lineHeight
* @property {number} letterSpacing
* @property {string} whiteSpace
* @property {string} overflowWrap
* @property {number} maxWidth
*/
/**
* Factory for the flex layout processing function. This is injected into a web worker so it
* must be entirely self-contained other than specific dependencies passed in as arguments.
* @param {object} Yoga - The yoga-layout implementation object.
* @param {function(fontUrl:string, callback:function)} loadFontFn - A function that
* loads a given font URL, invoking a callback when complete.
* @param {function(MeasureFunctionParams):{width:number}} measureFunction - A function that
* measures the intrinsic dimensions for a block of text with given styles and
* constraints. The measurement must occur synchronously.
* @return {function(FlexLayoutStyleNode):FlexLayoutResult}
*/
function createFlexLayoutProcessor(Yoga, loadFontFn, measureFunction) {
const YOGA_VALUE_MAPPINGS = {
align: {
'auto': 'ALIGN_AUTO',
'baseline': 'ALIGN_BASELINE',
'center': 'ALIGN_CENTER',
'flex-end': 'ALIGN_FLEX_END',
'flex-start': 'ALIGN_FLEX_START',
'stretch': 'ALIGN_STRETCH'
},
direction: {
'column': 'FLEX_DIRECTION_COLUMN',
'column-reverse': 'FLEX_DIRECTION_COLUMN_REVERSE',
'row': 'FLEX_DIRECTION_ROW',
'row-reverse': 'FLEX_DIRECTION_ROW_REVERSE'
},
edge: {
top: 'EDGE_TOP',
right: 'EDGE_RIGHT',
bottom: 'EDGE_BOTTOM',
left: 'EDGE_LEFT',
},
justify: {
'center': 'JUSTIFY_CENTER',
'flex-end': 'JUSTIFY_FLEX_END',
'flex-start': 'JUSTIFY_FLEX_START',
'space-around': 'JUSTIFY_SPACE_AROUND',
'space-between': 'JUSTIFY_SPACE_BETWEEN'
},
position: {
'absolute': 'POSITION_TYPE_ABSOLUTE',
'relative': 'POSITION_TYPE_RELATIVE'
},
wrap: {
'nowrap': 'WRAP_NO_WRAP',
'wrap': 'WRAP_WRAP'
}
}
const sides = ['Top', 'Right', 'Bottom', 'Left']
// Create functions for setting each supported style property on a Yoga node
const YOGA_SETTERS = Object.create(null)
// Simple properties
;[
'width',
'height',
'minWidth',
'minHeight',
'maxWidth',
'maxHeight',
'aspectRatio',
['flexDirection', YOGA_VALUE_MAPPINGS.direction],
'flex',
['flexWrap', YOGA_VALUE_MAPPINGS.wrap],
'flexBasis',
'flexGrow',
'flexShrink',
['alignContent', YOGA_VALUE_MAPPINGS.align],
['alignItems', YOGA_VALUE_MAPPINGS.align],
['alignSelf', YOGA_VALUE_MAPPINGS.align],
['justifyContent', YOGA_VALUE_MAPPINGS.justify]
].forEach(styleProp => {
let mapping = null
if (Array.isArray(styleProp)) {
mapping = styleProp[1]
styleProp = styleProp[0]
}
const setter = `set${styleProp.charAt(0).toUpperCase()}${styleProp.substr(1)}`
YOGA_SETTERS[styleProp] = mapping ?
(yogaNode, value) => {
if (mapping.hasOwnProperty(value)) {
value = Yoga[mapping[value]]
yogaNode[setter](value)
}
} :
(yogaNode, value) => {
yogaNode[setter](value)
}
})
// Position-related properties
YOGA_SETTERS.position = (yogaNode, value) => {
yogaNode.setPositionType(Yoga[YOGA_VALUE_MAPPINGS.position[value]])
}
sides.forEach(side => {
const edgeConst = YOGA_VALUE_MAPPINGS.edge[side.toLowerCase()]
YOGA_SETTERS[side.toLowerCase()] = (yogaNode, value) => {
yogaNode.setPosition(Yoga[edgeConst], value)
}
})
// Multi-side properties
;[
'margin',
'padding',
'border'
].forEach(styleProp => {
sides.forEach(side => {
const edgeConst = YOGA_VALUE_MAPPINGS.edge[side.toLowerCase()]
const setter = `set${styleProp.charAt(0).toUpperCase()}${styleProp.substr(1)}`
YOGA_SETTERS[`${styleProp}${side}`] = (yogaNode, value) => {
yogaNode[setter](Yoga[edgeConst], value)
}
})
})
function ensureAllFontsLoaded(styleTree, callback) {
const fonts = []
let loadedCount = 0
walkStyleTree(styleTree, node => {
if (node.text) fonts.push(node.font) //may be undef
})
if (fonts.length) {
for (let i = 0; i < fonts.length; i++) {
loadFontFn(fonts[i], () => {
loadedCount++
if (loadedCount === fonts.length) {
callback()
}
})
}
} else {
callback()
}
}
function walkStyleTree(styleTree, callback) {
callback(styleTree)
if (styleTree.children) {
for (let i = 0, len = styleTree.children.length; i < len; i++) {
walkStyleTree(styleTree.children[i], callback)
}
}
}
function process(styleTree, callback) {
// Init common node config
const yogaConfig = Yoga.Config.create()
yogaConfig.setPointScaleFactor(0) //disable value rounding
// Ensure all fonts required for measuring text nodes within this layout are pre-loaded,
// so that all text measurement calls can happen synchronously
ensureAllFontsLoaded(styleTree, () => {
// TODO for now, just to keep things simple, we'll rebuild the entire Yoga tree on every
// call, but we should look into persisting it across calls for more efficient updates
function populateNode(yogaNode, styleNode) {
if (!styleNode) {
throw new Error('Style node with no id')
}
for (let prop in styleNode) {
if (styleNode.hasOwnProperty(prop)) {
// Look for a style setter, and invoke it
const setter = YOGA_SETTERS[prop]
if (setter) {
setter(yogaNode, styleNode[prop])
}
// If the node has text, set up its measurement function
else if (prop === 'text') {
yogaNode.setMeasureFunc((innerWidth, widthMeasureMode, innerHeight, heightMeasureMode) => {
const params = {
text: styleNode.text,
font: styleNode.font,
fontSize: styleNode.fontSize,
lineHeight: styleNode.lineHeight,
letterSpacing: styleNode.letterSpacing,
whiteSpace: styleNode.whiteSpace,
overflowWrap: styleNode.overflowWrap,
maxWidth: isNaN(innerWidth) ? Infinity : innerWidth
}
// NOTE: this assumes the measureFunction will exec the callback synchronously; this works
// with current impl since we preload all needed fonts above, but it would be good to
// formalize that contract in the FontProcessor
let result = measureFunction(params)
if (result) {
// Apply a fudge factor to avoid issues where the flexbox layout result using this
// measurement ends up slightly smaller (due to rounding?) and making text wrap
result.width += styleNode.fontSize * 0.00001
}
return result || {width: 0, height: 0}
})
}
}
}
// Recurse to children
if (styleNode.children) {
for (let i = 0, len = styleNode.children.length; i < len; i++) {
const childYogaNode = Yoga.Node.createWithConfig(yogaConfig)
populateNode(childYogaNode, styleNode.children[i])
yogaNode.insertChild(childYogaNode, i)
}
}
// Store the Yoga node on the style object, so we can access each Yoga node's original
// context when traversing post-layout
styleNode.yogaNode = yogaNode
}
const root = Yoga.Node.createWithConfig(yogaConfig)
populateNode(root, styleTree)
// Perform the layout and collect the results as a flat id-to-computed-layout map
root.calculateLayout()
const results = Object.create(null)
walkStyleTree(styleTree, styleNode => {
const {id, yogaNode} = styleNode
results[id] = {
left: yogaNode.getComputedLeft(),
top: yogaNode.getComputedTop(),
width: yogaNode.getComputedWidth(),
height: yogaNode.getComputedHeight()
}
})
root.freeRecursive()
callback(results)
})
}
return process
}
export const flexLayoutProcessorWorkerModule = defineWorkerModule({
name: 'FlexLayoutProcessor',
dependencies: [
yogaFactory,
fontProcessorWorkerModule,
createFlexLayoutProcessor,
ThenableWorkerModule
],
init(yogaFactory, fontProcessor, create, Thenable) {
const Yoga = yogaFactory()
function measure(params) {
let result = null
fontProcessor.measure(params, r => {result = r})
return result
}
const process = create(Yoga, fontProcessor.loadFont, measure)
return function(styleTree) {
const thenable = new Thenable()
process(styleTree, thenable.resolve)
return thenable
}
}
})