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

166 lines
4.5 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'
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
}
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()))
}
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() })}
>
<div class={styles.actionsHolder}>
<Button
value={<Icon name="close" />}
variant={'inline'}
class={styles.close}
onClick={() => toggleEditorPanel()}
/>
</div>
<div class={clsx(styles.actionsHolder, styles.scrolled)}>
<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={clsx(styles.link, styles.linkWithIcon)}>
2023-05-03 16:13:48 +00:00
<Icon name="eye" class={styles.icon} />
{t('Preview')}
</a>
</p>
<p>
<a
class={clsx(styles.link, styles.linkWithIcon)}
onClick={() => toggleEditorPanel()}
2023-05-08 17:21:06 +00:00
href={getPagePath(router, 'edit', { shoutId: props.shoutId.toString() })}
2023-05-03 16:13:48 +00:00
>
<Icon name="pencil-outline" class={styles.icon} />
{t('Editing')}
</a>
</p>
<p>
<a class={clsx(styles.link, styles.linkWithIcon)}>
2023-05-03 16:13:48 +00:00
<Icon name="feed-discussion" class={styles.icon} />
{t('FAQ')}
</a>
</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} onClick={handleFixTypographyClick}>
{t('Fix typography')}
</span>
</p>
2023-05-03 16:13:48 +00:00
<p>
<a class={styles.link}>{t('Corrections history')}</a>
2023-05-03 16:13:48 +00:00
</p>
</section>
<section>
<p>
<a class={styles.link} href="/how-to-write-a-good-article">
{t('How to write a good article')}
</a>
</p>
<p>
<a class={styles.link} href="#">
{t('Hotkeys')}
</a>
</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>
</aside>
)
}