diff --git a/edit/base.js b/edit/base.js index 0764d8d9..582f43d7 100644 --- a/edit/base.js +++ b/edit/base.js @@ -33,6 +33,7 @@ const editor = { /** @type {'customName'|'name'} */ nameTarget: 'name', previewDelay: 200, // Chrome devtools uses 200 + saving: false, scrollInfo: null, cancel: () => location.assign('/manage.html'), diff --git a/edit/edit.js b/edit/edit.js index da4771f8..aa959128 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -61,19 +61,11 @@ baseInit.ready.then(async () => { //#endregion //#region events -const IGNORE_UPDATE_REASONS = [ - 'editPreview', - 'editPreviewEnd', - 'editSave', - // https://github.com/openstyles/stylus/issues/807 is closed without fix - // 'config, -]; - msg.onExtension(request => { const {style} = request; switch (request.method) { case 'styleUpdated': - if (editor.style.id === style.id && !IGNORE_UPDATE_REASONS.includes(request.reason)) { + if (editor.style.id === style.id) { handleExternalUpdate(request); } break; @@ -89,6 +81,14 @@ msg.onExtension(request => { }); async function handleExternalUpdate({style, reason}) { + if (reason === 'editPreview' || + reason === 'editPreviewEnd') { + return; + } + if (reason === 'editSave' && editor.saving) { + editor.saving = false; + return; + } if (reason === 'toggle') { if (editor.dirty.isDirty()) { 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) { $('#enabled').checked = enabled; editor.updateEnabledness(enabled); diff --git a/edit/sections-editor.js b/edit/sections-editor.js index 6326da8a..214a6a73 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -7,6 +7,7 @@ /* global editor */ /* global linterMan */ /* global prefs */ +/* global styleSectionsEqual */ // sections-util.js /* global t */// localization.js 'use strict'; @@ -86,9 +87,15 @@ function SectionsEditor() { }, async replaceStyle(newStyle) { + const sameCode = styleSectionsEqual(newStyle, getModel()); + if (!sameCode && !await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) { + return; + } dirty.clear(); // FIXME: avoid recreating all editors? - await initSections(newStyle.sections, {replace: true}); + if (!sameCode) { + await initSections(newStyle.sections, {replace: true}); + } Object.assign(style, newStyle); editor.updateClass(); updateMeta(); @@ -99,10 +106,7 @@ function SectionsEditor() { updateLivePreview(); }, - async save() { - if (!dirty.isDirty()) { - return; - } + async saveImpl() { let newStyle = getModel(); if (!validate(newStyle)) { return; diff --git a/edit/source-editor.js b/edit/source-editor.js index 9c7a1415..dc5e1388 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -61,8 +61,7 @@ function SourceEditor() { cm.focus(); } }, - async save() { - if (!dirty.isDirty()) return; + async saveImpl() { const sourceCode = cm.getValue(); try { const {customName, enabled, id} = style; @@ -221,7 +220,6 @@ function SourceEditor() { return; } - // TODO: also confirm in sections-editor? if (await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) { updateEnvironment(); if (!sameCode) {