2021-12-07 11:43:21 +00:00
|
|
|
/* global $ $$ */// dom.js
|
|
|
|
/* global API */// msg.js
|
2021-12-07 04:44:49 +00:00
|
|
|
/* exported StyleSettings */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function StyleSettings(editor) {
|
|
|
|
let {style} = editor;
|
|
|
|
|
|
|
|
const inputs = [
|
|
|
|
createInput('.style-update-url input', () => style.updateUrl || '',
|
|
|
|
e => API.styles.config(style.id, 'updateUrl', e.target.value)),
|
|
|
|
createRadio('.style-prefer-scheme input', () => style.preferScheme || 'none',
|
|
|
|
e => API.styles.config(style.id, 'preferScheme', e.target.value)),
|
2021-12-07 11:43:21 +00:00
|
|
|
...[
|
|
|
|
['.style-include', 'inclusions'],
|
|
|
|
['.style-exclude', 'exclusions'],
|
|
|
|
].map(createArea),
|
2021-12-07 04:44:49 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
update(style);
|
|
|
|
|
|
|
|
editor.on('styleChange', update);
|
|
|
|
|
|
|
|
function textToList(text) {
|
|
|
|
const list = text.split(/\s*\r?\n\s*/g);
|
|
|
|
return list.filter(Boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
function update(newStyle, reason) {
|
|
|
|
if (!newStyle.id) return;
|
|
|
|
if (reason === 'editSave' || reason === 'config') return;
|
|
|
|
style = newStyle;
|
2021-12-07 11:43:21 +00:00
|
|
|
$('.style-settings').disabled = false;
|
2021-12-07 04:44:49 +00:00
|
|
|
inputs.forEach(i => i.update());
|
|
|
|
}
|
|
|
|
|
2021-12-07 11:43:21 +00:00
|
|
|
function createArea([parentSel, type]) {
|
|
|
|
const sel = parentSel + ' textarea';
|
|
|
|
const el = $(sel);
|
|
|
|
el.on('input', () => {
|
|
|
|
const val = el.value;
|
|
|
|
el.rows = val.match(/^/gm).length + !val.endsWith('\n');
|
|
|
|
});
|
|
|
|
return createInput(sel,
|
|
|
|
() => {
|
|
|
|
const list = style[type] || [];
|
|
|
|
const text = list.join('\n');
|
|
|
|
el.rows = (list.length || 1) + 1;
|
|
|
|
return text;
|
|
|
|
},
|
|
|
|
() => API.styles.config(style.id, type, textToList(el.value))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-07 04:44:49 +00:00
|
|
|
function createRadio(selector, getter, setter) {
|
2021-12-07 11:43:21 +00:00
|
|
|
const els = $$(selector);
|
2021-12-07 04:44:49 +00:00
|
|
|
for (const el of els) {
|
|
|
|
el.addEventListener('change', e => {
|
|
|
|
if (el.checked) {
|
|
|
|
setter(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
update() {
|
|
|
|
for (const el of els) {
|
|
|
|
if (el.value === getter()) {
|
|
|
|
el.checked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createInput(selector, getter, setter) {
|
2021-12-07 11:43:21 +00:00
|
|
|
const el = $(selector);
|
2021-12-07 04:44:49 +00:00
|
|
|
el.addEventListener('change', setter);
|
|
|
|
return {
|
|
|
|
update() {
|
|
|
|
el.value = getter();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|