OptionsSelector.jsx 15 KB

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