-
Notifications
You must be signed in to change notification settings - Fork 64
/
util.js
128 lines (118 loc) · 3.18 KB
/
util.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
/**
* css util for SaltUI
* @author gnosaij
*
* Copyright 2014-2015, SaltUI Team, Alinw.
* All rights reserved.
*
* Usage:
* const {createStyleContext} = require('tingle-style');
* let componentStyle = createStyleContext('tGroupList');
*
* // 添加样式
* componentStyle.add(`
* body{
* background-color: red;
* }
* `);
*
* // 添加带有标识的样式,这样可以优化去重
* componentStyle.add('redBG', `
* body{
* background-color: red;
* }
* `);
*/
const doc = document;
/**
* 可使用`JS`动态插入样式的类
*/
class StyleContext {
/**
* 构造函数
* @param id {String} required 用于创建`style`元素的`id`标识,建议使用`componentName`作为`id`的值,避免全局冲突
*/
constructor(id) {
const t = this;
t.id = id;
t.rules = [];
t.createStyleEl();
}
createStyleEl() {
const t = this;
const el = doc.createElement('style');
el.id = `${t.id}-style`;
el.setAttribute('type', 'text/css');
const headEl = doc.getElementsByTagName('head')[0];
headEl.appendChild(el);
t.el = el;
}
addRule(rule) {
this.el.appendChild(doc.createTextNode(rule));
}
/**
* 外部接口 添加新的样式规则
*/
add(ruleId, rule) {
const t = this;
if (rule === undefined) {
rule = ruleId;
t.addRule(t.clearRuleIndent(rule));
} else if (ruleId && t.rules.indexOf(ruleId) === -1) {
t.rules.push(ruleId);
t.addRule(`/* ${ruleId} */\n${t.clearRuleIndent(rule)}`);
}
}
/**
* 删除多余的缩进 更方便阅读
*/
clearRuleIndent(rule) {
// 为了取出正确的首行缩进数量 需要去掉第一行的换行
rule = rule.replace(/^\n/, '');
const whiteSpaceLength = rule.match(/^\s*/)[0].length;
const firstLineIndentRegExp = new RegExp(`^\\s{${whiteSpaceLength}}`);
const otherLinesIndentResExp = new RegExp(`\\n\\s{${whiteSpaceLength}}`, 'mg');
// console.log(rule.match(otherLinesIndentResExp));
// 删除多余的缩进
rule = rule.replace(firstLineIndentRegExp, '').replace(otherLinesIndentResExp, '\n');
return rule;
}
}
// 创建一个Component的样式上下文
const existedInstances = {};
const createStyleContext = (contextId) => {
if (!contextId) {
console.error('The param(`contextId`) is required for `createStyleContext`(tingle-style/util.js) method. ');
return;
}
return existedInstances[contextId] || (existedInstances[contextId] = new StyleContext(contextId));
};
/**
* 添加长度单位,默认单位是`px`
* @param any {Number|String|*}
* @returns {Number|String} 返回Number时一定是0 否则返回的都是带有单位的长度字符串值
* @demo
* unitize(10) // 10px
* unitize('10px') // 10px
* unitize('1rem') // 1rem
* unitize() // 0
*/
const unitize = (any) => {
let ret;
if (typeof any === 'number') {
ret = `${any}px`;
} else if (typeof any === 'string') {
if (any.match(/^\d+$/)) {
ret = `${any}px`;
} else {
ret = any;
}
} else {
ret = 0;
}
return ret;
};
export default {
createStyleContext,
unitize,
};