Add: save usercss from multi-section editor

This commit is contained in:
eight 2017-10-06 17:05:51 +08:00
parent 0d4d058b00
commit da5626a6d7
2 changed files with 31 additions and 1 deletions

View File

@ -787,10 +787,18 @@
"message": "Enter a name.",
"description": "Error displayed when user saves without providing a name"
},
"stylePasteMozillaFormat": {
"message": "The code you pasted looks like mozilla format style. Would you like to import it?",
"description": "The confirmation to import mozilla format style from pasted text"
},
"styleSaveLabel": {
"message": "Save",
"description": "Label for save button for style editing"
},
"styleSaveAsUsercss": {
"message": "The source code contains usercss metadata. Would you like to save the source code as usercss?",
"description": "The confirmation to create usercss from the editor"
},
"styleSectionsTitle": {
"message": "Sections",
"description": "Title for the style sections section"

View File

@ -306,7 +306,8 @@ function setupCodeMirror(textarea, index) {
if (
text.includes('@-moz-document') &&
text.replace(/\/\*[\s\S]*?\*\//g, '')
.match(/@-moz-document[\s\r\n]+(url|url-prefix|domain|regexp)\(/)
.match(/@-moz-document[\s\r\n]+(url|url-prefix|domain|regexp)\(/) &&
confirm(t('stylePasteMozillaFormat'))
) {
event.preventDefault();
fromMozillaFormat();
@ -1476,7 +1477,28 @@ function save() {
editor.save();
}
function isUsercssSourceCode() {
const sourceCode = editors[0].getValue();
return /==UserStyle==[\s\S]+==\/UserStyle==/i.test(sourceCode);
}
function saveSectionStyle() {
if (!styleId && isUsercssSourceCode() && confirm(t('styleSaveAsUsercss'))) {
const sourceCode = editors[0].getValue();
onBackgroundReady()
.then(() => BG.usercssHelper.save({
reason: 'editSave',
sourceCode,
enabled: $('#enabled').checked
}))
.then(style => {
window.onbeforeunload = null;
location.href = `/edit.html?id=${style.id}`;
})
.catch(alert);
return;
}
updateLintReportIfEnabled(null, 0);
// save the contents of the CodeMirror editors back into the textareas