OptionsSelector.jsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.onClickMarkdownTableAutoFormatting = this.onClickMarkdownTableAutoFormatting.bind(this);
  41. this.onToggleConfigurationDropdown = this.onToggleConfigurationDropdown.bind(this);
  42. }
  43. onChangeTheme(newValue) {
  44. const { editorContainer } = this.props;
  45. const newOpts = Object.assign(editorContainer.state.editorOptions, { theme: newValue });
  46. editorContainer.setState({ editorOptions: newOpts });
  47. // save to localStorage
  48. editorContainer.saveOptsToLocalStorage();
  49. }
  50. onChangeKeymapMode(newValue) {
  51. const { editorContainer } = this.props;
  52. const newOpts = Object.assign(editorContainer.state.editorOptions, { keymapMode: newValue });
  53. editorContainer.setState({ editorOptions: newOpts });
  54. // save to localStorage
  55. editorContainer.saveOptsToLocalStorage();
  56. }
  57. onClickStyleActiveLine(event) {
  58. const { editorContainer } = this.props;
  59. // keep dropdown opened
  60. this._cddForceOpen = true;
  61. const newValue = !editorContainer.state.editorOptions.styleActiveLine;
  62. const newOpts = Object.assign(editorContainer.state.editorOptions, { styleActiveLine: newValue });
  63. editorContainer.setState({ editorOptions: newOpts });
  64. // save to localStorage
  65. editorContainer.saveOptsToLocalStorage();
  66. }
  67. onClickRenderMathJaxInRealtime(event) {
  68. const { editorContainer } = this.props;
  69. const newValue = !editorContainer.state.previewOptions.renderMathJaxInRealtime;
  70. const newOpts = Object.assign(editorContainer.state.previewOptions, { renderMathJaxInRealtime: newValue });
  71. editorContainer.setState({ previewOptions: newOpts });
  72. // save to localStorage
  73. editorContainer.saveOptsToLocalStorage();
  74. }
  75. onClickMarkdownTableAutoFormatting(event) {
  76. const { editorContainer } = this.props;
  77. const newValue = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
  78. const newOpts = Object.assign(editorContainer.state.editorOptions, { ignoreMarkdownTableAutoFormatting: newValue });
  79. editorContainer.setState({ editorOptions: newOpts });
  80. // save to localStorage
  81. editorContainer.saveOptsToLocalStorage();
  82. }
  83. onToggleConfigurationDropdown(newValue) {
  84. this.setState({ isCddMenuOpened: !this.state.isCddMenuOpened });
  85. }
  86. renderThemeSelector() {
  87. const { editorContainer } = this.props;
  88. const selectedTheme = editorContainer.state.editorOptions.theme;
  89. const menuItems = this.availableThemes.map((theme) => {
  90. return <button key={theme} className="dropdown-item" type="button" onClick={() => this.onChangeTheme(theme)}>{theme}</button>;
  91. });
  92. return (
  93. <div className="input-group flex-nowrap">
  94. <div className="input-group-prepend">
  95. <span className="input-group-text" id="igt-theme">Theme</span>
  96. </div>
  97. <div className="input-group-append dropup">
  98. <button
  99. type="button"
  100. className="btn btn-outline-secondary dropdown-toggle"
  101. data-toggle="dropdown"
  102. aria-haspopup="true"
  103. aria-expanded="false"
  104. aria-describedby="igt-theme"
  105. >
  106. {selectedTheme}
  107. </button>
  108. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  109. {menuItems}
  110. </div>
  111. </div>
  112. </div>
  113. );
  114. }
  115. renderKeymapModeSelector() {
  116. const { editorContainer } = this.props;
  117. const selectedKeymapMode = editorContainer.state.editorOptions.keymapMode;
  118. const menuItems = Object.keys(this.keymapModes).map((mode) => {
  119. const label = this.keymapModes[mode];
  120. const icon = (mode !== 'default')
  121. ? <img src={`/images/icons/${mode}.png`} width="16px" className="mr-2"></img>
  122. : null;
  123. return <button key={mode} className="dropdown-item" type="button" onClick={() => this.onChangeKeymapMode(mode)}>{icon}{label}</button>;
  124. });
  125. return (
  126. <div className="input-group flex-nowrap">
  127. <div className="input-group-prepend">
  128. <span className="input-group-text" id="igt-keymap">Keymap</span>
  129. </div>
  130. <div className="input-group-append dropup">
  131. <button
  132. type="button"
  133. className="btn btn-outline-secondary dropdown-toggle"
  134. data-toggle="dropdown"
  135. aria-haspopup="true"
  136. aria-expanded="false"
  137. aria-describedby="igt-keymap"
  138. >
  139. {selectedKeymapMode}
  140. </button>
  141. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  142. {menuItems}
  143. </div>
  144. </div>
  145. </div>
  146. );
  147. }
  148. renderConfigurationDropdown() {
  149. return (
  150. <div className="my-0 form-group">
  151. <Dropdown
  152. direction="up"
  153. className="grw-editor-configuration-dropdown"
  154. isOpen={this.state.isCddMenuOpened}
  155. toggle={this.onToggleConfigurationDropdown}
  156. >
  157. <DropdownToggle color="outline-secondary" caret>
  158. <i className="icon-settings"></i>
  159. </DropdownToggle>
  160. <DropdownMenu>
  161. {this.renderActiveLineMenuItem()}
  162. {this.renderRealtimeMathJaxMenuItem()}
  163. {this.renderMarkdownTableAutoFormattingMenuItem()}
  164. {/* <DropdownItem divider /> */}
  165. </DropdownMenu>
  166. </Dropdown>
  167. </div>
  168. );
  169. }
  170. renderActiveLineMenuItem() {
  171. const { t, editorContainer } = this.props;
  172. const isActive = editorContainer.state.editorOptions.styleActiveLine;
  173. const iconClasses = ['text-info'];
  174. if (isActive) {
  175. iconClasses.push('ti-check');
  176. }
  177. const iconClassName = iconClasses.join(' ');
  178. return (
  179. <DropdownItem toggle={false} onClick={this.onClickStyleActiveLine}>
  180. <div className="d-flex justify-content-between">
  181. <span className="icon-container"></span>
  182. <span className="menuitem-label">{ t('page_edit.Show active line') }</span>
  183. <span className="icon-container"><i className={iconClassName}></i></span>
  184. </div>
  185. </DropdownItem>
  186. );
  187. }
  188. renderRealtimeMathJaxMenuItem() {
  189. if (!this.state.isMathJaxEnabled) {
  190. return;
  191. }
  192. const { editorContainer } = this.props;
  193. const isEnabled = this.state.isMathJaxEnabled;
  194. const isActive = isEnabled && editorContainer.state.previewOptions.renderMathJaxInRealtime;
  195. const iconClasses = ['text-info'];
  196. if (isActive) {
  197. iconClasses.push('ti-check');
  198. }
  199. const iconClassName = iconClasses.join(' ');
  200. return (
  201. <DropdownItem toggle={false} onClick={this.onClickRenderMathJaxInRealtime}>
  202. <div className="d-flex justify-content-between">
  203. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px" alt="fx"></img></span>
  204. <span className="menuitem-label">MathJax Rendering</span>
  205. <span className="icon-container"><i className={iconClassName}></i></span>
  206. </div>
  207. </DropdownItem>
  208. );
  209. }
  210. renderMarkdownTableAutoFormattingMenuItem() {
  211. const { t, editorContainer } = this.props;
  212. // Auto-formatting was enabled before optionalizing, so we made it a disabled option(ignoreMarkdownTableAutoFormatting).
  213. const isActive = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
  214. const iconClasses = ['text-info'];
  215. if (isActive) {
  216. iconClasses.push('ti-check');
  217. }
  218. const iconClassName = iconClasses.join(' ');
  219. return (
  220. <DropdownItem toggle={false} onClick={this.onClickMarkdownTableAutoFormatting}>
  221. <div className="d-flex justify-content-between">
  222. <span className="icon-container"></span>
  223. <span className="menuitem-label">{ t('page_edit.auto_format_table') }</span>
  224. <span className="icon-container"><i className={iconClassName}></i></span>
  225. </div>
  226. </DropdownItem>
  227. );
  228. }
  229. render() {
  230. return (
  231. <div className="d-flex flex-row">
  232. <span>{this.renderThemeSelector()}</span>
  233. <span className="ml-2 ml-sm-4">{this.renderKeymapModeSelector()}</span>
  234. <span className="ml-2 ml-sm-4">{this.renderConfigurationDropdown()}</span>
  235. </div>
  236. );
  237. }
  238. }
  239. /**
  240. * Wrapper component for using unstated
  241. */
  242. const OptionsSelectorWrapper = withUnstatedContainers(OptionsSelector, [AppContainer, EditorContainer]);
  243. OptionsSelector.propTypes = {
  244. t: PropTypes.func.isRequired, // i18next
  245. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  246. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  247. };
  248. export default withTranslation()(OptionsSelectorWrapper);