-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout3.js
283 lines (240 loc) · 8.1 KB
/
layout3.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
// 收集元素进行
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 = 'center'
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 = []
flexLines.push(flexLine)
flexLine.push(item)
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
for (let i = 0; i < items.length; i++) {
const item = items[i]
const 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
}
}
})
}
console.log(items)
}
module.exports = layout;