proper cleanup on close + builtin event emitter

This commit is contained in:
tophf 2021-12-29 20:49:37 +03:00
parent b553325d7c
commit 54963ee298
2 changed files with 12 additions and 6 deletions

View File

@ -110,7 +110,7 @@ async function handleExternalUpdate({style, reason}) {
} else { } else {
await editor.replaceStyle(style); await editor.replaceStyle(style);
} }
editor.updateSettings(); window.dispatchEvent(new Event('styleSettings'));
} }
window.on('beforeunload', e => { window.on('beforeunload', e => {

View File

@ -8,8 +8,9 @@
'use strict'; 'use strict';
function StyleSettings() { function StyleSettings() {
const ssId = 'styleSettings';
const AUTOSAVE_DELAY = 500; // same as config-dialog.js const AUTOSAVE_DELAY = 500; // same as config-dialog.js
const ui = t.template.styleSettings.cloneNode(true); const ui = t.template[ssId].cloneNode(true);
const elAuto = $('[id="config.autosave"]', ui); const elAuto = $('[id="config.autosave"]', ui);
const elSave = $('#ss-save', ui); const elSave = $('#ss-save', ui);
const pendingSetters = new Map(); const pendingSetters = new Map();
@ -22,10 +23,10 @@ function StyleSettings() {
initArea('inclusions'), initArea('inclusions'),
initArea('exclusions'), initArea('exclusions'),
]; ];
(editor.updateSettings = () => { update();
updaters.forEach(fn => fn()); window.on(ssId, update);
})(); window.on('closeHelp', () => window.off(ssId, update), {once: true});
helpPopup.show(t('styleSettings'), ui, { helpPopup.show(t(ssId), ui, {
className: 'style-settings-popup', className: 'style-settings-popup',
}); });
elSave.onclick = save; elSave.onclick = save;
@ -81,6 +82,7 @@ function StyleSettings() {
function save() { function save() {
pendingSetters.forEach((fn, el) => fn(el.value)); pendingSetters.forEach((fn, el) => fn(el.value));
pendingSetters.clear();
helpPopup.div.classList.remove('dirty'); helpPopup.div.classList.remove('dirty');
elSave.disabled = true; elSave.disabled = true;
} }
@ -88,4 +90,8 @@ function StyleSettings() {
function textToList(text) { function textToList(text) {
return text.split(/\n/).map(s => s.trim()).filter(Boolean); return text.split(/\n/).map(s => s.trim()).filter(Boolean);
} }
function update() {
updaters.forEach(fn => fn());
}
} }