OptionsSelector.jsx 8.2 KB

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