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) {
const codeIsUpdated = request.codeIsUpdated !== false;
if (!isClean() && (!codeIsUpdated || !confirm(t('styleUpdateDiscardChanges')))) {
if (codeIsUpdated && editor && editor.isWarm() && !confirm(t('styleUpdateDiscardChanges'))) {
return;
}
doReplace();
function doReplace() {
if (!isUsercss(request.style)) {
initWithSectionStyle(request);
return;
}
editor.replaceStyle(request.style, codeIsUpdated);
if (codeIsUpdated) {
editor.replaceStyle(request.style);
} else {
editor.replaceMetas(request.style);
}
}
}
function onRuntimeMessage(request) {

View File

@ -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
};
}