Refactor getTabInfo to return a list, and create a renderTabs function

This commit is contained in:
Ian Bicking 2018-07-23 15:08:10 -05:00
Родитель 120df9fcc4
Коммит 75a6023675
2 изменённых файлов: 20 добавлений и 14 удалений

Просмотреть файл

@ -91,7 +91,18 @@ async function getTabInfo(tabIds, {wantsScreenshots, wantsReadability}) {
console.error("Error getting info for tab", tabId, tabInfo[tabId].url, ":", String(e));
}
}
return tabInfo;
return tabIds.map(id => tabInfo[id]);
}
async function renderTabs(tabIds, templateName) {
let { wantsScreenshots, wantsReadability } = templateMetadata.getTemplate(templateName);
let tabInfo = await getTabInfo(tabIds, {wantsScreenshots, wantsReadability});
let TemplateComponent = emailTemplates[templateMetadata.getTemplate(templateName).componentName];
if (!TemplateComponent) {
throw new Error(`No component found for template: ${templateName}`);
}
let html = emailTemplates.renderEmail(tabInfo, TemplateComponent);
return { html, tabInfo };
}
async function sendEmail(tabIds) {
@ -114,13 +125,7 @@ async function sendEmail(tabIds) {
loginInterrupt();
}
}, 1000);
let { wantsScreenshots, wantsReadability } = templateMetadata.getTemplate(selectedTemplate);
let tabInfo = await getTabInfo(tabIds, {wantsScreenshots, wantsReadability});
let TemplateComponent = emailTemplates[templateMetadata.getTemplate(selectedTemplate).componentName];
if (!TemplateComponent) {
throw new Error(`No component found for template: ${selectedTemplate}`);
}
let html = emailTemplates.renderEmail(tabIds.map(id => tabInfo[id]), TemplateComponent);
let { html, tabInfo } = await renderTabs(tabIds, selectedTemplate);
await browser.tabs.executeScript(newTab.id, {
file: "set-html-email.js",
});
@ -133,10 +138,7 @@ async function sendEmail(tabIds) {
}
async function copyTabHtml(tabIds) {
let { wantsScreenshots, wantsReadability } = templateMetadata.getTemplate(selectedTemplate);
let tabInfo = await getTabInfo(tabIds, {wantsScreenshots, wantsReadability});
let TemplateComponent = emailTemplates[templateMetadata.getTemplate(selectedTemplate).componentName];
let html = emailTemplates.renderEmail(tabIds.map(id => tabInfo[id]), TemplateComponent);
let { html } = await renderTabs(tabIds, selectedTemplate);
copyHtmlToClipboard(html);
}
@ -172,10 +174,14 @@ function loginInterrupt() {
async function closeManyTabs(composeTabId, otherTabInfo) {
let tabs = await browser.tabs.query({});
let toClose = [composeTabId];
let tabInfoById = {};
for (let tabInfo of otherTabInfo) {
tabInfoById[tabInfo.id] = tabInfo;
}
for (let tab of tabs) {
// Note that .url might be the canonical URL, but .urlBar is what shows up in the URL bar
// and the tab API
if (otherTabInfo[tab.id] && otherTabInfo[tab.id].urlBar === tab.url) {
if (tabInfoById[tab.id] && tabInfoById[tab.id].urlBar === tab.url) {
toClose.push(tab.id);
}
}

Просмотреть файл

@ -99,7 +99,7 @@ function showCloseButtons() {
showIframe("#done-container");
let done = iframeDocument.querySelector("#done");
let closeAllTabs = iframeDocument.querySelector("#close-all-tabs");
let numTabs = Object.keys(closeTabInfo).length;
let numTabs = closeTabInfo.length;
if (numTabs === 1) {
closeAllTabs.textContent = closeAllTabs.getAttribute("data-one-tab");
} else {