Mailspring/app/internal_packages/print/lib/print-window.ts
Ben Gotow 149b389508
Replace Babel with TypeScript compiler, switch entire app to TypeScript 🎉 (#1404)
* Switch to using Typescript instead of Babel

* Switch all es6 / jsx file extensions to ts / tsx

* Convert Utils to a TS module from module.exports style module

* Move everything from module.exports to typescript exports

* Define .d.ts files for mailspring-exports and component kit… Yes it seems this is the best option :(

* Load up on those @types

* Synthesize TS types from PropTypes for standard components

* Add types to Model classes and move constructor constants to instance vars

* 9800 => 7700 TS errors

* 7700 => 5600 TS errors

* 5600 => 5330 TS errors

* 5330 => 4866 TS errors

* 4866 => 4426 TS errors

* 4426 => 2411 TS errors

* 2411 > 1598 TS errors

* 1598 > 769 TS errors

* 769 > 129 TS errors

* 129 > 22 TS errors

* Fix runtime errors

* More runtime error fixes

* Remove support for custom .es6 file extension

* Remove a few odd remaining references to Nylas

* Don’t ship Typescript support in the compiled app for now

* Fix issues in compiled app - module resolution in TS is case sensitive?

* README updates

* Fix a few more TS errors

* Make “No Signature” option clickable + selectable

* Remove flicker when saving file and reloading keymaps

* Fix mail rule item height in preferences

* Fix missing spacing in thread sharing popover

* Fix scrollbar ticks being nested incorrectly

* Add Japanese as a manually reviewed language

* Prevent the thread list from “sticking”

* Re-use Sheet when switching root tabs, prevent sidebar from resetting

* Ensure specs run

* Update package configuration to avoid shpping types

* Turn eslint back on - we will opt-in to the TS rules one by one
2019-03-04 11:03:12 -08:00

95 lines
3.4 KiB
TypeScript

import path from 'path';
import fs from 'fs';
import { remote } from 'electron';
import { localized } from 'mailspring-exports';
const { app, BrowserWindow } = remote;
export default class PrintWindow {
browserWin: Electron.BrowserWindow;
tmpFile: string;
constructor({ subject, account, participants, styleTags, htmlContent, printMessages }) {
// This script will create the print prompt when loaded. We can also call
// print directly from this process, but inside print.js we can make sure to
// call window.print() after we've cleaned up the dom for printing
const tmp = app.getPath('temp');
const tmpMessagesPath = path.join(tmp, 'print.messages.js');
const preloadPath = path.join(__dirname, '..', 'static', 'print-preload.js');
const scriptPath = path.join(__dirname, '..', 'static', 'print.js');
const stylesPath = path.join(__dirname, '..', 'static', 'print-styles.css');
const participantsHtml = participants
.map(part => {
return `<li class="participant"><span>${part.name || ''} &lt;${part.email}&gt;</span></li>`;
})
.join('');
const content = `
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src * mailspring:; script-src 'self' chrome-extension://react-developer-tools; style-src * 'unsafe-inline' mailspring:; img-src * data: mailspring: file:;">
<meta charset="utf-8">
${styleTags}
<link rel="stylesheet" type="text/css" href="${stylesPath}">
</head>
<body>
<div id="print-header">
<div id="print-note" style="display: none;">
One or more messages in this thread were collapsed and will not be printed.
To print these messages, expand them in the main window.
</div>
<div style="padding: 10px 14px;">
<div id="close-button">
${localized('Close')}
</div>
<div id="print-button">
${localized('Print')}
</div>
<div id="print-pdf-button">
${localized('Save as PDF')}
</div>
<div class="logo-wrapper">
<span class="account">${account.name} &lt;${account.email}&gt;</span>
</div>
</div>
</div>
<div id="print-header-spacing"></div>
<h1 class="print-subject">${subject}</h1>
<div class="print-participants">
<ul>
${participantsHtml}
</ul>
</div>
${htmlContent}
<script type="text/javascript" src="${tmpMessagesPath}"></script>
<script type="text/javascript" src="${scriptPath}"></script>
</body>
</html>
`;
this.tmpFile = path.join(tmp, 'print.html');
this.browserWin = new BrowserWindow({
width: 800,
height: 600,
title: `${localized('Print')} - ${subject}`,
webPreferences: {
preload: preloadPath,
nodeIntegration: false,
contextIsolation: false,
},
});
this.browserWin.setMenu(null);
fs.writeFileSync(tmpMessagesPath, `window.printMessages = ${printMessages}`);
fs.writeFileSync(this.tmpFile, content);
}
/**
* Load our temp html file. Once the file is loaded it will run print.js, and
* that script will pop out the print dialog.
*/
load() {
this.browserWin.loadURL(`file://${this.tmpFile}`);
}
}