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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
const React = require('react');
const ReactRedux = require('react-redux');
const { injectIntl } = require('react-intl');
const ContextShape = require('../shapes/Context');
/**
* Use the GitBook context provided by ContextProvider to map actions to props
* @param {ReactComponent} Component
* @param {Function} mapActionsToProps
* @return {ReactComponent}
*/
function connectToActions(Component, mapActionsToProps) {
if (!mapActionsToProps) {
return Component;
}
return React.createClass({
displayName: `ConnectActions(${Component.displayName})`,
propTypes: {
children: React.PropTypes.node
},
contextTypes: {
gitbook: ContextShape.isRequired
},
render() {
const { gitbook } = this.context;
const { children, ...props } = this.props;
const { actions, store } = gitbook;
const actionsProps = mapActionsToProps(actions, store.dispatch);
return <Component {...props} {...actionsProps}>{children}</Component>;
}
});
}
/**
* Connect to i18n
* @param {ReactComponent} Component
* @return {ReactComponent}
*/
function connectToI18n(Component) {
return injectIntl(({intl, children, ...props}) => {
const i18n = {
t: (id, values) => intl.formatMessage({ id }, values)
};
return <Component {...props} i18n={i18n}>{children}</Component>;
});
}
/**
* Connect a component to the GitBook context (store and actions).
*
* @param {ReactComponent} Component
* @param {Function} mapStateToProps
* @return {ReactComponent}
*/
function connect(Component, mapStateToProps, mapActionsToProps) {
Component = ReactRedux.connect(mapStateToProps)(Component);
Component = connectToI18n(Component);
Component = connectToActions(Component, mapActionsToProps);
return Component;
}
module.exports = connect;
|