OptionsSelector.js 7.6 KB

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