import React, {
memo, useCallback, useMemo, useState,
} from 'react';
import { useTranslation } from 'next-i18next';
import {
Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
} from 'reactstrap';
import { useIsIndentSizeForced } from '~/stores/context';
import { useEditorSettings, useIsTextlintEnabled, useCurrentIndentSize } from '~/stores/editor';
import { DEFAULT_THEME, KeyMapMode } from '../../interfaces/editor-settings';
import { DownloadDictModal } from './DownloadDictModal';
const AVAILABLE_THEMES = [
'eclipse', 'elegant', 'neo', 'mdn-like', 'material', 'dracula', 'monokai', 'twilight',
];
const TYPICAL_INDENT_SIZE = [2, 4];
const ThemeSelector = (): JSX.Element => {
const { data: editorSettings, update } = useEditorSettings();
const menuItems = useMemo(() => (
<>
{ AVAILABLE_THEMES.map((theme) => {
return ;
}) }
>
), [update]);
const selectedTheme = editorSettings?.theme ?? DEFAULT_THEME;
return (
Theme
{menuItems}
);
};
type KeyMapModeToLabel = {
[key in KeyMapMode]: string;
}
const KEYMAP_LABEL_MAP: KeyMapModeToLabel = {
default: 'Default',
vim: 'Vim',
emacs: 'Emacs',
sublime: 'Sublime Text',
};
const KeymapSelector = memo((): JSX.Element => {
const { data: editorSettings, update } = useEditorSettings();
Object.keys(KEYMAP_LABEL_MAP);
const menuItems = useMemo(() => (
<>
{ (Object.keys(KEYMAP_LABEL_MAP) as KeyMapMode[]).map((keymapMode) => {
const keymapLabel = KEYMAP_LABEL_MAP[keymapMode];
const icon = (keymapMode !== 'default')
?
: null;
return ;
}) }
>
), [update]);
const selectedKeymapMode = editorSettings?.keymapMode ?? 'default';
return (
Keymap
{menuItems}
);
});
KeymapSelector.displayName = 'KeymapSelector';
type IndentSizeSelectorProps = {
isIndentSizeForced: boolean,
selectedIndentSize: number,
onChange: (indentSize: number) => void,
}
const IndentSizeSelector = memo(({ isIndentSizeForced, selectedIndentSize, onChange }: IndentSizeSelectorProps): JSX.Element => {
const menuItems = useMemo(() => (
<>
{ TYPICAL_INDENT_SIZE.map((indent) => {
return ;
}) }
>
), [onChange]);
return (
Indent
{menuItems}
);
});
IndentSizeSelector.displayName = 'IndentSizeSelector';
type ConfigurationDropdownProps = {
onConfirmEnableTextlint?: () => void,
}
const ConfigurationDropdown = memo(({ onConfirmEnableTextlint }: ConfigurationDropdownProps): JSX.Element => {
const { t } = useTranslation();
const [isCddMenuOpened, setCddMenuOpened] = useState(false);
const { data: editorSettings, update } = useEditorSettings();
const { data: isTextlintEnabled, mutate: mutateTextlintEnabled } = useIsTextlintEnabled();
const renderActiveLineMenuItem = useCallback(() => {
if (editorSettings == null) {
return <>>;
}
const isActive = editorSettings.styleActiveLine;
const iconClasses = ['text-info'];
if (isActive) {
iconClasses.push('ti ti-check');
}
const iconClassName = iconClasses.join(' ');
return (
update({ styleActiveLine: !isActive })}>
{ t('page_edit.Show active line') }
);
}, [editorSettings, update, t]);
const renderMarkdownTableAutoFormattingMenuItem = useCallback(() => {
if (editorSettings == null) {
return <>>;
}
const isActive = editorSettings.autoFormatMarkdownTable;
const iconClasses = ['text-info'];
if (isActive) {
iconClasses.push('ti ti-check');
}
const iconClassName = iconClasses.join(' ');
return (
update({ autoFormatMarkdownTable: !isActive })}>
{ t('page_edit.auto_format_table') }
);
}, [editorSettings, t, update]);
const renderIsTextlintEnabledMenuItem = useCallback(() => {
if (editorSettings == null) {
return <>>;
}
const clickHandler = () => {
if (isTextlintEnabled) {
mutateTextlintEnabled(false);
return;
}
if (editorSettings.textlintSettings?.neverAskBeforeDownloadLargeFiles) {
mutateTextlintEnabled(true);
return;
}
if (onConfirmEnableTextlint != null) {
onConfirmEnableTextlint();
}
};
const iconClasses = ['text-info'];
if (isTextlintEnabled) {
iconClasses.push('ti ti-check');
}
const iconClassName = iconClasses.join(' ');
return (
Textlint
);
}, [editorSettings, isTextlintEnabled, mutateTextlintEnabled, onConfirmEnableTextlint]);
return (
setCddMenuOpened(!isCddMenuOpened)}
>
{renderActiveLineMenuItem()}
{renderMarkdownTableAutoFormattingMenuItem()}
{renderIsTextlintEnabledMenuItem()}
{/* */}
);
});
ConfigurationDropdown.displayName = 'ConfigurationDropdown';
export const OptionsSelector = (): JSX.Element => {
const [isDownloadDictModalShown, setDownloadDictModalShown] = useState(false);
const { data: editorSettings, turnOffAskingBeforeDownloadLargeFiles } = useEditorSettings();
const { mutate: mutateTextlintEnabled } = useIsTextlintEnabled();
const { data: isIndentSizeForced } = useIsIndentSizeForced();
const { data: currentIndentSize, mutate: mutateCurrentIndentSize } = useCurrentIndentSize();
if (editorSettings == null || isIndentSizeForced == null || currentIndentSize == null) {
return <>>;
}
return (
<>
mutateCurrentIndentSize(newValue)}
/>
setDownloadDictModalShown(true)}
/>
{ editorSettings != null && !editorSettings.textlintSettings?.neverAskBeforeDownloadLargeFiles && (
{
mutateTextlintEnabled(true);
if (isSkipAskingAgainChecked) {
turnOffAskingBeforeDownloadLargeFiles();
}
setDownloadDictModalShown(false);
}}
onCancel={() => setDownloadDictModalShown(false)}
/>
)}
>
);
};