OptionsSelector.jsx 14 KB

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