diff --git a/client/components/edit-document/formatting-icons/index.tsx b/client/components/edit-document/formatting-icons/index.tsx index 0988a98b..08ba4133 100644 --- a/client/components/edit-document/formatting-icons/index.tsx +++ b/client/components/edit-document/formatting-icons/index.tsx @@ -5,124 +5,53 @@ import ImageIcon from "@geist-ui/icons/image" import { RefObject, useCallback, useMemo } from "react" import styles from "../document.module.css" import { Button, ButtonGroup } from "@geist-ui/core" +import { TextareaMarkdownRef } from "textarea-markdown-editor" // TODO: clean up const FormattingIcons = ({ textareaRef, - setText }: { - textareaRef?: RefObject - setText?: (text: string) => void + textareaRef?: RefObject }) => { - // 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,]) - 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) - const newText = `${before}**${selectedText}**${after}` - setText(newText) - } - }, [setText, textareaRef]) - - 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]) - - 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]) - - 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]) const formattingActions = useMemo( - () => [ - { - icon: , - name: "bold", - action: handleBoldClick - }, - { - icon: , - name: "italic", - action: handleItalicClick - }, - // { - // icon: , - // name: 'underline', - // action: handleUnderlineClick - // }, - { - icon: , - name: "hyperlink", - action: handleLinkClick - }, - { - icon: , - name: "image", - action: handleImageClick - } - ], - [handleBoldClick, handleImageClick, handleItalicClick, handleLinkClick] + () => { + const handleBoldClick = () => textareaRef?.current?.trigger("bold") + const handleItalicClick = () => textareaRef?.current?.trigger("italic") + const handleLinkClick = () => textareaRef?.current?.trigger("link") + const handleImageClick = () => textareaRef?.current?.trigger("image") + return [ + { + icon: , + name: "bold", + action: handleBoldClick + }, + { + icon: , + name: "italic", + action: handleItalicClick + }, + // { + // icon: , + // name: 'underline', + // action: handleUnderlineClick + // }, + { + icon: , + name: "hyperlink", + action: handleLinkClick + }, + { + icon: , + name: "image", + action: handleImageClick + } + ] + }, + [textareaRef] ) return ( diff --git a/client/components/edit-document/index.tsx b/client/components/edit-document/index.tsx index 2c1975c0..980a0866 100644 --- a/client/components/edit-document/index.tsx +++ b/client/components/edit-document/index.tsx @@ -9,16 +9,14 @@ import { import styles from "./document.module.css" import Trash from "@geist-ui/icons/trash" import FormattingIcons from "./formatting-icons" +import TextareaMarkdown, { TextareaMarkdownRef } from "textarea-markdown-editor"; import { Button, - ButtonGroup, - Card, Input, Spacer, Tabs, Textarea, - Tooltip } from "@geist-ui/core" import Preview from "@components/preview" @@ -27,7 +25,6 @@ type Props = { title?: string content?: string setTitle?: (title: string) => void - setContent?: (content: string) => void handleOnContentChange?: (e: ChangeEvent) => void initialTab?: "edit" | "preview" remove?: () => void @@ -40,11 +37,10 @@ const Document = ({ title, content, setTitle, - setContent, initialTab = "edit", handleOnContentChange }: Props) => { - const codeEditorRef = useRef(null) + const codeEditorRef = useRef(null) const [tab, setTab] = useState(initialTab) // const height = editable ? "500px" : '100%' const height = "100%" @@ -126,7 +122,7 @@ const Document = ({
{tab === "edit" && ( - + )} -