2022-03-22 23:06:15 -04:00
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-03-23 00:18:26 -04:00
|
|
|
import { memo, useRef, useState } from "react"
|
2022-03-06 19:46:59 -05:00
|
|
|
import styles from './document.module.css'
|
2022-03-21 18:55:21 -04:00
|
|
|
import Download from '@geist-ui/icons/download'
|
|
|
|
import ExternalLink from '@geist-ui/icons/externalLink'
|
2022-03-09 20:11:37 -05:00
|
|
|
import Skeleton from "react-loading-skeleton"
|
2022-03-21 23:43:50 -04:00
|
|
|
|
2022-03-26 03:05:05 -04:00
|
|
|
import { Button, Text, ButtonGroup, Spacer, Tabs, Textarea, Tooltip, Link, Tag } from "@geist-ui/core"
|
2022-03-23 15:36:29 -04:00
|
|
|
import HtmlPreview from "@components/preview"
|
2022-03-21 23:43:50 -04:00
|
|
|
|
2022-03-11 21:48:40 -05:00
|
|
|
// import Link from "next/link"
|
2022-03-06 19:46:59 -05:00
|
|
|
type Props = {
|
2022-03-23 00:18:26 -04:00
|
|
|
title: string
|
2022-03-07 01:16:08 -05:00
|
|
|
initialTab?: "edit" | "preview"
|
2022-03-09 20:11:37 -05:00
|
|
|
skeleton?: boolean
|
2022-03-23 00:18:26 -04:00
|
|
|
id: string
|
|
|
|
content: string
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-11 21:48:40 -05:00
|
|
|
const DownloadButton = ({ rawLink }: { rawLink?: string }) => {
|
|
|
|
return (<div className={styles.actionWrapper}>
|
|
|
|
<ButtonGroup className={styles.actions}>
|
|
|
|
<Tooltip text="Download">
|
|
|
|
<a href={`${rawLink}?download=true`} target="_blank" rel="noopener noreferrer">
|
|
|
|
<Button
|
|
|
|
scale={2 / 3} px={0.6}
|
|
|
|
icon={<Download />}
|
|
|
|
auto
|
|
|
|
aria-label="Download"
|
|
|
|
/>
|
|
|
|
</a>
|
|
|
|
</Tooltip>
|
|
|
|
<Tooltip text="Open raw in new tab">
|
|
|
|
<a href={rawLink} target="_blank" rel="noopener noreferrer">
|
|
|
|
<Button
|
|
|
|
scale={2 / 3} px={0.6}
|
|
|
|
icon={<ExternalLink />}
|
|
|
|
auto
|
|
|
|
aria-label="Open raw file in new tab"
|
|
|
|
/>
|
|
|
|
</a>
|
|
|
|
</Tooltip>
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-23 15:36:29 -04:00
|
|
|
const Document = ({ content, title, initialTab = 'edit', skeleton, id }: 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-21 03:46:15 -04:00
|
|
|
// const height = editable ? "500px" : '100%'
|
|
|
|
const height = "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-22 01:50:25 -04:00
|
|
|
const rawLink = () => {
|
2022-03-11 21:48:40 -05:00
|
|
|
if (id) {
|
|
|
|
return `/file/raw/${id}`
|
|
|
|
}
|
2022-03-22 01:50:25 -04:00
|
|
|
}
|
2022-03-11 21:48:40 -05:00
|
|
|
|
2022-03-09 20:11:37 -05:00
|
|
|
if (skeleton) {
|
|
|
|
return <>
|
|
|
|
<Spacer height={1} />
|
2022-03-23 19:42:56 -04:00
|
|
|
<div className={styles.card}>
|
2022-03-09 20:11:37 -05:00
|
|
|
<div className={styles.fileNameContainer}>
|
|
|
|
<Skeleton width={275} 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 >
|
2022-03-23 19:42:56 -04:00
|
|
|
</div>
|
2022-03-09 20:11:37 -05:00
|
|
|
</>
|
|
|
|
}
|
2022-03-22 01:50:25 -04:00
|
|
|
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
return (
|
2022-03-09 04:01:58 -05:00
|
|
|
<>
|
|
|
|
<Spacer height={1} />
|
2022-03-23 19:28:39 -04:00
|
|
|
<div className={styles.card}>
|
2022-03-26 03:05:05 -04:00
|
|
|
<Link href={`#${title}`} className={styles.fileNameContainer}>
|
|
|
|
<Tag height={"100%"} id={`${title}`} width={"100%"} style={{ borderRadius: 0 }}>
|
|
|
|
{title || 'Untitled'}
|
|
|
|
</Tag>
|
|
|
|
</Link>
|
2022-03-09 04:01:58 -05:00
|
|
|
<div className={styles.descriptionContainer}>
|
2022-03-23 00:18:26 -04:00
|
|
|
<DownloadButton rawLink={rawLink()} />
|
2022-03-09 04:01:58 -05:00
|
|
|
<Tabs onChange={handleTabChange} initialValue={initialTab} hideDivider leftSpace={0}>
|
2022-03-23 00:18:26 -04:00
|
|
|
<Tabs.Item label={"Raw"} value="edit">
|
2022-03-09 04:01:58 -05:00
|
|
|
{/* <textarea className={styles.lineCounter} wrap='off' readOnly ref={lineNumberRef}>1.</textarea> */}
|
2022-03-23 19:42:56 -04:00
|
|
|
<div style={{ marginTop: 'var(--gap-half)', display: 'flex', flexDirection: 'column' }}>
|
2022-03-09 04:01:58 -05:00
|
|
|
<Textarea
|
2022-03-23 00:18:26 -04:00
|
|
|
readOnly
|
2022-03-09 04:01:58 -05:00
|
|
|
ref={codeEditorRef}
|
|
|
|
value={content}
|
|
|
|
width="100%"
|
|
|
|
// 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-23 19:42:56 -04:00
|
|
|
<div style={{ marginTop: 'var(--gap-half)', }}>
|
2022-03-24 21:03:57 -04:00
|
|
|
<HtmlPreview height={height} fileId={id} content={content} title={title} />
|
2022-03-23 19:42:56 -04:00
|
|
|
</div>
|
2022-03-09 04:01:58 -05:00
|
|
|
</Tabs.Item>
|
|
|
|
</Tabs>
|
|
|
|
</div >
|
2022-03-23 19:28:39 -04:00
|
|
|
</div >
|
2022-03-09 04:01:58 -05:00
|
|
|
</>
|
2022-03-06 19:46:59 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default memo(Document)
|