Fix: separate toggle

This commit is contained in:
eight04 2021-12-07 02:45:58 +08:00
parent 7e61ea9691
commit 7a0d00a4ba
3 changed files with 31 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/* global $ $create messageBoxProxy waitForSheet */// dom.js /* global $ $create messageBoxProxy waitForSheet */// dom.js
/* global msg */// msg.js /* global msg API */// msg.js
/* global CodeMirror */ /* global CodeMirror */
/* global SectionsEditor */ /* global SectionsEditor */
/* global SourceEditor */ /* global SourceEditor */
@ -70,7 +70,14 @@ msg.onExtension(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 && !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; break;
case 'styleDeleted': case 'styleDeleted':

View File

@ -128,18 +128,18 @@ function SectionsEditor() {
editor.ready = initSections(style.sections); editor.ready = initSections(style.sections);
editor.on('styleChange', async (newStyle, reason) => { editor.on('styleToggled', newStyle => {
if (reason === 'new') return; // nothing is new for us if (!dirty.isDirty()) {
if (reason === 'toggle') { Object.assign(style, newStyle);
if (!dirty.isDirty()) { } else {
Object.assign(style, newStyle); editor.toggleStyle(newStyle.enabled);
updateHeader();
}
updateLivePreview();
return;
} }
updateHeader();
updateLivePreview();
});
editor.on('styleChange', (newStyle, reason) => {
if (reason === 'new') return; // nothing is new for us
if (reason === 'config') { if (reason === 'config') {
newStyle = await API.styles.get(newStyle.id);
delete newStyle.sections; delete newStyle.sections;
delete newStyle.name; delete newStyle.name;
delete newStyle.enabled; delete newStyle.enabled;
@ -147,7 +147,7 @@ function SectionsEditor() {
updateLivePreview(); updateLivePreview();
return; return;
} }
editor.replaceStyle(await API.styles.get(newStyle.id)); editor.replaceStyle(newStyle);
}); });
/** @param {EditorSection} section */ /** @param {EditorSection} section */

View File

@ -116,27 +116,25 @@ function SourceEditor() {
if (!$isTextInput(document.activeElement)) { if (!$isTextInput(document.activeElement)) {
cm.focus(); 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 === 'new') return;
if (reason === 'config') { if (reason === 'config') {
newStyle = await API.styles.get(newStyle.id);
delete newStyle.sourceCode; delete newStyle.sourceCode;
delete newStyle.name; delete newStyle.name;
Object.assign(style, newStyle); Object.assign(style, newStyle);
updateLivePreview(); updateLivePreview();
return; return;
} }
if (reason === 'toggle') { replaceStyle(newStyle);
if (dirty.isDirty()) {
editor.toggleStyle(newStyle.enabled);
} else {
style.enabled = newStyle.enabled;
}
updateMeta();
updateLivePreview();
return;
}
replaceStyle(await API.styles.get(newStyle.id));
}); });
async function preprocess(style) { async function preprocess(style) {