Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat: add window.screenshot for svg-convertible chrome screenshots (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl authored and tilmx committed Sep 19, 2018
1 parent f8e4fa0 commit fd259fe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/electron/create-app-message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as Electron from 'electron';
import * as Fs from 'fs';
import * as Os from 'os';
import * as Path from 'path';
import * as Message from '../message';
import * as Model from '../model';
import * as uuid from 'uuid';
Expand Down Expand Up @@ -144,6 +147,40 @@ export async function createAppMessageHandler(
default:
return;
}
break;
}
case Message.MessageType.ChromeScreenShot: {
if (!ctx.win) {
return;
}

ctx.win.webContents.printToPDF(
{
marginsType: 1,
printBackground: true,
pageSize: {
// px => micron
width: message.payload.width * 265,
height: message.payload.height * 265
// tslint:disable-next-line:no-any
} as any
},
(err, data) => {
if (err) {
return console.error(err.message);
}

const targetPath = Path.join(Os.tmpdir(), `${uuid.v4()}.pdf`);

Fs.writeFile(targetPath, data, error => {
if (error) {
return console.error(error);
}

Electron.shell.openExternal(`file://${targetPath}`);
});
}
);
}
}
};
Expand Down
13 changes: 11 additions & 2 deletions src/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export enum MessageType {
ChangeUserStore = 'user-store-change',
SelectElement = 'select-element',
HighlightElement = 'highlight-element',
WindowClose = 'window-close'
WindowClose = 'window-close',
ChromeScreenShot = 'chrome-screenshot',
}

export type Message =
Expand Down Expand Up @@ -151,7 +152,8 @@ export type Message =
| ChangeUserStore
| SelectElement
| HighlightElement
| WindowClose;
| WindowClose
| ChromeScreenShot;

export type ActivatePage = Envelope<MessageType.ActivatePage, { id: string }>;
export type AppLoaded = EmptyEnvelope<MessageType.AppLoaded>;
Expand Down Expand Up @@ -422,3 +424,10 @@ export type MobxAddMessage = Envelope<MessageType.MobxAdd, MobxAddPayload>;
export type MobxDeleteMessage = Envelope<MessageType.MobxDelete, MobxDeletePayload>;
export type MobxSpliceMessage = Envelope<MessageType.MobxSplice, MobxSplicePayload>;
export type WindowClose = EmptyEnvelope<MessageType.WindowClose>;
export type ChromeScreenShot = Envelope<
MessageType.ChromeScreenShot,
{
width: number;
height: number;
}
>;
13 changes: 13 additions & 0 deletions src/renderer/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ export function startRenderer(): void {

// tslint:disable-next-line:no-any
(window as any).store = store;

// tslint:disable-next-line:no-any
(window as any).screenshot = () => {
sender.send({
id: uuid.v4(),
type: MessageType.ChromeScreenShot,
payload: {
width: window.innerWidth,
height: window.innerHeight
}
});
};

console.log('Access ViewStore at .store');

createListeners({ store });
Expand Down

0 comments on commit fd259fe

Please sign in to comment.