stylus/edit/settings.js
eight 9d1243073b
Add: style settings (#1358)
* Add: style settings

* Change: use radio instead of select for dark/light mode

* Change: x -> Delete

* Change: (in|ex)clusion messages

* Fix: avoid extra space when there is no rule

* Fix: UI in mobile

* Change: delete priority

* Change: use textarea for include/exclude, remove isCodeUpdated

* Fix: separate toggle

* Fix: minor

* Fix: remove codeIsUpdated in styleman
2021-12-07 12:44:49 +08:00

66 lines
1.8 KiB
JavaScript

/* global API */
/* 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)),
createInput('.style-include textarea', () => (style.inclusions || []).join('\n'),
e => API.styles.config(style.id, 'inclusions', textToList(e.target.value))),
createInput('.style-exclude textarea', () => (style.exclusions || []).join('\n'),
e => API.styles.config(style.id, 'exclusions', textToList(e.target.value))),
];
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;
document.querySelector('.style-settings').disabled = false;
inputs.forEach(i => i.update());
}
function createRadio(selector, getter, setter) {
const els = document.querySelectorAll(selector);
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) {
const el = document.querySelector(selector);
el.addEventListener('change', setter);
return {
update() {
el.value = getter();
},
};
}
}