Change how var is saved

This commit is contained in:
eight 2017-09-01 14:38:46 +08:00
parent 9c2acd5cc9
commit 8607d779f9

View File

@ -152,7 +152,8 @@ var usercss = (function () {
style.vars[key] = { style.vars[key] = {
type: guessType(value), type: guessType(value),
label: label1 || label2, label: label1 || label2,
value: value value: null, // '.value' holds the value set by users.
default: value // '.default' holds the value extract from meta.
} }
)) ))
); );
@ -168,10 +169,12 @@ var usercss = (function () {
builder = BUILDER.default; builder = BUILDER.default;
} }
const vars = simpleVars(style.vars);
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
// preprocess // preprocess
if (builder.preprocess) { if (builder.preprocess) {
return builder.preprocess(style.source, style.vars); return builder.preprocess(style.source, vars);
} }
return style.source; return style.source;
}).then(mozStyle => }).then(mozStyle =>
@ -184,11 +187,24 @@ var usercss = (function () {
).then(() => { ).then(() => {
// postprocess // postprocess
if (builder.postprocess) { if (builder.postprocess) {
return builder.postprocess(style.sections, style.vars); return builder.postprocess(style.sections, vars);
} }
}).then(() => style); }).then(() => style);
} }
function simpleVars(vars) {
// simplify vars by merging `va.default` to `va.value`, so BUILDER don't
// need to test each va's default value.
return Object.keys(vars).reduce((output, key) => {
const va = vars[key];
output[key] = {
value: va.value === null || va.value === undefined ?
va.default : va.value
};
return output;
}, {});
}
function validate(style) { function validate(style) {
// mandatory fields // mandatory fields
for (const prop of ['name', 'namespace', 'version']) { for (const prop of ['name', 'namespace', 'version']) {