UNPKG

react-draft-wysiwyg

Version:
105 lines (91 loc) 2.65 kB
/* @flow */ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { toggleCustomInlineStyle, getSelectionCustomInlineStyle, } from 'draftjs-utils'; import LayoutComponent from './Component'; export default class FontFamily extends Component { static propTypes = { onChange: PropTypes.func.isRequired, editorState: PropTypes.object, modalHandler: PropTypes.object, config: PropTypes.object, translations: PropTypes.object, }; state: Object = { expanded: undefined, currentFontFamily: undefined, }; componentWillMount(): void { const { editorState, modalHandler } = this.props; if (editorState) { this.setState({ currentFontFamily: getSelectionCustomInlineStyle(editorState, ['FONTFAMILY']).FONTFAMILY, }); } modalHandler.registerCallBack(this.expandCollapse); } componentWillReceiveProps(properties: Object): void { if (properties.editorState && this.props.editorState !== properties.editorState) { this.setState({ currentFontFamily: getSelectionCustomInlineStyle(properties.editorState, ['FONTFAMILY']).FONTFAMILY, }); } } componentWillUnmount(): void { const { modalHandler } = this.props; modalHandler.deregisterCallBack(this.expandCollapse); } onExpandEvent: Function = (): void => { this.signalExpanded = !this.state.expanded; }; expandCollapse: Function = (): void => { this.setState({ expanded: this.signalExpanded, }); this.signalExpanded = false; } doExpand: Function = (): void => { this.setState({ expanded: true, }); }; doCollapse: Function = (): void => { this.setState({ expanded: false, }); }; toggleFontFamily: Function = (fontFamily: string) => { const { editorState, onChange } = this.props; const newState = toggleCustomInlineStyle( editorState, 'fontFamily', fontFamily, ); if (newState) { onChange(newState); } }; render(): Object { const { config, translations } = this.props; const { expanded, currentFontFamily } = this.state; const FontFamilyComponent = config.component || LayoutComponent; const fontFamily = currentFontFamily && currentFontFamily.substring(11); return ( <FontFamilyComponent translations={translations} config={config} currentState={{ fontFamily }} onChange={this.toggleFontFamily} expanded={expanded} onExpandEvent={this.onExpandEvent} doExpand={this.doExpand} doCollapse={this.doCollapse} /> ); } }