2022-04-09 20:48:19 -04:00
|
|
|
import Bold from "@geist-ui/icons/bold"
|
|
|
|
import Italic from "@geist-ui/icons/italic"
|
|
|
|
import Link from "@geist-ui/icons/link"
|
|
|
|
import ImageIcon from "@geist-ui/icons/image"
|
2022-03-08 03:37:18 -05:00
|
|
|
import { RefObject, useCallback, useMemo } from "react"
|
2022-04-09 20:48:19 -04:00
|
|
|
import styles from "../document.module.css"
|
2022-03-22 23:06:15 -04:00
|
|
|
import { Button, ButtonGroup } from "@geist-ui/core"
|
2022-03-08 03:37:18 -05:00
|
|
|
|
|
|
|
// TODO: clean up
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const FormattingIcons = ({
|
|
|
|
textareaRef,
|
|
|
|
setText
|
|
|
|
}: {
|
|
|
|
textareaRef?: RefObject<HTMLTextAreaElement>
|
|
|
|
setText?: (text: string) => void
|
|
|
|
}) => {
|
|
|
|
// const { textBefore, textAfter, selectedText } = useMemo(() => {
|
|
|
|
// if (textareaRef && textareaRef.current) {
|
|
|
|
// const textarea = textareaRef.current
|
|
|
|
// const text = textareaRef.current.value
|
|
|
|
// const selectionStart = textarea.selectionStart
|
|
|
|
// const selectionEnd = textarea.selectionEnd
|
|
|
|
// const textBefore = text.substring(0, selectionStart)
|
|
|
|
// const textAfter = text.substring(selectionEnd)
|
|
|
|
// const selectedText = text.substring(selectionStart, selectionEnd)
|
|
|
|
// return { textBefore, textAfter, selectedText }
|
|
|
|
// }
|
|
|
|
// return { textBefore: '', textAfter: '' }
|
|
|
|
// }, [textareaRef,])
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const handleBoldClick = useCallback(() => {
|
|
|
|
if (textareaRef?.current && setText) {
|
|
|
|
const selectionStart = textareaRef.current.selectionStart
|
|
|
|
const selectionEnd = textareaRef.current.selectionEnd
|
|
|
|
const text = textareaRef.current.value
|
|
|
|
const before = text.substring(0, selectionStart)
|
|
|
|
const after = text.substring(selectionEnd)
|
|
|
|
const selectedText = text.substring(selectionStart, selectionEnd)
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const newText = `${before}**${selectedText}**${after}`
|
|
|
|
setText(newText)
|
|
|
|
}
|
|
|
|
}, [setText, textareaRef])
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const handleItalicClick = useCallback(() => {
|
|
|
|
if (textareaRef?.current && setText) {
|
|
|
|
const selectionStart = textareaRef.current.selectionStart
|
|
|
|
const selectionEnd = textareaRef.current.selectionEnd
|
|
|
|
const text = textareaRef.current.value
|
|
|
|
const before = text.substring(0, selectionStart)
|
|
|
|
const after = text.substring(selectionEnd)
|
|
|
|
const selectedText = text.substring(selectionStart, selectionEnd)
|
|
|
|
const newText = `${before}*${selectedText}*${after}`
|
|
|
|
setText(newText)
|
|
|
|
}
|
|
|
|
}, [setText, textareaRef])
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const handleLinkClick = useCallback(() => {
|
|
|
|
if (textareaRef?.current && setText) {
|
|
|
|
const selectionStart = textareaRef.current.selectionStart
|
|
|
|
const selectionEnd = textareaRef.current.selectionEnd
|
|
|
|
const text = textareaRef.current.value
|
|
|
|
const before = text.substring(0, selectionStart)
|
|
|
|
const after = text.substring(selectionEnd)
|
|
|
|
const selectedText = text.substring(selectionStart, selectionEnd)
|
|
|
|
let formattedText = ""
|
|
|
|
if (selectedText.includes("http")) {
|
|
|
|
formattedText = `[](${selectedText})`
|
|
|
|
} else {
|
|
|
|
formattedText = `[${selectedText}](https://)`
|
|
|
|
}
|
|
|
|
const newText = `${before}${formattedText}${after}`
|
|
|
|
setText(newText)
|
|
|
|
}
|
|
|
|
}, [setText, textareaRef])
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const handleImageClick = useCallback(() => {
|
|
|
|
if (textareaRef?.current && setText) {
|
|
|
|
const selectionStart = textareaRef.current.selectionStart
|
|
|
|
const selectionEnd = textareaRef.current.selectionEnd
|
|
|
|
const text = textareaRef.current.value
|
|
|
|
const before = text.substring(0, selectionStart)
|
|
|
|
const after = text.substring(selectionEnd)
|
|
|
|
const selectedText = text.substring(selectionStart, selectionEnd)
|
|
|
|
let formattedText = ""
|
|
|
|
if (selectedText.includes("http")) {
|
|
|
|
formattedText = `![](${selectedText})`
|
|
|
|
} else {
|
|
|
|
formattedText = `![${selectedText}](https://)`
|
|
|
|
}
|
|
|
|
const newText = `${before}${formattedText}${after}`
|
|
|
|
setText(newText)
|
|
|
|
}
|
|
|
|
}, [setText, textareaRef])
|
2022-03-08 16:19:42 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const formattingActions = useMemo(
|
|
|
|
() => [
|
|
|
|
{
|
|
|
|
icon: <Bold />,
|
|
|
|
name: "bold",
|
|
|
|
action: handleBoldClick
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: <Italic />,
|
|
|
|
name: "italic",
|
|
|
|
action: handleItalicClick
|
|
|
|
},
|
|
|
|
// {
|
|
|
|
// icon: <Underline />,
|
|
|
|
// name: 'underline',
|
|
|
|
// action: handleUnderlineClick
|
|
|
|
// },
|
|
|
|
{
|
|
|
|
icon: <Link />,
|
|
|
|
name: "hyperlink",
|
|
|
|
action: handleLinkClick
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: <ImageIcon />,
|
|
|
|
name: "image",
|
|
|
|
action: handleImageClick
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[handleBoldClick, handleImageClick, handleItalicClick, handleLinkClick]
|
|
|
|
)
|
2022-03-08 03:37:18 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.actionWrapper}>
|
|
|
|
<ButtonGroup className={styles.actions}>
|
|
|
|
{formattingActions.map(({ icon, name, action }) => (
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
scale={2 / 3}
|
|
|
|
px={0.6}
|
|
|
|
aria-label={name}
|
|
|
|
key={name}
|
|
|
|
icon={icon}
|
|
|
|
onMouseDown={(e) => e.preventDefault()}
|
|
|
|
onClick={action}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-08 03:37:18 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 17:20:20 -04:00
|
|
|
export default FormattingIcons
|