Add: confirmation to replace dirty style

This commit is contained in:
eight 2017-10-09 00:43:00 +08:00
parent 228057d231
commit 1a6f9dfcaa
2 changed files with 45 additions and 29 deletions

View File

@ -824,6 +824,10 @@
} }
} }
}, },
"styleUpdateDiscardChanges": {
"message": "The style is changed outside of the editor. Would you like to reload the style?",
"description": "Confirmation to update the style in the editor"
},
"stylusUnavailableForURL": { "stylusUnavailableForURL": {
"message": "Stylus doesn't work on pages like this.", "message": "Stylus doesn't work on pages like this.",
"description": "Note in the toolbar pop-up when on a URL Stylus can't affect" "description": "Note in the toolbar pop-up when on a URL Stylus can't affect"

View File

@ -501,7 +501,7 @@ window.onbeforeunload = () => {
rememberWindowSize(); rememberWindowSize();
} }
document.activeElement.blur(); document.activeElement.blur();
if ((!editor && isCleanGlobal()) || (editor && !editor.isDirty())) { if (isClean()) {
return; return;
} }
updateLintReportIfEnabled(null, 0); updateLintReportIfEnabled(null, 0);
@ -509,6 +509,14 @@ window.onbeforeunload = () => {
return t('styleChangesNotSaved'); return t('styleChangesNotSaved');
}; };
function isClean() {
if (editor) {
return !editor.isDirty();
} else {
return isCleanGlobal();
}
}
function addAppliesTo(list, name, value) { function addAppliesTo(list, name, value) {
const showingEverything = $('.applies-to-everything', list) !== null; const showingEverything = $('.applies-to-everything', list) !== null;
// blow away 'Everything' if it's there // blow away 'Everything' if it's there
@ -1218,7 +1226,12 @@ function init() {
getStyle().then(style => { getStyle().then(style => {
styleId = style.id; styleId = style.id;
sessionStorage.justEditedStyleId = styleId; sessionStorage.justEditedStyleId = styleId;
initWithStyle({style});
if (!isUsercss(style)) {
initWithSectionStyle({style});
} else {
editor = createSourceEditor(style);
}
}); });
function getStyle() { function getStyle() {
@ -1263,32 +1276,14 @@ function setStyleMeta(style) {
$('#url').href = style.url || ''; $('#url').href = style.url || '';
} }
function initWithStyle(request) { function isUsercss(style) {
if (!isUsercss()) { if (style.usercssData) {
initWithSectionStyle(request); return true;
return;
} }
if (!style.id && prefs.get('newStyleFormat') === 'usercss') {
if (!editor) { return true;
editor = createSourceEditor(request.style);
return;
}
if (request.codeIsUpdated === false) {
editor.updateStyleMeta(request.style);
} else {
editor.replaceStyle(request.style);
}
function isUsercss() {
if (request.style.usercssData) {
return true;
}
if (!request.style.id && prefs.get('newStyleFormat') === 'usercss') {
return true;
}
return false;
} }
return false;
} }
function initWithSectionStyle({style, codeIsUpdated}) { function initWithSectionStyle({style, codeIsUpdated}) {
@ -1838,18 +1833,35 @@ function getParams() {
chrome.runtime.onMessage.addListener(onRuntimeMessage); chrome.runtime.onMessage.addListener(onRuntimeMessage);
function replaceStyle(request) {
if (!isClean() && !confirm(t('styleUpdateDiscardChanges'))) {
return;
}
if (!isUsercss(request.style)) {
initWithSectionStyle(request);
return;
}
if (request.codeIsUpdated === false) {
editor.updateStyleMeta(request.style);
} else {
editor.replaceStyle(request.style);
}
}
function onRuntimeMessage(request) { function onRuntimeMessage(request) {
switch (request.method) { switch (request.method) {
case 'styleUpdated': case 'styleUpdated':
if (styleId && styleId === request.style.id && request.reason !== 'editSave') { if (styleId && styleId === request.style.id && request.reason !== 'editSave' && request.reason !== 'config') {
if ((request.style.sections[0] || {}).code === null) { if ((request.style.sections[0] || {}).code === null) {
// the code-less style came from notifyAllTabs // the code-less style came from notifyAllTabs
onBackgroundReady().then(() => { onBackgroundReady().then(() => {
request.style = BG.cachedStyles.byId.get(request.style.id); request.style = BG.cachedStyles.byId.get(request.style.id);
initWithStyle(request); replaceStyle(request);
}); });
} else { } else {
initWithStyle(request); replaceStyle(request);
} }
} }
break; break;