webapp/src/components/Editor/Panel/Panel.tsx

259 lines
7.8 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
import { Button } from '../../_shared/Button'
import { Icon } from '../../_shared/Icon'
import { useLocalize } from '../../../context/localize'
import styles from './Panel.module.scss'
import { useEditorContext } from '../../../context/editor'
2023-05-03 16:13:48 +00:00
import { useOutsideClickHandler } from '../../../utils/useOutsideClickHandler'
import { useEscKeyDownHandler } from '../../../utils/useEscKeyDownHandler'
import { getPagePath } from '@nanostores/router'
import { router } from '../../../stores/router'
import { useEditorHTML } from 'solid-tiptap'
import Typograf from 'typograf'
import { createSignal, Show } from 'solid-js'
import { DarkModeToggle } from '../../_shared/DarkModeToggle'
const typograf = new Typograf({ locale: ['ru', 'en-US'] })
type Props = {
2023-05-08 17:21:06 +00:00
shoutId: number
2023-05-03 16:13:48 +00:00
}
export const Panel = (props: Props) => {
const { t } = useLocalize()
const {
isEditorPanelVisible,
wordCounter,
editorRef,
2023-05-08 17:21:06 +00:00
actions: { toggleEditorPanel, saveShout, publishShout }
} = useEditorContext()
2023-05-03 16:13:48 +00:00
const containerRef: { current: HTMLElement } = {
current: null
}
const [isShortcutsVisible, setIsShortcutsVisible] = createSignal(false)
const [isTypographyFixed, setIsTypographyFixed] = createSignal(false)
2023-05-03 16:13:48 +00:00
useOutsideClickHandler({
containerRef,
predicate: () => isEditorPanelVisible(),
handler: () => toggleEditorPanel()
})
useEscKeyDownHandler(() => {
if (isEditorPanelVisible()) {
toggleEditorPanel()
}
})
const handleSaveClick = () => {
2023-05-08 17:21:06 +00:00
saveShout()
}
const handlePublishClick = () => {
2023-05-08 17:21:06 +00:00
publishShout()
}
const handleFixTypographyClick = () => {
const html = useEditorHTML(() => editorRef.current())
editorRef.current().commands.setContent(typograf.execute(html()))
setIsTypographyFixed(true)
}
return (
2023-05-03 16:13:48 +00:00
<aside
ref={(el) => (containerRef.current = el)}
class={clsx('col-md-6', styles.Panel, { [styles.hidden]: !isEditorPanelVisible() })}
>
<Button
value={<Icon name="close" />}
variant={'inline'}
class={styles.close}
onClick={() => toggleEditorPanel()}
/>
<div class={clsx(styles.actionsHolder, styles.scrolled, { hidden: isShortcutsVisible() })}>
<section>
2023-05-03 16:13:48 +00:00
<p>
<span class={styles.link} onClick={handlePublishClick}>
2023-05-08 17:21:06 +00:00
{t('Publish')}
</span>
2023-05-03 16:13:48 +00:00
</p>
<p>
<span class={styles.link} onClick={handleSaveClick}>
2023-05-08 17:21:06 +00:00
{t('Save draft')}
</span>
2023-05-03 16:13:48 +00:00
</p>
</section>
<section>
2023-05-03 16:13:48 +00:00
<p>
<a class={styles.link}>{t('Invite co-authors')}</a>
2023-05-03 16:13:48 +00:00
</p>
<p>
<a
class={styles.link}
onClick={() => toggleEditorPanel()}
href={getPagePath(router, 'editSettings', { shoutId: props.shoutId.toString() })}
>
2023-05-03 16:13:48 +00:00
{t('Publication settings')}
</a>
</p>
<p>
<span class={styles.link}>{t('Corrections history')}</span>
2023-05-03 16:13:48 +00:00
</p>
</section>
<section>
<div class={styles.typograph}>
<div>
<span class={styles.link} onClick={handleFixTypographyClick}>
{t('Autotypograph')}
</span>
</div>
<Show when={isTypographyFixed()}>
<div class={clsx(styles.typographStatus, styles.typographStatusSuccess)}>{t('Fixed')}</div>
</Show>
</div>
<p>{t('Text checking')}</p>
</section>
<section>
<DarkModeToggle />
</section>
<section>
<p>
<a class={styles.link} href="/how-to-write-a-good-article">
{t('How to write a good article')}
</a>
</p>
<p>
<button class={styles.link} onClick={() => setIsShortcutsVisible(true)}>
{t('Hotkeys')}
</button>
</p>
<p>
<a class={styles.link} href="#">
{t('Help')}
</a>
</p>
</section>
<div class={styles.stats}>
<div>
{t('Characters')}: <em>{wordCounter().characters}</em>
</div>
<div>
{t('Words')}: <em>{wordCounter().words}</em>
</div>
2023-05-03 16:13:48 +00:00
{/*<div>*/}
{/* {t('Last rev.')}: <em>22.03.22 в 18:20</em>*/}
{/*</div>*/}
</div>
</div>
<div class={clsx(styles.actionsHolder, styles.scrolled, { hidden: !isShortcutsVisible() })}>
<p>
<button class={styles.backToMenuControl} onClick={() => setIsShortcutsVisible(false)}>
{t('back to menu"')}
</button>
</p>
<section class={styles.shortcutList}>
<p>
{t('bold')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>B</span>
</span>
</p>
<p>
{t('italic')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>I</span>
</span>
</p>
<p>
{t('add link')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>K</span>
</span>
</p>
</section>
<section class={styles.shortcutList}>
<p>
{t('header 1')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>Alt</span>
<span class={styles.shortcutButton}>1</span>
</span>
</p>
<p>
{t('header 2')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>Alt</span>
<span class={styles.shortcutButton}>2</span>
</span>
</p>
<p>
{t('header 3')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>Alt</span>
<span class={styles.shortcutButton}>3</span>
</span>
</p>
</section>
<section class={styles.shortcutList}>
<p>
{t('marker list')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>*</span>
<span class={styles.shortcutButton}>Space</span>
</span>
</p>
<p>
{t('number list')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>1</span>
<span class={styles.shortcutButton}>Space</span>
</span>
</p>
<p>
{t('delimiter')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>***</span>
<span class={styles.shortcutButton}>Enter</span>
</span>
</p>
</section>
<section class={styles.shortcutList}>
<p>
{t('cancel')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>Z</span>
</span>
</p>
<p>
{t('repeat')}
<span class={styles.shortcut}>
<span class={styles.shortcutButton}>Ctrl</span>
<span class={styles.shortcutButton}>Shift</span>
<span class={styles.shortcutButton}>Z</span>
</span>
</p>
</section>
</div>
</aside>
)
}