OptionsSelector.jsx 13 KB

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