diff --git a/background/background.js b/background/background.js index 33009abf..25aad251 100644 --- a/background/background.js +++ b/background/background.js @@ -16,6 +16,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, { getStylesInfo: styleManager.getStylesInfo, toggleStyle: styleManager.toggleStyle, deleteStyle: styleManager.deleteStyle, + getStylesInfoByUrl: styleManager.getStylesInfoByUrl, // saveStyle, // deleteStyle, diff --git a/background/style-manager.js b/background/style-manager.js index a7a06c7c..a82a69cb 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -25,7 +25,7 @@ const styleManager = (() => { editSave, toggleStyle, getAllStyles, // used by import-export - getStylesInfoForUrl, // used by popup + getStylesInfoByUrl, // used by popup countStyles, // TODO: get all styles API? // TODO: get style by ID? @@ -88,12 +88,23 @@ const styleManager = (() => { return true; } - function editSave(style) { - // const style = - return saveStyle(style); + function editSave(data) { + data = Object.assign({}, styles.get(data.id).data, data); + 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) { for (const [name, fn] in Object.entries(methods)) { @@ -163,42 +174,41 @@ const styleManager = (() => { msg.broadcastExtension({method: 'styleAdded', style: getStyleWithNoCode(newData)}), 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) { @@ -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) { @@ -276,6 +288,7 @@ const styleManager = (() => { code += section.code; } } + // FIXME: trim comment? return code; } diff --git a/popup/popup.js b/popup/popup.js index 3cfa9917..e8e65cd0 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -20,7 +20,7 @@ getActiveTab().then(tab => : getTabRealURL(tab) ).then(url => Promise.all([ (tabURL = URLS.supported(url) ? url : '') && - API.getStylesInfoForUrl(tabURL), + API.getStylesInfoByUrl(tabURL), onDOMready().then(initPopup), ])).then(([styles]) => { showStyles(styles);