summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-sharing
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-plugin-sharing')
-rw-r--r--packages/gitbook-plugin-sharing/.gitignore31
-rw-r--r--packages/gitbook-plugin-sharing/.npmignore2
-rw-r--r--packages/gitbook-plugin-sharing/README.md38
-rw-r--r--packages/gitbook-plugin-sharing/index.js10
-rw-r--r--packages/gitbook-plugin-sharing/package.json77
-rw-r--r--packages/gitbook-plugin-sharing/src/SITES.js72
-rw-r--r--packages/gitbook-plugin-sharing/src/components/ShareButton.js47
-rw-r--r--packages/gitbook-plugin-sharing/src/components/SharingButtons.js63
-rw-r--r--packages/gitbook-plugin-sharing/src/components/SiteButton.js29
-rw-r--r--packages/gitbook-plugin-sharing/src/index.js9
-rw-r--r--packages/gitbook-plugin-sharing/src/optionsShape.js20
-rw-r--r--packages/gitbook-plugin-sharing/src/shapes/options.js19
-rw-r--r--packages/gitbook-plugin-sharing/src/shapes/site.js13
13 files changed, 430 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-sharing/.gitignore b/packages/gitbook-plugin-sharing/.gitignore
new file mode 100644
index 0000000..ef47881
--- /dev/null
+++ b/packages/gitbook-plugin-sharing/.gitignore
@@ -0,0 +1,31 @@
+# Logs
+logs
+*.log
+
+# Runtime data
+pids
+*.pid
+*.seed
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directory
+# Deployed apps should consider commenting this line out:
+# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
+node_modules
+
+# vim swapfile
+*.swp
+
+# Plugin assets
+_assets/plugin.js
diff --git a/packages/gitbook-plugin-sharing/.npmignore b/packages/gitbook-plugin-sharing/.npmignore
new file mode 100644
index 0000000..a0e53cf
--- /dev/null
+++ b/packages/gitbook-plugin-sharing/.npmignore
@@ -0,0 +1,2 @@
+# Publish assets on NPM
+!_assets/plugin.js
diff --git a/packages/gitbook-plugin-sharing/README.md b/packages/gitbook-plugin-sharing/README.md
new file mode 100644
index 0000000..28ae0d4
--- /dev/null
+++ b/packages/gitbook-plugin-sharing/README.md
@@ -0,0 +1,38 @@
+# plugin-sharing
+
+This plugin adds sharing buttons in the GitBook website toolbar to share book on social networks.
+
+### Disable this plugin
+
+This is a default plugin and it can be disabled using a `book.json` configuration:
+
+```
+{
+ plugins: ["-sharing"]
+}
+```
+
+### Configuration
+
+This plugin can be configured in the `book.json`:
+
+Default configuration is:
+
+```js
+{
+ "pluginsConfig": {
+ "sharing": {
+ "facebook": true,
+ "twitter": true,
+ "google": false,
+ "weibo": false,
+ "instapaper": false,
+ "vk": false,
+ "all": [
+ "facebook", "google", "twitter",
+ "weibo", "instapaper"
+ ]
+ }
+ }
+}
+```
diff --git a/packages/gitbook-plugin-sharing/index.js b/packages/gitbook-plugin-sharing/index.js
new file mode 100644
index 0000000..e542ae8
--- /dev/null
+++ b/packages/gitbook-plugin-sharing/index.js
@@ -0,0 +1,10 @@
+
+module.exports = {
+ blocks: {
+
+ },
+
+ hooks: {
+
+ }
+};
diff --git a/packages/gitbook-plugin-sharing/package.json b/packages/gitbook-plugin-sharing/package.json
new file mode 100644
index 0000000..b0540e8
--- /dev/null
+++ b/packages/gitbook-plugin-sharing/package.json
@@ -0,0 +1,77 @@
+{
+ "name": "gitbook-plugin-sharing",
+ "description": "Sharing buttons in the toolbar",
+ "main": "index.js",
+ "browser": "./_assets/plugin.js",
+ "version": "4.0.0",
+ "gitbook": {
+ "properties": {
+ "facebook": {
+ "type": "boolean",
+ "default": true,
+ "title": "Facebook"
+ },
+ "twitter": {
+ "type": "boolean",
+ "default": true,
+ "title": "Twitter"
+ },
+ "google": {
+ "type": "boolean",
+ "default": false,
+ "title": "Google"
+ },
+ "weibo": {
+ "type": "boolean",
+ "default": false,
+ "description": "Weibo"
+ },
+ "instapaper": {
+ "type": "boolean",
+ "default": false,
+ "description": "Instapaper"
+ },
+ "vk": {
+ "type": "boolean",
+ "default": false,
+ "description": "VK"
+ },
+ "all": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "default": [
+ "facebook",
+ "google",
+ "twitter",
+ "weibo",
+ "instapaper"
+ ],
+ "uniqueItems": true
+ }
+ }
+ },
+ "dependencies": {
+ "gitbook-core": "4.0.0"
+ },
+ "devDependencies": {
+ "gitbook-plugin": "4.0.0"
+ },
+ "engines": {
+ "gitbook": ">=3.0.0"
+ },
+ "scripts": {
+ "build-js": "gitbook-plugin build ./src/index.js ./_assets/plugin.js",
+ "prepublish": "npm run build-js"
+ },
+ "homepage": "https://github.com/GitbookIO/gitbook",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/GitbookIO/gitbook.git"
+ },
+ "bugs": {
+ "url": "https://github.com/GitbookIO/gitbook/issues"
+ },
+ "license": "Apache-2.0"
+}
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;