From 54963ee298e6ab28110b056138a2f2a6128300cf Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 29 Dec 2021 20:49:37 +0300 Subject: [PATCH] proper cleanup on close + builtin event emitter --- edit/edit.js | 2 +- edit/settings.js | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/edit/edit.js b/edit/edit.js index 72546a82..a4fa8cb7 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -110,7 +110,7 @@ async function handleExternalUpdate({style, reason}) { } else { await editor.replaceStyle(style); } - editor.updateSettings(); + window.dispatchEvent(new Event('styleSettings')); } window.on('beforeunload', e => { diff --git a/edit/settings.js b/edit/settings.js index 0decb184..6a3bd313 100644 --- a/edit/settings.js +++ b/edit/settings.js @@ -8,8 +8,9 @@ 'use strict'; function StyleSettings() { + const ssId = 'styleSettings'; 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 elSave = $('#ss-save', ui); const pendingSetters = new Map(); @@ -22,10 +23,10 @@ function StyleSettings() { initArea('inclusions'), initArea('exclusions'), ]; - (editor.updateSettings = () => { - updaters.forEach(fn => fn()); - })(); - helpPopup.show(t('styleSettings'), ui, { + update(); + window.on(ssId, update); + window.on('closeHelp', () => window.off(ssId, update), {once: true}); + helpPopup.show(t(ssId), ui, { className: 'style-settings-popup', }); elSave.onclick = save; @@ -81,6 +82,7 @@ function StyleSettings() { function save() { pendingSetters.forEach((fn, el) => fn(el.value)); + pendingSetters.clear(); helpPopup.div.classList.remove('dirty'); elSave.disabled = true; } @@ -88,4 +90,8 @@ function StyleSettings() { function textToList(text) { return text.split(/\n/).map(s => s.trim()).filter(Boolean); } + + function update() { + updaters.forEach(fn => fn()); + } }