save-as-template button in editor

+ keep i18n attributes and use them as CSS selectors
+ reduce flicker when creating a new style
This commit is contained in:
tophf 2022-01-18 17:22:49 +03:00
parent 594ca3520c
commit 3d0bbb72f9
6 changed files with 24 additions and 33 deletions

View File

@ -1352,6 +1352,9 @@
"retrieveDropboxSync": { "retrieveDropboxSync": {
"message": "Dropbox Import" "message": "Dropbox Import"
}, },
"saveAsTemplate": {
"message": "Save as template"
},
"search": { "search": {
"message": "Search", "message": "Search",
"description": "Label before the search input field in the editor shown on Ctrl-F" "description": "Label before the search input field in the editor shown on Ctrl-F"
@ -1812,10 +1815,6 @@
"usercssReplaceTemplateConfirmation": { "usercssReplaceTemplateConfirmation": {
"message": "Replace the default template for new Usercss styles with the current code?" "message": "Replace the default template for new Usercss styles with the current code?"
}, },
"usercssReplaceTemplateName": {
"message": "Empty @name replaces the default template",
"description": "The text shown after @name when creating a new Usercss style"
},
"usercssReplaceTemplateSectionBody": { "usercssReplaceTemplateSectionBody": {
"message": "Insert code here...", "message": "Insert code here...",
"description": "The code placeholder comment in a new style created by clicking 'Write style' in the popup" "description": "The code placeholder comment in a new style created by clicking 'Write style' in the popup"

View File

@ -392,6 +392,9 @@
</a> </a>
</div> </div>
</div> </div>
<div class="options-column usercss-only">
<button i18n-text="saveAsTemplate"></button>
</div>
</div> </div>
</details> </details>
<details id="publish" data-pref="editor.publish.expanded" class="ignore-pref-if-compact"> <details id="publish" data-pref="editor.publish.expanded" class="ignore-pref-if-compact">

View File

@ -977,7 +977,7 @@ body.linter-disabled .hidden-unless-compact {
margin-top: 0; margin-top: 0;
} }
#options-wrapper .options-column:nth-child(2) { #options-wrapper .options-column:nth-child(n + 2) {
margin-top: .75rem; margin-top: .75rem;
} }
@ -1126,7 +1126,7 @@ body.linter-disabled .hidden-unless-compact {
.options-column > .usercss-only { .options-column > .usercss-only {
margin-bottom: 0; margin-bottom: 0;
} }
#options-wrapper .options-column:nth-child(2) { #options-wrapper .options-column:nth-child(n + 2) {
margin-top: 0; margin-top: 0;
} }
#options:not([open]), #options:not([open]),

View File

