2021-12-29 19:57:22 +00:00
|
|
|
/* global $ $$ moveFocus setupLivePrefs */// dom.js
|
2021-12-07 11:43:21 +00:00
|
|
|
/* global API */// msg.js
|
2021-12-29 19:57:22 +00:00
|
|
|
/* global editor */
|
|
|
|
/* global helpPopup */// util.js
|
|
|
|
/* global t */// localization.js
|
|
|
|
/* global debounce */// toolbox.js
|
2021-12-07 04:44:49 +00:00
|
|
|
/* exported StyleSettings */
|
|
|
|
'use strict';
|
|
|
|
|
2022-01-18 13:39:33 +00:00
|
|
|
async function StyleSettings() {
|
2021-12-29 19:57:22 +00:00
|
|
|
const AUTOSAVE_DELAY = 500; // same as config-dialog.js
|
|
|
|
const SS_ID = 'styleSettings';
|
2022-01-18 13:39:33 +00:00
|
|
|
await t.fetchTemplate('/edit/settings.html', SS_ID);
|
2021-12-29 19:57:22 +00:00
|
|
|
const {style} = editor;
|
|
|
|
const ui = t.template[SS_ID].cloneNode(true);
|
|
|
|
const elAuto = $('[id="config.autosave"]', ui);
|
|
|
|
const elSave = $('#ss-save', ui);
|
|
|
|
const pendingSetters = new Map();
|
|
|
|
const updaters = [
|
|
|
|
initInput('#ss-update-url', () => style.updateUrl || '',
|
|
|
|
val => API.styles.config(style.id, 'updateUrl', val)),
|
|
|
|
initRadio('ss-scheme', () => style.preferScheme || 'none',
|
|
|
|
val => API.styles.config(style.id, 'preferScheme', val)),
|
|
|
|
initArea('inclusions'),
|
|
|
|
initArea('exclusions'),
|
2021-12-07 04:44:49 +00:00
|
|
|
];
|
2021-12-29 19:57:22 +00:00
|
|
|
update();
|
|
|
|
window.on(SS_ID, update);
|
|
|
|
window.on('closeHelp', () => window.off(SS_ID, update), {once: true});
|
|
|
|
helpPopup.show(t(SS_ID), ui, {
|
|
|
|
className: 'style-settings-popup',
|
|
|
|
});
|
|
|
|
elSave.onclick = save;
|
|
|
|
$('#ss-close', ui).onclick = helpPopup.close;
|
|
|
|
setupLivePrefs([elAuto.id]);
|
|
|
|
moveFocus(ui, 0);
|
2021-12-07 04:44:49 +00:00
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function autosave(el, setter) {
|
|
|
|
pendingSetters.set(el, setter);
|
|
|
|
helpPopup.div.classList.add('dirty');
|
|
|
|
elSave.disabled = false;
|
|
|
|
if (elAuto.checked) debounce(save, AUTOSAVE_DELAY);
|
2021-12-07 04:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function initArea(type) {
|
|
|
|
const selector = `#ss-${type}`;
|
|
|
|
const el = $(selector, ui);
|
|
|
|
el.oninput = () => {
|
2021-12-07 11:43:21 +00:00
|
|
|
const val = el.value;
|
|
|
|
el.rows = val.match(/^/gm).length + !val.endsWith('\n');
|
2021-12-29 19:57:22 +00:00
|
|
|
};
|
|
|
|
return initInput(selector,
|
2021-12-07 11:43:21 +00:00
|
|
|
() => {
|
|
|
|
const list = style[type] || [];
|
|
|
|
const text = list.join('\n');
|
|
|
|
el.rows = (list.length || 1) + 1;
|
|
|
|
return text;
|
|
|
|
},
|
2021-12-29 19:57:22 +00:00
|
|
|
val => API.styles.config(style.id, type, textToList(val))
|
2021-12-07 11:43:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function initInput(selector, getter, setter) {
|
|
|
|
const el = $(selector, ui);
|
|
|
|
el.oninput = () => autosave(el, setter);
|
|
|
|
return () => {
|
|
|
|
const val = getter();
|
|
|
|
// Skipping if unchanged to preserve the Undo history of the input
|
|
|
|
if (el.value !== val) el.value = val;
|
2021-12-07 04:44:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-29 19:57:22 +00:00
|
|
|
function initRadio(name, getter, setter) {
|
|
|
|
for (const el of $$(`[name="${name}"]`, ui)) {
|
|
|
|
el.onchange = () => {
|
|
|
|
if (el.checked) autosave(el, setter);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
$(`[name="${name}"][value="${getter()}"]`, ui).checked = true;
|
2021-12-07 04:44:49 +00:00
|
|
|
};
|
|
|
|
}
|
2021-12-29 19:57:22 +00:00
|
|
|
|
|
|
|
function save() {
|
|
|
|
pendingSetters.forEach((fn, el) => fn(el.value));
|
|
|
|
pendingSetters.clear();
|
|
|
|
helpPopup.div.classList.remove('dirty');
|
|
|
|
elSave.disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function textToList(text) {
|
|
|
|
return text.split(/\n/).map(s => s.trim()).filter(Boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
updaters.forEach(fn => fn());
|
|
|
|
}
|
2021-12-07 04:44:49 +00:00
|
|
|
}
|