summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-plugin-search/src/index.js
blob: 298c088fcac2e56d77b4e9d4c0bec9728119154f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const GitBook = require('gitbook-core');

const SearchInput = require('./components/Input');
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');

    dispatch(Search.handleQuery(q));
};

module.exports = GitBook.createPlugin({
    activate: (dispatch, getState, { History, Components }) => {
        // Register the navigation handler
        dispatch(History.listen(onLocationChange));

        // Register components
        dispatch(Components.registerComponent(SearchInput, { role: 'search:input' }));
        dispatch(Components.registerComponent(SearchResults, { role: 'search:results' }));
    },
    reduce: reducers,
    actions: {
        Search
    }
});