move inSelectAndEditMode to inside EditPopup

This commit is contained in:
Abi Raja 2024-05-31 21:39:12 -04:00
parent e1265b2b75
commit 9fe32ba6e8
2 changed files with 20 additions and 26 deletions

View File

@ -2,7 +2,6 @@ import { useEffect, useRef, useState } from "react";
import classNames from "classnames"; import classNames from "classnames";
import useThrottle from "../hooks/useThrottle"; import useThrottle from "../hooks/useThrottle";
import EditPopup from "./select-and-edit/EditPopup"; import EditPopup from "./select-and-edit/EditPopup";
import { useAppStore } from "../store/app-store";
interface Props { interface Props {
code: string; code: string;
@ -11,8 +10,6 @@ interface Props {
} }
function Preview({ code, device, doUpdate }: Props) { function Preview({ code, device, doUpdate }: Props) {
const { inSelectAndEditMode } = useAppStore();
const iframeRef = useRef<HTMLIFrameElement | null>(null); const iframeRef = useRef<HTMLIFrameElement | null>(null);
// Don't update code more often than every 200ms. // Don't update code more often than every 200ms.
@ -51,12 +48,7 @@ function Preview({ code, device, doUpdate }: Props) {
} }
)} )}
></iframe> ></iframe>
<EditPopup <EditPopup event={clickEvent} iframeRef={iframeRef} doUpdate={doUpdate} />
event={clickEvent}
iframeRef={iframeRef}
inSelectAndEditMode={inSelectAndEditMode}
doUpdate={doUpdate}
/>
</div> </div>
); );
} }

View File

@ -2,33 +2,40 @@ import React, { useEffect, useRef, useState } from "react";
import { Textarea } from "../ui/textarea"; import { Textarea } from "../ui/textarea";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { addHighlight, getAdjustedCoordinates, removeHighlight } from "./utils"; import { addHighlight, getAdjustedCoordinates, removeHighlight } from "./utils";
import { useAppStore } from "../../store/app-store";
interface EditPopupProps { interface EditPopupProps {
event: MouseEvent | null; event: MouseEvent | null;
inSelectAndEditMode: boolean;
doUpdate: (updateInstruction: string, selectedElement?: HTMLElement) => void;
iframeRef: React.RefObject<HTMLIFrameElement>; iframeRef: React.RefObject<HTMLIFrameElement>;
doUpdate: (updateInstruction: string, selectedElement?: HTMLElement) => void;
} }
const EditPopup: React.FC<EditPopupProps> = ({ const EditPopup: React.FC<EditPopupProps> = ({
event, event,
inSelectAndEditMode,
doUpdate,
iframeRef, iframeRef,
doUpdate,
}) => { }) => {
// Edit state // App state
const [selectedElement, setSelectedElement] = useState< const { inSelectAndEditMode } = useAppStore();
HTMLElement | undefined
>(undefined); // Create a wrapper ref to store inSelectAndEditMode so the value is not stale
const [updateText, setUpdateText] = useState(""); // in a event listener
const inSelectAndEditModeRef = useRef(inSelectAndEditMode);
// Update the ref whenever the state changes
useEffect(() => {
inSelectAndEditModeRef.current = inSelectAndEditMode;
}, [inSelectAndEditMode]);
// Popup state // Popup state
const [popupVisible, setPopupVisible] = useState(false); const [popupVisible, setPopupVisible] = useState(false);
const [popupPosition, setPopupPosition] = useState({ x: 0, y: 0 }); const [popupPosition, setPopupPosition] = useState({ x: 0, y: 0 });
// Create a wrapper ref to store inSelectAndEditMode so the value is not stale // Edit state
// in a event listener const [selectedElement, setSelectedElement] = useState<
const inSelectAndEditModeRef = useRef(inSelectAndEditMode); HTMLElement | undefined
>(undefined);
const [updateText, setUpdateText] = useState("");
// Textarea ref for focusing // Textarea ref for focusing
const textareaRef = useRef<HTMLTextAreaElement | null>(null); const textareaRef = useRef<HTMLTextAreaElement | null>(null);
@ -47,11 +54,6 @@ const EditPopup: React.FC<EditPopupProps> = ({
setPopupVisible(false); setPopupVisible(false);
} }
// Update the ref whenever the state changes
useEffect(() => {
inSelectAndEditModeRef.current = inSelectAndEditMode;
}, [inSelectAndEditMode]);
// Remove highlight and reset state when not in select and edit mode // Remove highlight and reset state when not in select and edit mode
useEffect(() => { useEffect(() => {
if (!inSelectAndEditMode) { if (!inSelectAndEditMode) {