This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(export): use message system for all exporters
- Loading branch information
Showing
8 changed files
with
190 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,9 @@ | ||
import { WebviewTag } from 'electron'; | ||
import * as Fs from 'fs'; | ||
import * as Util from 'util'; | ||
|
||
const writeFile = Util.promisify(Fs.writeFile); | ||
|
||
export interface ExportResult { | ||
error?: Error; | ||
result?: Buffer; | ||
} | ||
|
||
export interface ExportWriteResult { | ||
error?: Error; | ||
} | ||
|
||
export abstract class Exporter { | ||
public contents: Buffer; | ||
|
||
public abstract async createExport(webview: WebviewTag): Promise<ExportResult>; | ||
|
||
public async writeToDisk(path: string): Promise<ExportWriteResult> { | ||
try { | ||
await writeFile(path, this.contents); | ||
return {}; | ||
} catch (error) { | ||
return { error }; | ||
} | ||
} | ||
public abstract execute(path: string): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,110 @@ | ||
import * as Url from 'url'; | ||
import * as uuid from 'uuid'; | ||
|
||
import * as Sender from '../message/client'; | ||
import { Exporter, ExportResult } from './exporter'; | ||
import { Exporter } from './exporter'; | ||
import { ServerMessageType } from '../message'; | ||
import { ViewStore } from '../store'; | ||
import * as Url from 'url'; | ||
import * as uuid from 'uuid'; | ||
|
||
export class PdfExporter extends Exporter { | ||
public async createExport(): Promise<ExportResult> { | ||
return new Promise<ExportResult>(resolve => { | ||
const id = uuid.v4(); | ||
const initial = 'data:text/html;<html></html>'; | ||
|
||
const webview = document.createElement('webview'); | ||
webview.style.position = 'fixed'; | ||
webview.style.top = '100vh'; | ||
webview.src = initial; | ||
webview.webpreferences = 'useContentSize=yes, javascript=no'; | ||
document.body.insertBefore(webview, document.body.firstChild); | ||
|
||
const store = ViewStore.getInstance(); | ||
let started; | ||
|
||
// (1) Request HTML contents from preview | ||
const start = () => { | ||
Sender.send({ | ||
payload: undefined, | ||
type: ServerMessageType.ContentRequest, | ||
id | ||
}); | ||
}; | ||
|
||
// (2) Receive HTML response from preview and load into webview | ||
// tslint:disable-next-line:no-any | ||
const receive = message => { | ||
if (message.type !== ServerMessageType.ContentResponse || message.id !== id) { | ||
return; | ||
} | ||
public execute(path: string): void { | ||
const id = uuid.v4(); | ||
const initial = 'data:text/html;<html></html>'; | ||
|
||
const webview = document.createElement('webview'); | ||
webview.style.position = 'fixed'; | ||
webview.style.top = '100vh'; | ||
webview.src = initial; | ||
webview.webpreferences = 'useContentSize=yes, javascript=no'; | ||
document.body.insertBefore(webview, document.body.firstChild); | ||
|
||
const store = ViewStore.getInstance(); | ||
let started; | ||
|
||
// (1) Request HTML contents from preview | ||
const start = () => { | ||
Sender.send({ | ||
payload: undefined, | ||
type: ServerMessageType.ContentRequest, | ||
id | ||
}); | ||
}; | ||
|
||
const payload = message.payload; | ||
// (2) Receive HTML response from preview and load into webview | ||
// tslint:disable-next-line:no-any | ||
const receive = message => { | ||
if (message.type !== ServerMessageType.ContentResponse || message.id !== id) { | ||
return; | ||
} | ||
|
||
const parsed = Url.parse(payload.location); | ||
const payload = message.payload; | ||
|
||
if (parsed.host !== `localhost:${store.getServerPort()}`) { | ||
return; | ||
} | ||
const parsed = Url.parse(payload.location); | ||
|
||
webview.style.width = `${payload.width}px`; | ||
webview.style.height = `${payload.height}px`; | ||
if (parsed.host !== `localhost:${store.getServerPort()}`) { | ||
return; | ||
} | ||
|
||
webview.loadURL( | ||
`data:text/html;charset=utf-8,${encodeURIComponent(payload.document)}`, | ||
{ | ||
baseURLForDataURL: payload.location | ||
webview.style.width = `${payload.width}px`; | ||
webview.style.height = `${payload.height}px`; | ||
|
||
webview.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(payload.document)}`, { | ||
baseURLForDataURL: payload.location | ||
}); | ||
}; | ||
|
||
// (3) Wait for webview to be ready and capture the page | ||
const createPdf = () => { | ||
if (started !== id) { | ||
return; | ||
} | ||
|
||
webview.printToPDF( | ||
{ | ||
marginsType: 1, | ||
pageSize: 'A4', | ||
printBackground: true, | ||
printSelectionOnly: false, | ||
landscape: false | ||
}, | ||
(error: Error, data: Buffer) => { | ||
if (error) { | ||
// Todo: Implement error message | ||
// resolve({ error }); | ||
return; | ||
} | ||
); | ||
}; | ||
|
||
// (3) Wait for webview to be ready and capture the page | ||
const createPdf = () => { | ||
if (started !== id) { | ||
return; | ||
} | ||
this.contents = data; | ||
|
||
webview.printToPDF( | ||
{ | ||
marginsType: 1, | ||
pageSize: 'A4', | ||
printBackground: true, | ||
printSelectionOnly: false, | ||
landscape: false | ||
}, | ||
(error: Error, data: Buffer) => { | ||
if (error) { | ||
resolve({ error }); | ||
return; | ||
} | ||
|
||
this.contents = data; | ||
resolve({ result: this.contents }); | ||
|
||
setTimeout(() => { | ||
document.body.removeChild(webview); | ||
}); | ||
} | ||
); | ||
}; | ||
Sender.send({ | ||
id: uuid.v4(), | ||
type: ServerMessageType.ExportPDF, | ||
payload: { path, content: this.contents } | ||
}); | ||
|
||
webview.addEventListener('did-finish-load', () => { | ||
if (webview.src === initial) { | ||
return; | ||
setTimeout(() => { | ||
document.body.removeChild(webview); | ||
}); | ||
} | ||
createPdf(); | ||
}); | ||
); | ||
}; | ||
|
||
webview.addEventListener('did-finish-load', () => { | ||
if (webview.src === initial) { | ||
return; | ||
} | ||
createPdf(); | ||
}); | ||
|
||
Sender.receive(receive); | ||
Sender.receive(receive); | ||
|
||
webview.addEventListener('dom-ready', () => { | ||
if (started === id) { | ||
return; | ||
} | ||
webview.addEventListener('dom-ready', () => { | ||
if (started === id) { | ||
return; | ||
} | ||
|
||
started = id; | ||
start(); | ||
}); | ||
started = id; | ||
start(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.