OptionsSelector.js 7.4 KB

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