|
|
|
@ -1,5 +1,5 @@
|
|
|
|
import { ButtonGroup, Button } from "@geist-ui/core"
|
|
|
|
import { ButtonGroup, Button } from "@geist-ui/core"
|
|
|
|
import { Bold, Italic, Underline, Link } from '@geist-ui/icons'
|
|
|
|
import { Bold, Italic, Underline, Link, Image as ImageIcon } from '@geist-ui/icons'
|
|
|
|
import { RefObject, useCallback, useMemo } from "react"
|
|
|
|
import { RefObject, useCallback, useMemo } from "react"
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: clean up
|
|
|
|
// TODO: clean up
|
|
|
|
@ -72,6 +72,27 @@ const FormattingIcons = ({ textareaRef, setText }: { textareaRef?: RefObject<HTM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [setText, textareaRef])
|
|
|
|
}, [setText, textareaRef])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleImageClick = useCallback((e) => {
|
|
|
|
|
|
|
|
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 = ``
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
formattedText = ``
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const newText = `${before}${formattedText}${after}`
|
|
|
|
|
|
|
|
setText(newText)
|
|
|
|
|
|
|
|
textareaRef.current.focus()
|
|
|
|
|
|
|
|
textareaRef.current.setSelectionRange(before.length + 1, before.length + 1 + selectedText.length)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, [setText, textareaRef])
|
|
|
|
|
|
|
|
|
|
|
|
const formattingActions = useMemo(() => [
|
|
|
|
const formattingActions = useMemo(() => [
|
|
|
|
{
|
|
|
|
{
|
|
|
|
icon: <Bold />,
|
|
|
|
icon: <Bold />,
|
|
|
|
@ -92,8 +113,13 @@ const FormattingIcons = ({ textareaRef, setText }: { textareaRef?: RefObject<HTM
|
|
|
|
icon: <Link />,
|
|
|
|
icon: <Link />,
|
|
|
|
name: 'hyperlink',
|
|
|
|
name: 'hyperlink',
|
|
|
|
action: handleLinkClick
|
|
|
|
action: handleLinkClick
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
icon: <ImageIcon />,
|
|
|
|
|
|
|
|
name: 'image',
|
|
|
|
|
|
|
|
action: handleImageClick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], [handleBoldClick, handleItalicClick, handleLinkClick])
|
|
|
|
], [handleBoldClick, handleImageClick, handleItalicClick, handleLinkClick])
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div style={{ position: 'relative', zIndex: 1 }}>
|
|
|
|
<div style={{ position: 'relative', zIndex: 1 }}>
|
|
|
|
@ -102,7 +128,7 @@ const FormattingIcons = ({ textareaRef, setText }: { textareaRef?: RefObject<HTM
|
|
|
|
right: 0,
|
|
|
|
right: 0,
|
|
|
|
}}>
|
|
|
|
}}>
|
|
|
|
{formattingActions.map(({ icon, name, action }) => (
|
|
|
|
{formattingActions.map(({ icon, name, action }) => (
|
|
|
|
<Button aria-label={name} key={name} icon={icon} onMouseDown={(e) => e.preventDefault()} onClick={action} />
|
|
|
|
<Button auto scale={2 / 3} px={0.6} aria-label={name} key={name} icon={icon} onMouseDown={(e) => e.preventDefault()} onClick={action} />
|
|
|
|
))}
|
|
|
|
))}
|
|
|
|
</ButtonGroup>
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|