Loëck Vézien
7 years ago
committed by
GitHub
9 changed files with 146 additions and 7 deletions
@ -0,0 +1,52 @@ |
|||||
|
// @flow
|
||||
|
|
||||
|
import React, { PureComponent } from 'react' |
||||
|
import { remote } from 'electron' |
||||
|
import queryString from 'query-string' |
||||
|
|
||||
|
import QRCode from 'components/base/QRCode' |
||||
|
import Box from 'components/base/Box' |
||||
|
import { AddressBox } from 'components/ReceiveBox' |
||||
|
|
||||
|
type State = { |
||||
|
data: Object | null, |
||||
|
} |
||||
|
|
||||
|
class PrintWrapper extends PureComponent<any, State> { |
||||
|
state = { |
||||
|
data: null, |
||||
|
} |
||||
|
|
||||
|
componentWillMount() { |
||||
|
this.setState({ |
||||
|
data: queryString.parse(this.props.location.search), |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
componentDidMount() { |
||||
|
window.requestAnimationFrame(() => |
||||
|
setTimeout(() => { |
||||
|
// hacky way to detect that render is ready
|
||||
|
// from the parent window
|
||||
|
remote.getCurrentWindow().minimize() |
||||
|
}, 300), |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
render() { |
||||
|
const { data } = this.state |
||||
|
if (!data) { |
||||
|
return null |
||||
|
} |
||||
|
const { address, amount } = data |
||||
|
return ( |
||||
|
<Box p={3} flow={3}> |
||||
|
<QRCode size={150} data={`bitcoin:${address}${amount ? `?amount=${amount}` : ''}`} /> |
||||
|
<AddressBox>{address}</AddressBox> |
||||
|
{amount && <AddressBox>{amount}</AddressBox>} |
||||
|
</Box> |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export default PrintWrapper |
@ -0,0 +1,61 @@ |
|||||
|
// @flow
|
||||
|
|
||||
|
import { PureComponent } from 'react' |
||||
|
import { remote } from 'electron' |
||||
|
import queryString from 'query-string' |
||||
|
|
||||
|
const { BrowserWindow } = remote |
||||
|
|
||||
|
type Props = { |
||||
|
render: Function, |
||||
|
data: Object, |
||||
|
} |
||||
|
|
||||
|
type State = { |
||||
|
isLoading: boolean, |
||||
|
} |
||||
|
|
||||
|
class Print extends PureComponent<Props, State> { |
||||
|
state = { |
||||
|
isLoading: false, |
||||
|
} |
||||
|
|
||||
|
handlePrint = () => { |
||||
|
const { data } = this.props |
||||
|
|
||||
|
this.setState({ isLoading: true }) |
||||
|
|
||||
|
const w = new BrowserWindow({ |
||||
|
show: false, |
||||
|
webPreferences: { |
||||
|
// Enable, among other things, the ResizeObserver
|
||||
|
experimentalFeatures: true, |
||||
|
}, |
||||
|
}) |
||||
|
|
||||
|
w.webContents.openDevTools() |
||||
|
|
||||
|
const url = __DEV__ |
||||
|
? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT || ''}` |
||||
|
: `file://${__dirname}/index.html` |
||||
|
|
||||
|
w.loadURL(`${url}/#/print?${queryString.stringify(data)}`) |
||||
|
|
||||
|
w.webContents.on('did-finish-load', () => { |
||||
|
w.on('minimize', () => { |
||||
|
w.webContents.print({}, () => { |
||||
|
w.destroy() |
||||
|
this.setState({ isLoading: false }) |
||||
|
}) |
||||
|
}) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
render() { |
||||
|
const { render } = this.props |
||||
|
const { isLoading } = this.state |
||||
|
return render(this.handlePrint, isLoading) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export default Print |
Loading…
Reference in new issue