diff options
Diffstat (limited to 'packages/gitbook-core')
-rw-r--r-- | packages/gitbook-core/src/actions/TYPES.js | 1 | ||||
-rw-r--r-- | packages/gitbook-core/src/actions/navigation.js | 39 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/createContext.js | 11 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/Plugin.js | 14 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/navigation.js | 11 |
5 files changed, 60 insertions, 16 deletions
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; |