-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout4.js
373 lines (318 loc) · 11.1 KB
/
layout4.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// 收集元素进行
function getStyle(element) {
if (!element.style)
element.style = {}
for (let prop in element.computedStyle) {
const p = element.computedStyle.value
element.style[prop] = element.computedStyle[prop].value
if (element.style[prop].toString().match(/px$/)) {
element.style[prop] = parseInt(element.style[prop])
}
if (element.style[prop].toString().match(/^[0-9\.]+$/)) {
element.style[prop] = parseInt(element.style[prop])
}
}
return element.style
}
function layout(element) {
if (!element.computedStyle)
return
const elementStyle = getStyle(element)
if (elementStyle.display !== 'flex')
return
const items = element.children.filter(e => e.type === 'element')
items.sort(function (a, b) {
return (a.order || 0) - (b.order || 0)
})
let style = elementStyle;
['width', 'height'].forEach(size => {
if (style[size] === 'auto' || style[size] === '') {
style[size] = null
}
})
if (!style.flexDirection || style.flexDirection === 'auto')
style.flexDirection = 'row'
if (!style.alignItems || style.alignItems === 'auto')
style.alignItems = 'stretch'
if (!style.justifyContent || style.justifyContent === 'auto')
style.justifyContent = 'flex-start'
if (!style.flexWrap || style.flexWrap === 'auto')
style.flexWrap = 'nowrap'
if (!style.alignContent || style.alignContent === 'auto')
style.alignContent = 'stretch'
let
mainSize, // 主轴size width / height
mainStart, // 主轴起点 left / right / top / bottom
mainEnd, // 主轴终点 left / right / top / bottom
mainSign, // 主轴符号位,用于 是否 reverse +1 / -1
mainBase, // 主轴开始的位置 0 / style.width
crossSize, // 交叉轴size
crossStart, // 交叉轴坐标起点
crossEnd, // 交叉轴坐标终点
crossSign, // 交叉轴符号位,用于 是否 reverse
crossBase; // 交叉轴开始的位置
if (style.flexDirection === 'row') {
mainSize = 'width'
mainStart = 'left'
mainEnd = 'right'
mainSign = +1
mainBase = 0
crossSize = 'height'
crossStart = 'top'
crossEnd = 'bottom'
} else if (style.flexDirection === 'row-reverse') {
mainSize = 'width'
mainStart = 'right'
mainEnd = 'left'
mainSign = -1
mainBase = style.width
crossSize = 'height'
crossStart = 'top'
crossEnd = 'bottom'
} else if (style.flexDirection === 'column') {
mainSize = 'height'
mainStart = 'top'
mainEnd = 'bottom'
mainSign = +1
mainBase = 0
crossSize = 'width'
crossStart = 'left'
crossEnd = 'right'
} else if (style.flexDirection === 'column-reverse') {
mainSize = 'height'
mainStart = 'bottom'
mainEnd = 'top'
mainSign = -1
mainBase = style.height
crossSize = 'width'
crossStart = 'left'
crossEnd = 'right'
}
if (style.flexWrap === 'wrap-reverse') {
let temp = crossStart
crossStart = crossEnd
crossEnd = temp
crossSign = -1
} else {
crossBase = 0
crossSign = +1
}
let isAutoMainSize = false
// 没有设置 mainSize 直接撑开
if (!style[mainSize]) { // auto sizing
elementStyle[mainSize] = 0
for (let i = 0; i < items.length; i++) {
let item = items[i]
if (itemStyle[mainSize] !== null || itemStyle[mainSize] !== (void 0))
elementStyle[mainSize] = elementStyle[mainSize] + itemStyle[mainSize]
}
isAutoMainSize = true
}
const flexLine = []
const flexLines = [flexLine]
let mainSpace = elementStyle[mainSize]
let crossSpace = 0
for (let i = 0; i < items.length; i++) {
const item = items[i]
const itemStyle = getStyle(item)
// 单个元素 mainSize
if (itemStyle[mainSize] === null) {
itemStyle[mainSize] = 0
}
if (itemStyle.flex) {
// mainSpace 不做处理
flexLine.push(item)
} else if (style.flexWrap === 'nowrap' && isAutoMainSize) {
mainSpace -= itemStyle[mainSize]
if (itemStyle[crossSize] !== null && itemStyle[crossSize] !== (void 0)) {
crossSpace = Math.max(crossSpace, itemStyle[crossSize])
}
flexLine.push(item)
} else {
// 当前flex 子项,大于 flex mainSize,自适应
if (itemStyle[mainSize] > style[mainSize]) {
itemStyle[mainSize] = style[mainSize]
}
// 当前flex 子项,大于 flex 容器剩余 mainSpace,另起新行
if (mainSpace < itemStyle[mainSize]) {
flexLine.mainSpace = mainSpace
flexLine.crossSpace = crossSpace
// 创建新行
flexLine = []
flexLine.push(item)
flexLines.push(flexLine)
mainSpace = style[mainSize]
crossSpace = 0
} else { // 未超过 flex 容器剩余 mainSpace,添加近行
flexLine.push(item)
}
// 处理交叉轴,只需要取 flex 子项最大 crossSize
if (itemStyle[crossSize] !== null && itemStyle[crossSize] !== (void 0)) {
crossSpace = Math.max(crossSpace, itemStyle[crossSize])
}
// flex 容器剩余 mainSpace
mainSpace -= itemStyle[mainSize]
}
}
flexLine.mainSpace = mainSpace
if (style.flexWrap === 'nowrap' || isAutoMainSize) {
flexLine.crossSpace = (style[crossSize] !== undefined) ? style[crossSize] : crossSpace
} else {
flexLine.crossSpace = crossSpace
}
if (mainSpace < 0) {
// 对负的 mainSpace, 所有该行 flex 子项等比例缩放(未设置 flex-shrink 默认值是1,也就是默认所有的 flex 子项都会收缩)
const scale = style[mainSize] / (style[mainSize] - mainSpace)
let currentMain = mainBase
for (let i = 0; i < items.length; i++) {
const item = items[i]
const itemStyle = getStyle(item)
if (itemStyle.flex) {
itemStyle[mainSize] = 0
}
itemStyle[mainSize] = itemStyle[mainSize] * scale
// flex 容器这一行内,flex 子项排布
itemStyle[mainStart] = currentMain
itemStyle[mainEnd] = itemStyle[mainStart] + mainSign * itemStyle[mainSize]
currentMain = itemStyle[mainEnd]
}
} else { // 可以容纳
flexLines.forEach(function (items) {
const mainSpace = items.mainSpace
let flexTotal = 0
let itemStyle = null
for (let i = 0; i < items.length; i++) {
const item = items[i]
itemStyle = getStyle(item)
if ((itemStyle.flex !== null) && (itemStyle.flex !== (void 0))) {
flexTotal += itemStyle.flex
continue
}
}
if (flexTotal > 0) { // 填充 flexLine 剩余 mainSpace 空间
let currentMain = mainBase
for (let i = 0; i < items.length; i++) {
const item = items[i]
const itemStyle = getStyle(item)
if (itemStyle.flex) {
itemStyle[mainSize] = (mainSpace / flexTotal) * itemStyle.flex
}
itemStyle[mainStart] = currentMain
itemStyle[mainEnd] = itemStyle[mainStart] + mainSign * itemStyle[mainSize]
currentMain = itemStyle[mainEnd]
}
} else {
let currentMain, gap;
if (style.justifyContent === 'flex-start') {
currentMain = mainBase
gap = 0
}
if (style.justifyContent === 'flex-end') {
currentMain = mainSpace * mainSign + mainBase
gap = 0
}
if (style.justifyContent === 'center') {
currentMain = mainSpace / 2 * mainSign + mainBase
gap = 0
}
if (style.justifyContent === 'space-between') {
gap = mainSpace / (items.length - 1) * mainSign
currentMain = mainBase
}
if (style.justifyContent === 'space-around') {
gap = mainSpace / items.length * mainSign
currentMain = gap / 2 + mainBase
}
if (style.justifyContent === 'space-evenly') {
gap = mainSpace / (items.length + 1) * mainSign
currentMain = gap + mainBase
}
for (let i = 0; i < items.length; i++) {
const item = items[i]
itemStyle[mainStart] = currentMain
itemStyle[mainEnd] = itemStyle[mainStart] + mainSign * itemStyle[mainSize]
currentMain = itemStyle[mainEnd] + gap
}
}
})
}
if (!style[crossSize]) { // 交叉轴,crossSize 未设定时默认为 count flexLines 每行最大 crossSpace 之和
crossSpace = 0
elementStyle[crossSize] = 0
for (let i = 0; i < flexLines.length; i++) {
elementStyle[crossSize] = elementStyle[crossSize] + flexLines[i].crossSpace
}
} else { // 设定后,计算出 最终的 crossSpace,为 crossSpace 减去每行最大 crossSpace,剩余空间,用作分配
crossSpace = style[crossSize]
for (let i = 0; i < flexLines.length; i++) {
crossSpace -= flexLines[i].crossSpace
}
}
if (style.flexWrap === 'wrap-reverse') {
crossBase = style[crossSize]
} else {
crossBase = 0
}
let lineSize = style[crossSize] / flexLines.length // 行高
let gap
if (style.alignContent === 'flex-start') {
crossBase += 0
gap = 0
}
if (style.alignContent === 'flex-end') {
crossBase += crossSign * crossSpace
gap = 0
}
if (style.alignContent === 'center') {
crossBase += crossSign * crossSpace / 2
gap = 0
}
if (style.alignContent === 'space-between') {
crossBase += 0
gap = crossSpace / (flexLines.length - 1)
}
if (style.alignContent === 'space-around') {
gap = crossSpace / (flexLines.length)
crossBase += crossSign * step / 2
}
if (style.alignContent === 'stretch') {
crossBase += 0
gap = 0
}
flexLines.forEach(function (items) {
let lineCrossSize = style.alignContent === 'stretch' ? // 拉伸 flex 子项,填满交叉轴
items.crossSpace + crossSpace / flexLines.length :
items.crossSpace
for (let i = 0; i < items.length; i++) {
const item = items[i]
const itemStyle = getStyle(item)
const align = itemStyle.alignSelf || style.alignItems // align-self指控制单独某一个flex子项的垂直对齐方式
// align-items属性,表示子项们
if (itemStyle[crossSize] === null) {
itemStyle[crossSize] = (align === 'stretch') ?
lineCrossSize : 0
}
if (align === 'flex-start') {
itemStyle[crossStart] = crossBase
itemStyle[crossEnd] = itemStyle[crossStart] + crossSign * itemStyle[crossSize]
}
if (align === 'flex-end') {
itemStyle[crossEnd] = crossBase + crossSign * lineCrossSize
itemStyle[crossStart] = itemStyle[crossEnd] - crossSign * itemStyle[crossSize]
}
if (align === 'center') {
itemStyle[crossStart] = crossBase + crossSign * (lineCrossSize - itemStyle[crossSize]) / 2
itemStyle[crossEnd] = itemStyle[crossStart] + crossSign * itemStyle[crossSize]
}
if (align === 'stretch') {
itemStyle[crossStart] = crossBase
itemStyle[crossEnd] = crossBase + crossSign * ((itemStyle[crossSize] !== null && itemStyle[crossSize] !== (void 0)) ?
itemStyle[crossSize] : lineCrossSize)
itemStyle[crossSize] = crossSign * (itemStyle[crossEnd] - itemStyle[crossStart])
}
}
crossBase += crossSign * (lineCrossSize + gap)
})
// console.log(items)
}
module.exports = layout;