OptionsSelector.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 '~/client/services/AppContainer';
  9. import EditorContainer from '~/client/services/EditorContainer';
  10. import { toastError } from '~/client/util/apiNotification';
  11. export const defaultEditorOptions = {
  12. theme: 'elegant',
  13. keymapMode: 'default',
  14. styleActiveLine: false,
  15. };
  16. export const defaultPreviewOptions = {
  17. renderMathJaxInRealtime: false,
  18. };
  19. class OptionsSelector extends React.Component {
  20. constructor(props) {
  21. super(props);
  22. const config = this.props.appContainer.getConfig();
  23. const isMathJaxEnabled = !!config.env.MATHJAX;
  24. this.state = {
  25. isCddMenuOpened: false,
  26. isMathJaxEnabled,
  27. isTextlintEnabled: false,
  28. };
  29. this.availableThemes = [
  30. 'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', 'monokai', 'twilight',
  31. ];
  32. this.keymapModes = {
  33. default: 'Default',
  34. vim: 'Vim',
  35. emacs: 'Emacs',
  36. sublime: 'Sublime Text',
  37. };
  38. this.typicalIndentSizes = [2, 4];
  39. this.onChangeTheme = this.onChangeTheme.bind(this);
  40. this.onChangeKeymapMode = this.onChangeKeymapMode.bind(this);
  41. this.onClickStyleActiveLine = this.onClickStyleActiveLine.bind(this);
  42. this.onClickRenderMathJaxInRealtime = this.onClickRenderMathJaxInRealtime.bind(this);
  43. this.onClickMarkdownTableAutoFormatting = this.onClickMarkdownTableAutoFormatting.bind(this);
  44. this.switchTextlintEnabledHandler = this.switchTextlintEnabledHandler.bind(this);
  45. this.updateIsTextlintEnabledToDB = this.updateIsTextlintEnabledToDB.bind(this);
  46. this.onToggleConfigurationDropdown = this.onToggleConfigurationDropdown.bind(this);
  47. this.onChangeIndentSize = this.onChangeIndentSize.bind(this);
  48. }
  49. async componentDidMount() {
  50. const { editorContainer } = this.props;
  51. const isTextlintEnabled = await editorContainer.retrieveEditorSettings();
  52. this.setState({ isTextlintEnabled });
  53. }
  54. onChangeTheme(newValue) {
  55. const { editorContainer } = this.props;
  56. const newOpts = Object.assign(editorContainer.state.editorOptions, { theme: newValue });
  57. editorContainer.setState({ editorOptions: newOpts });
  58. // save to localStorage
  59. editorContainer.saveOptsToLocalStorage();
  60. }
  61. onChangeKeymapMode(newValue) {
  62. const { editorContainer } = this.props;
  63. const newOpts = Object.assign(editorContainer.state.editorOptions, { keymapMode: newValue });
  64. editorContainer.setState({ editorOptions: newOpts });
  65. // save to localStorage
  66. editorContainer.saveOptsToLocalStorage();
  67. }
  68. onClickStyleActiveLine(event) {
  69. const { editorContainer } = this.props;
  70. // keep dropdown opened
  71. this._cddForceOpen = true;
  72. const newValue = !editorContainer.state.editorOptions.styleActiveLine;
  73. const newOpts = Object.assign(editorContainer.state.editorOptions, { styleActiveLine: newValue });
  74. editorContainer.setState({ editorOptions: newOpts });
  75. // save to localStorage
  76. editorContainer.saveOptsToLocalStorage();
  77. }
  78. onClickRenderMathJaxInRealtime(event) {
  79. const { editorContainer } = this.props;
  80. const newValue = !editorContainer.state.previewOptions.renderMathJaxInRealtime;
  81. const newOpts = Object.assign(editorContainer.state.previewOptions, { renderMathJaxInRealtime: newValue });
  82. editorContainer.setState({ previewOptions: newOpts });
  83. // save to localStorage
  84. editorContainer.saveOptsToLocalStorage();
  85. }
  86. onClickMarkdownTableAutoFormatting(event) {
  87. const { editorContainer } = this.props;
  88. const newValue = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
  89. const newOpts = Object.assign(editorContainer.state.editorOptions, { ignoreMarkdownTableAutoFormatting: newValue });
  90. editorContainer.setState({ editorOptions: newOpts });
  91. // save to localStorage
  92. editorContainer.saveOptsToLocalStorage();
  93. }
  94. async updateIsTextlintEnabledToDB(newVal) {
  95. const { appContainer } = this.props;
  96. try {
  97. await appContainer.apiv3Put('/personal-setting/editor-settings', { isTextlintEnabled: newVal });
  98. }
  99. catch (err) {
  100. toastError(err);
  101. }
  102. }
  103. async switchTextlintEnabledHandler() {
  104. const { editorContainer } = this.props;
  105. const newVal = !this.state.isTextlintEnabled;
  106. this.setState({ isTextlintEnabled: newVal });
  107. editorContainer.setState({ isTextlintEnabled: newVal });
  108. this.updateIsTextlintEnabledToDB(newVal);
  109. }
  110. onToggleConfigurationDropdown(newValue) {
  111. this.setState({ isCddMenuOpened: !this.state.isCddMenuOpened });
  112. }
  113. onChangeIndentSize(newValue) {
  114. const { editorContainer } = this.props;
  115. editorContainer.setState({ indentSize: newValue });
  116. }
  117. renderThemeSelector() {
  118. const { editorContainer } = this.props;
  119. const selectedTheme = editorContainer.state.editorOptions.theme;
  120. const menuItems = this.availableThemes.map((theme) => {
  121. return <button key={theme} className="dropdown-item" type="button" onClick={() => this.onChangeTheme(theme)}>{theme}</button>;
  122. });
  123. return (
  124. <div className="input-group flex-nowrap">
  125. <div className="input-group-prepend">
  126. <span className="input-group-text" id="igt-theme">Theme</span>
  127. </div>
  128. <div className="input-group-append dropup">
  129. <button
  130. type="button"
  131. className="btn btn-outline-secondary dropdown-toggle"
  132. data-toggle="dropdown"
  133. aria-haspopup="true"
  134. aria-expanded="false"
  135. aria-describedby="igt-theme"
  136. >
  137. {selectedTheme}
  138. </button>
  139. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  140. {menuItems}
  141. </div>
  142. </div>
  143. </div>
  144. );
  145. }
  146. renderKeymapModeSelector() {
  147. const { editorContainer } = this.props;
  148. const selectedKeymapMode = editorContainer.state.editorOptions.keymapMode;
  149. const menuItems = Object.keys(this.keymapModes).map((mode) => {
  150. const label = this.keymapModes[mode];
  151. const icon = (mode !== 'default')
  152. ? <img src={`/images/icons/${mode}.png`} width="16px" className="mr-2"></img>
  153. : null;
  154. return <button key={mode} className="dropdown-item" type="button" onClick={() => this.onChangeKeymapMode(mode)}>{icon}{label}</button>;
  155. });
  156. return (
  157. <div className="input-group flex-nowrap">
  158. <div className="input-group-prepend">
  159. <span className="input-group-text" id="igt-keymap">Keymap</span>
  160. </div>
  161. <div className="input-group-append dropup">
  162. <button
  163. type="button"
  164. className="btn btn-outline-secondary dropdown-toggle"
  165. data-toggle="dropdown"
  166. aria-haspopup="true"
  167. aria-expanded="false"
  168. aria-describedby="igt-keymap"
  169. >
  170. {selectedKeymapMode}
  171. </button>
  172. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  173. {menuItems}
  174. </div>
  175. </div>
  176. </div>
  177. );
  178. }
  179. renderConfigurationDropdown() {
  180. return (
  181. <div className="my-0 form-group">
  182. <Dropdown
  183. direction="up"
  184. className="grw-editor-configuration-dropdown"
  185. isOpen={this.state.isCddMenuOpened}
  186. toggle={this.onToggleConfigurationDropdown}
  187. >
  188. <DropdownToggle color="outline-secondary" caret>
  189. <i className="icon-settings"></i>
  190. </DropdownToggle>
  191. <DropdownMenu>
  192. {this.renderActiveLineMenuItem()}
  193. {this.renderRealtimeMathJaxMenuItem()}
  194. {this.renderMarkdownTableAutoFormattingMenuItem()}
  195. {this.renderIsTextlintEnabledMenuItem()}
  196. {/* <DropdownItem divider /> */}
  197. </DropdownMenu>
  198. </Dropdown>
  199. </div>
  200. );
  201. }
  202. renderActiveLineMenuItem() {
  203. const { t, editorContainer } = this.props;
  204. const isActive = editorContainer.state.editorOptions.styleActiveLine;
  205. const iconClasses = ['text-info'];
  206. if (isActive) {
  207. iconClasses.push('ti-check');
  208. }
  209. const iconClassName = iconClasses.join(' ');
  210. return (
  211. <DropdownItem toggle={false} onClick={this.onClickStyleActiveLine}>
  212. <div className="d-flex justify-content-between">
  213. <span className="icon-container"></span>
  214. <span className="menuitem-label">{ t('page_edit.Show active line') }</span>
  215. <span className="icon-container"><i className={iconClassName}></i></span>
  216. </div>
  217. </DropdownItem>
  218. );
  219. }
  220. renderRealtimeMathJaxMenuItem() {
  221. if (!this.state.isMathJaxEnabled) {
  222. return;
  223. }
  224. const { editorContainer } = this.props;
  225. const isEnabled = this.state.isMathJaxEnabled;
  226. const isActive = isEnabled && editorContainer.state.previewOptions.renderMathJaxInRealtime;
  227. const iconClasses = ['text-info'];
  228. if (isActive) {
  229. iconClasses.push('ti-check');
  230. }
  231. const iconClassName = iconClasses.join(' ');
  232. return (
  233. <DropdownItem toggle={false} onClick={this.onClickRenderMathJaxInRealtime}>
  234. <div className="d-flex justify-content-between">
  235. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px" alt="fx"></img></span>
  236. <span className="menuitem-label">MathJax Rendering</span>
  237. <span className="icon-container"><i className={iconClassName}></i></span>
  238. </div>
  239. </DropdownItem>
  240. );
  241. }
  242. renderMarkdownTableAutoFormattingMenuItem() {
  243. const { t, editorContainer } = this.props;
  244. // Auto-formatting was enabled before optionalizing, so we made it a disabled option(ignoreMarkdownTableAutoFormatting).
  245. const isActive = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
  246. const iconClasses = ['text-info'];
  247. if (isActive) {
  248. iconClasses.push('ti-check');
  249. }
  250. const iconClassName = iconClasses.join(' ');
  251. return (
  252. <DropdownItem toggle={false} onClick={this.onClickMarkdownTableAutoFormatting}>
  253. <div className="d-flex justify-content-between">
  254. <span className="icon-container"></span>
  255. <span className="menuitem-label">{ t('page_edit.auto_format_table') }</span>
  256. <span className="icon-container"><i className={iconClassName}></i></span>
  257. </div>
  258. </DropdownItem>
  259. );
  260. }
  261. renderIsTextlintEnabledMenuItem() {
  262. const isActive = this.state.isTextlintEnabled;
  263. const iconClasses = ['text-info'];
  264. if (isActive) {
  265. iconClasses.push('ti-check');
  266. }
  267. const iconClassName = iconClasses.join(' ');
  268. return (
  269. // eslint-disable-next-line no-console
  270. <DropdownItem toggle={false} onClick={this.switchTextlintEnabledHandler}>
  271. <div className="d-flex justify-content-between">
  272. <span className="icon-container"></span>
  273. <span className="menuitem-label">Textlint</span>
  274. <span className="icon-container"><i className={iconClassName}></i></span>
  275. </div>
  276. </DropdownItem>
  277. );
  278. }
  279. renderIndentSizeSelector() {
  280. const { appContainer, editorContainer } = this.props;
  281. const menuItems = this.typicalIndentSizes.map((indent) => {
  282. return <button key={indent} className="dropdown-item" type="button" onClick={() => this.onChangeIndentSize(indent)}>{indent}</button>;
  283. });
  284. return (
  285. <div className="input-group flex-nowrap">
  286. <div className="input-group-prepend">
  287. <span className="input-group-text" id="igt-indent">Indent</span>
  288. </div>
  289. <div className="input-group-append dropup">
  290. <button
  291. type="button"
  292. className="btn btn-outline-secondary dropdown-toggle"
  293. data-toggle="dropdown"
  294. aria-haspopup="true"
  295. aria-expanded="false"
  296. aria-describedby="igt-indent"
  297. disabled={appContainer.config.isIndentSizeForced}
  298. >
  299. {editorContainer.state.indentSize}
  300. </button>
  301. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  302. {menuItems}
  303. </div>
  304. </div>
  305. </div>
  306. );
  307. }
  308. render() {
  309. return (
  310. <div className="d-flex flex-row">
  311. <span>{this.renderThemeSelector()}</span>
  312. <span className="d-none d-sm-block ml-2 ml-sm-4">{this.renderKeymapModeSelector()}</span>
  313. <span className="ml-2 ml-sm-4">{this.renderIndentSizeSelector()}</span>
  314. <span className="ml-2 ml-sm-4">{this.renderConfigurationDropdown()}</span>
  315. </div>
  316. );
  317. }
  318. }
  319. /**
  320. * Wrapper component for using unstated
  321. */
  322. const OptionsSelectorWrapper = withUnstatedContainers(OptionsSelector, [AppContainer, EditorContainer]);
  323. OptionsSelector.propTypes = {
  324. t: PropTypes.func.isRequired, // i18next
  325. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  326. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  327. };
  328. export default withTranslation()(OptionsSelectorWrapper);