OptionsSelector.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', '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. const dataContent = (mode === 'default')
  117. ? label
  118. : `<img src='/images/icons/${mode}.png' width='16px' class='m-r-5'></img> ${label}`;
  119. optionElems.push(
  120. <option key={mode} value={mode} data-content={dataContent}>{label}</option>
  121. );
  122. }
  123. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  124. return (
  125. <FormGroup controlId="formControlsSelect">
  126. <ControlLabel>Keymap:</ControlLabel>
  127. <FormControl componentClass="select" placeholder="select" bsClass={bsClassName} className="btn-group-sm selectpicker"
  128. onChange={this.onChangeKeymapMode}
  129. inputRef={ el => this.keymapModeSelectorInputEl=el }>
  130. {optionElems}
  131. </FormControl>
  132. </FormGroup>
  133. )
  134. }
  135. renderConfigurationDropdown() {
  136. return (
  137. <FormGroup controlId="formControlsSelect">
  138. <Dropdown dropup id="configurationDropdown" className="configuration-dropdown"
  139. open={this.state.isCddMenuOpened} onToggle={this.onToggleConfigurationDropdown}>
  140. <Dropdown.Toggle bsSize="sm">
  141. <i className="icon-settings"></i>
  142. </Dropdown.Toggle>
  143. <Dropdown.Menu>
  144. {this.renderActiveLineMenuItem()}
  145. {this.renderRealtimeMathJaxMenuItem()}
  146. {/* <MenuItem divider /> */}
  147. </Dropdown.Menu>
  148. </Dropdown>
  149. </FormGroup>
  150. )
  151. }
  152. renderActiveLineMenuItem() {
  153. const isActive = this.state.editorOptions.styleActiveLine;
  154. const iconClasses = ['text-info']
  155. if (isActive) {
  156. iconClasses.push('ti-check')
  157. }
  158. const iconClassName = iconClasses.join(' ');
  159. return (
  160. <MenuItem onClick={this.onClickStyleActiveLine}>
  161. <span className="icon-container"></span>
  162. <span className="menuitem-label">Show active line</span>
  163. <span className="icon-container"><i className={iconClassName}></i></span>
  164. </MenuItem>
  165. )
  166. }
  167. renderRealtimeMathJaxMenuItem() {
  168. if (!this.state.isMathJaxEnabled) {
  169. return;
  170. }
  171. const isEnabled = this.state.isMathJaxEnabled;
  172. const isActive = isEnabled && this.state.previewOptions.renderMathJaxInRealtime;
  173. const iconClasses = ['text-info']
  174. if (isActive) {
  175. iconClasses.push('ti-check')
  176. }
  177. const iconClassName = iconClasses.join(' ');
  178. return (
  179. <MenuItem onClick={this.onClickRenderMathJaxInRealtime}>
  180. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px"></img></span>
  181. <span className="menuitem-label">MathJax Rendering</span>
  182. <i className={iconClassName}></i>
  183. </MenuItem>
  184. )
  185. }
  186. render() {
  187. return <span>
  188. <span className="m-l-5">{this.renderThemeSelector()}</span>
  189. <span className="m-l-5">{this.renderKeymapModeSelector()}</span>
  190. <span className="m-l-5">{this.renderConfigurationDropdown()}</span>
  191. </span>
  192. }
  193. }
  194. export class EditorOptions {
  195. constructor(props) {
  196. this.theme = 'elegant';
  197. this.keymapMode = 'default';
  198. this.styleActiveLine = false;
  199. Object.assign(this, props);
  200. }
  201. }
  202. export class PreviewOptions {
  203. constructor(props) {
  204. this.renderMathJaxInRealtime = false;
  205. Object.assign(this, props);
  206. }
  207. }
  208. OptionsSelector.propTypes = {
  209. crowi: PropTypes.object.isRequired,
  210. editorOptions: PropTypes.instanceOf(EditorOptions),
  211. previewOptions: PropTypes.instanceOf(PreviewOptions),
  212. onChange: PropTypes.func,
  213. };