OptionsSelector.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. 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. if (this.state.isSkipAskingAgainChecked) {
  117. this.updateIsTextlintEnabledToDB(newVal);
  118. }
  119. }
  120. switchTextlintEnabledHandler() {
  121. const { editorContainer } = this.props;
  122. if (editorContainer.state.isTextlintEnabled === null) {
  123. this.setState({ isDownloadDictModalShown: true });
  124. return;
  125. }
  126. this.toggleTextlint();
  127. }
  128. confirmEnableTextlintHandler(isSkipAskingAgainChecked) {
  129. this.setState(
  130. { isSkipAskingAgainChecked, isDownloadDictModalShown: false },
  131. () => this.toggleTextlint(),
  132. );
  133. }
  134. onToggleConfigurationDropdown(newValue) {
  135. this.setState({ isCddMenuOpened: !this.state.isCddMenuOpened });
  136. }
  137. onChangeIndentSize(newValue) {
  138. const { editorContainer } = this.props;
  139. editorContainer.setState({ indentSize: newValue });
  140. }
  141. renderThemeSelector() {
  142. const { editorContainer } = this.props;
  143. const selectedTheme = editorContainer.state.editorOptions.theme;
  144. const menuItems = this.availableThemes.map((theme) => {
  145. return <button key={theme} className="dropdown-item" type="button" onClick={() => this.onChangeTheme(theme)}>{theme}</button>;
  146. });
  147. return (
  148. <div className="input-group flex-nowrap">
  149. <div className="input-group-prepend">
  150. <span className="input-group-text" id="igt-theme">Theme</span>
  151. </div>
  152. <div className="input-group-append dropup">
  153. <button
  154. type="button"
  155. className="btn btn-outline-secondary dropdown-toggle"
  156. data-toggle="dropdown"
  157. aria-haspopup="true"
  158. aria-expanded="false"
  159. aria-describedby="igt-theme"
  160. >
  161. {selectedTheme}
  162. </button>
  163. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  164. {menuItems}
  165. </div>
  166. </div>
  167. </div>
  168. );
  169. }
  170. renderKeymapModeSelector() {
  171. const { editorContainer } = this.props;
  172. const selectedKeymapMode = editorContainer.state.editorOptions.keymapMode;
  173. const menuItems = Object.keys(this.keymapModes).map((mode) => {
  174. const label = this.keymapModes[mode];
  175. const icon = (mode !== 'default')
  176. ? <img src={`/images/icons/${mode}.png`} width="16px" className="mr-2"></img>
  177. : null;
  178. return <button key={mode} className="dropdown-item" type="button" onClick={() => this.onChangeKeymapMode(mode)}>{icon}{label}</button>;
  179. });
  180. return (
  181. <div className="input-group flex-nowrap">
  182. <div className="input-group-prepend">
  183. <span className="input-group-text" id="igt-keymap">Keymap</span>
  184. </div>
  185. <div className="input-group-append dropup">
  186. <button
  187. type="button"
  188. className="btn btn-outline-secondary dropdown-toggle"
  189. data-toggle="dropdown"
  190. aria-haspopup="true"
  191. aria-expanded="false"
  192. aria-describedby="igt-keymap"
  193. >
  194. {selectedKeymapMode}
  195. </button>
  196. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  197. {menuItems}
  198. </div>
  199. </div>
  200. </div>
  201. );
  202. }
  203. renderConfigurationDropdown() {
  204. return (
  205. <div className="my-0 form-group">
  206. <Dropdown
  207. direction="up"
  208. className="grw-editor-configuration-dropdown"
  209. isOpen={this.state.isCddMenuOpened}
  210. toggle={this.onToggleConfigurationDropdown}
  211. >
  212. <DropdownToggle color="outline-secondary" caret>
  213. <i className="icon-settings"></i>
  214. </DropdownToggle>
  215. <DropdownMenu>
  216. {this.renderActiveLineMenuItem()}
  217. {this.renderRealtimeMathJaxMenuItem()}
  218. {this.renderRealtimeDrawioMenuItem()}
  219. {this.renderMarkdownTableAutoFormattingMenuItem()}
  220. {this.renderIsTextlintEnabledMenuItem()}
  221. {/* <DropdownItem divider /> */}
  222. </DropdownMenu>
  223. </Dropdown>
  224. </div>
  225. );
  226. }
  227. renderActiveLineMenuItem() {
  228. const { t, editorContainer } = this.props;
  229. const isActive = editorContainer.state.editorOptions.styleActiveLine;
  230. const iconClasses = ['text-info'];
  231. if (isActive) {
  232. iconClasses.push('ti-check');
  233. }
  234. const iconClassName = iconClasses.join(' ');
  235. return (
  236. <DropdownItem toggle={false} onClick={this.onClickStyleActiveLine}>
  237. <div className="d-flex justify-content-between">
  238. <span className="icon-container"></span>
  239. <span className="menuitem-label">{ t('page_edit.Show active line') }</span>
  240. <span className="icon-container"><i className={iconClassName}></i></span>
  241. </div>
  242. </DropdownItem>
  243. );
  244. }
  245. renderRealtimeMathJaxMenuItem() {
  246. if (!this.state.isMathJaxEnabled) {
  247. return;
  248. }
  249. const { editorContainer } = this.props;
  250. const isEnabled = this.state.isMathJaxEnabled;
  251. const isActive = isEnabled && editorContainer.state.previewOptions.renderMathJaxInRealtime;
  252. const iconClasses = ['text-info'];
  253. if (isActive) {
  254. iconClasses.push('ti-check');
  255. }
  256. const iconClassName = iconClasses.join(' ');
  257. return (
  258. <DropdownItem toggle={false} onClick={this.onClickRenderMathJaxInRealtime}>
  259. <div className="d-flex justify-content-between">
  260. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px" alt="fx"></img></span>
  261. <span className="menuitem-label">MathJax Rendering</span>
  262. <span className="icon-container"><i className={iconClassName}></i></span>
  263. </div>
  264. </DropdownItem>
  265. );
  266. }
  267. renderRealtimeDrawioMenuItem() {
  268. const { editorContainer } = this.props;
  269. const isActive = editorContainer.state.previewOptions.renderDrawioInRealtime;
  270. const iconClasses = ['text-info'];
  271. if (isActive) {
  272. iconClasses.push('ti-check');
  273. }
  274. const iconClassName = iconClasses.join(' ');
  275. return (
  276. <DropdownItem toggle={false} onClick={this.onClickRenderDrawioInRealtime}>
  277. <div className="d-flex justify-content-between">
  278. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px" alt="fx"></img></span>
  279. <span className="menuitem-label">draw.io Rendering</span>
  280. <span className="icon-container"><i className={iconClassName}></i></span>
  281. </div>
  282. </DropdownItem>
  283. );
  284. }
  285. renderMarkdownTableAutoFormattingMenuItem() {
  286. const { t, editorContainer } = this.props;
  287. // Auto-formatting was enabled before optionalizing, so we made it a disabled option(ignoreMarkdownTableAutoFormatting).
  288. const isActive = !editorContainer.state.editorOptions.ignoreMarkdownTableAutoFormatting;
  289. const iconClasses = ['text-info'];
  290. if (isActive) {
  291. iconClasses.push('ti-check');
  292. }
  293. const iconClassName = iconClasses.join(' ');
  294. return (
  295. <DropdownItem toggle={false} onClick={this.onClickMarkdownTableAutoFormatting}>
  296. <div className="d-flex justify-content-between">
  297. <span className="icon-container"></span>
  298. <span className="menuitem-label">{ t('page_edit.auto_format_table') }</span>
  299. <span className="icon-container"><i className={iconClassName}></i></span>
  300. </div>
  301. </DropdownItem>
  302. );
  303. }
  304. renderIsTextlintEnabledMenuItem() {
  305. const isActive = this.props.editorContainer.state.isTextlintEnabled;
  306. const iconClasses = ['text-info'];
  307. if (isActive) {
  308. iconClasses.push('ti-check');
  309. }
  310. const iconClassName = iconClasses.join(' ');
  311. return (
  312. <DropdownItem toggle={false} onClick={this.switchTextlintEnabledHandler}>
  313. <div className="d-flex justify-content-between">
  314. <span className="icon-container"></span>
  315. <span className="menuitem-label">Textlint</span>
  316. <span className="icon-container"><i className={iconClassName}></i></span>
  317. </div>
  318. </DropdownItem>
  319. );
  320. }
  321. renderIndentSizeSelector() {
  322. const { appContainer, editorContainer } = this.props;
  323. const menuItems = this.typicalIndentSizes.map((indent) => {
  324. return <button key={indent} className="dropdown-item" type="button" onClick={() => this.onChangeIndentSize(indent)}>{indent}</button>;
  325. });
  326. return (
  327. <div className="input-group flex-nowrap">
  328. <div className="input-group-prepend">
  329. <span className="input-group-text" id="igt-indent">Indent</span>
  330. </div>
  331. <div className="input-group-append dropup">
  332. <button
  333. type="button"
  334. className="btn btn-outline-secondary dropdown-toggle"
  335. data-toggle="dropdown"
  336. aria-haspopup="true"
  337. aria-expanded="false"
  338. aria-describedby="igt-indent"
  339. disabled={appContainer.config.isIndentSizeForced}
  340. >
  341. {editorContainer.state.indentSize}
  342. </button>
  343. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  344. {menuItems}
  345. </div>
  346. </div>
  347. </div>
  348. );
  349. }
  350. render() {
  351. return (
  352. <>
  353. <div className="d-flex flex-row">
  354. <span>{this.renderThemeSelector()}</span>
  355. <span className="d-none d-sm-block ml-2 ml-sm-4">{this.renderKeymapModeSelector()}</span>
  356. <span className="ml-2 ml-sm-4">{this.renderIndentSizeSelector()}</span>
  357. <span className="ml-2 ml-sm-4">{this.renderConfigurationDropdown()}</span>
  358. </div>
  359. {!this.state.isSkipAskingAgainChecked && (
  360. <DownloadDictModal
  361. isModalOpen={this.state.isDownloadDictModalShown}
  362. onConfirmEnableTextlint={this.confirmEnableTextlintHandler}
  363. onCancel={() => this.setState({ isDownloadDictModalShown: false })}
  364. />
  365. )}
  366. </>
  367. );
  368. }
  369. }
  370. /**
  371. * Wrapper component for using unstated
  372. */
  373. const OptionsSelectorWrapper = withUnstatedContainers(OptionsSelector, [AppContainer, EditorContainer]);
  374. OptionsSelector.propTypes = {
  375. t: PropTypes.func.isRequired, // i18next
  376. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  377. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  378. };
  379. export default withTranslation()(OptionsSelectorWrapper);