diff options
Diffstat (limited to 'packages/gitbook-plugin-sharing/src')
8 files changed, 272 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-sharing/src/SITES.js b/packages/gitbook-plugin-sharing/src/SITES.js new file mode 100644 index 0000000..86eae74 --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/SITES.js @@ -0,0 +1,72 @@ +// All the sharing platforms +const SITES = { + + // One sharing platform + 'facebook': { + // Displayed name + label: 'Facebook', + + // Font-awesome icon id + icon: 'facebook', + + /** + * Share a page on this platform + * @param {String} url The url to share + * @param {String} title The title of the url page + */ + onShare(url, title) { + url = encodeURIComponent(url); + window.open(`http://www.facebook.com/sharer/sharer.php?s=100&p[url]=${url}`); + } + }, + + 'twitter': { + label: 'Twitter', + icon: 'twitter', + onShare(url, title) { + const status = encodeURIComponent(title + ' ' + url); + window.open(`http://twitter.com/home?status=${status}`); + } + }, + + 'google': { + label: 'Google+', + icon: 'google-plus', + onShare(url, title) { + url = encodeURIComponent(url); + window.open(`https://plus.google.com/share?url=${url}`); + } + }, + + 'weibo': { + label: 'Weibo', + icon: 'weibo', + onShare(url, title) { + url = encodeURIComponent(url); + title = encodeURIComponent(title); + window.open(`http://service.weibo.com/share/share.php?content=utf-8&url=${url}&title=${title}`); + } + }, + + 'instapaper': { + label: 'Instapaper', + icon: 'instapaper', + onShare(url, title) { + url = encodeURIComponent(url); + window.open(`http://www.instapaper.com/text?u=${url}`); + } + }, + + 'vk': { + label: 'VK', + icon: 'vk', + onShare(url, title) { + url = encodeURIComponent(url); + window.open(`http://vkontakte.ru/share.php?url=${url}`); + } + } +}; + +SITES.ALL = Object.keys(SITES); + +module.exports = SITES; 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..8983423 --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/components/ShareButton.js @@ -0,0 +1,47 @@ +const GitBook = require('gitbook-core'); +const { React, Dropdown, Backdrop } = 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() { + const { open } = this.state; + this.setState({ open: !open }); + }, + + render() { + const { siteIds, onShare } = this.props; + const { open } = this.state; + + return ( + <Dropdown.Container> + {open ? <Backdrop onClose={this.onToggle} /> : null} + + <GitBook.Button onClick={this.onToggle}> + <GitBook.Icon id="share-alt" /> + </GitBook.Button> + + {open ? ( + <Dropdown.Menu> + {siteIds.map((id) => ( + <Dropdown.ItemLink onClick={() => onShare(SITES[id])} key={id}> + {SITES[id].label} + </Dropdown.ItemLink> + ))} + </Dropdown.Menu>) : null} + </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..4f5ada9 --- /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.PropTypes.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; diff --git a/packages/gitbook-plugin-sharing/src/index.js b/packages/gitbook-plugin-sharing/src/index.js new file mode 100644 index 0000000..174adfc --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/index.js @@ -0,0 +1,9 @@ +const GitBook = require('gitbook-core'); +const SharingButtons = require('./components/SharingButtons'); + +module.exports = GitBook.createPlugin({ + activate: (dispatch, getState, { Components }) => { + // Dispatch initialization actions + dispatch(Components.registerComponent(SharingButtons, { role: 'toolbar:buttons:right' })); + } +}); diff --git a/packages/gitbook-plugin-sharing/src/optionsShape.js b/packages/gitbook-plugin-sharing/src/optionsShape.js new file mode 100644 index 0000000..dd51016 --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/optionsShape.js @@ -0,0 +1,20 @@ +const { + bool, + arrayOf, + oneOf, + shape +} = require('gitbook-core').React.PropTypes; + +const { ALL } = require('./SITES'); + +const optionsShape = shape({ + facebook: bool, + twitter: bool, + google: bool, + weibo: bool, + instapaper: bool, + vk: bool, + all: arrayOf(oneOf(ALL)).isRequired +}); + +module.exports = optionsShape; diff --git a/packages/gitbook-plugin-sharing/src/shapes/options.js b/packages/gitbook-plugin-sharing/src/shapes/options.js new file mode 100644 index 0000000..885feb6 --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/shapes/options.js @@ -0,0 +1,19 @@ +const { + bool, + arrayOf, + oneOf, + shape +} = require('gitbook-core').React.PropTypes; +const { ALL } = require('../SITES'); + +const optionsShape = shape({ + facebook: bool, + twitter: bool, + google: bool, + weibo: bool, + instapaper: bool, + vk: bool, + all: arrayOf(oneOf(ALL)).isRequired +}); + +module.exports = optionsShape; diff --git a/packages/gitbook-plugin-sharing/src/shapes/site.js b/packages/gitbook-plugin-sharing/src/shapes/site.js new file mode 100644 index 0000000..2227429 --- /dev/null +++ b/packages/gitbook-plugin-sharing/src/shapes/site.js @@ -0,0 +1,13 @@ +const { + string, + func, + shape +} = require('gitbook-core').React.PropTypes; + +const siteShape = shape({ + label: string.isRequired, + icon: string.isRequired, + onShare: func.isRequired +}); + +module.exports = siteShape; |