diff --git a/edit/edit.js b/edit/edit.js index ded0713f..c289810a 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -1910,16 +1910,22 @@ chrome.runtime.onMessage.addListener(onRuntimeMessage); function replaceStyle(request) { const codeIsUpdated = request.codeIsUpdated !== false; - if (!isClean() && (!codeIsUpdated || !confirm(t('styleUpdateDiscardChanges')))) { + if (codeIsUpdated && editor && editor.isWarm() && !confirm(t('styleUpdateDiscardChanges'))) { return; } + doReplace(); - if (!isUsercss(request.style)) { - initWithSectionStyle(request); - return; + function doReplace() { + if (!isUsercss(request.style)) { + initWithSectionStyle(request); + return; + } + if (codeIsUpdated) { + editor.replaceStyle(request.style); + } else { + editor.replaceMetas(request.style); + } } - - editor.replaceStyle(request.style, codeIsUpdated); } function onRuntimeMessage(request) { diff --git a/edit/source-editor.js b/edit/source-editor.js index b14dc448..6b93c0d4 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -7,6 +7,9 @@ 'use strict'; function createSourceEditor(style) { + // a flag for isWorking() + let hadBeenSaved = false; + // draw HTML $('#sections').innerHTML = ''; $('#name').disabled = true; @@ -614,6 +617,7 @@ ${section} cm.setCursor(cursor); } dirty.clear(); + hadBeenSaved = false; } function toggleStyle() { @@ -637,17 +641,33 @@ ${section} sourceCode: style.sourceCode })) .then(replaceStyle) + .then(() => { + hadBeenSaved = true; + }) .catch(err => { console.error(err); alert(err); }); } + function isWarm() { + // indicate that the editor had been touched by the user + return dirty.isDirty() || hadBeenSaved; + } + + function replaceMetas(newStyle) { + style.enabled = newStyle.enabled; + dirty.clear('enabled'); + updateMetas(); + } + return { replaceStyle, + replaceMetas, save, toggleStyle, isDirty: dirty.isDirty, - getStyle: () => style + getStyle: () => style, + isWarm }; }