diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-09-28 18:14:35 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-09-28 18:14:35 +0200 |
commit | 287c80b2e116572669f2a7073731ba7350e3619a (patch) | |
tree | 1572bab423b927e16bca4d56020aeec6442fe1a9 /packages/gitbook-plugin-search | |
parent | 26fc021b900f6b28a180fa8360313b3193e6bf5e (diff) | |
download | gitbook-287c80b2e116572669f2a7073731ba7350e3619a.zip gitbook-287c80b2e116572669f2a7073731ba7350e3619a.tar.gz gitbook-287c80b2e116572669f2a7073731ba7350e3619a.tar.bz2 |
Start search plugin
Diffstat (limited to 'packages/gitbook-plugin-search')
-rw-r--r-- | packages/gitbook-plugin-search/index.js | 4 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/package.json | 24 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/actions/search.js | 49 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/actions/types.js | 7 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/components/Input.js | 47 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/components/Results.js | 28 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/index.js | 13 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/reducers/index.js | 3 | ||||
-rw-r--r-- | packages/gitbook-plugin-search/src/reducers/search.js | 37 |
9 files changed, 212 insertions, 0 deletions
diff --git a/packages/gitbook-plugin-search/index.js b/packages/gitbook-plugin-search/index.js new file mode 100644 index 0000000..5803889 --- /dev/null +++ b/packages/gitbook-plugin-search/index.js @@ -0,0 +1,4 @@ + +module.exports = { + +}; diff --git a/packages/gitbook-plugin-search/package.json b/packages/gitbook-plugin-search/package.json new file mode 100644 index 0000000..df84726 --- /dev/null +++ b/packages/gitbook-plugin-search/package.json @@ -0,0 +1,24 @@ +{ + "name": "gitbook-plugin-search", + "description": "Search integration in GitBook", + "main": "index.js", + "version": "2.0.0", + "dependencies": { + "gitbook-core": "^0.0.0" + }, + "devDependencies": { + "gitbook-plugin": "*" + }, + "engines": { + "gitbook": ">=3.0.0" + }, + "homepage": "https://github.com/GitbookIO/gitbook", + "repository": { + "type": "git", + "url": "https://github.com/GitbookIO/gitbook.git" + }, + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/GitbookIO/gitbook/issues" + } +} diff --git a/packages/gitbook-plugin-search/src/actions/search.js b/packages/gitbook-plugin-search/src/actions/search.js new file mode 100644 index 0000000..e9852cc --- /dev/null +++ b/packages/gitbook-plugin-search/src/actions/search.js @@ -0,0 +1,49 @@ +const { Promise, Immutable } = require('gitbook-core'); +const { List } = Immutable; +const TYPES = require('./types'); + +/** + * Start a search query + * @param {String} q + * @return {Action} + */ +function query(q) { + return (dispatch, getState) => { + const handlers = getState().search; + + dispatch({ type: TYPES.UPDATE_QUERY, query: q }); + + return Promise.reduce(handlers, (results, handler) => { + return Promise(handler(q)) + .then(handlerResults => results.concat(handlerResults)); + }, List()) + .then(results => { + dispatch({ type: TYPES.UPDATE_RESULTS, query: q }); + }); + }; +} + +/** + * Register a search handler + * @param {String} name + * @param {Function} handler + * @return {Action} + */ +function registerHandler(name, handler) { + return { type: TYPES.REGISTER_HANDLER, name, handler }; +} + +/** + * Unregister a search handler + * @param {String} name + * @return {Action} + */ +function unregisterHandler(name) { + return { type: TYPES.UNREGISTER_HANDLER, name }; +} + +module.exports = { + query, + registerHandler, + unregisterHandler +}; diff --git a/packages/gitbook-plugin-search/src/actions/types.js b/packages/gitbook-plugin-search/src/actions/types.js new file mode 100644 index 0000000..a88b1f0 --- /dev/null +++ b/packages/gitbook-plugin-search/src/actions/types.js @@ -0,0 +1,7 @@ + +module.exports = { + REGISTER_HANDLER: 'search/register_handler', + UNREGISTER_HANDLER: 'search/unregister_handler', + UPDATE_QUERY: 'search/update_query', + UPDATE_RESULTS: 'search/update_results' +}; diff --git a/packages/gitbook-plugin-search/src/components/Input.js b/packages/gitbook-plugin-search/src/components/Input.js new file mode 100644 index 0000000..c4d1288 --- /dev/null +++ b/packages/gitbook-plugin-search/src/components/Input.js @@ -0,0 +1,47 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; + +const search = require('../actions/search'); + +const DEBOUNCE_MS = 600; + +const SearchInput = React.createClass({ + propTypes: { + dispatch: React.PropTypes.func + }, + + onUpdateQuery(q) { + const { dispatch } = this.props; + + dispatch(search.query(q)); + }, + + onKeyDown(event) { + const { value } = event.currentTarget; + + if (this.debouncedSearch) { + clearTimeout(this.debouncedSearch); + } + + this.debouncedSearch = setTimeout(() => { + this.debouncedSearch = null; + this.onUpdateQuery(value); + }, DEBOUNCE_MS); + }, + + componentWillUnmount() { + if (this.debouncedSearch) { + clearTimeout(this.debouncedSearch); + } + }, + + render() { + return ( + <div className="Search/Input"> + <input type="text" placeholder="" onKeyDown={this.onKeyDown} /> + </div> + ); + } +}); + +module.exports = GitBook.connect(SearchInput); diff --git a/packages/gitbook-plugin-search/src/components/Results.js b/packages/gitbook-plugin-search/src/components/Results.js new file mode 100644 index 0000000..99bc63d --- /dev/null +++ b/packages/gitbook-plugin-search/src/components/Results.js @@ -0,0 +1,28 @@ +const GitBook = require('gitbook-core'); +const { React } = GitBook; + +const SearchResults = React.createClass({ + propTypes: { + results: GitBook.Shapes.list + }, + + render() { + const { results } = this.props; + + return ( + <div className="Search/Results"> + {results.map(result => { + + })} + </div> + ); + } +}); + +function mapStateToProps(state) { + return { + results: state.search.results + }; +} + +module.exports = GitBook.connect(SearchResults, mapStateToProps); diff --git a/packages/gitbook-plugin-search/src/index.js b/packages/gitbook-plugin-search/src/index.js new file mode 100644 index 0000000..94a5d35 --- /dev/null +++ b/packages/gitbook-plugin-search/src/index.js @@ -0,0 +1,13 @@ +const GitBook = require('gitbook-core'); + +const SearchInput = require('./components/Input'); +const SearchResults = require('./components/Results'); +const reducers = require('./reducers'); + +module.exports = GitBook.createPlugin( + (dispatch, state) => { + dispatch(GitBook.registerComponent(SearchInput, { role: 'search:input' })); + dispatch(GitBook.registerComponent(SearchResults, { role: 'search:results' })); + }, + reducers +); diff --git a/packages/gitbook-plugin-search/src/reducers/index.js b/packages/gitbook-plugin-search/src/reducers/index.js new file mode 100644 index 0000000..bfce2bd --- /dev/null +++ b/packages/gitbook-plugin-search/src/reducers/index.js @@ -0,0 +1,3 @@ +const GitBook = require('gitbook-core'); + +module.exports = GitBook.createReducer('search', require('./search')); diff --git a/packages/gitbook-plugin-search/src/reducers/search.js b/packages/gitbook-plugin-search/src/reducers/search.js new file mode 100644 index 0000000..ac49698 --- /dev/null +++ b/packages/gitbook-plugin-search/src/reducers/search.js @@ -0,0 +1,37 @@ +const GitBook = require('gitbook-core'); +const { Record, List, OrderedMap } = GitBook.Immutable; + +const TYPES = require('../actions/types'); + +const Result = Record({ + url: String(''), + title: String(''), + body: String('') +}); + +const SearchState = Record({ + // Current query + query: String(''), + // Current list of results + results: List(), + // Search handlers + handlers: OrderedMap() +}); + +module.exports = (state = SearchState(), action) => { + switch (action.type) { + + case TYPES.REGISTER_HANDLER: + return state.merge({ + handlers: state.handlers.set(action.name, action.handler) + }); + + case TYPES.UNREGISTER_HANDLER: + return state.merge({ + handlers: state.handlers.remove(action.name) + }); + + default: + return state; + } +}; |