code cosmetics
This commit is contained in:
parent
4338f67352
commit
a66a1f8ed6
|
@ -45,7 +45,7 @@ const BUILDERS = Object.assign(Object.create(null), {
|
||||||
},
|
},
|
||||||
|
|
||||||
uso: {
|
uso: {
|
||||||
async pre(source, vars) {
|
pre(source, vars) {
|
||||||
require(['/js/color/color-converter']); /* global colorConverter */
|
require(['/js/color/color-converter']); /* global colorConverter */
|
||||||
const pool = new Map();
|
const pool = new Map();
|
||||||
return doReplace(source);
|
return doReplace(source);
|
||||||
|
@ -99,26 +99,24 @@ const BUILDERS = Object.assign(Object.create(null), {
|
||||||
});
|
});
|
||||||
|
|
||||||
/* exported compileUsercss */
|
/* exported compileUsercss */
|
||||||
|
/**
|
||||||
|
* @param {string} preprocessor
|
||||||
|
* @param {string} code
|
||||||
|
* @param {Object} [vars] - WARNING: each var's `value` will be overwritten
|
||||||
|
(not a problem currently as this code runs in a worker so `vars` is just a copy)
|
||||||
|
* @returns {Promise<{sections, errors}>}
|
||||||
|
*/
|
||||||
async function compileUsercss(preprocessor, code, vars) {
|
async function compileUsercss(preprocessor, code, vars) {
|
||||||
let builder = BUILDERS[preprocessor];
|
let builder = BUILDERS[preprocessor];
|
||||||
if (!builder) {
|
if (!builder) {
|
||||||
builder = BUILDERS.default;
|
builder = BUILDERS.default;
|
||||||
if (preprocessor != null) console.warn(`Unknown preprocessor "${preprocessor}"`);
|
if (preprocessor != null) console.warn(`Unknown preprocessor "${preprocessor}"`);
|
||||||
}
|
}
|
||||||
// simplify vars by merging `va.default` to `va.value`, so BUILDER don't
|
if (vars) {
|
||||||
// need to test each va's default value.
|
simplifyUsercssVars(vars);
|
||||||
vars = Object.entries(vars || {}).reduce((output, [key, va]) => {
|
} else {
|
||||||
// TODO: handle customized image
|
vars = {};
|
||||||
const prop = va.value == null ? 'default' : 'value';
|
}
|
||||||
const value =
|
|
||||||
/^(select|dropdown|image)$/.test(va.type) ?
|
|
||||||
va.options.find(o => o.name === va[prop]).value :
|
|
||||||
/^(number|range)$/.test(va.type) && va.units ?
|
|
||||||
va[prop] + va.units :
|
|
||||||
va[prop];
|
|
||||||
output[key] = Object.assign({}, va, {value});
|
|
||||||
return output;
|
|
||||||
}, {});
|
|
||||||
if (builder.pre) {
|
if (builder.pre) {
|
||||||
code = await builder.pre(code, vars);
|
code = await builder.pre(code, vars);
|
||||||
}
|
}
|
||||||
|
@ -129,3 +127,26 @@ async function compileUsercss(preprocessor, code, vars) {
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds units and sets `null` values to their defaults
|
||||||
|
* WARNING: the old value is overwritten
|
||||||
|
*/
|
||||||
|
function simplifyUsercssVars(vars) {
|
||||||
|
for (const va of Object.values(vars)) {
|
||||||
|
let value = va.value != null ? va.value : va.default;
|
||||||
|
switch (va.type) {
|
||||||
|
case 'select':
|
||||||
|
case 'dropdown':
|
||||||
|
case 'image':
|
||||||
|
// TODO: handle customized image
|
||||||
|
value = va.options.find(o => o.name === value).value;
|
||||||
|
break;
|
||||||
|
case 'number':
|
||||||
|
case 'range':
|
||||||
|
value += va.units || '';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
va.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user