OptionsSelector.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import React, {
  2. memo, useCallback, useMemo, useState,
  3. } from 'react';
  4. import {
  5. type EditorTheme, type KeyMapMode, PasteMode, AllPasteMode, DEFAULT_KEYMAP, DEFAULT_PASTE_MODE, DEFAULT_THEME,
  6. } from '@growi/editor';
  7. import { useTranslation } from 'next-i18next';
  8. import Image from 'next/image';
  9. import {
  10. Dropdown, DropdownToggle, DropdownMenu, Input, FormGroup,
  11. } from 'reactstrap';
  12. import { useIsIndentSizeForced } from '~/stores-universal/context';
  13. import { useEditorSettings, useCurrentIndentSize } from '~/stores/editor';
  14. import {
  15. useIsDeviceLargerThanMd,
  16. } from '~/stores/ui';
  17. type RadioListItemProps = {
  18. onClick: () => void,
  19. icon?: React.ReactNode,
  20. text: string,
  21. checked?: boolean
  22. }
  23. const RadioListItem = (props: RadioListItemProps): JSX.Element => {
  24. const {
  25. onClick, icon, text, checked,
  26. } = props;
  27. return (
  28. <li className="list-group-item border-0 d-flex align-items-center">
  29. <input
  30. onClick={onClick}
  31. className="form-check-input me-3"
  32. type="radio"
  33. name="listGroupRadio"
  34. id={`editor_config_radio_item_${text}`}
  35. checked={checked}
  36. />
  37. {icon}
  38. <label className="form-check-label stretched-link fs-6" htmlFor={`editor_config_radio_item_${text}`}>{text}</label>
  39. </li>
  40. );
  41. };
  42. type SelectorProps = {
  43. header: string,
  44. onClickBefore: () => void,
  45. items: JSX.Element,
  46. }
  47. const Selector = (props: SelectorProps): JSX.Element => {
  48. const { header, onClickBefore, items } = props;
  49. return (
  50. <div className="d-flex flex-column w-100">
  51. <button type="button" className="btn border-0 d-flex align-items-center text-muted ms-2" onClick={onClickBefore}>
  52. <span className="material-symbols-outlined fs-5 py-0 me-1">navigate_before</span>
  53. <label>{header}</label>
  54. </button>
  55. <hr className="my-1" />
  56. <ul className="list-group d-flex ms-2">
  57. { items }
  58. </ul>
  59. </div>
  60. );
  61. };
  62. type EditorThemeToLabel = {
  63. [key in EditorTheme]: string;
  64. }
  65. const EDITORTHEME_LABEL_MAP: EditorThemeToLabel = {
  66. defaultlight: 'DefaultLight',
  67. eclipse: 'Eclipse',
  68. basic: 'Basic',
  69. ayu: 'Ayu',
  70. rosepine: 'Rosé Pine',
  71. defaultdark: 'DefaultDark',
  72. material: 'Material',
  73. nord: 'Nord',
  74. cobalt: 'Cobalt',
  75. kimbie: 'Kimbie',
  76. };
  77. const ThemeSelector = memo(({ onClickBefore }: {onClickBefore: () => void}): JSX.Element => {
  78. const { t } = useTranslation();
  79. const { data: editorSettings, update } = useEditorSettings();
  80. const selectedTheme = editorSettings?.theme ?? DEFAULT_THEME;
  81. const listItems = useMemo(() => (
  82. <>
  83. { (Object.keys(EDITORTHEME_LABEL_MAP) as EditorTheme[]).map((theme) => {
  84. const themeLabel = EDITORTHEME_LABEL_MAP[theme];
  85. return (
  86. <RadioListItem onClick={() => update({ theme })} text={themeLabel} checked={theme === selectedTheme} />
  87. );
  88. }) }
  89. </>
  90. ), [update, selectedTheme]);
  91. return (
  92. <Selector header={t('page_edit.theme')} onClickBefore={onClickBefore} items={listItems} />
  93. );
  94. });
  95. ThemeSelector.displayName = 'ThemeSelector';
  96. type KeyMapModeToLabel = {
  97. [key in KeyMapMode]: string;
  98. }
  99. const KEYMAP_LABEL_MAP: KeyMapModeToLabel = {
  100. default: 'Default',
  101. vim: 'Vim',
  102. emacs: 'Emacs',
  103. vscode: 'Visual Studio Code',
  104. };
  105. const KeymapSelector = memo(({ onClickBefore }: {onClickBefore: () => void}): JSX.Element => {
  106. const { t } = useTranslation();
  107. const { data: editorSettings, update } = useEditorSettings();
  108. const selectedKeymapMode = editorSettings?.keymapMode ?? DEFAULT_KEYMAP;
  109. const listItems = useMemo(() => (
  110. <>
  111. { (Object.keys(KEYMAP_LABEL_MAP) as KeyMapMode[]).map((keymapMode) => {
  112. const keymapLabel = KEYMAP_LABEL_MAP[keymapMode];
  113. const icon = (keymapMode !== 'default')
  114. ? <Image src={`/images/icons/${keymapMode}.png`} width={16} height={16} className="me-2" alt={keymapMode} />
  115. : null;
  116. return (
  117. <RadioListItem onClick={() => update({ keymapMode })} icon={icon} text={keymapLabel} checked={keymapMode === selectedKeymapMode} />
  118. );
  119. }) }
  120. </>
  121. ), [update, selectedKeymapMode]);
  122. return (
  123. <Selector header={t('page_edit.keymap')} onClickBefore={onClickBefore} items={listItems} />
  124. );
  125. });
  126. KeymapSelector.displayName = 'KeymapSelector';
  127. const TYPICAL_INDENT_SIZE = [2, 4];
  128. const IndentSizeSelector = memo(({ onClickBefore }: {onClickBefore: () => void}): JSX.Element => {
  129. const { t } = useTranslation();
  130. const { data: currentIndentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
  131. const listItems = useMemo(() => (
  132. <>
  133. { TYPICAL_INDENT_SIZE.map((indent) => {
  134. return (
  135. <RadioListItem onClick={() => mutateCurrentIndentSize(indent)} text={indent.toString()} checked={indent === currentIndentSize} />
  136. );
  137. }) }
  138. </>
  139. ), [currentIndentSize, mutateCurrentIndentSize]);
  140. return (
  141. <Selector header={t('page_edit.indent')} onClickBefore={onClickBefore} items={listItems} />
  142. );
  143. });
  144. IndentSizeSelector.displayName = 'IndentSizeSelector';
  145. const PasteSelector = memo(({ onClickBefore }: {onClickBefore: () => void}): JSX.Element => {
  146. const { t } = useTranslation();
  147. const { data: editorSettings, update } = useEditorSettings();
  148. const selectedPasteMode = editorSettings?.pasteMode ?? DEFAULT_PASTE_MODE;
  149. const listItems = useMemo(() => (
  150. <>
  151. { (AllPasteMode).map((pasteMode) => {
  152. return (
  153. <RadioListItem onClick={() => update({ pasteMode })} text={t(`page_edit.paste.${pasteMode}`) ?? ''} checked={pasteMode === selectedPasteMode} />
  154. );
  155. }) }
  156. </>
  157. ), [update, t, selectedPasteMode]);
  158. return (
  159. <Selector header={t('page_edit.paste.title')} onClickBefore={onClickBefore} items={listItems} />
  160. );
  161. });
  162. PasteSelector.displayName = 'PasteSelector';
  163. type SwitchItemProps = {
  164. inputId: string,
  165. onChange: () => void,
  166. checked: boolean,
  167. text: string,
  168. };
  169. const SwitchItem = memo((props: SwitchItemProps): JSX.Element => {
  170. const {
  171. inputId, onChange, checked, text,
  172. } = props;
  173. return (
  174. <FormGroup switch>
  175. <Input id={inputId} type="switch" checked={checked} onChange={onChange} />
  176. <label htmlFor={inputId}>{text}</label>
  177. </FormGroup>
  178. );
  179. });
  180. const ConfigurationSelector = memo((): JSX.Element => {
  181. const { t } = useTranslation();
  182. const { data: editorSettings, update } = useEditorSettings();
  183. const renderActiveLineMenuItem = useCallback(() => {
  184. if (editorSettings == null) {
  185. return <></>;
  186. }
  187. const isActive = editorSettings.styleActiveLine;
  188. return (
  189. <SwitchItem
  190. inputId="switchActiveLine"
  191. onChange={() => update({ styleActiveLine: !isActive })}
  192. checked={isActive}
  193. text={t('page_edit.Show active line')}
  194. />
  195. );
  196. }, [editorSettings, update, t]);
  197. const renderMarkdownTableAutoFormattingMenuItem = useCallback(() => {
  198. if (editorSettings == null) {
  199. return <></>;
  200. }
  201. const isActive = editorSettings.autoFormatMarkdownTable;
  202. return (
  203. <SwitchItem
  204. inputId="switchTableAutoFormatting"
  205. onChange={() => update({ autoFormatMarkdownTable: !isActive })}
  206. checked={isActive}
  207. text={t('page_edit.auto_format_table')}
  208. />
  209. );
  210. }, [editorSettings, t, update]);
  211. return (
  212. <div className="mx-3 mt-1">
  213. {renderActiveLineMenuItem()}
  214. {renderMarkdownTableAutoFormattingMenuItem()}
  215. </div>
  216. );
  217. });
  218. ConfigurationSelector.displayName = 'ConfigurationSelector';
  219. type ChangeStateButtonProps = {
  220. onClick: () => void,
  221. header: string,
  222. data: string,
  223. disabled?: boolean,
  224. }
  225. const ChangeStateButton = memo((props: ChangeStateButtonProps): JSX.Element => {
  226. const {
  227. onClick, header, data, disabled,
  228. } = props;
  229. return (
  230. <button type="button" className="d-flex align-items-center btn btn-sm border-0 my-1" disabled={disabled} onClick={onClick}>
  231. <label className="ms-2 me-auto">{header}</label>
  232. <label className="text-muted d-flex align-items-center ms-2 me-1">
  233. {data}
  234. <span className="material-symbols-outlined fs-5 py-0">navigate_next</span>
  235. </label>
  236. </button>
  237. );
  238. });
  239. const OptionsStatus = {
  240. Home: 'Home',
  241. Theme: 'Theme',
  242. Keymap: 'Keymap',
  243. Indent: 'Indent',
  244. Paste: 'Paste',
  245. } as const;
  246. type OptionStatus = typeof OptionsStatus[keyof typeof OptionsStatus];
  247. export const OptionsSelector = (): JSX.Element => {
  248. const { t } = useTranslation();
  249. const [dropdownOpen, setDropdownOpen] = useState(false);
  250. const [status, setStatus] = useState<OptionStatus>(OptionsStatus.Home);
  251. const { data: editorSettings } = useEditorSettings();
  252. const { data: currentIndentSize } = useCurrentIndentSize();
  253. const { data: isIndentSizeForced } = useIsIndentSizeForced();
  254. const { data: isDeviceLargerThanMd } = useIsDeviceLargerThanMd();
  255. if (editorSettings == null || currentIndentSize == null || isIndentSizeForced == null) {
  256. return <></>;
  257. }
  258. return (
  259. <Dropdown isOpen={dropdownOpen} toggle={() => { setStatus(OptionsStatus.Home); setDropdownOpen(!dropdownOpen) }} direction="up" className="">
  260. <DropdownToggle
  261. className={`btn btn-sm btn-outline-neutral-secondary d-flex align-items-center justify-content-center
  262. ${isDeviceLargerThanMd ? '' : 'border-0'}
  263. ${dropdownOpen ? 'active' : ''}
  264. `}
  265. >
  266. <span className="material-symbols-outlined py-0 fs-5"> settings </span>
  267. {
  268. isDeviceLargerThanMd
  269. ? <label className="ms-1 me-1">{t('page_edit.editor_config')}</label>
  270. : <></>
  271. }
  272. </DropdownToggle>
  273. <DropdownMenu container="body">
  274. {
  275. status === OptionsStatus.Home && (
  276. <div className="d-flex flex-column">
  277. <label className="text-muted ms-3">
  278. {t('page_edit.editor_config')}
  279. </label>
  280. <hr className="my-1" />
  281. <ChangeStateButton
  282. onClick={() => setStatus(OptionsStatus.Theme)}
  283. header={t('page_edit.theme')}
  284. data={EDITORTHEME_LABEL_MAP[editorSettings.theme ?? ''] ?? ''}
  285. />
  286. <hr className="my-1" />
  287. <ChangeStateButton
  288. onClick={() => setStatus(OptionsStatus.Keymap)}
  289. header={t('page_edit.keymap')}
  290. data={KEYMAP_LABEL_MAP[editorSettings.keymapMode ?? ''] ?? ''}
  291. />
  292. <hr className="my-1" />
  293. <ChangeStateButton
  294. disabled={isIndentSizeForced}
  295. onClick={() => setStatus(OptionsStatus.Indent)}
  296. header={t('page_edit.indent')}
  297. data={currentIndentSize.toString() ?? ''}
  298. />
  299. <hr className="my-1" />
  300. <ChangeStateButton
  301. onClick={() => setStatus(OptionsStatus.Paste)}
  302. header={t('page_edit.paste.title')}
  303. data={t(`page_edit.paste.${editorSettings.pasteMode ?? PasteMode.both}`) ?? ''}
  304. />
  305. <hr className="my-1" />
  306. <ConfigurationSelector />
  307. </div>
  308. )
  309. }
  310. { status === OptionsStatus.Theme && (
  311. <ThemeSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  312. )
  313. }
  314. { status === OptionsStatus.Keymap && (
  315. <KeymapSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  316. )
  317. }
  318. { status === OptionsStatus.Indent && (
  319. <IndentSizeSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  320. )
  321. }
  322. { status === OptionsStatus.Paste && (
  323. <PasteSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  324. )}
  325. </DropdownMenu>
  326. </Dropdown>
  327. );
  328. };