summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-09-05 11:04:36 +0200
committerSamy Pessé <samypesse@gmail.com>2016-09-05 11:04:36 +0200
commitb170b4d197822f3949b63df69e063d07cbb8e5c9 (patch)
tree556d9919e09eb12bea462a3d3a7801a1cea0611e /packages/gitbook-core
parenta14ca3e268e95a7eab59fb205b41da7331d57631 (diff)
downloadgitbook-b170b4d197822f3949b63df69e063d07cbb8e5c9.zip
gitbook-b170b4d197822f3949b63df69e063d07cbb8e5c9.tar.gz
gitbook-b170b4d197822f3949b63df69e063d07cbb8e5c9.tar.bz2
Start basis of gitbook-core
Diffstat (limited to 'packages/gitbook-core')
-rw-r--r--packages/gitbook-core/package.json22
-rw-r--r--packages/gitbook-core/src/InjectedComponent.js9
-rw-r--r--packages/gitbook-core/src/InjectedComponentSet.js9
-rw-r--r--packages/gitbook-core/src/actions/TYPES.js5
-rw-r--r--packages/gitbook-core/src/connect.js13
-rw-r--r--packages/gitbook-core/src/createPlugin.js16
-rw-r--r--packages/gitbook-core/src/createReducer.js17
-rw-r--r--packages/gitbook-core/src/createStore.js32
-rw-r--r--packages/gitbook-core/src/index.js17
-rw-r--r--packages/gitbook-core/src/reducers/components.js7
-rw-r--r--packages/gitbook-core/src/reducers/index.js5
11 files changed, 152 insertions, 0 deletions
diff --git a/packages/gitbook-core/package.json b/packages/gitbook-core/package.json
new file mode 100644
index 0000000..16de7c6
--- /dev/null
+++ b/packages/gitbook-core/package.json
@@ -0,0 +1,22 @@
+{
+ "name": "gitbook-core",
+ "version": "0.0.0",
+ "description": "Core for GitBook plugins API",
+ "main": "./src/index.js",
+ "dependencies": {
+ "immutable": "^3.8.1",
+ "react": "^15.3.1",
+ "react-dom": "^15.3.1",
+ "react-redux": "^4.4.5",
+ "redux": "^3.5.2"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/GitbookIO/gitbook.git"
+ },
+ "author": "GitBook Inc. <contact@gitbook.com>",
+ "license": "Apache-2.0",
+ "bugs": {
+ "url": "https://github.com/GitbookIO/gitbook/issues"
+ }
+}
diff --git a/packages/gitbook-core/src/InjectedComponent.js b/packages/gitbook-core/src/InjectedComponent.js
new file mode 100644
index 0000000..1a2f1df
--- /dev/null
+++ b/packages/gitbook-core/src/InjectedComponent.js
@@ -0,0 +1,9 @@
+const React = require('react');
+
+const InjectedComponent = React.createClass({
+ render() {
+
+ }
+});
+
+module.exports = InjectedComponent;
diff --git a/packages/gitbook-core/src/InjectedComponentSet.js b/packages/gitbook-core/src/InjectedComponentSet.js
new file mode 100644
index 0000000..4269b73
--- /dev/null
+++ b/packages/gitbook-core/src/InjectedComponentSet.js
@@ -0,0 +1,9 @@
+const React = require('react');
+
+const InjectedComponentSet = React.createClass({
+ render() {
+
+ }
+});
+
+module.exports = InjectedComponentSet;
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js
new file mode 100644
index 0000000..b5c4b43
--- /dev/null
+++ b/packages/gitbook-core/src/actions/TYPES.js
@@ -0,0 +1,5 @@
+
+module.exports = {
+ REGISTER_COMPONENT: 'components:register',
+ UNREGISTER_COMPONENT: 'components:unregister'
+};
diff --git a/packages/gitbook-core/src/connect.js b/packages/gitbook-core/src/connect.js
new file mode 100644
index 0000000..2bd9cb9
--- /dev/null
+++ b/packages/gitbook-core/src/connect.js
@@ -0,0 +1,13 @@
+const ReactRedux = require('react-redux');
+
+/**
+ * Connect a component to the GitBook store
+ * @param {ReactComponent} Component
+ * @param {Function} mapStateToProps
+ * @return {ReactComponent}
+ */
+function connect(Component, mapStateToProps) {
+ return ReactRedux.connect(Component, mapStateToProps);
+}
+
+module.exports = connect;
diff --git a/packages/gitbook-core/src/createPlugin.js b/packages/gitbook-core/src/createPlugin.js
new file mode 100644
index 0000000..1fcf495
--- /dev/null
+++ b/packages/gitbook-core/src/createPlugin.js
@@ -0,0 +1,16 @@
+
+/**
+ * Create a plugin to extend the state and the views.
+ *
+ * @param {Function(dispatch, state)} onInitialState
+ * @param {Funciton(state, action)} onReduceState
+ * @return {Plugin}
+ */
+function createPlugin(onInitialState, onReduceState) {
+ return {
+ onInitialState,
+ onReduceState
+ };
+}
+
+module.exports = createPlugin;
diff --git a/packages/gitbook-core/src/createReducer.js b/packages/gitbook-core/src/createReducer.js
new file mode 100644
index 0000000..4af6373
--- /dev/null
+++ b/packages/gitbook-core/src/createReducer.js
@@ -0,0 +1,17 @@
+
+/**
+ * Helper to create a reducer that extend the store.
+ *
+ * @param {String} property
+ * @param {Function(state, action): state} reduce
+ * @return {Function(state, action): state}
+ */
+function createReducer(name, reduce) {
+ return function(state, action) {
+ const value = state[name];
+ state[name] = reduce(value, action);
+ return state;
+ };
+}
+
+module.exports = createReducer;
diff --git a/packages/gitbook-core/src/createStore.js b/packages/gitbook-core/src/createStore.js
new file mode 100644
index 0000000..ecc351f
--- /dev/null
+++ b/packages/gitbook-core/src/createStore.js
@@ -0,0 +1,32 @@
+const Redux = require('redux');
+const ReduxThunk = require('redux-thunk').default;
+
+const reducers = require('./reducers');
+
+/**
+ * Create a new redux store from an initial state and a list of plugins.
+ * Each plugin entry is the result of {createPlugin}.
+ *
+ * @param {Array<Plugin>} plugins
+ * @param {Object} initialState
+ * @return {ReduxStore} store
+ */
+function createStore(plugins, initialState) {
+ const pluginReducers = plugins.map(plugin => plugin.onReduceState);
+ const reducer = Redux.combine(reducers, ...pluginReducers);
+
+ const store = Redux.createStore(
+ reducer,
+ initialState,
+ Redux.applyMiddleware(ReduxThunk)
+ );
+
+ // Initialize the plugins
+ plugins.forEach(plugin => {
+ plugin.onInitialState(store.dispatch, store.getState);
+ });
+
+ return store;
+}
+
+module.exports = createStore;
diff --git a/packages/gitbook-core/src/index.js b/packages/gitbook-core/src/index.js
new file mode 100644
index 0000000..bd9124b
--- /dev/null
+++ b/packages/gitbook-core/src/index.js
@@ -0,0 +1,17 @@
+const ACTIONS = require('./actions/TYPES');
+const connect = require('./connect');
+const InjectedComponent = require('./InjectedComponent');
+const InjectedComponentSet = require('./InjectedComponentSet');
+const createPlugin = require('./createPlugin');
+const createReducer = require('./createReducer');
+const createStore = require('./createStore');
+
+module.exports = {
+ ACTIONS,
+ connect,
+ createPlugin,
+ createReducer,
+ createStore,
+ InjectedComponent,
+ InjectedComponentSet
+};
diff --git a/packages/gitbook-core/src/reducers/components.js b/packages/gitbook-core/src/reducers/components.js
new file mode 100644
index 0000000..8cb9c4b
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/components.js
@@ -0,0 +1,7 @@
+const Immutable = require('immutable');
+
+function reduceComponents(state, action) {
+
+}
+
+module.exports = reduceComponents;
diff --git a/packages/gitbook-core/src/reducers/index.js b/packages/gitbook-core/src/reducers/index.js
new file mode 100644
index 0000000..05ff70f
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/index.js
@@ -0,0 +1,5 @@
+const Redux = require('redux');
+
+module.exports = Redux.combineReducers({
+ components: require('./components')
+});