OptionsSelector.jsx 11 KB

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