-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout2.js
191 lines (159 loc) · 5.07 KB
/
layout2.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
// 收集元素进行
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 = 'strech'
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) {
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
console.log(items)
}
module.exports = layout;