OptionsSelector.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import FormGroup from 'react-bootstrap/es/FormGroup';
  4. import FormControl from 'react-bootstrap/es/FormControl';
  5. import ControlLabel from 'react-bootstrap/es/ControlLabel';
  6. import Button from 'react-bootstrap/es/Button';
  7. import Dropdown from 'react-bootstrap/es/Dropdown';
  8. import MenuItem from 'react-bootstrap/es/MenuItem';
  9. import OverlayTrigger from 'react-bootstrap/es/OverlayTrigger';
  10. import Tooltip from 'react-bootstrap/es/Tooltip';
  11. export default class OptionsSelector extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. const config = this.props.crowi.getConfig();
  15. const isMathJaxEnabled = !!config.env.MATHJAX;
  16. this.state = {
  17. editorOptions: this.props.editorOptions || new EditorOptions(),
  18. previewOptions: this.props.previewOptions || new PreviewOptions(),
  19. isCddMenuOpened: false,
  20. isMathJaxEnabled,
  21. }
  22. this.availableThemes = [
  23. 'elegant', 'neo', 'mdn-like', 'material', 'monokai', 'twilight'
  24. ]
  25. this.keymapModes = {
  26. default: 'Default',
  27. vim: 'Vim',
  28. emacs: 'Emacs',
  29. sublime: 'Sublime Text',
  30. }
  31. this.onChangeTheme = this.onChangeTheme.bind(this);
  32. this.onChangeKeymapMode = this.onChangeKeymapMode.bind(this);
  33. this.onClickStyleActiveLine = this.onClickStyleActiveLine.bind(this);
  34. this.onClickRenderMathJaxInRealtime = this.onClickRenderMathJaxInRealtime.bind(this);
  35. this.onToggleConfigurationDropdown = this.onToggleConfigurationDropdown.bind(this);
  36. }
  37. componentDidMount() {
  38. this.init();
  39. }
  40. init() {
  41. this.themeSelectorInputEl.value = this.state.editorOptions.theme;
  42. }
  43. onChangeTheme() {
  44. const newValue = this.themeSelectorInputEl.value;
  45. const newOpts = Object.assign(this.state.editorOptions, {theme: newValue});
  46. this.setState({editorOptions: newOpts});
  47. // dispatch event
  48. this.dispatchOnChange();
  49. }
  50. onChangeKeymapMode() {
  51. const newValue = this.keymapModeSelectorInputEl.value;
  52. const newOpts = Object.assign(this.state.editorOptions, {keymapMode: newValue});
  53. this.setState({editorOptions: newOpts});
  54. // dispatch event
  55. this.dispatchOnChange();
  56. }
  57. onClickStyleActiveLine(event) {
  58. // keep dropdown opened
  59. this._cddForceOpen = true;
  60. const newValue = !this.state.editorOptions.styleActiveLine;
  61. const newOpts = Object.assign(this.state.editorOptions, {styleActiveLine: newValue});
  62. this.setState({editorOptions: newOpts});
  63. // dispatch event
  64. this.dispatchOnChange();
  65. }
  66. onClickRenderMathJaxInRealtime(event) {
  67. // keep dropdown opened
  68. this._cddForceOpen = true;
  69. const newValue = !this.state.previewOptions.renderMathJaxInRealtime;
  70. const newOpts = Object.assign(this.state.previewOptions, {renderMathJaxInRealtime: newValue});
  71. this.setState({previewOptions: newOpts});
  72. // dispatch event
  73. this.dispatchOnChange();
  74. }
  75. /*
  76. * see: https://github.com/react-bootstrap/react-bootstrap/issues/1490#issuecomment-207445759
  77. */
  78. onToggleConfigurationDropdown(newValue) {
  79. if (this._cddForceOpen) {
  80. this.setState({ isCddMenuOpened: true });
  81. this._cddForceOpen = false;
  82. }
  83. else {
  84. this.setState({ isCddMenuOpened: newValue });
  85. }
  86. }
  87. /**
  88. * dispatch onChange event
  89. */
  90. dispatchOnChange() {
  91. if (this.props.onChange != null) {
  92. this.props.onChange(this.state.editorOptions, this.state.previewOptions);
  93. }
  94. }
  95. renderThemeSelector() {
  96. const optionElems = this.availableThemes.map((theme) => {
  97. return <option key={theme} value={theme}>{theme}</option>;
  98. });
  99. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  100. return (
  101. <FormGroup controlId="formControlsSelect">
  102. <ControlLabel>Theme:</ControlLabel>
  103. <FormControl componentClass="select" placeholder="select" bsClass={bsClassName} className="btn-group-sm selectpicker"
  104. onChange={this.onChangeTheme}
  105. inputRef={ el => this.themeSelectorInputEl=el }>
  106. {optionElems}
  107. </FormControl>
  108. </FormGroup>
  109. )
  110. }
  111. renderKeymapModeSelector() {
  112. const optionElems = [];
  113. for (let mode in this.keymapModes) {
  114. const label = this.keymapModes[mode];
  115. optionElems.push(<option key={mode} value={mode}>{label}</option>);
  116. }
  117. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  118. return (
  119. <FormGroup controlId="formControlsSelect">
  120. <ControlLabel>Mode:</ControlLabel>
  121. <FormControl componentClass="select" placeholder="select" bsClass={bsClassName} className="btn-group-sm selectpicker"
  122. onChange={this.onChangeKeymapMode}
  123. inputRef={ el => this.keymapModeSelectorInputEl=el }>
  124. {optionElems}
  125. </FormControl>
  126. </FormGroup>
  127. )
  128. }
  129. renderConfigurationDropdown() {
  130. return (
  131. <FormGroup controlId="formControlsSelect">
  132. <Dropdown dropup id="configurationDropdown" className="configuration-dropdown"
  133. open={this.state.isCddMenuOpened} onToggle={this.onToggleConfigurationDropdown}>
  134. <Dropdown.Toggle bsSize="sm">
  135. <i className="icon-settings"></i>
  136. </Dropdown.Toggle>
  137. <Dropdown.Menu>
  138. {this.renderActiveLineMenuItem()}
  139. {this.renderRealtimeMathJaxMenuItem()}
  140. {/* <MenuItem divider /> */}
  141. </Dropdown.Menu>
  142. </Dropdown>
  143. </FormGroup>
  144. )
  145. }
  146. renderActiveLineMenuItem() {
  147. const isActive = this.state.editorOptions.styleActiveLine;
  148. const iconClasses = ['text-info']
  149. if (isActive) {
  150. iconClasses.push('ti-check')
  151. }
  152. const iconClassName = iconClasses.join(' ');
  153. return (
  154. <MenuItem onClick={this.onClickStyleActiveLine}>
  155. <span className="icon-container"></span>
  156. <span className="menuitem-label">Show active line</span>
  157. <span className="icon-container"><i className={iconClassName}></i></span>
  158. </MenuItem>
  159. )
  160. }
  161. renderRealtimeMathJaxMenuItem() {
  162. if (!this.state.isMathJaxEnabled) {
  163. return;
  164. }
  165. const isEnabled = this.state.isMathJaxEnabled;
  166. const isActive = isEnabled && this.state.previewOptions.renderMathJaxInRealtime;
  167. const iconClasses = ['text-info']
  168. if (isActive) {
  169. iconClasses.push('ti-check')
  170. }
  171. const iconClassName = iconClasses.join(' ');
  172. return (
  173. <MenuItem onClick={this.onClickRenderMathJaxInRealtime}>
  174. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px"></img></span>
  175. <span className="menuitem-label">MathJax Rendering</span>
  176. <i className={iconClassName}></i>
  177. </MenuItem>
  178. )
  179. }
  180. render() {
  181. return <span>
  182. <span className="m-l-5">{this.renderThemeSelector()}</span>
  183. <span className="m-l-5">{this.renderKeymapModeSelector()}</span>
  184. <span className="m-l-5">{this.renderConfigurationDropdown()}</span>
  185. </span>
  186. }
  187. }
  188. export class EditorOptions {
  189. constructor(props) {
  190. this.theme = 'elegant';
  191. this.keymapMode = 'default';
  192. this.styleActiveLine = false;
  193. Object.assign(this, props);
  194. }
  195. }
  196. export class PreviewOptions {
  197. constructor(props) {
  198. this.renderMathJaxInRealtime = false;
  199. Object.assign(this, props);
  200. }
  201. }
  202. OptionsSelector.propTypes = {
  203. crowi: PropTypes.object.isRequired,
  204. editorOptions: PropTypes.instanceOf(EditorOptions),
  205. previewOptions: PropTypes.instanceOf(PreviewOptions),
  206. onChange: PropTypes.func,
  207. };