Add: importStyle API

This commit is contained in:
eight 2018-10-12 23:32:03 +08:00
parent c55675912e
commit 4d06435486
2 changed files with 16 additions and 22 deletions

View File

@ -16,6 +16,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
deleteStyle: styleManager.deleteStyle, deleteStyle: styleManager.deleteStyle,
installStyle: styleManager.installStyle, installStyle: styleManager.installStyle,
editSave: styleManager.editSave, editSave: styleManager.editSave,
importStyle: styleManager.importStyle,
styleExists: styleManager.styleExists, styleExists: styleManager.styleExists,
getStyle: styleManager.get, getStyle: styleManager.get,

View File

@ -29,9 +29,10 @@ const styleManager = (() => {
getSectionsByUrl, getSectionsByUrl,
installStyle, installStyle,
deleteStyle, deleteStyle,
setStyleExclusions,
editSave, editSave,
importStyle,
toggleStyle, toggleStyle,
setStyleExclusions,
getAllStyles, // used by import-export getAllStyles, // used by import-export
getStylesInfoByUrl, // used by popup getStylesInfoByUrl, // used by popup
styleExists, styleExists,
@ -124,6 +125,12 @@ const styleManager = (() => {
return true; return true;
} }
function importStyle(data) {
// FIXME: is it a good idea to save the data directly?
return saveStyle(data)
.then(newData => handleSave(newData));
}
function installStyle(data) { function installStyle(data) {
const style = styles.get(data.id); const style = styles.get(data.id);
if (!style) { if (!style) {
@ -137,11 +144,7 @@ const styleManager = (() => {
data.originalDigest = digest; data.originalDigest = digest;
return saveStyle(data); return saveStyle(data);
}) })
.then(newData => handleSave( .then(newData => handleSave(newData, style ? 'update' : 'install'));
newData,
style ? 'update' : 'install',
style ? 'styleUpdated' : 'styleAdded'
));
} }
function editSave(data) { function editSave(data) {
@ -152,17 +155,13 @@ const styleManager = (() => {
data = Object.assign(createNewStyle(), data); data = Object.assign(createNewStyle(), data);
} }
return saveStyle(data) return saveStyle(data)
.then(newData => handleSave( .then(newData => handleSave(newData, 'editSave'));
newData,
'editSave',
style ? 'styleUpdated' : 'styleAdded'
));
} }
function setStyleExclusions(id, exclusions) { function setStyleExclusions(id, exclusions) {
const data = Object.assign({}, styles.get(id).data, {exclusions}); const data = Object.assign({}, styles.get(id).data, {exclusions});
return saveStyle(data) return saveStyle(data)
.then(newData => handleSave(newData, 'exclusions', 'styleUpdated')); .then(newData => handleSave(newData, 'exclusions'));
} }
function deleteStyle(id) { function deleteStyle(id) {
@ -237,15 +236,6 @@ const styleManager = (() => {
}); });
} }
// function importStyle(style) {
// FIXME: move this to importer
// style.originalDigest = style.originalDigest || style.styleDigest; // TODO: remove in the future
// delete style.styleDigest; // TODO: remove in the future
// if (typeof style.originalDigest !== 'string' || style.originalDigest.length !== 40) {
// delete style.originalDigest;
// }
// }
function saveStyle(style) { function saveStyle(style) {
if (!style.name) { if (!style.name) {
throw new Error('style name is empty'); throw new Error('style name is empty');
@ -262,15 +252,18 @@ const styleManager = (() => {
}); });
} }
function handleSave(data, reason, method) { function handleSave(data, reason) {
const style = styles.get(data.id); const style = styles.get(data.id);
let method;
if (!style) { if (!style) {
styles.set(data.id, { styles.set(data.id, {
appliesTo: new Set(), appliesTo: new Set(),
data data
}); });
method = 'styleAdded';
} else { } else {
style.data = data; style.data = data;
method = 'styleUpdated';
} }
return broadcastStyleUpdated(data, reason, method) return broadcastStyleUpdated(data, reason, method)
.then(() => data); .then(() => data);