Fix: add isWarm flag to decide if the editor can be reloaded

This commit is contained in:
eight 2017-10-16 16:08:13 +08:00
parent 0a6de8edb8
commit ce348d4f86
2 changed files with 33 additions and 7 deletions

View File

@ -1910,16 +1910,22 @@ chrome.runtime.onMessage.addListener(onRuntimeMessage);
function replaceStyle(request) { function replaceStyle(request) {
const codeIsUpdated = request.codeIsUpdated !== false; const codeIsUpdated = request.codeIsUpdated !== false;
if (!isClean() && (!codeIsUpdated || !confirm(t('styleUpdateDiscardChanges')))) { if (codeIsUpdated && editor && editor.isWarm() && !confirm(t('styleUpdateDiscardChanges'))) {
return; return;
} }
doReplace();
if (!isUsercss(request.style)) { function doReplace() {
initWithSectionStyle(request); if (!isUsercss(request.style)) {
return; initWithSectionStyle(request);
return;
}
if (codeIsUpdated) {
editor.replaceStyle(request.style);
} else {
editor.replaceMetas(request.style);
}
} }
editor.replaceStyle(request.style, codeIsUpdated);
} }
function onRuntimeMessage(request) { function onRuntimeMessage(request) {

View File

@ -7,6 +7,9 @@
'use strict'; 'use strict';
function createSourceEditor(style) { function createSourceEditor(style) {
// a flag for isWorking()
let hadBeenSaved = false;
// draw HTML // draw HTML
$('#sections').innerHTML = ''; $('#sections').innerHTML = '';
$('#name').disabled = true; $('#name').disabled = true;
@ -614,6 +617,7 @@ ${section}
cm.setCursor(cursor); cm.setCursor(cursor);
} }
dirty.clear(); dirty.clear();
hadBeenSaved = false;
} }
function toggleStyle() { function toggleStyle() {
@ -637,17 +641,33 @@ ${section}
sourceCode: style.sourceCode sourceCode: style.sourceCode
})) }))
.then(replaceStyle) .then(replaceStyle)
.then(() => {
hadBeenSaved = true;
})
.catch(err => { .catch(err => {
console.error(err); console.error(err);
alert(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 { return {
replaceStyle, replaceStyle,
replaceMetas,
save, save,
toggleStyle, toggleStyle,
isDirty: dirty.isDirty, isDirty: dirty.isDirty,
getStyle: () => style getStyle: () => style,
isWarm
}; };
} }