From ce348d4f864ed286d7891eedfd4d90e51c7f0006 Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 16 Oct 2017 16:08:13 +0800 Subject: [PATCH] Fix: add isWarm flag to decide if the editor can be reloaded --- edit/edit.js | 18 ++++++++++++------ edit/source-editor.js | 22 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 7 deletions(-) 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 }; }