Add: handle styleUpdated message

This commit is contained in:
eight 2018-10-14 19:09:06 +08:00
parent f85d4de39b
commit 46027120ec
2 changed files with 23 additions and 12 deletions

View File

@ -281,7 +281,7 @@ const styleManager = (() => {
// FIXME: do we want to cache this? Who would like to open popup rapidly // FIXME: do we want to cache this? Who would like to open popup rapidly
// or search the DB with the same URL? // or search the DB with the same URL?
const result = []; const result = [];
const datas = !id ? styles.values.map(s => s.data) : const datas = !id ? [...styles.values()].map(s => s.data) :
styles.has(id) ? [styles.get(id).data] : []; styles.has(id) ? [styles.get(id).data] : [];
for (const data of datas) { for (const data of datas) {
let excluded = false; let excluded = false;

View File

@ -53,7 +53,7 @@ function onRuntimeMessage(msg) {
case 'styleUpdated': case 'styleUpdated':
case 'exclusionsUpdated': case 'exclusionsUpdated':
if (msg.reason === 'editPreview' || msg.reason === 'editPreviewEnd') return; if (msg.reason === 'editPreview' || msg.reason === 'editPreviewEnd') return;
handleUpdate(msg.style); handleUpdate(msg);
break; break;
case 'styleDeleted': case 'styleDeleted':
handleDelete(msg.style.id); handleDelete(msg.style.id);
@ -499,20 +499,31 @@ Object.assign(handleEvent, {
}); });
function handleUpdate(style) { function handleUpdate({style, reason}) {
if ($(ENTRY_ID_PREFIX + style.id)) {
createStyleElement({style, check: true});
return;
}
if (!tabURL) return; if (!tabURL) return;
// Add an entry when a new style for the current url is installed
API.getStylesByUrl(tabURL, style.id).then(([style]) => { fetchStyle()
if (style) { .then(style => {
if (!style) {
return;
}
if ($(ENTRY_ID_PREFIX + style.id)) {
createStyleElement({style});
return;
}
document.body.classList.remove('blocked'); document.body.classList.remove('blocked');
$$.remove('.blocked-info, #no-styles'); $$.remove('.blocked-info, #no-styles');
createStyleElement({style, check: true}); createStyleElement({style});
})
.catch(console.error);
function fetchStyle() {
if (reason === 'toggle' && $(ENTRY_ID_PREFIX + style.id)) {
return Promise.resolve(style);
} }
}); return API.getStylesByUrl(tabURL, style.id)
.then(([style]) => style);
}
} }