2022-03-09 17:57:44 -05:00
|
|
|
import { Button, Card, Input, Spacer, Tabs, Textarea } from "@geist-ui/core"
|
|
|
|
import { ChangeEvent, memo, useMemo, useRef, useState } from "react"
|
2022-03-06 19:46:59 -05:00
|
|
|
import styles from './document.module.css'
|
|
|
|
import MarkdownPreview from '../preview'
|
|
|
|
import { Trash } from '@geist-ui/icons'
|
2022-03-08 03:37:18 -05:00
|
|
|
import FormattingIcons from "../formatting-icons"
|
2022-03-09 20:11:37 -05:00
|
|
|
import Skeleton from "react-loading-skeleton"
|
2022-03-06 19:46:59 -05:00
|
|
|
type Props = {
|
2022-03-09 20:11:37 -05:00
|
|
|
editable?: boolean
|
2022-03-06 19:46:59 -05:00
|
|
|
remove?: () => void
|
|
|
|
title?: string
|
|
|
|
content?: string
|
|
|
|
setTitle?: (title: string) => void
|
|
|
|
setContent?: (content: string) => void
|
2022-03-07 01:16:08 -05:00
|
|
|
initialTab?: "edit" | "preview"
|
2022-03-09 20:11:37 -05:00
|
|
|
skeleton?: boolean
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-09 20:11:37 -05:00
|
|
|
const Document = ({ remove, editable, title, content, setTitle, setContent, initialTab = 'edit', skeleton }: Props) => {
|
2022-03-06 19:46:59 -05:00
|
|
|
const codeEditorRef = useRef<HTMLTextAreaElement>(null)
|
2022-03-08 03:43:18 -05:00
|
|
|
const [tab, setTab] = useState(initialTab)
|
2022-03-08 19:39:24 -05:00
|
|
|
const height = editable ? "500px" : '100%'
|
2022-03-08 03:43:18 -05:00
|
|
|
|
|
|
|
const handleTabChange = (newTab: string) => {
|
|
|
|
if (newTab === 'edit') {
|
|
|
|
codeEditorRef.current?.focus()
|
|
|
|
}
|
|
|
|
setTab(newTab as 'edit' | 'preview')
|
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-09 17:57:44 -05:00
|
|
|
const getType = useMemo(() => {
|
|
|
|
if (!title) return
|
|
|
|
const pathParts = title.split(".")
|
|
|
|
const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
|
|
|
|
return language
|
|
|
|
}, [title])
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
const removeFile = (remove?: () => void) => {
|
|
|
|
if (remove) {
|
|
|
|
if (content && content.trim().length > 0) {
|
|
|
|
const confirmed = window.confirm("Are you sure you want to remove this file?")
|
|
|
|
if (confirmed) {
|
|
|
|
remove()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 20:11:37 -05:00
|
|
|
if (skeleton) {
|
|
|
|
return <>
|
|
|
|
<Spacer height={1} />
|
|
|
|
<Card marginBottom={'var(--gap)'} marginTop={'var(--gap)'} style={{ maxWidth: 980, margin: "0 auto" }}>
|
|
|
|
<div className={styles.fileNameContainer}>
|
|
|
|
<Skeleton width={275} height={36} />
|
|
|
|
{editable && <Skeleton width={36} height={36} />}
|
|
|
|
</div>
|
|
|
|
<div className={styles.descriptionContainer}>
|
|
|
|
<div style={{ flexDirection: 'row', display: 'flex' }}><Skeleton width={125} height={36} /></div>
|
|
|
|
<Skeleton width={'100%'} height={350} />
|
|
|
|
</div >
|
|
|
|
</Card>
|
|
|
|
</>
|
|
|
|
}
|
2022-03-06 19:46:59 -05:00
|
|
|
return (
|
2022-03-09 04:01:58 -05:00
|
|
|
<>
|
|
|
|
<Spacer height={1} />
|
|
|
|
<Card marginBottom={'var(--gap)'} marginTop={'var(--gap)'} style={{ maxWidth: 980, margin: "0 auto" }}>
|
|
|
|
<div className={styles.fileNameContainer}>
|
|
|
|
<Input
|
|
|
|
placeholder="MyFile.md"
|
|
|
|
value={title}
|
|
|
|
onChange={(event: ChangeEvent<HTMLInputElement>) => setTitle ? setTitle(event.target.value) : null}
|
|
|
|
marginTop="var(--gap-double)"
|
|
|
|
size={1.2}
|
|
|
|
font={1.2}
|
|
|
|
label="Filename"
|
|
|
|
disabled={!editable}
|
|
|
|
width={"100%"}
|
|
|
|
/>
|
2022-03-09 04:50:55 -05:00
|
|
|
{remove && editable && <Button type="abort" ghost icon={<Trash />} auto height={'36px'} width={'36px'} onClick={() => removeFile(remove)} />}
|
2022-03-09 04:01:58 -05:00
|
|
|
</div>
|
|
|
|
<div className={styles.descriptionContainer}>
|
|
|
|
{tab === 'edit' && editable && <FormattingIcons setText={setContent} textareaRef={codeEditorRef} />}
|
|
|
|
<Tabs onChange={handleTabChange} initialValue={initialTab} hideDivider leftSpace={0}>
|
|
|
|
<Tabs.Item label={editable ? "Edit" : "Raw"} value="edit">
|
|
|
|
{/* <textarea className={styles.lineCounter} wrap='off' readOnly ref={lineNumberRef}>1.</textarea> */}
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
|
|
|
<Textarea
|
|
|
|
ref={codeEditorRef}
|
|
|
|
placeholder="Type some contents..."
|
|
|
|
value={content}
|
|
|
|
onChange={(event) => setContent ? setContent(event.target.value) : null}
|
|
|
|
width="100%"
|
|
|
|
disabled={!editable}
|
|
|
|
// TODO: Textarea should grow to fill parent if height == 100%
|
|
|
|
style={{ flex: 1, minHeight: 350 }}
|
|
|
|
resize="vertical"
|
|
|
|
className={styles.textarea}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Tabs.Item>
|
|
|
|
<Tabs.Item label="Preview" value="preview">
|
2022-03-09 17:57:44 -05:00
|
|
|
<MarkdownPreview height={height} content={content} type={getType} />
|
2022-03-09 04:01:58 -05:00
|
|
|
</Tabs.Item>
|
|
|
|
</Tabs>
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-03-09 04:01:58 -05:00
|
|
|
</div >
|
|
|
|
</Card >
|
|
|
|
<Spacer height={1} />
|
|
|
|
</>
|
2022-03-06 19:46:59 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default memo(Document)
|