From 575e747e76494a8e5b8506c23c3ee13a072af4dc Mon Sep 17 00:00:00 2001 From: Stian Alexander Solli Date: Fri, 1 Mar 2024 17:52:53 +0100 Subject: [PATCH 01/11] Started with creating a markdown editor, still needs more functionality and some css fixing --- src/components/molecules/forms/eventForm/EventForm.tsx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/components/molecules/forms/eventForm/EventForm.tsx b/src/components/molecules/forms/eventForm/EventForm.tsx index 305daa9f..79083825 100644 --- a/src/components/molecules/forms/eventForm/EventForm.tsx +++ b/src/components/molecules/forms/eventForm/EventForm.tsx @@ -18,6 +18,7 @@ import { Button } from '@chakra-ui/react'; import { createEvent, uploadEventPicture } from 'api'; import { useHistory } from 'react-router-dom'; import Textarea from 'components/atoms/textarea/Textarea'; +import MarkdownEditor from 'components/atoms/markdown/MarkdownEditor'; import ToggleButton from 'components/atoms/toggleButton/ToggleButton'; import DropdownHeader from 'components/atoms/dropdown/dropdownHeader/DropdownHeader'; import { useToast } from 'hooks/useToast'; @@ -197,6 +198,15 @@ const EventForm = () => { error={fields['description'].error} /> + + From f6d28db159eba22d173b51e216c8e0a5d26bf644 Mon Sep 17 00:00:00 2001 From: Stian Alexander Solli Date: Fri, 1 Mar 2024 17:53:11 +0100 Subject: [PATCH 02/11] Started with creating a markdown editor, still needs more functionality and some css fixing --- .../atoms/markdown/MarkdownEditor.tsx | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/components/atoms/markdown/MarkdownEditor.tsx diff --git a/src/components/atoms/markdown/MarkdownEditor.tsx b/src/components/atoms/markdown/MarkdownEditor.tsx new file mode 100644 index 00000000..8e5b40da --- /dev/null +++ b/src/components/atoms/markdown/MarkdownEditor.tsx @@ -0,0 +1,139 @@ +import React, { useState, ChangeEvent, TextareaHTMLAttributes, useEffect } from 'react'; +import ReactMarkdown from 'react-markdown'; +import styles from '../textarea/textarea.module.scss'; +import { Button, Stack } from '@chakra-ui/react' +import { MdInsertLink , MdFormatBold, MdFormatItalic } from "react-icons/md" + + +interface MarkdownEditorProps extends TextareaHTMLAttributes { + label?: string; + maxWidth?: number; + minWidth?: number; + error?: string[] | undefined; + resize?: boolean; + } + +const MarkdownEditor: React.FC = ({ + error, + maxWidth, + minWidth, + label, + value, + resize, + onChange, + ...rest +}) => { + const [input, setInput] = useState(""); + const [isFocused, setIsFocused] = useState(false); + // 2 is textarea default value for rows + const [rows, setRows] = useState(2); + const [view, setShowPreview] = useState(false); // Add state variable for toggle + + const handleInputChange = (event: ChangeEvent) => { + setInput(event.target.value); + }; + + const getLabelStyle = () => { + return !!input || isFocused + ? `${styles.label} ${styles.styledLabel}` + : styles.label; + }; + + useEffect(() => { + if (!resize) { + return; + } + const rowlen = input ? input.toString().split('\n').length : 2; + const max = 14; + setRows(rowlen < max ? rowlen : max); + }, [input, resize]); + + const showEditor = () => { + setShowPreview(false); + }; + const showPreview = () => { + setShowPreview(true); + }; + + + const insertLink = () => { + const textarea = document.getElementById('myEditor') as HTMLTextAreaElement; + const text = "[Display text](URL)" + if (textarea !== null){ + let position = textarea.selectionStart; + const end = position + text.length; + textarea.setRangeText(text, position, end, 'select'); + } + }; + const insertBold = () => { + const textarea = document.getElementById('myEditor') as HTMLTextAreaElement; + const text = "**Text**" + if (textarea !== null){ + let position = textarea.selectionStart; + const end = position + text.length; + textarea.setRangeText(text, position, end, 'select'); + } + }; + const insertItalic = () => { + const textarea = document.getElementById('myEditor') as HTMLTextAreaElement; + const text = "[Display text](URL)" + if (textarea !== null){ + let position = textarea.selectionStart; + const end = position + text.length; + textarea.setRangeText(text, position, end, 'select'); + } + }; + const insertHeader = () => { + const textarea = document.getElementById('myEditor') as HTMLTextAreaElement; + const text = "[Display text](URL)" + if (textarea !== null){ + let position = textarea.selectionStart; + const end = position + text.length; + textarea.setRangeText(text, position, end, 'select'); + } + }; + + + + return ( +
+ + + + + + + {view ? ( // Conditionally render markdown or preview based on toggle state + {input} + ) :
+