diff --git a/edit/sections-editor.js b/edit/sections-editor.js index 79605374..e32e8a1c 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -9,7 +9,7 @@ function createSectionsEditor(style) { let INC_ID = 0; // an increment id that is used by various object to track the order const dirty = dirtyReporter(); - dirty.onChange(updateTitle); + dirty.onChange(updateDirty); const container = $('#sections'); const sections = []; @@ -18,7 +18,7 @@ function createSectionsEditor(style) { nameEl.addEventListener('input', () => { dirty.modify('name', style.name, nameEl.value); style.name = nameEl.value; - updateTitle(); + updateDirty(); }); const enabledEl = $('#enabled'); @@ -413,7 +413,7 @@ function createSectionsEditor(style) { nameEl.value = style.name || ''; enabledEl.checked = style.enabled !== false; $('#url').href = style.url || ''; - updateTitle(); + updateDirty(); } function updateLivePreview() { @@ -424,12 +424,12 @@ function createSectionsEditor(style) { livePreview.update(getModel()); } - function updateTitle() { - const name = style.name; - const clean = !dirty.isDirty(); - const title = !style.id ? t('addStyleTitle') : name; - document.title = (clean ? '' : '* ') + title; - $('#save-button').disabled = clean; + function updateDirty() { + const isDirty = dirty.isDirty(); + const name = style.id ? style.name : t('addStyleTitle'); + document.title = (isDirty ? '* ' : '') + name; + document.body.classList.toggle('dirty', isDirty); + $('#save-button').disabled = !isDirty; } function initSection({ diff --git a/edit/source-editor.js b/edit/source-editor.js index 59209c5b..68b9c695 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -16,12 +16,7 @@ function createSourceEditor(style) { $('#sections').appendChild($create('.single-editor')); const dirty = dirtyReporter(); - dirty.onChange(() => { - const isDirty = dirty.isDirty(); - document.body.classList.toggle('dirty', isDirty); - $('#save-button').disabled = !isDirty; - updateTitle(); - }); + dirty.onChange(updateDirty); // normalize style if (!style.id) setupNewStyle(style); @@ -171,16 +166,16 @@ function createSourceEditor(style) { $('#name').value = style.name; $('#enabled').checked = style.enabled; $('#url').href = style.url; - updateTitle(); + updateDirty(); return cm.setPreprocessor((style.usercssData || {}).preprocessor); } - function updateTitle() { - const newTitle = (dirty.isDirty() ? '* ' : '') + - (style.id ? style.name : t('addStyleTitle')); - if (document.title !== newTitle) { - document.title = newTitle; - } + function updateDirty() { + const isDirty = dirty.isDirty(); + const name = style.id ? style.name : t('addStyleTitle'); + document.title = (isDirty ? '* ' : '') + name; + document.body.classList.toggle('dirty', isDirty); + $('#save-button').disabled = !isDirty; } function replaceStyle(newStyle, codeIsUpdated) {