From da5626a6d73fdd92eb01d894d8ff186a9fb8aea3 Mon Sep 17 00:00:00 2001 From: eight Date: Fri, 6 Oct 2017 17:05:51 +0800 Subject: [PATCH] Add: save usercss from multi-section editor --- _locales/en/messages.json | 8 ++++++++ edit/edit.js | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index ee2ebe3d..c569ab1c 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -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" diff --git a/edit/edit.js b/edit/edit.js index ebd2c0ac..29ea1fff 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -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