OptionsSelector.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import React, {
  2. memo, useCallback, useMemo, useState,
  3. } from 'react';
  4. import {
  5. type EditorTheme, type KeyMapMode, DEFAULT_KEYMAP, 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/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. type SwitchItemProps = {
  146. onClick: () => void,
  147. checked: boolean,
  148. text: string,
  149. };
  150. const SwitchItem = memo((props: SwitchItemProps): JSX.Element => {
  151. const { onClick, checked, text } = props;
  152. return (
  153. <FormGroup switch>
  154. <Input type="switch" checked={checked} onClick={onClick} />
  155. <label>{text}</label>
  156. </FormGroup>
  157. );
  158. });
  159. const ConfigurationSelector = memo((): JSX.Element => {
  160. const { t } = useTranslation();
  161. const { data: editorSettings, update } = useEditorSettings();
  162. const renderActiveLineMenuItem = useCallback(() => {
  163. if (editorSettings == null) {
  164. return <></>;
  165. }
  166. const isActive = editorSettings.styleActiveLine;
  167. return (
  168. <SwitchItem onClick={() => update({ styleActiveLine: !isActive })} checked={isActive} text={t('page_edit.Show active line')} />
  169. );
  170. }, [editorSettings, update, t]);
  171. const renderMarkdownTableAutoFormattingMenuItem = useCallback(() => {
  172. if (editorSettings == null) {
  173. return <></>;
  174. }
  175. const isActive = editorSettings.autoFormatMarkdownTable;
  176. return (
  177. <SwitchItem onClick={() => update({ autoFormatMarkdownTable: !isActive })} checked={isActive} text={t('page_edit.auto_format_table')} />
  178. );
  179. }, [editorSettings, t, update]);
  180. return (
  181. <div className="mx-3 mt-1">
  182. {renderActiveLineMenuItem()}
  183. {renderMarkdownTableAutoFormattingMenuItem()}
  184. </div>
  185. );
  186. });
  187. ConfigurationSelector.displayName = 'ConfigurationSelector';
  188. type ChangeStateButtonProps = {
  189. onClick: () => void,
  190. header: string,
  191. data: string,
  192. disabled?: boolean,
  193. }
  194. const ChangeStateButton = memo((props: ChangeStateButtonProps): JSX.Element => {
  195. const {
  196. onClick, header, data, disabled,
  197. } = props;
  198. return (
  199. <button type="button" className="d-flex align-items-center btn btn-sm border-0 my-1" disabled={disabled} onClick={onClick}>
  200. <label className="ms-2 me-auto">{header}</label>
  201. <label className="text-muted d-flex align-items-center ms-2 me-1">
  202. {data}
  203. <span className="material-symbols-outlined fs-5 py-0">navigate_next</span>
  204. </label>
  205. </button>
  206. );
  207. });
  208. const OptionsStatus = {
  209. Home: 'Home',
  210. Theme: 'Theme',
  211. Keymap: 'Keymap',
  212. Indent: 'Indent',
  213. } as const;
  214. type OptionStatus = typeof OptionsStatus[keyof typeof OptionsStatus];
  215. export const OptionsSelector = (): JSX.Element => {
  216. const { t } = useTranslation();
  217. const [dropdownOpen, setDropdownOpen] = useState(false);
  218. const [status, setStatus] = useState<OptionStatus>(OptionsStatus.Home);
  219. const { data: editorSettings } = useEditorSettings();
  220. const { data: currentIndentSize } = useCurrentIndentSize();
  221. const { data: isIndentSizeForced } = useIsIndentSizeForced();
  222. const { data: isDeviceLargerThanMd } = useIsDeviceLargerThanMd();
  223. if (editorSettings == null || currentIndentSize == null || isIndentSizeForced == null) {
  224. return <></>;
  225. }
  226. return (
  227. <Dropdown isOpen={dropdownOpen} toggle={() => { setStatus(OptionsStatus.Home); setDropdownOpen(!dropdownOpen) }} direction="up" className="">
  228. <DropdownToggle
  229. className={`btn btn-sm btn-outline-neutral-secondary d-flex align-items-center justify-content-center
  230. ${isDeviceLargerThanMd ? '' : 'border-0'}
  231. ${dropdownOpen ? 'active' : ''}
  232. `}
  233. >
  234. <span className="material-symbols-outlined py-0 fs-5"> settings </span>
  235. {
  236. isDeviceLargerThanMd
  237. ? <label className="ms-1 me-1">{t('page_edit.editor_config')}</label>
  238. : <></>
  239. }
  240. </DropdownToggle>
  241. <DropdownMenu container="body">
  242. {
  243. status === OptionsStatus.Home && (
  244. <div className="d-flex flex-column">
  245. <label className="text-muted ms-3">
  246. {t('page_edit.editor_config')}
  247. </label>
  248. <hr className="my-1" />
  249. <ChangeStateButton
  250. onClick={() => setStatus(OptionsStatus.Theme)}
  251. header={t('page_edit.theme')}
  252. data={EDITORTHEME_LABEL_MAP[editorSettings.theme ?? ''] ?? ''}
  253. />
  254. <hr className="my-1" />
  255. <ChangeStateButton
  256. onClick={() => setStatus(OptionsStatus.Keymap)}
  257. header={t('page_edit.keymap')}
  258. data={KEYMAP_LABEL_MAP[editorSettings.keymapMode ?? ''] ?? ''}
  259. />
  260. <hr className="my-1" />
  261. <ChangeStateButton
  262. disabled={isIndentSizeForced}
  263. onClick={() => setStatus(OptionsStatus.Indent)}
  264. header={t('page_edit.indent')}
  265. data={currentIndentSize.toString() ?? ''}
  266. />
  267. <hr className="my-1" />
  268. <ConfigurationSelector />
  269. </div>
  270. )
  271. }
  272. { status === OptionsStatus.Theme && (
  273. <ThemeSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  274. )
  275. }
  276. { status === OptionsStatus.Keymap && (
  277. <KeymapSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  278. )
  279. }
  280. { status === OptionsStatus.Indent && (
  281. <IndentSizeSelector onClickBefore={() => setStatus(OptionsStatus.Home)} />
  282. )
  283. }
  284. </DropdownMenu>
  285. </Dropdown>
  286. );
  287. };