diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 76becab3..38202629 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -171,6 +171,14 @@ "message": "No", "description": "'No' button in a confirm dialog" }, + "confirmDefault": { + "message": "Use default", + "description": "'Set to default' button in a confirm dialog" + }, + "confirmSave": { + "message": "Save", + "description": "'Save' button in a confirm dialog" + }, "confirmStop": { "message": "Stop", "description": "'Stop' button in a confirm dialog" diff --git a/manage/manage.js b/manage/manage.js index 081b3d22..1df09ba9 100644 --- a/manage/manage.js +++ b/manage/manage.js @@ -282,34 +282,84 @@ Object.assign(handleEvent, { }, config(event, {styleMeta: style}) { - let isChanged = false; + const form = buildConfigForm(); messageBox({ title: `Configure ${style.name}`, className: 'regular-form', - contents: buildConfigForm(), - buttons: [t('confirmClose')] - }).then(() => { - if (!isChanged) { + contents: form.el, + buttons: [ + t('confirmSave'), + { + textContent: t('confirmDefault'), + onclick: form.useDefault + }, + t('confirmCancel') + ] + }).then(result => { + if (result.button !== 0 && !result.enter) { return; } style.reason = 'config'; + const vars = form.getVars(); + let dirty = false; + for (const key of Object.keys(vars)) { + if (vars[key].dirty) { + dirty = true; + style.vars[key].value = vars[key].value; + } + } + if (!dirty) { + return; + } saveStyleSafe(style); }); function buildConfigForm() { const labels = []; - for (const va of Object.values(style.vars)) { - const input = $element({tag: 'input', type: 'text', value: va.value}); - input.oninput = () => { - isChanged = true; - va.value = input.value; - animateElement(input, {className: 'value-update'}); + const vars = deepCopy(style.vars); + for (const key of Object.keys(vars)) { + const va = vars[key]; + va.input = $element({tag: 'input', type: 'text', value: va.value}); + va.input.oninput = () => { + va.dirty = true; + va.value = va.input.value; }; - const label = $element({tag: 'label', appendChild: [va.label, input]}); + const label = $element({ + tag: 'label', + appendChild: [va.label, va.input] + }); labels.push(label); } - return labels; + drawValues(); + + function drawValues() { + for (const key of Object.keys(vars)) { + const va = vars[key]; + va.input.value = va.value === null || va.value === undefined ? + va.default : va.value; + } + } + + function useDefault() { + for (const key of Object.keys(vars)) { + const va = vars[key]; + va.dirty = va.value !== null && va.value !== undefined && + va.value !== va.default; + va.value = null; + } + drawValues(); + } + + function getVars() { + return vars; + } + + return { + el: labels, + useDefault, + getVars + }; } }, diff --git a/msgbox/msgbox.css b/msgbox/msgbox.css index 59a49636..59b87984 100644 --- a/msgbox/msgbox.css +++ b/msgbox/msgbox.css @@ -145,17 +145,3 @@ border-radius: .25rem; border-width: 1px; } - -#message-box.regular-form input[type=text].value-update { - animation-name: input-fadeout; - animation-duration: .4s; -} - -@keyframes input-fadeout { - from { - background: palegreen; - } - to { - background: white; - } -}