OptionsSelector.tsx 9.7 KB

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