From 939539611f0cad12056f7be78ef6b2128b90b779 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Wed, 29 May 2024 11:03:40 -0400 Subject: [PATCH 01/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a7531a..1762f91 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ We also just added experimental support for taking a video/screen recording of a ## Sponsors - + ## 🚀 Try It Out without no install From 97cdb093a5fb655b1cbd8db71848ffd8bbb507f2 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Wed, 29 May 2024 14:40:46 -0400 Subject: [PATCH 02/10] basic UI for selecting and editing --- frontend/src/components/Preview.tsx | 77 ++++++++++++++++++- .../components/select-and-edit/EditPopup.tsx | 52 +++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 frontend/src/components/select-and-edit/EditPopup.tsx diff --git a/frontend/src/components/Preview.tsx b/frontend/src/components/Preview.tsx index eb9ea6d..5e0c759 100644 --- a/frontend/src/components/Preview.tsx +++ b/frontend/src/components/Preview.tsx @@ -1,6 +1,7 @@ -import { useEffect, useRef } from "react"; +import { useEffect, useRef, useState } from "react"; import classNames from "classnames"; import useThrottle from "../hooks/useThrottle"; +import EditPopup from "./select-and-edit/EditPopup"; interface Props { code: string; @@ -13,9 +14,70 @@ function Preview({ code, device }: Props) { // Don't update code more often than every 200ms. const throttledCode = useThrottle(code, 200); + const [selectedElement, setSelectedElement] = useState( + null + ); + const [popupVisible, setPopupVisible] = useState(false); + const [popupPosition, setPopupPosition] = useState({ x: 0, y: 0 }); + const [editText, setEditText] = useState(""); + + console.log(selectedElement); + + function updateHighlight(targetElement: HTMLElement) { + setSelectedElement((prev) => { + // Remove style from previous element + if (prev) { + prev.style.outline = ""; + prev.style.backgroundColor = ""; + } + // Add style to new element + targetElement.style.outline = "2px dashed #1846db"; + targetElement.style.backgroundColor = "#bfcbf5"; + return targetElement; + }); + } + + function handleClick(event: MouseEvent) { + const { clientX, clientY } = event; + + // Prevent default to avoid issues like label clicks triggering textareas, etc. + event.preventDefault(); + + const targetElement = event.target as HTMLElement; + + // Return if no target element + if (!targetElement) { + return; + } + + // Highlight the selected element + updateHighlight(targetElement); + + // Show popup at click position + setPopupVisible(false); + setPopupPosition({ x: clientX, y: clientY }); + setPopupVisible(true); + } + + function handleEditSubmit() { + if (selectedElement) { + selectedElement.innerText = editText; + } + setPopupVisible(false); + } + useEffect(() => { - if (iframeRef.current) { - iframeRef.current.srcdoc = throttledCode; + const iframe = iframeRef.current; + if (iframe) { + iframe.srcdoc = throttledCode; + + // Related to + iframe.addEventListener("load", function () { + iframe.contentWindow?.document.body.addEventListener( + "click", + handleClick + ); + }); } }, [throttledCode]); @@ -34,6 +96,15 @@ function Preview({ code, device }: Props) { } )} > + ); } diff --git a/frontend/src/components/select-and-edit/EditPopup.tsx b/frontend/src/components/select-and-edit/EditPopup.tsx new file mode 100644 index 0000000..c6560c6 --- /dev/null +++ b/frontend/src/components/select-and-edit/EditPopup.tsx @@ -0,0 +1,52 @@ +import React, { useEffect, useRef } from "react"; +import { Textarea } from "../ui/textarea"; +import { Button } from "../ui/button"; + +interface EditPopupProps { + popupVisible: boolean; + popupPosition: { x: number; y: number }; + editText: string; + setEditText: (text: string) => void; + handleEditSubmit: () => void; +} + +const EditPopup: React.FC = ({ + popupVisible, + popupPosition, + editText, + setEditText, + handleEditSubmit, +}) => { + const textareaRef = useRef(null); + + useEffect(() => { + if (popupVisible && textareaRef.current) { + textareaRef.current.focus(); + } + }, [popupVisible]); + + // useEffect(() => { + // if (!popupVisible) { + // setEditText(""); + // } + // }, [popupVisible, setEditText]); + + return ( +
+