dedupe only on right-click or shift-click

This commit is contained in:
tophf 2021-12-23 05:39:44 +03:00
parent e9ed8dc946
commit 0fec0c6123
3 changed files with 16 additions and 5 deletions

View File

@ -391,6 +391,10 @@
"excludeStyleByUrlLabel": { "excludeStyleByUrlLabel": {
"message": "Exclude the current URL" "message": "Exclude the current URL"
}, },
"exportDeduplicate": {
"message": "Shift-click or Right-click will export a smaller file for UserCSS styles.\nSuch styles are importable only in Stylus 1.5.23 or newer.",
"description": "Tooltip for the button to export all styles in the 'manage' page"
},
"exportLabel": { "exportLabel": {
"message": "Export", "message": "Export",
"description": "Label for the button to export a style ('edit' page) or all styles ('manage' page)" "description": "Label for the button to export a style ('edit' page) or all styles ('manage' page)"

View File

@ -323,7 +323,7 @@
<summary><h2 id="backup-title" i18n-text="backupButtons"></h2></summary> <summary><h2 id="backup-title" i18n-text="backupButtons"></h2></summary>
<span id="backup-message" i18n-text="backupMessage"></span> <span id="backup-message" i18n-text="backupMessage"></span>
<div id="backup-buttons"> <div id="backup-buttons">
<button id="file-all-styles" i18n-text="exportLabel"></button> <button id="file-all-styles" i18n-text="exportLabel" i18n-title="exportDeduplicate"></button>
<button id="unfile-all-styles" i18n-text="importLabel"></button> <button id="unfile-all-styles" i18n-text="importLabel"></button>
<button id="sync-styles" i18n-text="optionsCustomizeSync"></button> <button id="sync-styles" i18n-text="optionsCustomizeSync"></button>
</div> </div>

View File

@ -14,7 +14,10 @@
*/// dom.js */// dom.js
'use strict'; 'use strict';
$('#file-all-styles').onclick = exportToFile; Object.assign($('#file-all-styles'), {
onclick: exportToFile,
oncontextmenu: exportToFile,
});
$('#unfile-all-styles').onclick = () => importFromFile({fileTypeFilter: '.json'}); $('#unfile-all-styles').onclick = () => importFromFile({fileTypeFilter: '.json'});
Object.assign(document.body, { Object.assign(document.body, {
@ -325,13 +328,17 @@ async function importFromString(jsonString) {
} }
} }
async function exportToFile() { /** @param {MouseEvent} e */
async function exportToFile(e) {
e.preventDefault();
await require(['/js/storage-util']); await require(['/js/storage-util']);
const shouldClean = e.shiftKey || e.button === 2;
const styles = await API.styles.getAll();
const data = [ const data = [
Object.assign({ Object.assign({
[prefs.STORAGE_KEY]: prefs.values, [prefs.STORAGE_KEY]: prefs.values,
}, await chromeSync.getLZValues()), }, await chromeSync.getLZValues()),
...(await API.styles.getAll()).map(cleanupStyle), ...shouldClean ? styles.map(cleanupStyle) : styles,
]; ];
const text = JSON.stringify(data, null, ' '); const text = JSON.stringify(data, null, ' ');
const type = 'application/json'; const type = 'application/json';
@ -340,7 +347,7 @@ async function exportToFile() {
download: generateFileName(), download: generateFileName(),
type, type,
}).dispatchEvent(new MouseEvent('click')); }).dispatchEvent(new MouseEvent('click'));
/** strip `sections`, `null` and empty objects */ /** Stripping `sections`, `null` and empty objects */
function cleanupStyle(style) { function cleanupStyle(style) {
const copy = {}; const copy = {};
for (const [key, val] of Object.entries(style)) { for (const [key, val] of Object.entries(style)) {