summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/api/navigation.md2
-rw-r--r--packages/gitbook-core/src/actions/TYPES.js1
-rw-r--r--packages/gitbook-core/src/actions/navigation.js39
-rw-r--r--packages/gitbook-core/src/lib/createContext.js11
-rw-r--r--packages/gitbook-core/src/models/Plugin.js14
-rw-r--r--packages/gitbook-core/src/reducers/navigation.js11
-rw-r--r--packages/gitbook-plugin-hints/src/index.js2
-rw-r--r--packages/gitbook-plugin-lunr/src/index.js2
-rw-r--r--packages/gitbook-plugin-search/src/actions/search.js9
-rw-r--r--packages/gitbook-plugin-search/src/index.js7
-rw-r--r--packages/gitbook-plugin-sharing/src/index.js2
-rw-r--r--packages/gitbook-plugin-theme-default/src/index.js2
-rw-r--r--packages/gitbook-plugin/template/src/index.js7
13 files changed, 85 insertions, 24 deletions
diff --git a/docs/api/navigation.md b/docs/api/navigation.md
index 667aa66..b0a1d2d 100644
--- a/docs/api/navigation.md
+++ b/docs/api/navigation.md
@@ -19,3 +19,5 @@ module.exports = GitBook.createPlugin({
```
The `onLocationChanged` will be triggered for initial state.
+
+### Changing the url
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js
index e758a44..43eaac0 100644
--- a/packages/gitbook-core/src/actions/TYPES.js
+++ b/packages/gitbook-core/src/actions/TYPES.js
@@ -4,6 +4,7 @@ module.exports = {
REGISTER_COMPONENT: 'components/register',
UNREGISTER_COMPONENT: 'components/unregister',
// Navigation
+ NAVIGATION_LISTEN: 'navigation/listen',
PAGE_FETCH_START: 'navigation/fetch:start',
PAGE_FETCH_END: 'navigation/fetch:end',
PAGE_FETCH_ERROR: 'navigation/fetch:error',
diff --git a/packages/gitbook-core/src/actions/navigation.js b/packages/gitbook-core/src/actions/navigation.js
index 4afd4fb..fa82bcb 100644
--- a/packages/gitbook-core/src/actions/navigation.js
+++ b/packages/gitbook-core/src/actions/navigation.js
@@ -17,6 +17,32 @@ const SUPPORTED = (
const history = isServerSide ? createMemoryHistory() : createBrowserHistory();
/**
+ * Initialize the navigation
+ */
+function activate() {
+ return (dispatch, getState) => {
+ const { listeners } = getState().navigation;
+
+ history.listen(location => {
+ location = Location.fromNative(location);
+
+ listeners.forEach(listener => {
+ listener(location, dispatch, getState);
+ });
+ });
+ };
+}
+
+/**
+ * Cleanup the navigation
+ */
+function deactivate() {
+ return (dispatch, getState) => {
+
+ };
+}
+
+/**
* Push a new url into the navigation
* @param {String|Location} location
* @return {Action} action
@@ -63,16 +89,11 @@ function redirect(uri) {
/**
* Listen to url change
- * @param {Function} fn
+ * @param {Function} listener
* @return {Action} action
*/
-function listen(fn) {
- return (dispatch, getState) => {
- history.listen(location => {
- location = Location.fromNative(location);
- fn(location, dispatch, getState);
- });
- };
+function listen(listener) {
+ return { type: ACTION_TYPES.NAVIGATION_LISTEN, listener };
}
/**
@@ -152,6 +173,8 @@ function updateQuery(query) {
}
module.exports = {
+ activate,
+ deactivate,
listen,
pushURI,
fetchPage,
diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js
index ec240be..2a826d1 100644
--- a/packages/gitbook-core/src/lib/createContext.js
+++ b/packages/gitbook-core/src/lib/createContext.js
@@ -13,7 +13,18 @@ const Navigation = require('../actions/navigation');
const isBrowser = (typeof window !== 'undefined');
+/**
+ * The core plugin defines the defualt behaviour of GitBook and provides
+ * actions to other plugins.
+ * @type {Plugin}
+ */
const corePlugin = new Plugin({
+ activate: (dispatch) => {
+ dispatch(Navigation.activate());
+ },
+ deactivate: (dispatch) => {
+ dispatch(Navigation.deactivate());
+ },
reduce: coreReducers,
actions: {
Components, I18n, Navigation
diff --git a/packages/gitbook-core/src/models/Plugin.js b/packages/gitbook-core/src/models/Plugin.js
index 7ca5a86..07b1976 100644
--- a/packages/gitbook-core/src/models/Plugin.js
+++ b/packages/gitbook-core/src/models/Plugin.js
@@ -1,17 +1,19 @@
const { Record } = require('immutable');
const DEFAULTS = {
- init: ((dispatch, getState) => {}),
- reduce: ((state, action) => state),
- actions: {}
+ activate: ((dispatch, getState) => {}),
+ deactivate: ((dispatch, getState) => {}),
+ reduce: ((state, action) => state),
+ actions: {}
};
class Plugin extends Record(DEFAULTS) {
constructor(plugin) {
super({
- init: plugin.init || DEFAULTS.init,
- reduce: plugin.reduce || DEFAULTS.reduce,
- actions: plugin.actions || DEFAULTS.actions
+ activate: plugin.activate || DEFAULTS.activate,
+ deactivate: plugin.deactivate || DEFAULTS.deactivate,
+ reduce: plugin.reduce || DEFAULTS.reduce,
+ actions: plugin.actions || DEFAULTS.actions
});
}
}
diff --git a/packages/gitbook-core/src/reducers/navigation.js b/packages/gitbook-core/src/reducers/navigation.js
index 9831cd5..439ba21 100644
--- a/packages/gitbook-core/src/reducers/navigation.js
+++ b/packages/gitbook-core/src/reducers/navigation.js
@@ -1,11 +1,13 @@
-const { Record } = require('immutable');
+const { Record, List } = require('immutable');
const ACTION_TYPES = require('../actions/TYPES');
const NavigationState = Record({
// Are we loading a new page
loading: Boolean(false),
// Did we fail loading a page?
- error: null
+ error: null,
+ // Listener for history changes
+ listeners: List()
});
function reduceNavigation(state, action) {
@@ -28,6 +30,11 @@ function reduceNavigation(state, action) {
error: action.error
});
+ case ACTION_TYPES.NAVIGATION_LISTEN:
+ return state.merge({
+ listeners: state.listeners.push(action.listener)
+ });
+
default:
return state;
diff --git a/packages/gitbook-plugin-hints/src/index.js b/packages/gitbook-plugin-hints/src/index.js
index 2356995..be29f1b 100644
--- a/packages/gitbook-plugin-hints/src/index.js
+++ b/packages/gitbook-plugin-hints/src/index.js
@@ -34,7 +34,7 @@ const HintAlert = React.createClass({
});
module.exports = GitBook.createPlugin({
- init: (dispatch, getState, { Components }) => {
+ activate: (dispatch, getState, { Components }) => {
dispatch(Components.registerComponent(HintAlert, { role: 'block:hint' }));
}
});
diff --git a/packages/gitbook-plugin-lunr/src/index.js b/packages/gitbook-plugin-lunr/src/index.js
index 0eb6e04..f1dc913 100644
--- a/packages/gitbook-plugin-lunr/src/index.js
+++ b/packages/gitbook-plugin-lunr/src/index.js
@@ -13,7 +13,7 @@ function searchHandler(query) {
}
module.exports = GitBook.createPlugin({
- init: (dispatch, getState, { Search }) => {
+ activate: (dispatch, getState, { Search }) => {
dispatch(Search.registerHandler('lunr', searchHandler));
},
reduce: (state, action) => state
diff --git a/packages/gitbook-plugin-search/src/actions/search.js b/packages/gitbook-plugin-search/src/actions/search.js
index b4f811e..c71eda2 100644
--- a/packages/gitbook-plugin-search/src/actions/search.js
+++ b/packages/gitbook-plugin-search/src/actions/search.js
@@ -2,6 +2,14 @@ const { Promise, Immutable } = require('gitbook-core');
const { List } = Immutable;
const TYPES = require('./types');
+/*
+ Search workflow:
+
+ 1. Typing in the search input
+ 2. Trigger an update of the url
+ 3. An update of the url, trigger an update of search results
+ */
+
/**
* Start a search query
* @param {String} q
@@ -13,7 +21,6 @@ function query(q) {
};
}
-
/**
* Update results for a query
* @param {String} q
diff --git a/packages/gitbook-plugin-search/src/index.js b/packages/gitbook-plugin-search/src/index.js
index 05c2056..bc6c406 100644
--- a/packages/gitbook-plugin-search/src/index.js
+++ b/packages/gitbook-plugin-search/src/index.js
@@ -5,6 +5,11 @@ const SearchResults = require('./components/Results');
const reducers = require('./reducers');
const Search = require('./actions/search');
+/**
+ * Url of the page changed, we update the search according to this.
+ * @param {GitBook.Location} location
+ * @param {Function} dispatch
+ */
const onLocationChange = (location, dispatch) => {
const { query } = location;
const q = query.get('q');
@@ -13,7 +18,7 @@ const onLocationChange = (location, dispatch) => {
};
module.exports = GitBook.createPlugin({
- init: (dispatch, getState, { Navigation, Components }) => {
+ activate: (dispatch, getState, { Navigation, Components }) => {
// Register the navigation handler
dispatch(Navigation.listen(onLocationChange));
diff --git a/packages/gitbook-plugin-sharing/src/index.js b/packages/gitbook-plugin-sharing/src/index.js
index d8e52bf..e3ffdfb 100644
--- a/packages/gitbook-plugin-sharing/src/index.js
+++ b/packages/gitbook-plugin-sharing/src/index.js
@@ -1,7 +1,7 @@
const GitBook = require('gitbook-core');
module.exports = GitBook.createPlugin({
- init: (dispatch, getState) => {
+ activate: (dispatch, getState) => {
},
reduce: (state, action) => state
diff --git a/packages/gitbook-plugin-theme-default/src/index.js b/packages/gitbook-plugin-theme-default/src/index.js
index a07901c..c391908 100644
--- a/packages/gitbook-plugin-theme-default/src/index.js
+++ b/packages/gitbook-plugin-theme-default/src/index.js
@@ -6,7 +6,7 @@ const locales = require('./i18n');
module.exports = GitBook.createPlugin({
- init: (dispatch, state, { Components, I18n }) => {
+ activate: (dispatch, state, { Components, I18n }) => {
dispatch(Components.registerComponent(Theme, { role: 'Body' }));
dispatch(I18n.registerLocales(locales));
},
diff --git a/packages/gitbook-plugin/template/src/index.js b/packages/gitbook-plugin/template/src/index.js
index d8e52bf..0fe8869 100644
--- a/packages/gitbook-plugin/template/src/index.js
+++ b/packages/gitbook-plugin/template/src/index.js
@@ -1,8 +1,11 @@
const GitBook = require('gitbook-core');
module.exports = GitBook.createPlugin({
- init: (dispatch, getState) => {
-
+ activate: (dispatch, getState) => {
+ // Dispatch initialization actions
+ },
+ deactivate: (dispatch, getState) => {
+ // Dispatch cleanup actions
},
reduce: (state, action) => state
});