2019-11-07 14:06:59 +08:00
|
|
|
|
// 设置自定义颜色
|
2019-11-05 21:32:50 +08:00
|
|
|
|
function setColorWithTemplate(template) {
|
2019-12-05 21:12:13 +08:00
|
|
|
|
return function (color) {
|
2019-11-05 21:32:50 +08:00
|
|
|
|
let custom_theme = JSON.parse(JSON.stringify(template));
|
|
|
|
|
custom_theme.block.h1['border-bottom'] = `2px solid ${color}`;
|
|
|
|
|
custom_theme.block.h2['background'] = color;
|
|
|
|
|
custom_theme.block.h3['border-left'] = `3px solid ${color}`;
|
2019-11-08 10:30:32 +08:00
|
|
|
|
custom_theme.block.h4['color'] = color;
|
2019-11-05 21:32:50 +08:00
|
|
|
|
custom_theme.inline.strong['color'] = color;
|
|
|
|
|
return custom_theme
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let setColor = setColorWithTemplate(default_theme);
|
2019-12-05 17:03:54 +08:00
|
|
|
|
|
|
|
|
|
function customCssWithTemplate(template) {
|
2019-12-05 21:12:13 +08:00
|
|
|
|
return function (jsonString) {
|
2019-12-05 17:03:54 +08:00
|
|
|
|
let custom_theme = JSON.parse(JSON.stringify(template));
|
|
|
|
|
custom_theme.block.h1 = jsonString.h1;
|
|
|
|
|
custom_theme.block.h2 = jsonString.h2;
|
|
|
|
|
custom_theme.block.h3 = jsonString.h3;
|
|
|
|
|
custom_theme.block.h4 = jsonString.h4;
|
|
|
|
|
custom_theme.block.p = jsonString.p;
|
|
|
|
|
return custom_theme;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let customCss = customCssWithTemplate(default_theme);
|
2019-12-05 21:12:13 +08:00
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将CSS形式的字符串转换为JSON
|
|
|
|
|
*
|
|
|
|
|
* @param {css字符串} css
|
|
|
|
|
*/
|
2019-12-05 21:12:13 +08:00
|
|
|
|
function css2json(css) {
|
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 移除CSS所有注释
|
2019-12-05 21:12:13 +08:00
|
|
|
|
while ((open = css.indexOf("/*")) !== -1 &&
|
|
|
|
|
(close = css.indexOf("*/")) !== -1) {
|
|
|
|
|
css = css.substring(0, open) + css.substring(close + 2);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 初始化返回值
|
2019-12-05 21:12:13 +08:00
|
|
|
|
let json = {};
|
|
|
|
|
|
|
|
|
|
while (css.length > 0) {
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 存储第一个左/右花括号的下标
|
2019-12-05 21:12:13 +08:00
|
|
|
|
const lbracket = css.indexOf('{');
|
|
|
|
|
const rbracket = css.indexOf('}');
|
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 第一步:将声明转换为Object,如:
|
|
|
|
|
// `font: 'Times New Roman' 1em; color: #ff0000; margin-top: 1em;`
|
|
|
|
|
// ==>
|
|
|
|
|
// `{"font": "'Times New Roman' 1em", "color": "#ff0000", "margin-top": "1em"}`
|
|
|
|
|
|
|
|
|
|
// 辅助方法:将array转为object
|
2019-12-05 21:12:13 +08:00
|
|
|
|
function toObject(array) {
|
|
|
|
|
let ret = {};
|
|
|
|
|
array.forEach(e => {
|
|
|
|
|
const index = e.indexOf(':');
|
|
|
|
|
const property = e.substring(0, index).trim();
|
|
|
|
|
const value = e.substring(index + 1).trim();
|
|
|
|
|
ret[property] = value;
|
|
|
|
|
});
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 切割声明块并移除空白符,然后放入数组中
|
2019-12-05 21:12:13 +08:00
|
|
|
|
let declarations = css.substring(lbracket + 1, rbracket)
|
|
|
|
|
.split(";")
|
|
|
|
|
.map(e => e.trim())
|
2019-12-06 17:08:05 +08:00
|
|
|
|
.filter(e => e.length > 0); // 移除所有""空值
|
2019-12-05 21:12:13 +08:00
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 转为Object对象
|
2019-12-05 21:12:13 +08:00
|
|
|
|
declarations = toObject(declarations);
|
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 第二步:选择器处理,每个选择器会与它对应的声明相关联,如:
|
|
|
|
|
// `h1, p#bar {color: red}`
|
|
|
|
|
// ==>
|
2019-12-05 21:12:13 +08:00
|
|
|
|
// {"h1": {color: red}, "p#bar": {color: red}}
|
|
|
|
|
|
|
|
|
|
let selectors = css.substring(0, lbracket)
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 以,切割,并移除空格:`"h1, p#bar, span.foo"` => ["h1", "p#bar", "span.foo"]
|
2019-12-05 21:12:13 +08:00
|
|
|
|
.split(",")
|
|
|
|
|
.map(selector => selector.trim());
|
|
|
|
|
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 迭代赋值
|
2019-12-05 21:12:13 +08:00
|
|
|
|
selectors.forEach(selector => {
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 若不存在,则先初始化
|
2019-12-05 21:12:13 +08:00
|
|
|
|
if (!json[selector]) json[selector] = {};
|
2019-12-06 17:08:05 +08:00
|
|
|
|
// 赋值到JSON
|
2019-12-05 21:12:13 +08:00
|
|
|
|
Object.keys(declarations).forEach(key => {
|
|
|
|
|
json[selector][key] = declarations[key];
|
|
|
|
|
});
|
|
|
|
|
});
|
2019-12-06 17:08:05 +08:00
|
|
|
|
|
|
|
|
|
// 继续下个声明块
|
2019-12-05 21:12:13 +08:00
|
|
|
|
css = css.slice(rbracket + 1).trim()
|
|
|
|
|
}
|
2019-12-06 17:08:05 +08:00
|
|
|
|
|
|
|
|
|
// 返回JSON形式的结果串
|
2019-12-05 21:12:13 +08:00
|
|
|
|
return json;
|
2019-12-06 17:08:05 +08:00
|
|
|
|
}
|