confirm reload in sectioned editor

This commit is contained in:
tophf 2022-01-10 19:12:29 +03:00
parent ba9d904ccc
commit 6e4fc3236e
4 changed files with 27 additions and 17 deletions

View File

@ -33,6 +33,7 @@ const editor = {
/** @type {'customName'|'name'} */ /** @type {'customName'|'name'} */
nameTarget: 'name', nameTarget: 'name',
previewDelay: 200, // Chrome devtools uses 200 previewDelay: 200, // Chrome devtools uses 200
saving: false,
scrollInfo: null, scrollInfo: null,
cancel: () => location.assign('/manage.html'), cancel: () => location.assign('/manage.html'),

View File

@ -61,19 +61,11 @@ baseInit.ready.then(async () => {
//#endregion //#endregion
//#region events //#region events
const IGNORE_UPDATE_REASONS = [
'editPreview',
'editPreviewEnd',
'editSave',
// https://github.com/openstyles/stylus/issues/807 is closed without fix
// 'config,
];
msg.onExtension(request => { msg.onExtension(request => {
const {style} = request; const {style} = request;
switch (request.method) { switch (request.method) {
case 'styleUpdated': case 'styleUpdated':
if (editor.style.id === style.id && !IGNORE_UPDATE_REASONS.includes(request.reason)) { if (editor.style.id === style.id) {
handleExternalUpdate(request); handleExternalUpdate(request);
} }
break; break;
@ -89,6 +81,14 @@ msg.onExtension(request => {
}); });
async function handleExternalUpdate({style, reason}) { async function handleExternalUpdate({style, reason}) {
if (reason === 'editPreview' ||
reason === 'editPreviewEnd') {
return;
}
if (reason === 'editSave' && editor.saving) {
editor.saving = false;
return;
}
if (reason === 'toggle') { if (reason === 'toggle') {
if (editor.dirty.isDirty()) { if (editor.dirty.isDirty()) {
editor.toggleStyle(style.enabled); editor.toggleStyle(style.enabled);
@ -191,6 +191,13 @@ window.on('beforeunload', e => {
} }
}, },
async save() {
if (dirty.isDirty()) {
editor.saving = true;
await editor.saveImpl();
}
},
toggleStyle(enabled = !style.enabled) { toggleStyle(enabled = !style.enabled) {
$('#enabled').checked = enabled; $('#enabled').checked = enabled;
editor.updateEnabledness(enabled); editor.updateEnabledness(enabled);

View File

@ -7,6 +7,7 @@
/* global editor */ /* global editor */
/* global linterMan */ /* global linterMan */
/* global prefs */ /* global prefs */
/* global styleSectionsEqual */ // sections-util.js
/* global t */// localization.js /* global t */// localization.js
'use strict'; 'use strict';
@ -86,9 +87,15 @@ function SectionsEditor() {
}, },
async replaceStyle(newStyle) { async replaceStyle(newStyle) {
const sameCode = styleSectionsEqual(newStyle, getModel());
if (!sameCode && !await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) {
return;
}
dirty.clear(); dirty.clear();
// FIXME: avoid recreating all editors? // FIXME: avoid recreating all editors?
if (!sameCode) {
await initSections(newStyle.sections, {replace: true}); await initSections(newStyle.sections, {replace: true});
}
Object.assign(style, newStyle); Object.assign(style, newStyle);
editor.updateClass(); editor.updateClass();
updateMeta(); updateMeta();
@ -99,10 +106,7 @@ function SectionsEditor() {
updateLivePreview(); updateLivePreview();
}, },
async save() { async saveImpl() {
if (!dirty.isDirty()) {
return;
}
let newStyle = getModel(); let newStyle = getModel();
if (!validate(newStyle)) { if (!validate(newStyle)) {
return; return;

View File

@ -61,8 +61,7 @@ function SourceEditor() {
cm.focus(); cm.focus();
} }
}, },
async save() { async saveImpl() {
if (!dirty.isDirty()) return;
const sourceCode = cm.getValue(); const sourceCode = cm.getValue();
try { try {
const {customName, enabled, id} = style; const {customName, enabled, id} = style;
@ -221,7 +220,6 @@ function SourceEditor() {
return; return;
} }
// TODO: also confirm in sections-editor?
if (await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) { if (await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) {
updateEnvironment(); updateEnvironment();
if (!sameCode) { if (!sameCode) {