2017-03-25 05:54:58 +00:00
|
|
|
/* globals getStyles, saveStyle, invalidateCache, refreshAllTabs */
|
2017-02-03 09:24:47 +00:00
|
|
|
'use strict';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
const STYLISH_DUMP_FILE_EXT = '.txt';
|
|
|
|
const STYLUS_BACKUP_FILE_EXT = '.json';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
function importFromFile({fileTypeFilter, file} = {}) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const fileInput = document.createElement('input');
|
|
|
|
if (file) {
|
|
|
|
readFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fileInput.style.display = 'none';
|
2017-02-03 09:24:47 +00:00
|
|
|
fileInput.type = 'file';
|
2017-03-15 18:18:17 +00:00
|
|
|
fileInput.accept = fileTypeFilter || STYLISH_DUMP_FILE_EXT;
|
2017-02-03 09:24:47 +00:00
|
|
|
fileInput.acceptCharset = 'utf-8';
|
2017-01-30 19:02:50 +00:00
|
|
|
|
2017-02-03 09:24:47 +00:00
|
|
|
document.body.appendChild(fileInput);
|
|
|
|
fileInput.initialValue = fileInput.value;
|
2017-03-15 18:18:17 +00:00
|
|
|
fileInput.onchange = readFile;
|
|
|
|
fileInput.click();
|
|
|
|
|
|
|
|
function readFile() {
|
|
|
|
if (file || fileInput.value !== fileInput.initialValue) {
|
|
|
|
file = file || fileInput.files[0];
|
|
|
|
if (file.size > 100*1000*1000) {
|
|
|
|
console.warn("100MB backup? I don't believe you.");
|
|
|
|
importFromString('').then(resolve);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document.body.style.cursor = 'wait';
|
|
|
|
const fReader = new FileReader();
|
|
|
|
fReader.onloadend = event => {
|
2017-02-03 09:24:47 +00:00
|
|
|
fileInput.remove();
|
2017-03-15 18:18:17 +00:00
|
|
|
importFromString(event.target.result).then(numStyles => {
|
|
|
|
document.body.style.cursor = '';
|
|
|
|
resolve(numStyles);
|
|
|
|
});
|
2017-02-03 09:24:47 +00:00
|
|
|
};
|
2017-03-15 18:18:17 +00:00
|
|
|
fReader.readAsText(file, 'utf-8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
|
2017-03-15 18:18:17 +00:00
|
|
|
function importFromString(jsonString) {
|
|
|
|
const json = runTryCatch(() => Array.from(JSON.parse(jsonString))) || [];
|
2017-03-24 02:51:44 +00:00
|
|
|
const oldStyles = json.length && deepCopyStyles();
|
|
|
|
const oldStylesByName = json.length && new Map(
|
|
|
|
oldStyles.map(style => [style.name.trim(), style]));
|
|
|
|
const stats = {
|
|
|
|
added: {names: [], ids: [], legend: 'added'},
|
|
|
|
unchanged: {names: [], ids: [], legend: 'identical skipped'},
|
|
|
|
metaAndCode: {names: [], ids: [], legend: 'updated both meta info and code'},
|
|
|
|
metaOnly: {names: [], ids: [], legend: 'updated meta info'},
|
|
|
|
codeOnly: {names: [], ids: [], legend: 'updated code'},
|
|
|
|
invalid: {names: [], legend: 'invalid skipped'},
|
|
|
|
};
|
|
|
|
let index = 0;
|
|
|
|
return new Promise(proceed);
|
2017-03-15 18:18:17 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function proceed(resolve) {
|
|
|
|
while (index < json.length) {
|
|
|
|
const item = json[index++];
|
|
|
|
if (!item || !item.name || !item.name.trim() || typeof item != 'object'
|
|
|
|
|| (item.sections && !(item.sections instanceof Array))) {
|
|
|
|
stats.invalid.names.push(`#${index}: ${limitString(item && item.name || '')}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item.name = item.name.trim();
|
|
|
|
const byId = (cachedStyles.byId.get(item.id) || {}).style;
|
|
|
|
const byName = oldStylesByName.get(item.name);
|
|
|
|
const oldStyle = byId && byId.name.trim() == item.name || !byName ? byId : byName;
|
|
|
|
if (oldStyle == byName && byName) {
|
|
|
|
item.id = byName.id;
|
|
|
|
}
|
|
|
|
const oldStyleKeys = oldStyle && Object.keys(oldStyle);
|
|
|
|
const metaEqual = oldStyleKeys &&
|
|
|
|
oldStyleKeys.length == Object.keys(item).length &&
|
|
|
|
oldStyleKeys.every(k => k == 'sections' || oldStyle[k] === item[k]);
|
|
|
|
const codeEqual = oldStyle && styleSectionsEqual(oldStyle, item);
|
|
|
|
if (metaEqual && codeEqual) {
|
|
|
|
stats.unchanged.names.push(oldStyle.name);
|
|
|
|
stats.unchanged.ids.push(oldStyle.id);
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-25 02:35:54 +00:00
|
|
|
saveStyle(Object.assign(item, {
|
|
|
|
reason: 'import',
|
|
|
|
notify: false,
|
|
|
|
})).then(style => {
|
2017-03-24 02:51:44 +00:00
|
|
|
setTimeout(proceed, 0, resolve);
|
|
|
|
if (!oldStyle) {
|
|
|
|
stats.added.names.push(style.name);
|
|
|
|
stats.added.ids.push(style.id);
|
|
|
|
}
|
|
|
|
else if (!metaEqual && !codeEqual) {
|
|
|
|
stats.metaAndCode.names.push(reportNameChange(oldStyle, style));
|
|
|
|
stats.metaAndCode.ids.push(style.id);
|
|
|
|
}
|
|
|
|
else if (!codeEqual) {
|
|
|
|
stats.codeOnly.names.push(style.name);
|
|
|
|
stats.codeOnly.ids.push(style.id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stats.metaOnly.names.push(reportNameChange(oldStyle, style));
|
|
|
|
stats.metaOnly.ids.push(style.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
done(resolve);
|
2017-03-15 18:18:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function done(resolve) {
|
|
|
|
const numChanged = stats.metaAndCode.names.length +
|
|
|
|
stats.metaOnly.names.length +
|
|
|
|
stats.codeOnly.names.length +
|
|
|
|
stats.added.names.length;
|
|
|
|
Promise.resolve(numChanged && refreshAllTabs()).then(() => {
|
|
|
|
scrollTo(0, 0);
|
|
|
|
const report = Object.keys(stats)
|
|
|
|
.filter(kind => stats[kind].names.length)
|
|
|
|
.map(kind => `<details data-id="${kind}">
|
|
|
|
<summary><b>${stats[kind].names.length} ${stats[kind].legend}</b></summary>
|
|
|
|
<small>` + stats[kind].names.map((name, i) =>
|
|
|
|
`<div data-id="${stats[kind].ids[i]}">${name}</div>`).join('') + `
|
|
|
|
</small>
|
|
|
|
</details>`)
|
|
|
|
.join('');
|
|
|
|
const box = messageBox({
|
|
|
|
title: 'Finished importing styles',
|
|
|
|
contents: report || 'Nothing was changed.',
|
|
|
|
buttons: [t('confirmOK'), numChanged && t('undo')],
|
|
|
|
onclick: btnIndex => btnIndex == 1 && undo(),
|
|
|
|
});
|
|
|
|
bindClick(box);
|
|
|
|
resolve(numChanged);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function undo() {
|
|
|
|
const oldStylesById = new Map(oldStyles.map(style => [style.id, style]));
|
|
|
|
const newIds = [
|
|
|
|
...stats.metaAndCode.ids,
|
|
|
|
...stats.metaOnly.ids,
|
|
|
|
...stats.codeOnly.ids,
|
|
|
|
...stats.added.ids,
|
|
|
|
];
|
|
|
|
index = 0;
|
|
|
|
return new Promise(undoNextId)
|
|
|
|
.then(refreshAllTabs)
|
|
|
|
.then(() => messageBox({
|
|
|
|
title: 'Import has been undone',
|
|
|
|
contents: newIds.length + ' styles were reverted.',
|
|
|
|
buttons: [t('confirmOK')],
|
|
|
|
}));
|
|
|
|
function undoNextId(resolve) {
|
|
|
|
if (index == newIds.length) {
|
|
|
|
resolve();
|
|
|
|
return;
|
2017-02-03 09:24:47 +00:00
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
const id = newIds[index++];
|
|
|
|
deleteStyle(id, {notify: false}).then(id => {
|
|
|
|
const oldStyle = oldStylesById.get(id);
|
|
|
|
if (oldStyle) {
|
2017-03-25 02:35:54 +00:00
|
|
|
saveStyle(Object.assign(oldStyle, {
|
|
|
|
reason: 'undoImport',
|
|
|
|
notify: false,
|
|
|
|
}))
|
2017-03-24 02:51:44 +00:00
|
|
|
.then(() => setTimeout(undoNextId, 0, resolve));
|
|
|
|
} else {
|
|
|
|
setTimeout(undoNextId, 0, resolve);
|
|
|
|
}
|
|
|
|
});
|
2017-02-03 09:24:47 +00:00
|
|
|
}
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function bindClick(box) {
|
2017-03-25 05:54:58 +00:00
|
|
|
for (let block of $$('details')) {
|
2017-03-24 02:51:44 +00:00
|
|
|
if (block.dataset.id != 'invalid') {
|
|
|
|
block.style.cursor = 'pointer';
|
|
|
|
block.onclick = event => {
|
|
|
|
const styleElement = $(`[style-id="${event.target.dataset.id}"]`);
|
|
|
|
if (styleElement) {
|
|
|
|
scrollElementIntoView(styleElement);
|
2017-03-25 05:54:58 +00:00
|
|
|
animateElement(styleElement, {className: 'highlight'});
|
2017-03-24 02:51:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function deepCopyStyles() {
|
|
|
|
const clonedStyles = [];
|
|
|
|
for (let style of cachedStyles.list || []) {
|
|
|
|
style = Object.assign({}, style);
|
|
|
|
style.sections = style.sections.slice();
|
|
|
|
for (let i = 0, section; (section = style.sections[i]); i++) {
|
|
|
|
const copy = style.sections[i] = Object.assign({}, section);
|
|
|
|
for (let propName in copy) {
|
|
|
|
const prop = copy[propName];
|
|
|
|
if (prop instanceof Array) {
|
|
|
|
copy[propName] = prop.slice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clonedStyles.push(style);
|
|
|
|
}
|
|
|
|
return clonedStyles;
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function limitString(s, limit = 100) {
|
|
|
|
return s.length <= limit ? s : s.substr(0, limit) + '...';
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
function reportNameChange(oldStyle, newStyle) {
|
|
|
|
return newStyle.name != oldStyle.name
|
|
|
|
? oldStyle.name + ' —> ' + newStyle.name
|
|
|
|
: oldStyle.name;
|
|
|
|
}
|
2017-02-05 06:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
|
|
|
|
$('#file-all-styles').onclick = () => {
|
2017-03-06 14:44:20 +00:00
|
|
|
getStyles({}, function (styles) {
|
2017-03-24 02:51:44 +00:00
|
|
|
const text = JSON.stringify(styles, null, '\t');
|
|
|
|
const fileName = generateFileName();
|
2017-03-06 14:44:20 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
const url = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
|
2017-03-08 08:13:35 +00:00
|
|
|
// for long URLs; https://github.com/schomery/stylish-chrome/issues/13#issuecomment-284582600
|
|
|
|
fetch(url)
|
|
|
|
.then(res => res.blob())
|
|
|
|
.then(blob => {
|
|
|
|
let a = document.createElement('a');
|
|
|
|
a.setAttribute('download', fileName);
|
|
|
|
a.setAttribute('href', URL.createObjectURL(blob));
|
|
|
|
a.dispatchEvent(new MouseEvent('click'));
|
|
|
|
});
|
2017-02-05 06:25:44 +00:00
|
|
|
});
|
2017-03-24 02:51:44 +00:00
|
|
|
|
|
|
|
function generateFileName() {
|
|
|
|
const today = new Date();
|
|
|
|
const dd = ('0' + today.getDate()).substr(-2);
|
|
|
|
const mm = ('0' + (today.getMonth() + 1)).substr(-2);
|
|
|
|
const yyyy = today.getFullYear();
|
|
|
|
return `stylus-${mm}-${dd}-${yyyy}${STYLUS_BACKUP_FILE_EXT}`;
|
|
|
|
}
|
2017-03-15 18:18:17 +00:00
|
|
|
};
|
2017-02-05 06:25:44 +00:00
|
|
|
|
|
|
|
|
2017-03-25 05:54:58 +00:00
|
|
|
$('#unfile-all-styles').onclick = () => {
|
2017-03-24 02:51:44 +00:00
|
|
|
importFromFile({fileTypeFilter: STYLUS_BACKUP_FILE_EXT});
|
2017-03-15 18:18:17 +00:00
|
|
|
};
|
2017-02-05 06:25:44 +00:00
|
|
|
|
2017-03-24 02:51:44 +00:00
|
|
|
Object.assign(document.body, {
|
|
|
|
ondragover(event) {
|
2017-03-15 18:18:17 +00:00
|
|
|
const hasFiles = event.dataTransfer.types.includes('Files');
|
|
|
|
event.dataTransfer.dropEffect = hasFiles || event.target.type == 'search' ? 'copy' : 'none';
|
2017-03-24 02:51:44 +00:00
|
|
|
this.classList.toggle('dropzone', hasFiles);
|
2017-03-15 18:18:17 +00:00
|
|
|
if (hasFiles) {
|
|
|
|
event.preventDefault();
|
2017-03-24 02:51:44 +00:00
|
|
|
clearTimeout(this.fadeoutTimer);
|
|
|
|
this.classList.remove('fadeout');
|
2017-02-05 06:25:44 +00:00
|
|
|
}
|
2017-03-15 18:18:17 +00:00
|
|
|
},
|
2017-03-24 02:51:44 +00:00
|
|
|
ondragend(event) {
|
2017-03-25 05:54:58 +00:00
|
|
|
animateElement(this, {className: 'fadeout'}).then(() => {
|
2017-03-24 02:51:44 +00:00
|
|
|
this.style.animationDuration = '';
|
2017-03-25 05:54:58 +00:00
|
|
|
this.classList.remove('dropzone');
|
2017-03-24 02:51:44 +00:00
|
|
|
});
|
2017-03-15 18:18:17 +00:00
|
|
|
},
|
2017-03-24 02:51:44 +00:00
|
|
|
ondragleave(event) {
|
2017-03-15 18:18:17 +00:00
|
|
|
// Chrome sets screen coords to 0 on Escape key pressed or mouse out of document bounds
|
|
|
|
if (!event.screenX && !event.screenX) {
|
2017-03-24 02:51:44 +00:00
|
|
|
this.ondragend();
|
2017-03-15 18:18:17 +00:00
|
|
|
}
|
|
|
|
},
|
2017-03-24 02:51:44 +00:00
|
|
|
ondrop(event) {
|
|
|
|
this.ondragend();
|
2017-03-15 18:18:17 +00:00
|
|
|
if (event.dataTransfer.files.length) {
|
|
|
|
event.preventDefault();
|
2017-03-24 02:51:44 +00:00
|
|
|
importFromFile({file: event.dataTransfer.files[0]});
|
2017-03-15 18:18:17 +00:00
|
|
|
}
|
|
|
|
},
|
2017-02-05 06:25:44 +00:00
|
|
|
});
|