OptionsSelector.js 8.1 KB

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