2017-09-27 02:42:18 +08:00
|
|
|
import { AccountStore, Actions } from 'mailspring-exports';
|
2015-12-04 03:52:51 +08:00
|
|
|
import PrintWindow from './print-window';
|
|
|
|
|
|
|
|
class Printer {
|
|
|
|
constructor() {
|
|
|
|
this.unsub = Actions.printThread.listen(this._printThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
_printThread(thread, htmlContent) {
|
|
|
|
if (!thread) throw new Error('Printing: No thread active!');
|
2017-09-27 02:33:08 +08:00
|
|
|
const account = AccountStore.accountForId(thread.accountId);
|
2015-12-04 03:52:51 +08:00
|
|
|
|
|
|
|
// Get the <nylas-styles> tag present in the document
|
2017-07-12 05:12:57 +08:00
|
|
|
const styleTag = document.getElementsByTagName('managed-styles')[0];
|
2015-12-04 03:52:51 +08:00
|
|
|
// These iframes should correspond to the message iframes when a thread is
|
|
|
|
// focused
|
|
|
|
const iframes = document.getElementsByTagName('iframe');
|
|
|
|
// Grab the html inside the iframes
|
2017-09-27 02:33:08 +08:00
|
|
|
const messagesHtml = [].slice.call(iframes).map(iframe => {
|
2015-12-04 03:52:51 +08:00
|
|
|
return iframe.contentDocument.documentElement.innerHTML;
|
|
|
|
});
|
|
|
|
|
|
|
|
const win = new PrintWindow({
|
|
|
|
subject: thread.subject,
|
|
|
|
account: {
|
2016-01-09 01:31:24 +08:00
|
|
|
name: account.name,
|
|
|
|
email: account.emailAddress,
|
2015-12-04 03:52:51 +08:00
|
|
|
},
|
|
|
|
participants: thread.participants,
|
|
|
|
styleTags: styleTag.innerHTML,
|
|
|
|
htmlContent,
|
|
|
|
printMessages: JSON.stringify(messagesHtml),
|
|
|
|
});
|
|
|
|
win.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this.unsub();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Printer;
|