import React, { useRef, useCallback, useEffect, useContext } from "react"; import { useEditor } from "tldraw"; import DragContext, { DragContextType, useDragContext, } from "../../lib/context"; import { handleExternalDroppedContent } from "../../lib/createEmbeds"; const stripHtmlTags = (html: string): string => { const div = document.createElement("div"); div.innerHTML = html; return div.textContent || div.innerText || ""; }; function formatTextToRatio(text: string) { const totalWidth = text.length; const maxLineWidth = Math.floor(totalWidth / 4); const words = text.split(" "); let lines = []; let currentLine = ""; words.forEach((word) => { // Check if adding the next word exceeds the maximum line width if ((currentLine + word).length <= maxLineWidth) { currentLine += (currentLine ? " " : "") + word; } else { // If the current line is full, push it to new line lines.push(currentLine); currentLine = word; } }); if (currentLine) { lines.push(currentLine); } return lines.join("\n"); } function DropZone() { const dropRef = useRef(null); const { isDraggingOver, setIsDraggingOver } = useDragContext(); const editor = useEditor(); const handleDragLeave = () => { setIsDraggingOver(false); console.log("leaver"); }; useEffect(() => { setInterval(() => { editor.selectAll(); const shapes = editor.getSelectedShapes(); const text = shapes.filter((s) => s.type === "text"); console.log("hrhh", text); }, 5000); }, []); const handleDrop = useCallback((event: DragEvent) => { event.preventDefault(); setIsDraggingOver(false); const dt = event.dataTransfer; if (!dt) { return; } const items = dt.items; for (let i = 0; i < items.length; i++) { if (items[i]!.kind === "file" && items[i]!.type.startsWith("image/")) { const file = items[i]!.getAsFile(); if (file) { const reader = new FileReader(); reader.onload = (e) => { if (e.target) { // setDroppedImage(e.target.result as string); } }; reader.readAsDataURL(file); } } else if (items[i]!.kind === "string") { items[i]!.getAsString((data) => { const cleanText = stripHtmlTags(data); const onethree = formatTextToRatio(cleanText); handleExternalDroppedContent({ editor, text: onethree }); }); } } }, []); useEffect(() => { const divElement = dropRef.current; if (divElement) { divElement.addEventListener("drop", handleDrop); divElement.addEventListener("dragleave", handleDragLeave); } return () => { if (divElement) { divElement.removeEventListener("drop", handleDrop); divElement.addEventListener("dragleave", handleDragLeave); } }; }, []); return (
{isDraggingOver && ( <>

Drop here to add Content on Canvas

)}
); } function TopRight() { return ( ); } function TopLeft() { return ( ); } function BottomLeft() { return ( ); } function BottomRight() { return ( ); } export default DropZone;