Add number & range variables. See #492

This commit is contained in:
Rob Garrison 2018-09-04 21:16:40 -05:00
parent ec477f0c02
commit 1632a8f364
3 changed files with 52 additions and 14 deletions

View File

@ -29,7 +29,7 @@ var usercss = (() => {
['version', 0], ['version', 0],
]); ]);
const MANDATORY_META = ['name', 'namespace', 'version']; const MANDATORY_META = ['name', 'namespace', 'version'];
const META_VARS = ['text', 'color', 'checkbox', 'select', 'dropdown', 'image']; const META_VARS = ['text', 'color', 'checkbox', 'select', 'dropdown', 'image', 'number', 'range'];
const META_URLS = [...KNOWN_META.keys()].filter(k => k.endsWith('URL')); const META_URLS = [...KNOWN_META.keys()].filter(k => k.endsWith('URL'));
const BUILDER = { const BUILDER = {
@ -203,6 +203,22 @@ var usercss = (() => {
break; break;
} }
case 'range': {
state.errorPrefix = 'Invalid JSON: ';
parseJSONValue(state);
state.errorPrefix = '';
// [ start, end, step, default ]
if (Array.isArray(state.value) && state.value.length === 4) {
result.range = state.value;
result.default = state.value[3];
} else {
// not a range, fallback to text
result.type = 'text';
result.default = state.value;
}
break;
}
case 'dropdown': case 'dropdown':
case 'image': { case 'image': {
if (text[re.lastIndex] !== '{') { if (text[re.lastIndex] !== '{') {
@ -235,7 +251,7 @@ var usercss = (() => {
} }
default: { default: {
// text, color // text, color, number
parseStringToEnd(state); parseStringToEnd(state);
result.default = state.value; result.default = state.value;
} }

View File

@ -94,7 +94,8 @@
flex-shrink: 0; flex-shrink: 0;
} }
.config-value:not(.onoffswitch):not(.select-resizer) > :not(:last-child) { .config-value:not(.onoffswitch):not(.select-resizer) > :not(:last-child),
.current-value {
margin-right: 4px; margin-right: 4px;
} }

View File

@ -213,7 +213,7 @@ function configDialog(style) {
]) ])
]); ]);
for (const va of vars) { for (const va of vars) {
let children; let children, options;
switch (va.type) { switch (va.type) {
case 'color': case 'color':
children = [ children = [
@ -259,14 +259,27 @@ function configDialog(style) {
break; break;
default: default:
children = [ options = {
va.input = $create('input.config-value', { va,
va, type: va.type,
type: 'text', onchange: updateVarOnChange,
onchange: updateVarOnChange, oninput: updateVarOnInput,
oninput: updateVarOnInput, };
}), if (va.type === 'range') {
]; options.min = va.range[0];
options.max = va.range[1];
options.step = va.range[2];
options.value = va.default;
children = [
$create('span.current-value', {textContent: va.value}),
va.input = $create('input.config-value', options),
];
} else {
children = [
va.input = $create('input.config-value', options),
];
}
break; break;
} }
@ -287,6 +300,9 @@ function configDialog(style) {
function updateVarOnChange() { function updateVarOnChange() {
this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0'; this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0';
if (this.type === 'range') {
$('.current-value', this.parentNode).textContent = this.va.value;
}
} }
function updateVarOnInput(event, debounced = false) { function updateVarOnInput(event, debounced = false) {
@ -307,9 +323,14 @@ function configDialog(style) {
} }
} else if (va.type === 'checkbox') { } else if (va.type === 'checkbox') {
va.input.checked = Number(value); va.input.checked = Number(value);
} else {
va.input.value = value;
} }
if (va.type === 'range') {
const span = $('.current-value', va.input.parentNode);
if (span) {
span.textContent = value;
}
}
va.input.value = value;
if (!prefs.get('config.autosave')) { if (!prefs.get('config.autosave')) {
renderValueState(va); renderValueState(va);
} }