diff options
author | Nicolas Gaborit <soreine.plume@gmail.com> | 2016-10-13 12:56:46 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-10-13 12:56:46 +0200 |
commit | 3aea3e5d88384822440517c9a2b722c405547155 (patch) | |
tree | 84ef0448c7e2e7ed95941f2795e86ca65f7cd809 /packages/gitbook-core/src/components/HotKeys.js | |
parent | 95b3b4ebb7277f7a96ee79e5d75baafb3b5aab1e (diff) | |
download | gitbook-3aea3e5d88384822440517c9a2b722c405547155.zip gitbook-3aea3e5d88384822440517c9a2b722c405547155.tar.gz gitbook-3aea3e5d88384822440517c9a2b722c405547155.tar.bz2 |
Adapt plugin sharing (#1553)
* Reuse old package config
* Add plugin config shape
* Add ButtonGroup to core components
* List all sharing sites
* Displaying buttons from config
* First iteration of Dropdown component (need CSS)
* Using Dropdown for sharing button
* Create HotKeys component
* Move Backdrop to its own file
* Trying a cleaner API for Dropdown
* Add README.md
* livereload: Add missing gitbook-plugin dependency
* sharing: Now use Immutable state
* sharing: Adapt quickly to new Dropdown
* sharing: Fix sharing from dropdown
Diffstat (limited to 'packages/gitbook-core/src/components/HotKeys.js')
-rw-r--r-- | packages/gitbook-core/src/components/HotKeys.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/gitbook-core/src/components/HotKeys.js b/packages/gitbook-core/src/components/HotKeys.js new file mode 100644 index 0000000..c2c67c8 --- /dev/null +++ b/packages/gitbook-core/src/components/HotKeys.js @@ -0,0 +1,56 @@ +const React = require('react'); +const Mousetrap = require('mousetrap'); +const { string, node, func, shape, arrayOf } = React.PropTypes; + +const bindingShape = shape({ + // A key "escape", a combination of key "mod+s", or a key sequence "ctrl+x ctrl+s" + key: string.isRequired, + // function (event) {} + handler: func.isRequired +}); + +/** + * Defines hotkeys globally when this component is mounted. + * + * keymap = [{ + * key: 'escape', + * handler: (e) => quit() + * }, { + * key: 'mod+s', + * handler: (e) => save() + * }] + * + * <HotKeys keymap={keymap}> + * < ... /> + * </HotKeys> + */ + +const HotKeys = React.createClass({ + propTypes: { + children: node.isRequired, + keymap: arrayOf(bindingShape) + }, + + getDefaultProps() { + return { keymap: [] }; + }, + + componentDidMount() { + this.props.keymap.forEach((binding) => { + Mousetrap.bind(binding.key, binding.handler); + }); + }, + + componentWillUnmount() { + this.props.keymap.forEach((binding) => { + Mousetrap.unbind(binding.key, binding.handler); + }); + }, + + render() { + // Simply render the only child + return React.Children.only(this.props.children); + } +}); + +module.exports = HotKeys; |