-
Notifications
You must be signed in to change notification settings - Fork 3
/
layout1.js
123 lines (100 loc) · 3.01 KB
/
layout1.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
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
}
}
module.exports = layout;