summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-core')
-rw-r--r--packages/gitbook-core/src/actions/TYPES.js14
-rw-r--r--packages/gitbook-core/src/actions/i18n.js15
-rw-r--r--packages/gitbook-core/src/createStore.js36
-rw-r--r--packages/gitbook-core/src/index.js19
-rw-r--r--packages/gitbook-core/src/lib/bootstrap.js (renamed from packages/gitbook-core/src/bootstrap.js)14
-rw-r--r--packages/gitbook-core/src/lib/composeReducer.js (renamed from packages/gitbook-core/src/composeReducer.js)0
-rw-r--r--packages/gitbook-core/src/lib/connect.js (renamed from packages/gitbook-core/src/connect.js)0
-rw-r--r--packages/gitbook-core/src/lib/createContext.js52
-rw-r--r--packages/gitbook-core/src/lib/createPlugin.js (renamed from packages/gitbook-core/src/createPlugin.js)14
-rw-r--r--packages/gitbook-core/src/lib/createReducer.js (renamed from packages/gitbook-core/src/createReducer.js)0
-rw-r--r--packages/gitbook-core/src/lib/renderWithContext.js (renamed from packages/gitbook-core/src/renderWithStore.js)14
-rw-r--r--packages/gitbook-core/src/reducers/i18n.js25
-rw-r--r--packages/gitbook-core/src/reducers/index.js7
13 files changed, 136 insertions, 74 deletions
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js
index 510c1d9..e758a44 100644
--- a/packages/gitbook-core/src/actions/TYPES.js
+++ b/packages/gitbook-core/src/actions/TYPES.js
@@ -1,10 +1,12 @@
module.exports = {
// Components
- REGISTER_COMPONENT: 'components:register',
- UNREGISTER_COMPONENT: 'components:unregister',
- //Navigation
- PAGE_FETCH_START: 'navigation:fetch:start',
- PAGE_FETCH_END: 'navigation:fetch:end',
- PAGE_FETCH_ERROR: 'navigation:fetch:error'
+ REGISTER_COMPONENT: 'components/register',
+ UNREGISTER_COMPONENT: 'components/unregister',
+ // Navigation
+ PAGE_FETCH_START: 'navigation/fetch:start',
+ PAGE_FETCH_END: 'navigation/fetch:end',
+ PAGE_FETCH_ERROR: 'navigation/fetch:error',
+ // i18n
+ I18N_REGISTER_LOCALE: 'i18n/register:locale'
};
diff --git a/packages/gitbook-core/src/actions/i18n.js b/packages/gitbook-core/src/actions/i18n.js
new file mode 100644
index 0000000..e0a2d69
--- /dev/null
+++ b/packages/gitbook-core/src/actions/i18n.js
@@ -0,0 +1,15 @@
+const ACTION_TYPES = require('./TYPES');
+
+/**
+ * Register messages for a locale
+ * @param {String} locale
+ * @param {Map<String:String>} messages
+ * @return {Action}
+ */
+function registerLocale(locale, messages) {
+ return { type: ACTION_TYPES.I18N_REGISTER_LOCALE, locale, messages };
+}
+
+module.exports = {
+ registerLocale
+};
diff --git a/packages/gitbook-core/src/createStore.js b/packages/gitbook-core/src/createStore.js
deleted file mode 100644
index 201a647..0000000
--- a/packages/gitbook-core/src/createStore.js
+++ /dev/null
@@ -1,36 +0,0 @@
-const Redux = require('redux');
-const ReduxThunk = require('redux-thunk').default;
-
-const coreReducers = require('./reducers');
-const composeReducer = require('./composeReducer');
-
-/**
- * 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 = composeReducer(...[coreReducers].concat(pluginReducers));
-
- const store = Redux.createStore(
- (state, action) => {
- console.log('[store]', action.type);
- return reducer(state, action);
- },
- initialState,
- Redux.compose(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
index cb107c8..c83fc3a 100644
--- a/packages/gitbook-core/src/index.js
+++ b/packages/gitbook-core/src/index.js
@@ -17,25 +17,26 @@ const { registerComponent } = require('./actions/components');
const ACTIONS = require('./actions/TYPES');
const Shapes = require('./shapes');
-const connect = require('./connect');
-const createPlugin = require('./createPlugin');
-const createReducer = require('./createReducer');
-const createStore = require('./createStore');
-const composeReducer = require('./composeReducer');
-const bootstrap = require('./bootstrap');
-const renderWithStore = require('./renderWithStore');
+const connect = require('./lib/connect');
+const createPlugin = require('./lib/createPlugin');
+const createReducer = require('./lib/createReducer');
+const createContext = require('./lib/createContext');
+const composeReducer = require('./lib/composeReducer');
+const bootstrap = require('./lib/bootstrap');
+const renderWithContext = require('./lib/renderWithContext');
module.exports = {
ACTIONS,
bootstrap,
- renderWithStore,
+ renderWithContext,
connect,
createPlugin,
createReducer,
- createStore,
+ createContext,
composeReducer,
registerComponent,
// React Components
+ IntlProvider,
InjectedComponent,
InjectedComponentSet,
HTMLContent,
diff --git a/packages/gitbook-core/src/bootstrap.js b/packages/gitbook-core/src/lib/bootstrap.js
index 0a2e981..f5183d1 100644
--- a/packages/gitbook-core/src/bootstrap.js
+++ b/packages/gitbook-core/src/lib/bootstrap.js
@@ -1,8 +1,8 @@
const ReactDOM = require('react-dom');
-const getPayload = require('./lib/getPayload');
-const createStore = require('./createStore');
-const renderWithStore = require('./renderWithStore');
+const getPayload = require('./getPayload');
+const createContext = require('./createContext');
+const renderWithContext = require('./renderWithContext');
/**
* Bootstrap GitBook on the browser (this function should not be called on the server side)
@@ -11,17 +11,15 @@ function bootstrap() {
const initialState = getPayload(window.document);
const plugins = window.gitbookPlugins;
- console.log(initialState);
-
const mountNode = document.getElementById('content');
// Create the redux store
- const store = createStore(plugins, initialState);
+ const context = createContext(plugins, initialState);
- window.appStore = store;
+ window.gitbookContext = context;
// Render with the store
- const el = renderWithStore(store);
+ const el = renderWithContext(context);
ReactDOM.render(el, mountNode);
}
diff --git a/packages/gitbook-core/src/composeReducer.js b/packages/gitbook-core/src/lib/composeReducer.js
index fa2a589..fa2a589 100644
--- a/packages/gitbook-core/src/composeReducer.js
+++ b/packages/gitbook-core/src/lib/composeReducer.js
diff --git a/packages/gitbook-core/src/connect.js b/packages/gitbook-core/src/lib/connect.js
index e18158e..e18158e 100644
--- a/packages/gitbook-core/src/connect.js
+++ b/packages/gitbook-core/src/lib/connect.js
diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js
new file mode 100644
index 0000000..516688e
--- /dev/null
+++ b/packages/gitbook-core/src/lib/createContext.js
@@ -0,0 +1,52 @@
+/* eslint-disable no-console */
+const { Record } = require('immutable');
+const Redux = require('redux');
+const ReduxThunk = require('redux-thunk').default;
+
+const coreReducers = require('../reducers');
+const composeReducer = require('./composeReducer');
+
+const GitBookContext = Record({
+ store: null,
+ actions: {}
+}, 'GitBookContext');
+
+/**
+ * Create a new context containing 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 {GitBookContext} context
+ */
+function createContext(plugins, initialState) {
+ // Compose the reducer from core with plugins
+ const pluginReducers = plugins.map(plugin => plugin.reduce);
+ const reducer = composeReducer(...[coreReducers].concat(pluginReducers));
+
+ // Get actions from all plugins
+ const actions = plugins.reduce((accu, plugin) => {
+ return { ...accu, ...plugin.actions };
+ });
+
+ const store = Redux.createStore(
+ (state, action) => {
+ console.log('[store]', action.type);
+ return reducer(state, action);
+ },
+ initialState,
+ Redux.compose(Redux.applyMiddleware(ReduxThunk))
+ );
+
+ // Initialize the plugins
+ plugins.forEach(plugin => {
+ plugin.init(store.dispatch, store.getState);
+ });
+
+ return GitBookContext({
+ store,
+ actions
+ });
+}
+
+module.exports = createContext;
diff --git a/packages/gitbook-core/src/createPlugin.js b/packages/gitbook-core/src/lib/createPlugin.js
index d00c99d..7a49053 100644
--- a/packages/gitbook-core/src/createPlugin.js
+++ b/packages/gitbook-core/src/lib/createPlugin.js
@@ -1,4 +1,7 @@
+const defaultInit = ((dispatch, getState) => {});
+const defaultReduce = ((state, action) => state);
+
/**
* Create a plugin to extend the state and the views.
*
@@ -6,12 +9,15 @@
* @param {Funciton(state, action)} onReduceState
* @return {Plugin}
*/
-function createPlugin(onInitialState, onReduceState) {
- onReduceState = onReduceState || ((state, action) => state);
+function createPlugin({ init, reduce, actions }) {
+ init = init || defaultInit;
+ reduce = reduce || defaultReduce;
+ actions = actions || {};
const plugin = {
- onInitialState,
- onReduceState
+ init,
+ reduce,
+ actions
};
if (typeof window !== 'undefined') {
diff --git a/packages/gitbook-core/src/createReducer.js b/packages/gitbook-core/src/lib/createReducer.js
index 2ebecfb..2ebecfb 100644
--- a/packages/gitbook-core/src/createReducer.js
+++ b/packages/gitbook-core/src/lib/createReducer.js
diff --git a/packages/gitbook-core/src/renderWithStore.js b/packages/gitbook-core/src/lib/renderWithContext.js
index 55594c4..b9e2cfd 100644
--- a/packages/gitbook-core/src/renderWithStore.js
+++ b/packages/gitbook-core/src/lib/renderWithContext.js
@@ -1,18 +1,18 @@
const React = require('react');
const { Provider } = require('react-redux');
-const { InjectedComponent } = require('./components/InjectedComponent');
-const PJAXWrapper = require('./components/PJAXWrapper');
-const IntlProvider = require('./components/IntlProvider');
+const { InjectedComponent } = require('../components/InjectedComponent');
+const PJAXWrapper = require('../components/PJAXWrapper');
+const IntlProvider = require('../components/IntlProvider');
/**
* Render the application for a store
- * @param {ReduxStore} store
+ * @param {GitBookContext} context
* @return {React.Element} element
*/
-function renderWithStore(store) {
+function renderWithContext(context) {
return (
- <Provider store={store}>
+ <Provider store={context.store}>
<PJAXWrapper>
<IntlProvider>
<InjectedComponent matching={{ role: 'Body' }} />
@@ -22,4 +22,4 @@ function renderWithStore(store) {
);
}
-module.exports = renderWithStore;
+module.exports = renderWithContext;
diff --git a/packages/gitbook-core/src/reducers/i18n.js b/packages/gitbook-core/src/reducers/i18n.js
new file mode 100644
index 0000000..46df8fc
--- /dev/null
+++ b/packages/gitbook-core/src/reducers/i18n.js
@@ -0,0 +1,25 @@
+const { Record, Map } = require('immutable');
+const ACTION_TYPES = require('../actions/TYPES');
+
+const I18nState = Record({
+ locale: 'en',
+ // Map of locale -> Map<String:String>
+ messages: Map()
+});
+
+function reduceI18n(state, action) {
+ state = state || I18nState();
+ switch (action.type) {
+
+ case ACTION_TYPES.I18N_REGISTER_LOCALE:
+ return state.merge({
+ messages: state.messages.set(action.locale, Map(action.messages))
+ });
+
+ default:
+ return state;
+
+ }
+}
+
+module.exports = reduceI18n;
diff --git a/packages/gitbook-core/src/reducers/index.js b/packages/gitbook-core/src/reducers/index.js
index 4785c1c..956172b 100644
--- a/packages/gitbook-core/src/reducers/index.js
+++ b/packages/gitbook-core/src/reducers/index.js
@@ -1,11 +1,10 @@
-const ACTION_TYPES = require('../actions/TYPES');
-
-const composeReducer = require('../composeReducer');
-const createReducer = require('../createReducer');
+const composeReducer = require('../lib/composeReducer');
+const createReducer = require('../lib/createReducer');
module.exports = composeReducer(
createReducer('components', require('./components')),
createReducer('navigation', require('./navigation')),
+ createReducer('i18n', require('./i18n')),
// GitBook JSON
createReducer('page', require('./page')),
createReducer('summary', require('./summary')),