From b431754d713b067205bfec377792034825aa701e Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Wed, 4 Oct 2023 07:44:59 +0800 Subject: [PATCH] fix input ref not found error --- app/src/components/EditorProvider.tsx | 151 ++++++++++++++++++++++++++ app/src/components/RichEditor.tsx | 143 ------------------------ app/src/routes/Home.tsx | 4 +- 3 files changed, 153 insertions(+), 145 deletions(-) create mode 100644 app/src/components/EditorProvider.tsx delete mode 100644 app/src/components/RichEditor.tsx diff --git a/app/src/components/EditorProvider.tsx b/app/src/components/EditorProvider.tsx new file mode 100644 index 00000000..29bb629f --- /dev/null +++ b/app/src/components/EditorProvider.tsx @@ -0,0 +1,151 @@ +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "./ui/dialog.tsx"; +import { Edit, Image, MenuSquare, PanelRight } from "lucide-react"; +import { useTranslation } from "react-i18next"; +import "../assets/editor.less"; +import { Textarea } from "./ui/textarea.tsx"; +import Markdown from "./Markdown.tsx"; +import { useEffect, useRef, useState } from "react"; +import { Toggle } from "./ui/toggle.tsx"; +import {mobile} from "../utils.ts"; + +type RichEditorProps = { + value: string; + onChange: (value: string) => void; + className?: string; + id?: string; + placeholder?: string; + maxLength?: number; +}; + +function RichEditor({ + value, + onChange, + id, + placeholder, + maxLength, +}: RichEditorProps) { + const input = useRef(null); + const [openPreview, setOpenPreview] = useState(!mobile); + const [openInput, setOpenInput] = useState(true); + + useEffect(() => { + if (!input.current) return; + const target = input.current as HTMLElement; + const preview = target.parentElement?.querySelector( + ".editor-preview", + ) as HTMLElement; + + const listener = () => { + preview.style.height = `${target.clientHeight}px`; + }; + target.addEventListener("transitionstart", listener); + setInterval(listener, 250); + target.addEventListener("scroll", () => { + preview.scrollTop = target.scrollTop; + }); + + preview.style.height = `${target.clientHeight}px`; + + if (openInput) target.focus(); + }, [input]); + + return ( +
+
+
+ { + setOpenPreview(false); + setOpenInput(true); + }} + > + + + + { + setOpenPreview(true); + setOpenInput(true); + }} + > + + + + { + setOpenPreview(true); + setOpenInput(false); + }} + > + + +
+
+
+ {openInput && ( +