diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-10-13 13:23:19 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-10-13 13:23:19 +0200 |
commit | c42c5897fee1841d2be0207faeda81bc42f250e2 (patch) | |
tree | 45fd9a773ba05ae362390e63ce0f2eaaa0154ede /packages/gitbook-plugin-sharing/src/components | |
parent | 3aea3e5d88384822440517c9a2b722c405547155 (diff) | |
download | gitbook-c42c5897fee1841d2be0207faeda81bc42f250e2.zip gitbook-c42c5897fee1841d2be0207faeda81bc42f250e2.tar.gz gitbook-c42c5897fee1841d2be0207faeda81bc42f250e2.tar.bz2 |
Refactor code of plugin sharing
Diffstat (limited to 'packages/gitbook-plugin-sharing/src/components')
3 files changed, 136 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-sharing/src/components/ShareButton.js b/packages/gitbook-plugin-sharing/src/components/ShareButton.js new file mode 100644 index 0000000..5a6cd6e --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/components/ShareButton.js @@ -0,0 +1,44 @@ +const GitBook = require('gitbook-core'); +const { React, Dropdown } = GitBook; + +const SITES = require('../SITES'); + +// Share button with dropdown list of sites +const ShareButton = React.createClass({ + propTypes: { + siteIds: React.PropTypes.arrayOf(React.PropTypes.string).isRequired, + onShare: React.PropTypes.func.isRequired + }, + + getInitialState() { + return { open: false }; + }, + + onToggle() { + this.setState({ open: !this.state.open }); + }, + + render() { + const { siteIds, onShare } = this.props; + + const items = siteIds.map((id) => ( + <Dropdown.Item onClick={() => onShare(SITES[id])} key={id}> + {SITES[id].label} + </Dropdown.Item> + )); + + return ( + <Dropdown.Container> + <GitBook.Button onClick={this.onToggle}> + <GitBook.Icon id="share-alt" /> + </GitBook.Button> + + <Dropdown.Menu open={this.state.open}> + {items} + </Dropdown.Menu> + </Dropdown.Container> + ); + } +}); + +module.exports = ShareButton; diff --git a/packages/gitbook-plugin-sharing/src/components/SharingButtons.js b/packages/gitbook-plugin-sharing/src/components/SharingButtons.js new file mode 100644 index 0000000..445458a --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/components/SharingButtons.js @@ -0,0 +1,63 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; + +const SITES = require('../SITES'); +const optionsShape = require('../shapes/options'); +const SiteButton = require('./SiteButton'); +const ShareButton = require('./ShareButton'); + +/** + * Displays the group of sharing buttons + */ +const SharingButtons = React.createClass({ + propTypes: { + options: optionsShape.isRequired, + page: GitBook.Shapes.Page.isRequired + }, + + onShare(site) { + site.onShare(location.href, this.props.page.title); + }, + + render() { + const { options } = this.props; + + // Highlighted sites + const mainButtons = SITES + .ALL + .filter(id => options[id]) + .map(id => <SiteButton key={id} onShare={this.onShare} site={SITES[id]} />); + + // Other sites + let shareButton = undefined; + if (options.all.length > 0) { + shareButton = ( + <ShareButton siteIds={options.all} + onShare={this.onShare} /> + ); + } + + return ( + <GitBook.ButtonGroup> + { mainButtons } + { shareButton } + </GitBook.ButtonGroup> + ); + } +}); + +function mapStateToProps(state) { + let options = state.config.getIn(['pluginsConfig', 'sharing']); + if (options) { + options = options.toJS(); + } else { + options = { all: [] }; + } + + return { + page: state.page, + options + }; +} + +module.exports = GitBook.connect(SharingButtons, mapStateToProps); diff --git a/packages/gitbook-plugin-sharing/src/components/SiteButton.js b/packages/gitbook-plugin-sharing/src/components/SiteButton.js new file mode 100644 index 0000000..e03720d --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/components/SiteButton.js @@ -0,0 +1,29 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; + +const siteShape = require('../shapes/site'); + +// An individual site sharing button +const SiteButton = React.createClass({ + propTypes: { + site: siteShape.isRequired, + onShare: React.PropTypes.func.isRequired + }, + + onClick(e) { + e.preventDefault(); + this.props.onShare(this.props.site); + }, + + render() { + const { site } = this.props; + + return ( + <GitBook.Button onClick={this.onClick}> + <GitBook.Icon id={site.icon}/> + </GitBook.Button> + ); + } +}); + +module.exports = SiteButton; |