-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (95 loc) · 3.01 KB
/
index.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
'use strict';
import React, {
StyleSheet,
Dimensions,
Platform
} from 'react-native';
const {width} = Dimensions.get('window');
const DEFAULT_WIDTH = 320;
const CStyleSheet = {
minWidth: undefined,
maxWidth: undefined,
baseWidth: DEFAULT_WIDTH,
ratio: width / DEFAULT_WIDTH,
isROn: true,
setBase: function (baseWidth = DEFAULT_WIDTH) {
this.baseWidth = baseWidth;
this.updateRatio();
},
setMinWidth: function (minWidth) {
this.minWidth = minWidth;
this.updateRatio();
},
setMaxWidth: function (maxWidth) {
this.maxWidth = maxWidth;
this.updateRatio();
},
updateRatio: function () {
if (width < this.minWidth) {
this.ratio = this.minWidth / this.baseWidth;
} else if (width > this.maxWidth) {
this.ratio = this.maxWidth / this.baseWidth;
} else {
this.ratio = width / this.baseWidth;
}
},
create: function (styleObj) {
return StyleSheet.create(this.revise(styleObj));
},
revise: function(styleObj) {
// revise, expect: flex, opacity
for (var styleKey in styleObj) {
// Freeze keys
var _keys = Object.keys(styleObj[styleKey]);
for (var i in _keys) {
var key = _keys[i];
// cross platform
if (key === 'ios' || key === 'android') {
if (key === Platform.OS) {
for (var platformKey in styleObj[styleKey][key]) {
if (typeof styleObj[styleKey][key][platformKey] === 'number' && platformKey !== 'flex' && platformKey !== 'opacity') {
// ignore rules with '$' start
if (platformKey.charAt(0) === '$') {
styleObj[styleKey][platformKey.substr(1)] = styleObj[styleKey][key][platformKey];
continue;
}
styleObj[styleKey][platformKey] = this.r(styleObj[styleKey][key][platformKey]);
} else {
// directly assign to the parent
styleObj[styleKey][platformKey] = styleObj[styleKey][key][platformKey];
}
}
}
// delete hack style key
delete styleObj[styleKey][key];
continue;
}
if (typeof styleObj[styleKey][key] === 'number' && key !== 'flex' && key !== 'opacity') {
// ignore rules with '$' start
if (key.charAt(0) === '$') {
styleObj[styleKey][key.substr(1)] = styleObj[styleKey][key];
delete styleObj[styleKey][key];
continue;
} else {
styleObj[styleKey][key] = this.r(styleObj[styleKey][key]);
}
}
}
}
return styleObj;
},
switchR: function(isROn) {
var prevStatus = this.isROn;
this.isROn = !!isROn;
return prevStatus;
},
r: function(num) {
if (this.isROn && Math.abs(num) > 1) {
return Math.round(num * this.ratio);
}
return num;
}
};
//可能丢失StyleSheet原型上的属性,不过StyleSheet原型上没有特殊属性
const newStyleSheet = Object.assign({}, StyleSheet, CStyleSheet);
export default newStyleSheet;