diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:59:40 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:59:40 +0200 |
commit | 0e74c9bad5b184528880d499485e1154c000b1b5 (patch) | |
tree | 6628dc4ab27d6273727d6470dfcf99b11e8cdda1 /packages/gitbook-core/src | |
parent | d7e415b0b396639b7e5c64d61f47569b40ced053 (diff) | |
download | gitbook-0e74c9bad5b184528880d499485e1154c000b1b5.zip gitbook-0e74c9bad5b184528880d499485e1154c000b1b5.tar.gz gitbook-0e74c9bad5b184528880d499485e1154c000b1b5.tar.bz2 |
Add action to register a component
Diffstat (limited to 'packages/gitbook-core/src')
-rw-r--r-- | packages/gitbook-core/src/actions/components.js | 37 | ||||
-rw-r--r-- | packages/gitbook-core/src/components/InjectedComponent.js | 18 | ||||
-rw-r--r-- | packages/gitbook-core/src/index.js | 4 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/components.js | 16 |
4 files changed, 56 insertions, 19 deletions
diff --git a/packages/gitbook-core/src/actions/components.js b/packages/gitbook-core/src/actions/components.js new file mode 100644 index 0000000..89b0c9e --- /dev/null +++ b/packages/gitbook-core/src/actions/components.js @@ -0,0 +1,37 @@ +const ACTION_TYPES = require('./TYPES'); + +/** + * Find all components matching a descriptor + * @param {List<ComponentDescriptor>} state + * @param {String} matching.role + */ +function findMatchingComponents(state, matching) { + return state + .filter(({descriptor}) => { + if (matching.role && matching.role === descriptor.role) { + return false; + } + + return true; + }) + .map(component => component.Component); +} + +/** + * Register a new component + * @param {React.Class} Component + * @param {Descriptor} descriptor + * @return {Action} + */ +function registerComponent(Component, descriptor) { + return { + type: ACTION_TYPES.REGISTER_COMPONENT, + Component, + descriptor + }; +} + +module.exports = { + findMatchingComponents, + registerComponent +}; diff --git a/packages/gitbook-core/src/components/InjectedComponent.js b/packages/gitbook-core/src/components/InjectedComponent.js index fe386ef..f8e5b04 100644 --- a/packages/gitbook-core/src/components/InjectedComponent.js +++ b/packages/gitbook-core/src/components/InjectedComponent.js @@ -2,6 +2,7 @@ const React = require('react'); const connect = require('./connect'); const UnsafeComponent = require('./UnsafeComponent'); +const { findMatchingComponents } = require('../actions/components'); /* Public: InjectedComponent makes it easy to include a set of dynamically registered @@ -52,23 +53,6 @@ const InjectedComponentSet = React.createClass({ }); /** - * Find all components matching a descriptor - * @param {List<ComponentDescriptor>} components - * @param {String} matching.role - */ -function findMatchingComponents(components, matching) { - return components - .filter(({descriptor}) => { - if (matching.role && matching.role === descriptor.role) { - return false; - } - - return true; - }) - .map(component => component.Component); -} - -/** * Map Redux state to InjectedComponentSet's props */ function mapStateToProps(state, props) { diff --git a/packages/gitbook-core/src/index.js b/packages/gitbook-core/src/index.js index 5059463..dcd7580 100644 --- a/packages/gitbook-core/src/index.js +++ b/packages/gitbook-core/src/index.js @@ -1,9 +1,10 @@ +const { InjectedComponent, InjectedComponentSet } = require('./components/InjectedComponent'); +const { registerComponent } = require('./actions/components'); const ACTIONS = require('./actions/TYPES'); const connect = require('./connect'); const createPlugin = require('./createPlugin'); const createReducer = require('./createReducer'); const createStore = require('./createStore'); -const { InjectedComponent, InjectedComponentSet } = require('./components/InjectedComponent'); module.exports = { ACTIONS, @@ -11,6 +12,7 @@ module.exports = { createPlugin, createReducer, createStore, + registerComponent, InjectedComponent, InjectedComponentSet }; diff --git a/packages/gitbook-core/src/reducers/components.js b/packages/gitbook-core/src/reducers/components.js index 8cb9c4b..8eebb34 100644 --- a/packages/gitbook-core/src/reducers/components.js +++ b/packages/gitbook-core/src/reducers/components.js @@ -1,7 +1,21 @@ -const Immutable = require('immutable'); +const { List } = require('immutable'); +const ACTION_TYPES = require('../actions/TYPES'); function reduceComponents(state, action) { + state = state || List(); + switch (action.type) { + + case ACTION_TYPES.REGISTER_COMPONENT: + return state.push({ + Component: action.Component, + descriptor: action.descriptor + }); + + default: + return state; + + } } module.exports = reduceComponents; |