OptionsSelector.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { translate } from 'react-i18next';
  4. import FormGroup from 'react-bootstrap/es/FormGroup';
  5. import FormControl from 'react-bootstrap/es/FormControl';
  6. import ControlLabel from 'react-bootstrap/es/ControlLabel';
  7. import Dropdown from 'react-bootstrap/es/Dropdown';
  8. import MenuItem from 'react-bootstrap/es/MenuItem';
  9. class OptionsSelector extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. const config = this.props.crowi.getConfig();
  13. const isMathJaxEnabled = !!config.env.MATHJAX;
  14. this.state = {
  15. editorOptions: this.props.editorOptions || new EditorOptions(),
  16. previewOptions: this.props.previewOptions || new PreviewOptions(),
  17. isCddMenuOpened: false,
  18. isMathJaxEnabled,
  19. };
  20. this.availableThemes = [
  21. 'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', 'monokai', 'twilight'
  22. ];
  23. this.keymapModes = {
  24. default: 'Default',
  25. vim: 'Vim',
  26. emacs: 'Emacs',
  27. sublime: 'Sublime Text',
  28. };
  29. this.onChangeTheme = this.onChangeTheme.bind(this);
  30. this.onChangeKeymapMode = this.onChangeKeymapMode.bind(this);
  31. this.onClickStyleActiveLine = this.onClickStyleActiveLine.bind(this);
  32. this.onClickRenderMathJaxInRealtime = this.onClickRenderMathJaxInRealtime.bind(this);
  33. this.onToggleConfigurationDropdown = this.onToggleConfigurationDropdown.bind(this);
  34. }
  35. componentDidMount() {
  36. this.init();
  37. }
  38. init() {
  39. this.themeSelectorInputEl.value = this.state.editorOptions.theme;
  40. this.keymapModeSelectorInputEl.value = this.state.editorOptions.keymapMode;
  41. }
  42. onChangeTheme() {
  43. const newValue = this.themeSelectorInputEl.value;
  44. const newOpts = Object.assign(this.state.editorOptions, {theme: newValue});
  45. this.setState({editorOptions: newOpts});
  46. // dispatch event
  47. this.dispatchOnChange();
  48. }
  49. onChangeKeymapMode() {
  50. const newValue = this.keymapModeSelectorInputEl.value;
  51. const newOpts = Object.assign(this.state.editorOptions, {keymapMode: newValue});
  52. this.setState({editorOptions: newOpts});
  53. // dispatch event
  54. this.dispatchOnChange();
  55. }
  56. onClickStyleActiveLine(event) {
  57. // keep dropdown opened
  58. this._cddForceOpen = true;
  59. const newValue = !this.state.editorOptions.styleActiveLine;
  60. const newOpts = Object.assign(this.state.editorOptions, {styleActiveLine: newValue});
  61. this.setState({editorOptions: newOpts});
  62. // dispatch event
  63. this.dispatchOnChange();
  64. }
  65. onClickRenderMathJaxInRealtime(event) {
  66. // keep dropdown opened
  67. this._cddForceOpen = true;
  68. const newValue = !this.state.previewOptions.renderMathJaxInRealtime;
  69. const newOpts = Object.assign(this.state.previewOptions, {renderMathJaxInRealtime: newValue});
  70. this.setState({previewOptions: newOpts});
  71. // dispatch event
  72. this.dispatchOnChange();
  73. }
  74. /*
  75. * see: https://github.com/react-bootstrap/react-bootstrap/issues/1490#issuecomment-207445759
  76. */
  77. onToggleConfigurationDropdown(newValue) {
  78. if (this._cddForceOpen) {
  79. this.setState({ isCddMenuOpened: true });
  80. this._cddForceOpen = false;
  81. }
  82. else {
  83. this.setState({ isCddMenuOpened: newValue });
  84. }
  85. }
  86. /**
  87. * dispatch onChange event
  88. */
  89. dispatchOnChange() {
  90. this.props.onChange(this.state.editorOptions, this.state.previewOptions);
  91. }
  92. renderThemeSelector() {
  93. const optionElems = this.availableThemes.map((theme) => {
  94. return <option key={theme} value={theme}>{theme}</option>;
  95. });
  96. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  97. return (
  98. <FormGroup controlId="formControlsSelect" className="my-0">
  99. <ControlLabel>Theme:</ControlLabel>
  100. <FormControl componentClass="select" placeholder="select" bsClass={bsClassName} className="btn-group-sm selectpicker"
  101. onChange={this.onChangeTheme}
  102. inputRef={ el => this.themeSelectorInputEl=el }>
  103. {optionElems}
  104. </FormControl>
  105. </FormGroup>
  106. );
  107. }
  108. renderKeymapModeSelector() {
  109. const optionElems = [];
  110. for (let mode in this.keymapModes) {
  111. const label = this.keymapModes[mode];
  112. const dataContent = (mode === 'default')
  113. ? label
  114. : `<img src='/images/icons/${mode}.png' width='16px' class='m-r-5'></img> ${label}`;
  115. optionElems.push(
  116. <option key={mode} value={mode} data-content={dataContent}>{label}</option>
  117. );
  118. }
  119. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  120. return (
  121. <FormGroup controlId="formControlsSelect" className="my-0">
  122. <ControlLabel>Keymap:</ControlLabel>
  123. <FormControl componentClass="select" placeholder="select" bsClass={bsClassName} className="btn-group-sm selectpicker"
  124. onChange={this.onChangeKeymapMode}
  125. inputRef={ el => this.keymapModeSelectorInputEl=el }>
  126. {optionElems}
  127. </FormControl>
  128. </FormGroup>
  129. );
  130. }
  131. renderConfigurationDropdown() {
  132. return (
  133. <FormGroup controlId="formControlsSelect" className="my-0">
  134. <Dropdown dropup id="configurationDropdown" className="configuration-dropdown"
  135. open={this.state.isCddMenuOpened} onToggle={this.onToggleConfigurationDropdown}>
  136. <Dropdown.Toggle bsSize="sm">
  137. <i className="icon-settings"></i>
  138. </Dropdown.Toggle>
  139. <Dropdown.Menu>
  140. {this.renderActiveLineMenuItem()}
  141. {this.renderRealtimeMathJaxMenuItem()}
  142. {/* <MenuItem divider /> */}
  143. </Dropdown.Menu>
  144. </Dropdown>
  145. </FormGroup>
  146. );
  147. }
  148. renderActiveLineMenuItem() {
  149. const { t } = this.props;
  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">{ t('page_edit.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. t: PropTypes.func.isRequired, // i18next
  207. crowi: PropTypes.object.isRequired,
  208. editorOptions: PropTypes.instanceOf(EditorOptions).isRequired,
  209. previewOptions: PropTypes.instanceOf(PreviewOptions).isRequired,
  210. onChange: PropTypes.func.isRequired,
  211. };
  212. export default translate()(OptionsSelector);