hide Settings button in new styles

This commit is contained in:
tophf 2021-12-29 13:26:27 +03:00
parent ac7f29b02f
commit bbb3585f2f
6 changed files with 40 additions and 77 deletions

View File

@ -11,7 +11,6 @@
debounce debounce
getOwnTab getOwnTab
sessionStore sessionStore
tryCatch
tryJSONparse tryJSONparse
tryURL tryURL
*/// toolbox.js */// toolbox.js
@ -24,7 +23,6 @@
const editor = { const editor = {
style: null, style: null,
dirty: DirtyReporter(), dirty: DirtyReporter(),
events: {},
isUsercss: false, isUsercss: false,
isWindowed: false, isWindowed: false,
lazyKeymaps: { lazyKeymaps: {
@ -39,18 +37,6 @@ const editor = {
cancel: () => location.assign('/manage.html'), cancel: () => location.assign('/manage.html'),
emit(name, ...args) {
for (const fn of editor.events[name] || []) {
tryCatch(fn, ...args);
}
},
on(name, fn) {
(editor.events[name] || (
editor.events[name] = new Set()
)).add(fn);
},
updateClass() { updateClass() {
document.documentElement.classList.toggle('is-new-style', !editor.style.id); document.documentElement.classList.toggle('is-new-style', !editor.style.id);
}, },
@ -65,19 +51,6 @@ const editor = {
}, },
}; };
editor.on('styleUpdated', (newStyle, reason) => {
if (reason === 'config') {
delete newStyle.sourceCode;
delete newStyle.sections;
delete newStyle.name;
delete newStyle.enabled;
Object.assign(editor.style, newStyle);
editor.updateLivePreview();
} else if (reason !== 'new') {
editor.replaceStyle(newStyle);
}
});
//#region pre-init //#region pre-init
const baseInit = (() => { const baseInit = (() => {

View File

@ -34,6 +34,7 @@ a:hover {
} }
html.is-new-style #preview-label, html.is-new-style #preview-label,
html.is-new-style #style-cfg-btn,
html.is-new-style #publish, html.is-new-style #publish,
.hidden { .hidden {
display: none !important; display: none !important;

View File

@ -74,14 +74,7 @@ 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)) {
if (request.reason === 'toggle') { handleExternalUpdate(request);
editor.emit('styleToggled', request.style);
} else {
API.styles.get(request.style.id)
.then(style => {
editor.emit('styleUpdated', style, request.reason);
});
}
} }
break; break;
case 'styleDeleted': case 'styleDeleted':
@ -95,6 +88,31 @@ msg.onExtension(request => {
} }
}); });
async function handleExternalUpdate({style, reason}) {
if (reason === 'toggle') {
if (editor.dirty.isDirty()) {
editor.toggleStyle(style.enabled);
} else {
Object.assign(editor.style, style);
}
editor.updateMeta();
editor.updateLivePreview();
return;
}
style = await API.styles.get(style.id);
if (reason === 'config') {
delete style.sourceCode;
delete style.sections;
delete style.name;
delete style.enabled;
Object.assign(editor.style, style);
editor.updateLivePreview();
} else {
await editor.replaceStyle(style);
}
editor.updateSettings();
}
window.on('beforeunload', e => { window.on('beforeunload', e => {
let pos; let pos;
if (editor.isWindowed && if (editor.isWindowed &&

View File

@ -23,7 +23,7 @@ function SectionsEditor() {
let headerOffset; // in compact mode the header is at the top so it reduces the available height let headerOffset; // in compact mode the header is at the top so it reduces the available height
let cmExtrasHeight; // resize grip + borders let cmExtrasHeight; // resize grip + borders
updateHeader(); updateMeta();
rerouteHotkeys.toggle(true); // enabled initially because we don't always focus a CodeMirror rerouteHotkeys.toggle(true); // enabled initially because we don't always focus a CodeMirror
editor.livePreview.init(); editor.livePreview.init();
container.classList.add('section-editor'); container.classList.add('section-editor');
@ -44,6 +44,7 @@ function SectionsEditor() {
closestVisible, closestVisible,
updateLivePreview, updateLivePreview,
updateMeta,
getEditors() { getEditors() {
return sections.filter(s => !s.removed).map(s => s.cm); return sections.filter(s => !s.removed).map(s => s.cm);
@ -90,7 +91,7 @@ function SectionsEditor() {
await initSections(newStyle.sections, {replace: true}); await initSections(newStyle.sections, {replace: true});
Object.assign(style, newStyle); Object.assign(style, newStyle);
editor.updateClass(); editor.updateClass();
updateHeader(); updateMeta();
// Go from new style URL to edit style URL // Go from new style URL to edit style URL
if (style.id && !/[&?]id=/.test(location.search)) { if (style.id && !/[&?]id=/.test(location.search)) {
history.replaceState({}, document.title, `${location.pathname}?id=${style.id}`); history.replaceState({}, document.title, `${location.pathname}?id=${style.id}`);
@ -108,9 +109,6 @@ function SectionsEditor() {
} }
newStyle = await API.styles.editSave(newStyle); newStyle = await API.styles.editSave(newStyle);
destroyRemovedSections(); destroyRemovedSections();
if (!style.id) {
editor.emit('styleUpdated', newStyle, 'new');
}
sessionStore.justEditedStyleId = newStyle.id; sessionStore.justEditedStyleId = newStyle.id;
editor.replaceStyle(newStyle, false); editor.replaceStyle(newStyle, false);
}, },
@ -128,16 +126,6 @@ function SectionsEditor() {
editor.ready = initSections(style.sections); editor.ready = initSections(style.sections);
editor.on('styleToggled', newStyle => {
if (!dirty.isDirty()) {
Object.assign(style, newStyle);
} else {
editor.toggleStyle(newStyle.enabled);
}
updateHeader();
updateLivePreview();
});
/** @param {EditorSection} section */ /** @param {EditorSection} section */
function fitToContent(section) { function fitToContent(section) {
const {cm, cm: {display: {wrapper, sizer}}} = section; const {cm, cm: {display: {wrapper, sizer}}} = section;
@ -477,7 +465,7 @@ function SectionsEditor() {
} }
} }
function updateHeader() { function updateMeta() {
$('#name').value = style.customName || style.name || ''; $('#name').value = style.customName || style.name || '';
$('#enabled').checked = style.enabled !== false; $('#enabled').checked = style.enabled !== false;
$('#url').href = style.url || ''; $('#url').href = style.url || '';

View File

@ -7,7 +7,7 @@
'use strict'; 'use strict';
function StyleSettings() { function StyleSettings() {
let {style} = editor; const {style} = editor;
const ui = t.template.styleSettings.cloneNode(true); const ui = t.template.styleSettings.cloneNode(true);
const inputs = [ const inputs = [
createInput('.style-update-url input', () => style.updateUrl || '', createInput('.style-update-url input', () => style.updateUrl || '',
@ -19,8 +19,9 @@ function StyleSettings() {
['.style-exclude', 'exclusions'], ['.style-exclude', 'exclusions'],
].map(createArea), ].map(createArea),
]; ];
update(style); (editor.updateSettings = () => {
editor.on('styleUpdated', update); inputs.forEach(i => i.update());
})();
helpPopup.show(t('styleSettings'), $create([ helpPopup.show(t('styleSettings'), $create([
ui, ui,
$create('.buttons', [ $create('.buttons', [
@ -39,13 +40,6 @@ function StyleSettings() {
return list.filter(Boolean); return list.filter(Boolean);
} }
function update(newStyle, reason) {
if (!newStyle.id) return;
if (reason === 'editSave') return;
style = newStyle;
inputs.forEach(i => i.update());
}
function createArea([parentSel, type]) { function createArea([parentSel, type]) {
const sel = parentSel + ' textarea'; const sel = parentSel + ' textarea';
const el = $(sel, ui); const el = $(sel, ui);

View File

@ -45,6 +45,7 @@ function SourceEditor() {
sections: sectionFinder.sections, sections: sectionFinder.sections,
replaceStyle, replaceStyle,
updateLivePreview, updateLivePreview,
updateMeta,
closestVisible: () => cm, closestVisible: () => cm,
getEditors: () => [cm], getEditors: () => [cm],
getEditorTitle: () => '', getEditorTitle: () => '',
@ -70,9 +71,6 @@ function SourceEditor() {
messageBoxProxy.alert(t('usercssAvoidOverwriting'), 'danger', t('genericError')); messageBoxProxy.alert(t('usercssAvoidOverwriting'), 'danger', t('genericError'));
} else { } else {
res = await API.usercss.editSave({customName, enabled, id, sourceCode}); res = await API.usercss.editSave({customName, enabled, id, sourceCode});
if (!id) {
editor.emit('styleUpdated', res.style, 'new');
}
// Awaiting inside `try` so that exceptions go to our `catch` // Awaiting inside `try` so that exceptions go to our `catch`
await replaceStyle(res.style); await replaceStyle(res.style);
} }
@ -116,15 +114,6 @@ function SourceEditor() {
if (!$isTextInput(document.activeElement)) { if (!$isTextInput(document.activeElement)) {
cm.focus(); cm.focus();
} }
editor.on('styleToggled', newStyle => {
if (dirty.isDirty()) {
editor.toggleStyle(newStyle.enabled);
} else {
style.enabled = newStyle.enabled;
}
updateMeta();
updateLivePreview();
});
async function preprocess(style) { async function preprocess(style) {
const res = await API.usercss.build({ const res = await API.usercss.build({
@ -220,7 +209,7 @@ function SourceEditor() {
cm.setPreprocessor((style.usercssData || {}).preprocessor); cm.setPreprocessor((style.usercssData || {}).preprocessor);
} }
function replaceStyle(newStyle) { async function replaceStyle(newStyle) {
dirty.clear('name'); dirty.clear('name');
const sameCode = newStyle.sourceCode === cm.getValue(); const sameCode = newStyle.sourceCode === cm.getValue();
if (sameCode) { if (sameCode) {
@ -232,8 +221,8 @@ function SourceEditor() {
return; return;
} }
Promise.resolve(messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))).then(ok => { // TODO: also confirm in sections-editor?
if (!ok) return; if (await messageBoxProxy.confirm(t('styleUpdateDiscardChanges'))) {
updateEnvironment(); updateEnvironment();
if (!sameCode) { if (!sameCode) {
const cursor = cm.getCursor(); const cursor = cm.getCursor();
@ -246,7 +235,7 @@ function SourceEditor() {
updateLivePreview(); updateLivePreview();
} }
dirty.clear(); dirty.clear();
}); }
function updateEnvironment() { function updateEnvironment() {
if (style.id !== newStyle.id) { if (style.id !== newStyle.id) {