Mailspring/app/internal_packages/composer/lib/action-bar-plugins.jsx

79 lines
2.1 KiB
React
Raw Normal View History

2017-09-27 02:33:08 +08:00
import classnames from 'classnames';
import { React, PropTypes, ComponentRegistry } from 'mailspring-exports';
2017-09-27 02:46:00 +08:00
import { InjectedComponentSet } from 'mailspring-component-kit';
2017-09-27 02:33:08 +08:00
const ROLE = 'Composer:ActionButton';
2016-04-30 07:19:52 +08:00
export default class ActionBarPlugins extends React.Component {
2017-09-27 02:33:08 +08:00
static displayName = 'ActionBarPlugins';
static propTypes = {
2017-09-27 02:33:08 +08:00
draft: PropTypes.object,
session: PropTypes.object,
isValidDraft: PropTypes.func,
};
constructor(props) {
super(props);
2017-09-27 02:33:08 +08:00
this.state = this._getStateFromStores();
}
componentDidMount() {
2017-09-27 02:33:08 +08:00
this._usub = ComponentRegistry.listen(this._onComponentsChange);
}
componentWillUnmount() {
this._usub();
}
_onComponentsChange = () => {
if (this._getPluginsLength() > 0) {
// The `InjectedComponentSet` also listens to the ComponentRegistry.
// Since we can't guarantee the order the listeners are fired in and
// we want to make sure we add the class after the injected component
// set has rendered, put the call in this requestAnimationFrame
//
// It also takes 2 frames to reliably get all of the icons painted.
window.requestAnimationFrame(() => {
window.requestAnimationFrame(() => {
2017-09-27 02:33:08 +08:00
this.setState(this._getStateFromStores());
});
});
}
2017-09-27 02:33:08 +08:00
};
_getPluginsLength() {
2017-09-27 02:33:08 +08:00
return ComponentRegistry.findComponentsMatching({ role: ROLE }).length;
}
_getStateFromStores() {
return {
pluginsLoaded: this._getPluginsLength() > 0,
2017-09-27 02:33:08 +08:00
};
}
render() {
const className = classnames({
2017-09-27 02:33:08 +08:00
'action-bar-animation-wrap': true,
'plugins-loaded': this.state.pluginsLoaded,
});
return (
<span className={className}>
2016-10-18 08:59:33 +08:00
<div className="action-bar-cover" />
<InjectedComponentSet
className="composer-action-bar-plugins"
2017-09-27 02:33:08 +08:00
matching={{ role: ROLE }}
exposedProps={{
draft: this.props.draft,
threadId: this.props.draft.threadId,
headerMessageId: this.props.draft.headerMessageId,
session: this.props.session,
isValidDraft: this.props.isValidDraft,
}}
/>
</span>
2017-09-27 02:33:08 +08:00
);
}
}