OptionsSelector.jsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
  6. } from 'reactstrap';
  7. import { createSubscribedElement } from '../UnstatedUtils';
  8. import EditorContainer from '../../services/EditorContainer';
  9. export const defaultEditorOptions = {
  10. theme: 'elegant',
  11. keymapMode: 'default',
  12. styleActiveLine: false,
  13. };
  14. export const defaultPreviewOptions = {
  15. renderMathJaxInRealtime: false,
  16. };
  17. class OptionsSelector extends React.Component {
  18. constructor(props) {
  19. super(props);
  20. const config = this.props.crowi.getConfig();
  21. const isMathJaxEnabled = !!config.env.MATHJAX;
  22. this.state = {
  23. isCddMenuOpened: false,
  24. isMathJaxEnabled,
  25. };
  26. this.availableThemes = [
  27. 'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', 'monokai', 'twilight',
  28. ];
  29. this.keymapModes = {
  30. default: 'Default',
  31. vim: 'Vim',
  32. emacs: 'Emacs',
  33. sublime: 'Sublime Text',
  34. };
  35. this.onChangeTheme = this.onChangeTheme.bind(this);
  36. this.onChangeKeymapMode = this.onChangeKeymapMode.bind(this);
  37. this.onClickStyleActiveLine = this.onClickStyleActiveLine.bind(this);
  38. this.onClickRenderMathJaxInRealtime = this.onClickRenderMathJaxInRealtime.bind(this);
  39. this.onToggleConfigurationDropdown = this.onToggleConfigurationDropdown.bind(this);
  40. }
  41. onChangeTheme(newValue) {
  42. const { editorContainer } = this.props;
  43. const newOpts = Object.assign(editorContainer.state.editorOptions, { theme: newValue });
  44. editorContainer.setState({ editorOptions: newOpts });
  45. // save to localStorage
  46. editorContainer.saveOptsToLocalStorage();
  47. }
  48. onChangeKeymapMode(newValue) {
  49. const { editorContainer } = this.props;
  50. const newOpts = Object.assign(editorContainer.state.editorOptions, { keymapMode: newValue });
  51. editorContainer.setState({ editorOptions: newOpts });
  52. // save to localStorage
  53. editorContainer.saveOptsToLocalStorage();
  54. }
  55. onClickStyleActiveLine(event) {
  56. const { editorContainer } = this.props;
  57. // keep dropdown opened
  58. this._cddForceOpen = true;
  59. const newValue = !editorContainer.state.editorOptions.styleActiveLine;
  60. const newOpts = Object.assign(editorContainer.state.editorOptions, { styleActiveLine: newValue });
  61. editorContainer.setState({ editorOptions: newOpts });
  62. // save to localStorage
  63. editorContainer.saveOptsToLocalStorage();
  64. }
  65. onClickRenderMathJaxInRealtime(event) {
  66. const { editorContainer } = this.props;
  67. const newValue = !editorContainer.state.previewOptions.renderMathJaxInRealtime;
  68. const newOpts = Object.assign(editorContainer.state.previewOptions, { renderMathJaxInRealtime: newValue });
  69. editorContainer.setState({ previewOptions: newOpts });
  70. // save to localStorage
  71. editorContainer.saveOptsToLocalStorage();
  72. }
  73. onToggleConfigurationDropdown(newValue) {
  74. this.setState({ isCddMenuOpened: !this.state.isCddMenuOpened });
  75. }
  76. renderThemeSelector() {
  77. const { editorContainer } = this.props;
  78. const selectedTheme = editorContainer.state.editorOptions.theme;
  79. const menuItems = this.availableThemes.map((theme) => {
  80. return <button key={theme} className="dropdown-item" type="button" onClick={() => this.onChangeTheme(theme)}>{theme}</button>;
  81. });
  82. return (
  83. <div className="my-0 form-group">
  84. <label>Theme:</label>
  85. <div className="btn-group btn-group-sm dropup">
  86. <button className="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  87. {selectedTheme}
  88. </button>
  89. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  90. {menuItems}
  91. </div>
  92. </div>
  93. </div>
  94. );
  95. }
  96. renderKeymapModeSelector() {
  97. const { editorContainer } = this.props;
  98. const selectedKeymapMode = editorContainer.state.editorOptions.keymapMode;
  99. const menuItems = Object.keys(this.keymapModes).map((mode) => {
  100. const label = this.keymapModes[mode];
  101. const icon = (mode !== 'default')
  102. ? <img src={`/images/icons/${mode}.png`} width="16px" className="mr-2"></img>
  103. : null;
  104. return <button key={mode} className="dropdown-item" type="button" onClick={() => this.onChangeKeymapMode(mode)}>{icon}{label}</button>;
  105. });
  106. return (
  107. <div className="my-0 form-group">
  108. <label>Keymap:</label>
  109. <div className="btn-group btn-group-sm dropup">
  110. <button className="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  111. {selectedKeymapMode}
  112. </button>
  113. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  114. {menuItems}
  115. </div>
  116. </div>
  117. </div>
  118. );
  119. }
  120. renderConfigurationDropdown() {
  121. return (
  122. <div className="my-0 form-group">
  123. <Dropdown
  124. direction="up"
  125. size="sm"
  126. className="grw-editor-configuration-dropdown"
  127. isOpen={this.state.isCddMenuOpened}
  128. toggle={this.onToggleConfigurationDropdown}
  129. >
  130. <DropdownToggle color="light" caret>
  131. <i className="icon-settings"></i>
  132. </DropdownToggle>
  133. <DropdownMenu>
  134. {this.renderActiveLineMenuItem()}
  135. {this.renderRealtimeMathJaxMenuItem()}
  136. {/* <DropdownItem divider /> */}
  137. </DropdownMenu>
  138. </Dropdown>
  139. </div>
  140. );
  141. }
  142. renderActiveLineMenuItem() {
  143. const { t, editorContainer } = this.props;
  144. const isActive = editorContainer.state.editorOptions.styleActiveLine;
  145. const iconClasses = ['text-info'];
  146. if (isActive) {
  147. iconClasses.push('ti-check');
  148. }
  149. const iconClassName = iconClasses.join(' ');
  150. return (
  151. <DropdownItem toggle={false} onClick={this.onClickStyleActiveLine}>
  152. <span className="icon-container"></span>
  153. <span className="menuitem-label">{ t('page_edit.Show active line') }</span>
  154. <span className="icon-container"><i className={iconClassName}></i></span>
  155. </DropdownItem>
  156. );
  157. }
  158. renderRealtimeMathJaxMenuItem() {
  159. if (!this.state.isMathJaxEnabled) {
  160. return;
  161. }
  162. const { editorContainer } = this.props;
  163. const isEnabled = this.state.isMathJaxEnabled;
  164. const isActive = isEnabled && editorContainer.state.previewOptions.renderMathJaxInRealtime;
  165. const iconClasses = ['text-info'];
  166. if (isActive) {
  167. iconClasses.push('ti-check');
  168. }
  169. const iconClassName = iconClasses.join(' ');
  170. return (
  171. <DropdownItem toggle={false} onClick={this.onClickRenderMathJaxInRealtime}>
  172. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px" alt="fx"></img></span>
  173. <span className="menuitem-label">MathJax Rendering</span>
  174. <i className={iconClassName}></i>
  175. </DropdownItem>
  176. );
  177. }
  178. render() {
  179. return (
  180. <div className="d-flex flex-row">
  181. <span className="ml-2">{this.renderThemeSelector()}</span>
  182. <span className="ml-2">{this.renderKeymapModeSelector()}</span>
  183. <span className="ml-2">{this.renderConfigurationDropdown()}</span>
  184. </div>
  185. );
  186. }
  187. }
  188. /**
  189. * Wrapper component for using unstated
  190. */
  191. const OptionsSelectorWrapper = (props) => {
  192. return createSubscribedElement(OptionsSelector, props, [EditorContainer]);
  193. };
  194. OptionsSelector.propTypes = {
  195. t: PropTypes.func.isRequired, // i18next
  196. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  197. crowi: PropTypes.object.isRequired,
  198. };
  199. export default withTranslation()(OptionsSelectorWrapper);