From 7a0d00a4babc3710da9c1a331adf5e376478f02e Mon Sep 17 00:00:00 2001 From: eight04 Date: Tue, 7 Dec 2021 02:45:58 +0800 Subject: [PATCH] Fix: separate toggle --- edit/edit.js | 11 +++++++++-- edit/sections-editor.js | 22 +++++++++++----------- edit/source-editor.js | 24 +++++++++++------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/edit/edit.js b/edit/edit.js index eea81cac..2e171735 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -1,5 +1,5 @@ /* global $ $create messageBoxProxy waitForSheet */// dom.js -/* global msg */// msg.js +/* global msg API */// msg.js /* global CodeMirror */ /* global SectionsEditor */ /* global SourceEditor */ @@ -70,7 +70,14 @@ msg.onExtension(request => { switch (request.method) { case 'styleUpdated': if (editor.style.id === style.id && !IGNORE_UPDATE_REASONS.includes(request.reason)) { - editor.emit('styleChange', request.style, request.reason, request.codeIsUpdated); + if (request.reason === 'toggle') { + editor.emit('styleToggled', request.style); + } else { + API.styles.get(request.style.id) + .then(style => { + editor.emit('styleChange', style, request.reason); + }); + } } break; case 'styleDeleted': diff --git a/edit/sections-editor.js b/edit/sections-editor.js index 103240b1..306babb5 100644 --- a/edit/sections-editor.js +++ b/edit/sections-editor.js @@ -128,18 +128,18 @@ function SectionsEditor() { editor.ready = initSections(style.sections); - editor.on('styleChange', async (newStyle, reason) => { - if (reason === 'new') return; // nothing is new for us - if (reason === 'toggle') { - if (!dirty.isDirty()) { - Object.assign(style, newStyle); - updateHeader(); - } - updateLivePreview(); - return; + editor.on('styleToggled', newStyle => { + if (!dirty.isDirty()) { + Object.assign(style, newStyle); + } else { + editor.toggleStyle(newStyle.enabled); } + updateHeader(); + updateLivePreview(); + }); + editor.on('styleChange', (newStyle, reason) => { + if (reason === 'new') return; // nothing is new for us if (reason === 'config') { - newStyle = await API.styles.get(newStyle.id); delete newStyle.sections; delete newStyle.name; delete newStyle.enabled; @@ -147,7 +147,7 @@ function SectionsEditor() { updateLivePreview(); return; } - editor.replaceStyle(await API.styles.get(newStyle.id)); + editor.replaceStyle(newStyle); }); /** @param {EditorSection} section */ diff --git a/edit/source-editor.js b/edit/source-editor.js index e372fa92..138a34f1 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -116,27 +116,25 @@ function SourceEditor() { if (!$isTextInput(document.activeElement)) { cm.focus(); } - editor.on('styleChange', async (newStyle, reason) => { + editor.on('styleToggled', newStyle => { + if (dirty.isDirty()) { + editor.toggleStyle(newStyle.enabled); + } else { + style.enabled = newStyle.enabled; + } + updateMeta(); + updateLivePreview(); + }); + editor.on('styleChange', (newStyle, reason) => { if (reason === 'new') return; if (reason === 'config') { - newStyle = await API.styles.get(newStyle.id); delete newStyle.sourceCode; delete newStyle.name; Object.assign(style, newStyle); updateLivePreview(); return; } - if (reason === 'toggle') { - if (dirty.isDirty()) { - editor.toggleStyle(newStyle.enabled); - } else { - style.enabled = newStyle.enabled; - } - updateMeta(); - updateLivePreview(); - return; - } - replaceStyle(await API.styles.get(newStyle.id)); + replaceStyle(newStyle); }); async function preprocess(style) {