@ -4,6 +4,7 @@
/* global SectionsEditor */ /* global SectionsEditor */
/* global SourceEditor */ /* global SourceEditor */
/* global baseInit */ /* global baseInit */
/* global chromeSync */// storage-util.js
/* global clipString createHotkeyInput helpPopup */// util.js /* global clipString createHotkeyInput helpPopup */// util.js
/* global closeCurrentTab deepEqual sessionStore tryJSONparse */// toolbox.js /* global closeCurrentTab deepEqual sessionStore tryJSONparse */// toolbox.js
/* global cmFactory */ /* global cmFactory */
@ -16,7 +17,10 @@
//#region init //#region init
baseInit.ready.then(async () => { baseInit.ready.then(async () => {
await waitForSheet(); [editor.template] = await Promise.all([
editor.isUsercss && !editor.style.id && chromeSync.getLZValue(chromeSync.LZ_KEY.usercssTemplate),
waitForSheet(),
]);
(editor.isUsercss ? SourceEditor : SectionsEditor)(); (editor.isUsercss ? SourceEditor : SectionsEditor)();
await editor.ready; await editor.ready;
editor.ready = true; editor.ready = true;

View File

@ -17,20 +17,19 @@
function SourceEditor() { function SourceEditor() {
const {style, /** @type DirtyReporter */dirty} = editor; const {style, /** @type DirtyReporter */dirty} = editor;
let savedGeneration; let savedGeneration;
let placeholderName = '';
let prevMode = NaN; let prevMode = NaN;
$$remove('.sectioned-only'); $$remove('.sectioned-only');
$('#header').on('wheel', headerOnScroll); $('#header').on('wheel', headerOnScroll);
$('#sections').textContent = ''; $('#sections').textContent = '';
$('#sections').appendChild($create('.single-editor')); $('#sections').appendChild($create('.single-editor'));
$('[i18n-text="saveAsTemplate"]').onclick = saveTemplate;
if (!style.id) setupNewStyle(style);
const cm = cmFactory.create($('.single-editor')); const cm = cmFactory.create($('.single-editor'));
const sectionFinder = MozSectionFinder(cm); const sectionFinder = MozSectionFinder(cm);
const sectionWidget = MozSectionWidget(cm, sectionFinder); const sectionWidget = MozSectionWidget(cm, sectionFinder);
editor.livePreview.init(preprocess); editor.livePreview.init(preprocess);
if (!style.id) setupNewStyle();
createMetaCompiler(meta => { createMetaCompiler(meta => {
style.usercssData = meta; style.usercssData = meta;
style.name = meta.name; style.name = meta.name;
@ -75,13 +74,7 @@ function SourceEditor() {
} }
showLog(res); showLog(res);
} catch (err) { } catch (err) {
const i = err.index; showSaveError(err);
const isNameEmpty = i > 0 &&
err.code === 'missingValue' &&
sourceCode.slice(sourceCode.lastIndexOf('\n', i - 1), i).trim().endsWith('@name');
return isNameEmpty
? saveTemplate(sourceCode)
: showSaveError(err);
} }
}, },
scrollToEditor: () => {}, scrollToEditor: () => {},
@ -160,7 +153,7 @@ function SourceEditor() {
return name; return name;
} }
async function setupNewStyle(style) { function setupNewStyle() {
style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) + style.sections[0].code = ' '.repeat(prefs.get('editor.tabSize')) +
`/* ${t('usercssReplaceTemplateSectionBody')} */`; `/* ${t('usercssReplaceTemplateSectionBody')} */`;
let section = MozDocMapper.styleToCss(style); let section = MozDocMapper.styleToCss(style);
@ -177,17 +170,11 @@ function SourceEditor() {
@author Me @author Me
==/UserStyle== */ ==/UserStyle== */
`.replace(/^\s+/gm, ''); `.replace(/^\s+/gm, '');
style.name = [style.name, new Date().toLocaleString()].filter(Boolean).join(' - ');
dirty.clear('sourceGeneration');
style.sourceCode = '';
placeholderName = `${style.name || t('usercssReplaceTemplateName')} - ${new Date().toLocaleString()}`;
let code = await chromeSync.getLZValue(chromeSync.LZ_KEY.usercssTemplate);
code = code || DEFAULT_CODE;
code = code.replace(/@name(\s*)(?=[\r\n])/, (str, space) =>
`${str}${space ? '' : ' '}${placeholderName}`);
// strip the last dummy section if any, add an empty line followed by the section // strip the last dummy section if any, add an empty line followed by the section
style.sourceCode = code.replace(/\s*@-moz-document[^{]*{[^}]*}\s*$|\s+$/g, '') + '\n\n' + section; style.sourceCode = (editor.template || DEFAULT_CODE)
.replace(/(@name)(?:([\t\x20]+).*|\n)/, (_, k, space) => `${k}${space || ' '}${style.name}`)
.replace(/\s*@-moz-document[^{]*{[^}]*}\s*$|\s+$/g, '') + '\n\n' + section;
cm.startOperation(); cm.startOperation();
cm.setValue(style.sourceCode); cm.setValue(style.sourceCode);
cm.clearHistory(); cm.clearHistory();
@ -199,9 +186,7 @@ function SourceEditor() {
function updateMeta() { function updateMeta() {
const name = style.customName || style.name; const name = style.customName || style.name;
if (name !== placeholderName) { $('#name').value = name;
$('#name').value = name;
}
$('#enabled').checked = style.enabled; $('#enabled').checked = style.enabled;
$('#url').href = style.url; $('#url').href = style.url;
editor.updateName(); editor.updateName();
@ -236,9 +221,10 @@ function SourceEditor() {
} }
} }
async function saveTemplate(code) { async function saveTemplate() {
if (await messageBoxProxy.confirm(t('usercssReplaceTemplateConfirmation'))) { if (await messageBoxProxy.confirm(t('usercssReplaceTemplateConfirmation'))) {
const key = chromeSync.LZ_KEY.usercssTemplate; const key = chromeSync.LZ_KEY.usercssTemplate;
const code = cm.getValue();
await chromeSync.setLZValue(key, code); await chromeSync.setLZValue(key, code);
if (await chromeSync.getLZValue(key) !== code) { if (await chromeSync.getLZValue(key) !== code) {
messageBoxProxy.alert(t('syncStorageErrorSaving')); messageBoxProxy.alert(t('syncStorageErrorSaving'));

View File

@ -73,7 +73,6 @@ Object.assign(t, {
if (toInsert) { if (toInsert) {
node.insertBefore(toInsert, before || null); node.insertBefore(toInsert, before || null);
} }
node.removeAttribute(name);
} }
} }
}, },