-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
193 lines (185 loc) · 4.96 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
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
/**
* @Description: measure-extend v1.0.1
* @author jarvis
*/
(function (SMApp) {
if (!$) {
console.warn('measure-extend load error, $ is undefined');
return
}
if (!SMApp) {
console.warn('measure-extend load error, SMApp is undefined');
return
}
Function.prototype.after = function (fn) {
const _self = this;
return function () {
_self.apply(this, arguments)
fn.apply(this)
}
};
var _ = function(str){
return (I18N[lang] && I18N[lang][str])? I18N[lang][str]: str;
};
/**
* 单位配置信息
* 如需新增配置,可在此添加(如果添加的配置中需要name,直接赋值就行,不需要调用`_`方法)
*/
var unitsData = [
{
units: [
{
name: _('Standard'),
unit: 'px',
scale: 1
}
]
},
{
name: _('iOS Devices'),
units: [
{
name: _('Points') + ' @1x',
unit: 'pt',
scale: 1
},
{
name: _('Retina') + ' @2x',
unit: 'pt',
scale: 2
},
{
name: _('Retina HD') + ' @3x',
unit: 'pt',
scale: 3
}
]
},
{
name: _('Android Devices'),
units: [
{
name: 'LDPI @0.75x',
unit: 'dp/sp',
scale: .75
},
{
name: 'MDPI @1x',
unit: 'dp/sp',
scale: 1
},
{
name: 'HDPI @1.5x',
unit: 'dp/sp',
scale: 1.5
},
{
name: 'XHDPI @2x',
unit: 'dp/sp',
scale: 2
},
{
name: 'XXHDPI @3x',
unit: 'dp/sp',
scale: 3
},
{
name: 'XXXHDPI @4x',
unit: 'dp/sp',
scale: 4
}
]
},
{
name: _('Web View'),
units: [
{
name: 'CSS Rem 12px',
unit: 'rem',
scale: 12
},
{
name: 'CSS Rem 14px',
unit: 'rem',
scale: 14
},
{
name: 'CSS Rem 16px',
unit: 'rem',
scale: 16
},
{
name: 'css vw 375px',
unit: 'vw',
scale: 3.75
},
{
name: 'css rpx @2x',
unit: 'rpx',
scale: 0.5
}
]
}
];
SMApp.fn.unit = function () {
var self = this,
unitHtml = [],
unitList = [],
unitCurrent = '',
hasCurrent = '';
$.each(unitsData, function(index, data){
if(data.name) unitList.push('<li class="sub-title">' + _(data.name) + '</li>');
$.each(data.units, function(index, unit){
var checked = '';
// if(unit.scale == self.configs.scale){
if( unit.unit == self.configs.unit && unit.scale == self.configs.scale ){
checked = ' checked="checked"';
hasCurrent = _(unit.name);
}
unitList.push('<li><label><input type="radio" name="resolution" data-name="' + _(unit.name) + '" data-unit="' + unit.unit + '" data-scale="' + unit.scale + '"' + checked + '><span>' + _(unit.name) + '</span></label></li>');
// }
});
});
if(!hasCurrent){
unitCurrent = '<li><label><input type="radio" name="resolution" data-name="' + _('Custom') + ' (' + self.configs.scale + ', ' + self.configs.unit + ')" data-unit="' + self.configs.unit + '" data-scale="' + self.configs.scale + '" checked="checked"><span>' + _('Custom') + ' (' + self.configs.scale + ', ' + self.configs.unit + ')</span></label></li>';
hasCurrent = _('Custom') + ' (' + self.configs.scale + ', ' + self.configs.unit + ')';
}
unitHtml.push(
'<div class="overlay"></div>',
'<h3>' + _('Design resolution') + '</h3>',
'<p>' + hasCurrent + '</p>',
'<ul>',
unitCurrent,
unitList.join(''),
'</ul>'
);
$('#unit').html(unitHtml.join(''));
};
SMApp.fn.renderInspector = SMApp.fn.renderInspector.after(function () {
const codeTemplateEle = $('#css');
const cssArray = (codeTemplateEle.val()).split(';');
const adapterLayerDataCssCode = (cssArray) => {
return cssArray.map(item => {
if (!item) {
return item
}
const attrName = item.split(':')[0]
let attrValues = (item.split(':')[1]).split(' ')
const unit = this.configs.unit.split('/')[0]
attrValues = attrValues.map(valueItem => {
const pxIndex = valueItem.indexOf('px')
const unitIndex = pxIndex === -1 ? valueItem.indexOf(unit) : pxIndex
if (unitIndex !== -1) {
const value = valueItem.substring(0, unitIndex)
const newAttrValue = this.unitSize(value, false)
return newAttrValue
}
return valueItem
})
return attrName + ':' + attrValues.join(' ') + ';';
})
};
const newLayerDataCssCode = adapterLayerDataCssCode(cssArray);
codeTemplateEle.val(newLayerDataCssCode.join(''));
});
})(SMApp);