Add: getStylesInfoByUrl

This commit is contained in:
eight 2018-10-06 13:45:37 +08:00
parent 095998f07c
commit fedf844ddd
3 changed files with 57 additions and 43 deletions

View File

@ -16,6 +16,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
getStylesInfo: styleManager.getStylesInfo, getStylesInfo: styleManager.getStylesInfo,
toggleStyle: styleManager.toggleStyle, toggleStyle: styleManager.toggleStyle,
deleteStyle: styleManager.deleteStyle, deleteStyle: styleManager.deleteStyle,
getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
// saveStyle, // saveStyle,
// deleteStyle, // deleteStyle,

View File

@ -25,7 +25,7 @@ const styleManager = (() => {
editSave, editSave,
toggleStyle, toggleStyle,
getAllStyles, // used by import-export getAllStyles, // used by import-export
getStylesInfoForUrl, // used by popup getStylesInfoByUrl, // used by popup
countStyles, countStyles,
// TODO: get all styles API? // TODO: get all styles API?
// TODO: get style by ID? // TODO: get style by ID?
@ -88,12 +88,23 @@ const styleManager = (() => {
return true; return true;
} }
function editSave(style) { function editSave(data) {
// const style = data = Object.assign({}, styles.get(data.id).data, data);
return saveStyle(style); return saveStyle(data)
.then(newData =>
broadcastStyleUpdated(newData)
.then(() => newData)
);
} }
function setStyleExclusions() {} function setStyleExclusions(id, exclusions) {
const data = Object.assign({}, styles.get(id), {exclusions});
return saveStyle(data)
.then(newData =>
broadcastStyleUpdated(newData)
.then(() => newData)
);
}
function ensurePrepared(methods) { function ensurePrepared(methods) {
for (const [name, fn] in Object.entries(methods)) { for (const [name, fn] in Object.entries(methods)) {
@ -163,42 +174,41 @@ const styleManager = (() => {
msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}), msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}),
msg.broadcastTab(tab => getStyleAddedMessage(tab, newData, appliesTo)) msg.broadcastTab(tab => getStyleAddedMessage(tab, newData, appliesTo))
]); ]);
} else {
const excluded = new Set();
const updated = new Map();
for (const url of style.appliesTo) {
const code = getAppliedCode(url, newData);
const cache = cachedStyleForUrl.get(url);
if (!code) {
excluded.add(url);
if (cache) {
delete cache[newData.id];
}
} else {
updated.set(url, code);
cache[newData.id] = code;
}
}
style.appliesTo = new Set(updated.keys());
return Promise.all([
msg.broadcastExtension({method: 'styleUpdated', style: getStyleWithNoCode(newData)}),
msg.broadcastTab(tab => {
if (excluded.has(tab.url)) {
return {
method: 'styleDeleted',
style: {id: newData.id}
};
}
if (updated.has(tab.url)) {
return {
method: 'styleUpdated',
style: {id: newData.id, sections: updated.get(tab.url)}
};
}
return getStyleAddedMessage(tab, newData, style.appliesTo);
})
]);
} }
const excluded = new Set();
const updated = new Map();
for (const url of style.appliesTo) {
const code = getAppliedCode(url, newData);
const cache = cachedStyleForUrl.get(url);
if (!code) {
excluded.add(url);
if (cache) {
delete cache[newData.id];
}
} else {
updated.set(url, code);
cache[newData.id] = code;
}
}
style.appliesTo = new Set(updated.keys());
return Promise.all([
msg.broadcastExtension({method: 'styleUpdated', style: getStyleWithNoCode(newData)}),
msg.broadcastTab(tab => {
if (excluded.has(tab.url)) {
return {
method: 'styleDeleted',
style: {id: newData.id}
};
}
if (updated.has(tab.url)) {
return {
method: 'styleUpdated',
style: {id: newData.id, sections: updated.get(tab.url)}
};
}
return getStyleAddedMessage(tab, newData, style.appliesTo);
})
]);
} }
function getStyleAddedMessage(tab, data, appliesTo) { function getStyleAddedMessage(tab, data, appliesTo) {
@ -243,8 +253,10 @@ const styleManager = (() => {
}); });
} }
function getStylesInfoForUrl(url) { function getStylesInfoByUrl(url) {
const sections = getSectionsByUrl(url);
return Object.keys(sections)
.map(k => getStyleWithNoCode(styles.get(Number(k)).data));
} }
function getSectionsByUrl(url, filterId) { function getSectionsByUrl(url, filterId) {
@ -276,6 +288,7 @@ const styleManager = (() => {
code += section.code; code += section.code;
} }
} }
// FIXME: trim comment?
return code; return code;
} }

View File

@ -20,7 +20,7 @@ getActiveTab().then(tab =>
: getTabRealURL(tab) : getTabRealURL(tab)
).then(url => Promise.all([ ).then(url => Promise.all([
(tabURL = URLS.supported(url) ? url : '') && (tabURL = URLS.supported(url) ? url : '') &&
API.getStylesInfoForUrl(tabURL), API.getStylesInfoByUrl(tabURL),
onDOMready().then(initPopup), onDOMready().then(initPopup),
])).then(([styles]) => { ])).then(([styles]) => {
showStyles(styles); showStyles(styles);