1
1
mirror of https://github.com/jxnblk/mdx-deck.git synced 2024-11-10 14:21:06 +03:00

Merge pull request #200 from jacobmischka/master

Add flag to disable puppeteer sandbox
This commit is contained in:
Brent Jackson 2019-02-18 16:46:03 -05:00 committed by GitHub
commit 0047cd7390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 123 additions and 103 deletions

107
cli.js
View File

@ -6,14 +6,15 @@ const open = require('react-dev-utils/openBrowser')
const chalk = require('chalk') const chalk = require('chalk')
const remark = { const remark = {
emoji: require('remark-emoji'), emoji: require('remark-emoji'),
unwrapImages: require('remark-unwrap-images') unwrapImages: require('remark-unwrap-images'),
} }
const pkg = require('./package.json') const pkg = require('./package.json')
const config = require('pkg-conf').sync('mdx-deck') const config = require('pkg-conf').sync('mdx-deck')
const log = require('./lib/log') const log = require('./lib/log')
const cli = meow(` const cli = meow(
`
${chalk.gray('Usage')} ${chalk.gray('Usage')}
$ ${chalk.magenta('mdx-deck deck.mdx')} $ ${chalk.magenta('mdx-deck deck.mdx')}
@ -46,56 +47,70 @@ const cli = meow(`
--width Width in pixels --width Width in pixels
--height Height in pixels --height Height in pixels
`, { ${chalk.gray('PDF options')}
description: chalk.magenta('[mdx-deck] ') + chalk.gray(pkg.description),
flags: {
port: {
type: 'string',
alias: 'p'
},
hotPort: {
type: 'string',
},
host: {
type: 'string',
alias: 'h'
},
open: {
type: 'boolean',
alias: 'o',
default: true
},
outDir: {
type: 'string',
alias: 'd'
},
outFile: {
type: 'string',
},
html: {
type: 'boolean',
default: true
},
webpack: {
type: 'string',
}
}
})
const [ cmd, file ] = cli.input --no-sandbox Disable Puppeteer sandbox
`,
{
description: chalk.magenta('[mdx-deck] ') + chalk.gray(pkg.description),
flags: {
port: {
type: 'string',
alias: 'p',
},
host: {
type: 'string',
alias: 'h',
},
hotPort: {
type: 'string',
},
open: {
type: 'boolean',
alias: 'o',
default: true,
},
outDir: {
type: 'string',
alias: 'd',
},
outFile: {
type: 'string',
},
html: {
type: 'boolean',
default: true,
},
webpack: {
type: 'string',
},
sandbox: {
type: 'boolean',
default: true,
},
},
}
)
const [cmd, file] = cli.input
const doc = file || cmd const doc = file || cmd
if (!doc) cli.showHelp(0) if (!doc) cli.showHelp(0)
const opts = Object.assign({ const opts = Object.assign(
dirname: path.dirname(path.resolve(doc)), {
globals: { dirname: path.dirname(path.resolve(doc)),
FILENAME: JSON.stringify(path.resolve(doc)) globals: {
FILENAME: JSON.stringify(path.resolve(doc)),
},
host: 'localhost',
port: 8080,
outDir: 'dist',
}, },
host: 'localhost', config,
port: 8080, cli.flags
outDir: 'dist', )
}, config, cli.flags)
opts.outDir = path.resolve(opts.outDir) opts.outDir = path.resolve(opts.outDir)
if (opts.webpack) { if (opts.webpack) {

View File

@ -4,11 +4,7 @@ const mkdirp = require('mkdirp')
const puppeteer = require('puppeteer') const puppeteer = require('puppeteer')
module.exports = async (opts = {}) => { module.exports = async (opts = {}) => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
// This can be used to control some UI components's visibility like: Appear,... // This can be used to control some UI components's visibility like: Appear,...
page.setUserAgent('MDX-Deck/1.7.7 Print/PDF')
const { const {
outDir, outDir,
@ -16,12 +12,17 @@ module.exports = async (opts = {}) => {
port, port,
width = 1280, width = 1280,
height = 960, height = 960,
sandbox = true,
} = opts } = opts
const args = sandbox ? [] : ['--no-sandbox', '--disable-setuid-sandbox']
const browser = await puppeteer.launch({ args })
const page = await browser.newPage()
page.setUserAgent('MDX-Deck/1.7.7 Print/PDF')
const filename = path.join(outDir, outFile) const filename = path.join(outDir, outFile)
if (!fs.existsSync(outDir)) mkdirp.sync(outDir) if (!fs.existsSync(outDir)) mkdirp.sync(outDir)
await page.goto('http://localhost:' + port, { await page.goto('http://localhost:' + port, {
waitUntil: 'networkidle2' waitUntil: 'networkidle2',
}) })
await page.pdf({ await page.pdf({
@ -29,7 +30,7 @@ module.exports = async (opts = {}) => {
height, height,
path: filename, path: filename,
scale: 1, scale: 1,
printBackground: true printBackground: true,
}) })
await browser.close() await browser.close()

View File

@ -4,47 +4,50 @@ import { withDeck } from './context'
import { setSteps } from './updaters' import { setSteps } from './updaters'
import { modes } from './constants' import { modes } from './constants'
export default withDeck(class Appear extends React.Component { export default withDeck(
static propTypes = { class Appear extends React.Component {
children: PropTypes.node.isRequired, static propTypes = {
deck: PropTypes.object.isRequired children: PropTypes.node.isRequired,
} deck: PropTypes.object.isRequired,
}
constructor (props) { constructor(props) {
super(props) super(props)
const { update, index } = props.deck const { update, index } = props.deck
const steps = React.Children.toArray(props.children).length const steps = React.Children.toArray(props.children).length
update(setSteps(index, steps)) update(setSteps(index, steps))
} }
render() { render() {
const children = React.Children.toArray(this.props.children) const children = React.Children.toArray(this.props.children).map(
.map(child => typeof child === 'string' child => (typeof child === 'string' ? <div>{child}</div> : child)
? <div>{child}</div>
: child
) )
const { step, mode } = this.props.deck const { step, mode } = this.props.deck
if (mode === modes.grid) { if (mode === modes.grid) {
return children return children
}
if (
typeof window !== 'undefined' &&
window.navigator.userAgent.includes('Print/PDF')
) {
return children
}
return (
<React.Fragment>
{children.map((child, i) =>
React.cloneElement(child, {
key: i,
style: {
...((child.props || {}).style || {}),
visibility: step >= i + 1 ? 'visible' : 'hidden',
},
})
)}
</React.Fragment>
)
} }
if (typeof window !== 'undefined' && window.navigator.userAgent.includes('Print/PDF')) {
return children;
}
return (
<React.Fragment>
{children.map((child, i) => (
React.cloneElement(child, {
key: i,
style: {
...((child.props || {}).style || {}),
visibility: (step >= i + 1) ? 'visible' : 'hidden'
}
})
))}
</React.Fragment>
)
} }
}) )

View File

@ -10,7 +10,7 @@ const Button = styled.button(
fontSize: '12px', fontSize: '12px',
fontWeight: 'bold', fontWeight: 'bold',
borderRadius: '4px', borderRadius: '4px',
border: 'none' border: 'none',
}, },
space, space,
color color
@ -18,7 +18,7 @@ const Button = styled.button(
Button.propTypes = { Button.propTypes = {
...space.propTypes, ...space.propTypes,
...color.propTypes ...color.propTypes,
} }
Button.defaultProps = { Button.defaultProps = {
@ -26,7 +26,7 @@ Button.defaultProps = {
px: 2, px: 2,
py: 1, py: 1,
color: 'white', color: 'white',
bg: '#333' bg: '#333',
} }
export default Button export default Button

View File

@ -31,14 +31,14 @@ export const Presenter = ({
bg="black" bg="black"
css={{ css={{
flexDirection: 'column', flexDirection: 'column',
height: '100vh' height: '100vh',
}}> }}>
<Flex my="auto"> <Flex my="auto">
<Box <Box
mx="auto" mx="auto"
width={5 / 8} width={5 / 8}
css={{ css={{
border: '1px solid rgba(128, 128, 128, 0.25)' border: '1px solid rgba(128, 128, 128, 0.25)',
}}> }}>
<Zoom zoom={5 / 8}> <Zoom zoom={5 / 8}>
<Root {...props}>{props.children}</Root> <Root {...props}>{props.children}</Root>
@ -49,12 +49,12 @@ export const Presenter = ({
mx="auto" mx="auto"
css={{ css={{
flex: 'none', flex: 'none',
flexDirection: 'column' flexDirection: 'column',
}}> }}>
<Box <Box
mx="auto" mx="auto"
css={{ css={{
border: '1px solid rgba(128, 128, 128, 0.25)' border: '1px solid rgba(128, 128, 128, 0.25)',
}}> }}>
<Zoom zoom={1 / 4}> <Zoom zoom={1 / 4}>
<Root {...props}> <Root {...props}>
@ -69,7 +69,7 @@ export const Presenter = ({
<Box <Box
py={3} py={3}
css={{ css={{
flex: 'auto' flex: 'auto',
}}> }}>
{notes} {notes}
</Box> </Box>
@ -100,7 +100,7 @@ Presenter.propTypes = {
step: PropTypes.number.isRequired, step: PropTypes.number.isRequired,
slides: PropTypes.array, slides: PropTypes.array,
mode: PropTypes.string, mode: PropTypes.string,
metadata: PropTypes.object metadata: PropTypes.object,
} }
export default Presenter export default Presenter

View File

@ -236,7 +236,8 @@ export class SlideDeck extends React.Component {
}}> }}>
<Provider {...this.state} update={this.update}> <Provider {...this.state} update={this.update}>
{mode === modes.grid ? ( {mode === modes.grid ? (
<Grid {...context} /> <Grid {...context} slides={slides} update={this.update} />
) : ( ) : (
<Swipeable <Swipeable
onSwipedLeft={() => this.update(next)} onSwipedLeft={() => this.update(next)}

View File

@ -8,7 +8,7 @@ class Timer extends React.Component {
state = { state = {
on: false, on: false,
time: new Date().toLocaleTimeString(), time: new Date().toLocaleTimeString(),
seconds: 0 seconds: 0,
} }
toggle = () => { toggle = () => {
@ -23,7 +23,7 @@ class Timer extends React.Component {
const now = new Date() const now = new Date()
this.setState(state => ({ this.setState(state => ({
time: now.toLocaleTimeString(), time: now.toLocaleTimeString(),
seconds: state.on ? state.seconds + 1 : state.seconds seconds: state.on ? state.seconds + 1 : state.seconds,
})) }))
} }

View File

@ -11,7 +11,7 @@ const FullCode = styled.div([], {
width: '100vw', width: '100vw',
height: '100vh', height: '100vh',
overflow: 'auto', overflow: 'auto',
} },
}) })
export default FullCode export default FullCode