diff options
Diffstat (limited to 'packages/gitbook-core/src')
-rw-r--r-- | packages/gitbook-core/src/actions/TYPES.js | 2 | ||||
-rw-r--r-- | packages/gitbook-core/src/actions/navigation.js | 22 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/createContext.js | 12 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/navigation.js | 17 |
4 files changed, 47 insertions, 6 deletions
diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js index e758a44..3177dbd 100644 --- a/packages/gitbook-core/src/actions/TYPES.js +++ b/packages/gitbook-core/src/actions/TYPES.js @@ -7,6 +7,8 @@ module.exports = { PAGE_FETCH_START: 'navigation/fetch:start', PAGE_FETCH_END: 'navigation/fetch:end', PAGE_FETCH_ERROR: 'navigation/fetch:error', + PAGE_UPDATE_ANCHOR: 'navigation/update:anchor', + PAGE_UPDATE_QUERY: 'navigation/update:query', // i18n I18N_REGISTER_LOCALE: 'i18n/register:locale' }; diff --git a/packages/gitbook-core/src/actions/navigation.js b/packages/gitbook-core/src/actions/navigation.js index 60be593..f13e878 100644 --- a/packages/gitbook-core/src/actions/navigation.js +++ b/packages/gitbook-core/src/actions/navigation.js @@ -123,8 +123,28 @@ function fetchArticle(article) { return fetchPage(article.path); } +/** + * Update anchor for current page + * @param {String} anchor + * @return {Action} action + */ +function updateAnchor(anchor) { + return { type: ACTION_TYPES.PAGE_UPDATE_ANCHOR, anchor }; +} + +/** + * Update query for current page + * @param {Object|Map} query + * @return {Action} action + */ +function updateQuery(query) { + return { type: ACTION_TYPES.PAGE_UPDATE_QUERY, query }; +} + module.exports = { pushURI, fetchPage, - fetchArticle + fetchArticle, + updateAnchor, + updateQuery }; diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js index d19e0f6..ec240be 100644 --- a/packages/gitbook-core/src/lib/createContext.js +++ b/packages/gitbook-core/src/lib/createContext.js @@ -11,6 +11,8 @@ const Components = require('../actions/components'); const I18n = require('../actions/i18n'); const Navigation = require('../actions/navigation'); +const isBrowser = (typeof window !== 'undefined'); + const corePlugin = new Plugin({ reduce: coreReducers, actions: { @@ -38,13 +40,19 @@ function createContext(plugins, initialState) { return Object.assign(accu, plugin.actions); }, {}); + // Create thunk middleware which include actions + const thunk = ReduxThunk.withExtraArgument(actions); + + // Create the redux store const store = Redux.createStore( (state, action) => { - console.log('[store]', action.type); + if (isBrowser) { + console.log('[store]', action.type); + } return reducer(state, action); }, initialState, - Redux.compose(Redux.applyMiddleware(ReduxThunk)) + Redux.compose(Redux.applyMiddleware(thunk)) ); // Initialize the plugins diff --git a/packages/gitbook-core/src/reducers/navigation.js b/packages/gitbook-core/src/reducers/navigation.js index a895186..625adef 100644 --- a/packages/gitbook-core/src/reducers/navigation.js +++ b/packages/gitbook-core/src/reducers/navigation.js @@ -1,9 +1,15 @@ -const { Record } = require('immutable'); +const { Record, Map } = require('immutable'); const ACTION_TYPES = require('../actions/TYPES'); const NavigationState = Record({ - loading: false, - error: null + // Are we loading a new page + loading: Boolean(false), + // Did we fail loading a page? + error: null, + // Query string + query: Map(), + // Current anchor + anchor: String('') }); function reduceNavigation(state, action) { @@ -26,6 +32,11 @@ function reduceNavigation(state, action) { error: action.error }); + case ACTION_TYPES.PAGE_UPDATE_ANCHOR: + return state.merge({ + anchor: action.anchor + }); + default: return state; |