OptionsSelector.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import React, {
  2. memo, useCallback, useMemo, useState,
  3. } from 'react';
  4. import { useTranslation } from 'next-i18next';
  5. import {
  6. Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
  7. } from 'reactstrap';
  8. import { useIsIndentSizeForced } from '~/stores/context';
  9. import { useEditorSettings, useIsTextlintEnabled, useCurrentIndentSize } from '~/stores/editor';
  10. import { DEFAULT_THEME, KeyMapMode } from '../../interfaces/editor-settings';
  11. import { DownloadDictModal } from './DownloadDictModal';
  12. const AVAILABLE_THEMES = [
  13. 'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', 'monokai', 'twilight',
  14. ];
  15. const TYPICAL_INDENT_SIZE = [2, 4];
  16. const ThemeSelector = (): JSX.Element => {
  17. const { data: editorSettings, update } = useEditorSettings();
  18. const menuItems = useMemo(() => (
  19. <>
  20. { AVAILABLE_THEMES.map((theme) => {
  21. return <button key={theme} className="dropdown-item" type="button" onClick={() => update({ theme })}>{theme}</button>;
  22. }) }
  23. </>
  24. ), [update]);
  25. const selectedTheme = editorSettings?.theme ?? DEFAULT_THEME;
  26. return (
  27. <div className="input-group flex-nowrap">
  28. <div className="input-group-prepend">
  29. <span className="input-group-text" id="igt-theme">Theme</span>
  30. </div>
  31. <div className="input-group-append dropup">
  32. <button
  33. type="button"
  34. className="btn btn-outline-secondary dropdown-toggle"
  35. data-toggle="dropdown"
  36. aria-haspopup="true"
  37. aria-expanded="false"
  38. aria-describedby="igt-theme"
  39. >
  40. {selectedTheme}
  41. </button>
  42. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  43. {menuItems}
  44. </div>
  45. </div>
  46. </div>
  47. );
  48. };
  49. type KeyMapModeToLabel = {
  50. [key in KeyMapMode]: string;
  51. }
  52. const KEYMAP_LABEL_MAP: KeyMapModeToLabel = {
  53. default: 'Default',
  54. vim: 'Vim',
  55. emacs: 'Emacs',
  56. sublime: 'Sublime Text',
  57. };
  58. const KeymapSelector = memo((): JSX.Element => {
  59. const { data: editorSettings, update } = useEditorSettings();
  60. Object.keys(KEYMAP_LABEL_MAP);
  61. const menuItems = useMemo(() => (
  62. <>
  63. { (Object.keys(KEYMAP_LABEL_MAP) as KeyMapMode[]).map((keymapMode) => {
  64. const keymapLabel = KEYMAP_LABEL_MAP[keymapMode];
  65. const icon = (keymapMode !== 'default')
  66. ? <img src={`/images/icons/${keymapMode}.png`} width="16px" className="mr-2"></img>
  67. : null;
  68. return <button key={keymapMode} className="dropdown-item" type="button" onClick={() => update({ keymapMode })}>{icon}{keymapLabel}</button>;
  69. }) }
  70. </>
  71. ), [update]);
  72. const selectedKeymapMode = editorSettings?.keymapMode ?? 'default';
  73. return (
  74. <div className="input-group flex-nowrap">
  75. <div className="input-group-prepend">
  76. <span className="input-group-text" id="igt-keymap">Keymap</span>
  77. </div>
  78. <div className="input-group-append dropup">
  79. <button
  80. type="button"
  81. className="btn btn-outline-secondary dropdown-toggle"
  82. data-toggle="dropdown"
  83. aria-haspopup="true"
  84. aria-expanded="false"
  85. aria-describedby="igt-keymap"
  86. >
  87. { editorSettings != null && selectedKeymapMode}
  88. </button>
  89. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  90. {menuItems}
  91. </div>
  92. </div>
  93. </div>
  94. );
  95. });
  96. KeymapSelector.displayName = 'KeymapSelector';
  97. type IndentSizeSelectorProps = {
  98. isIndentSizeForced: boolean,
  99. selectedIndentSize: number,
  100. onChange: (indentSize: number) => void,
  101. }
  102. const IndentSizeSelector = memo(({ isIndentSizeForced, selectedIndentSize, onChange }: IndentSizeSelectorProps): JSX.Element => {
  103. const menuItems = useMemo(() => (
  104. <>
  105. { TYPICAL_INDENT_SIZE.map((indent) => {
  106. return <button key={indent} className="dropdown-item" type="button" onClick={() => onChange(indent)}>{indent}</button>;
  107. }) }
  108. </>
  109. ), [onChange]);
  110. return (
  111. <div className="input-group flex-nowrap">
  112. <div className="input-group-prepend">
  113. <span className="input-group-text" id="igt-indent">Indent</span>
  114. </div>
  115. <div className="input-group-append dropup">
  116. <button
  117. type="button"
  118. className="btn btn-outline-secondary dropdown-toggle"
  119. data-toggle="dropdown"
  120. aria-haspopup="true"
  121. aria-expanded="false"
  122. aria-describedby="igt-indent"
  123. disabled={isIndentSizeForced}
  124. >
  125. {selectedIndentSize}
  126. </button>
  127. <div className="dropdown-menu" aria-labelledby="dropdownMenuLink">
  128. {menuItems}
  129. </div>
  130. </div>
  131. </div>
  132. );
  133. });
  134. IndentSizeSelector.displayName = 'IndentSizeSelector';
  135. type ConfigurationDropdownProps = {
  136. onConfirmEnableTextlint?: () => void,
  137. }
  138. const ConfigurationDropdown = memo(({ onConfirmEnableTextlint }: ConfigurationDropdownProps): JSX.Element => {
  139. const { t } = useTranslation();
  140. const [isCddMenuOpened, setCddMenuOpened] = useState(false);
  141. const { data: editorSettings, update } = useEditorSettings();
  142. const { data: isTextlintEnabled, mutate: mutateTextlintEnabled } = useIsTextlintEnabled();
  143. const renderActiveLineMenuItem = useCallback(() => {
  144. if (editorSettings == null) {
  145. return <></>;
  146. }
  147. const isActive = editorSettings.styleActiveLine;
  148. const iconClasses = ['text-info'];
  149. if (isActive) {
  150. iconClasses.push('ti ti-check');
  151. }
  152. const iconClassName = iconClasses.join(' ');
  153. return (
  154. <DropdownItem toggle={false} onClick={() => update({ styleActiveLine: !isActive })}>
  155. <div className="d-flex justify-content-between">
  156. <span className="icon-container"></span>
  157. <span className="menuitem-label">{ t('page_edit.Show active line') }</span>
  158. <span className="icon-container"><i className={iconClassName}></i></span>
  159. </div>
  160. </DropdownItem>
  161. );
  162. }, [editorSettings, update, t]);
  163. const renderMarkdownTableAutoFormattingMenuItem = useCallback(() => {
  164. if (editorSettings == null) {
  165. return <></>;
  166. }
  167. const isActive = editorSettings.autoFormatMarkdownTable;
  168. const iconClasses = ['text-info'];
  169. if (isActive) {
  170. iconClasses.push('ti ti-check');
  171. }
  172. const iconClassName = iconClasses.join(' ');
  173. return (
  174. <DropdownItem toggle={false} onClick={() => update({ autoFormatMarkdownTable: !isActive })}>
  175. <div className="d-flex justify-content-between">
  176. <span className="icon-container"></span>
  177. <span className="menuitem-label">{ t('page_edit.auto_format_table') }</span>
  178. <span className="icon-container"><i className={iconClassName}></i></span>
  179. </div>
  180. </DropdownItem>
  181. );
  182. }, [editorSettings, t, update]);
  183. const renderIsTextlintEnabledMenuItem = useCallback(() => {
  184. if (editorSettings == null) {
  185. return <></>;
  186. }
  187. const clickHandler = () => {
  188. if (isTextlintEnabled) {
  189. mutateTextlintEnabled(false);
  190. return;
  191. }
  192. if (editorSettings.textlintSettings?.neverAskBeforeDownloadLargeFiles) {
  193. mutateTextlintEnabled(true);
  194. return;
  195. }
  196. if (onConfirmEnableTextlint != null) {
  197. onConfirmEnableTextlint();
  198. }
  199. };
  200. const iconClasses = ['text-info'];
  201. if (isTextlintEnabled) {
  202. iconClasses.push('ti ti-check');
  203. }
  204. const iconClassName = iconClasses.join(' ');
  205. return (
  206. <DropdownItem toggle={false} onClick={clickHandler}>
  207. <div className="d-flex justify-content-between">
  208. <span className="icon-container"></span>
  209. <span className="menuitem-label">Textlint</span>
  210. <span className="icon-container"><i className={iconClassName}></i></span>
  211. </div>
  212. </DropdownItem>
  213. );
  214. }, [editorSettings, isTextlintEnabled, mutateTextlintEnabled, onConfirmEnableTextlint]);
  215. return (
  216. <div className="my-0 form-group">
  217. <Dropdown
  218. direction="up"
  219. className="grw-editor-configuration-dropdown"
  220. isOpen={isCddMenuOpened}
  221. toggle={() => setCddMenuOpened(!isCddMenuOpened)}
  222. >
  223. <DropdownToggle color="outline-secondary" caret>
  224. <i className="icon-settings"></i>
  225. </DropdownToggle>
  226. <DropdownMenu>
  227. {renderActiveLineMenuItem()}
  228. {renderMarkdownTableAutoFormattingMenuItem()}
  229. {renderIsTextlintEnabledMenuItem()}
  230. {/* <DropdownItem divider /> */}
  231. </DropdownMenu>
  232. </Dropdown>
  233. </div>
  234. );
  235. });
  236. ConfigurationDropdown.displayName = 'ConfigurationDropdown';
  237. export const OptionsSelector = (): JSX.Element => {
  238. const [isDownloadDictModalShown, setDownloadDictModalShown] = useState(false);
  239. const { data: editorSettings, turnOffAskingBeforeDownloadLargeFiles } = useEditorSettings();
  240. const { mutate: mutateTextlintEnabled } = useIsTextlintEnabled();
  241. const { data: isIndentSizeForced } = useIsIndentSizeForced();
  242. const { data: currentIndentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
  243. if (editorSettings == null || isIndentSizeForced == null || currentIndentSize == null) {
  244. return <></>;
  245. }
  246. return (
  247. <>
  248. <div className="d-flex flex-row">
  249. <span>
  250. <ThemeSelector />
  251. </span>
  252. <span className="d-none d-sm-block ml-2 ml-sm-4">
  253. <KeymapSelector />
  254. </span>
  255. <span className="ml-2 ml-sm-4">
  256. <IndentSizeSelector
  257. isIndentSizeForced={isIndentSizeForced}
  258. selectedIndentSize={currentIndentSize}
  259. onChange={newValue => mutateCurrentIndentSize(newValue)}
  260. />
  261. </span>
  262. <span className="ml-2 ml-sm-4">
  263. <ConfigurationDropdown
  264. onConfirmEnableTextlint={() => setDownloadDictModalShown(true)}
  265. />
  266. </span>
  267. </div>
  268. { editorSettings != null && !editorSettings.textlintSettings?.neverAskBeforeDownloadLargeFiles && (
  269. <DownloadDictModal
  270. isModalOpen={isDownloadDictModalShown}
  271. onEnableTextlint={(isSkipAskingAgainChecked) => {
  272. mutateTextlintEnabled(true);
  273. if (isSkipAskingAgainChecked) {
  274. turnOffAskingBeforeDownloadLargeFiles();
  275. }
  276. setDownloadDictModalShown(false);
  277. }}
  278. onCancel={() => setDownloadDictModalShown(false)}
  279. />
  280. )}
  281. </>
  282. );
  283. };