aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/canvas/dropComponent.tsx
blob: df7a55ad7b38b5d75275a94d9be09c14e571f6cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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<HTMLDivElement | null>(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 (
		<div
			className={`h-full flex justify-center items-center w-full absolute top-0 left-0 z-[100000] pointer-events-none  ${isDraggingOver && "bg-[#2c3439ad]  pointer-events-auto"}`}
			ref={dropRef}
		>
			{isDraggingOver && (
				<>
					<div className="absolute top-4 left-8">
						<TopRight />
					</div>
					<div className="absolute top-4 right-8">
						<TopLeft />
					</div>
					<div className="absolute bottom-4 left-8">
						<BottomLeft />
					</div>
					<div className="absolute bottom-4 right-8">
						<BottomRight />
					</div>
					<h2 className="text-2xl">Drop here to add Content on Canvas</h2>
				</>
			)}
		</div>
	);
}

function TopRight() {
	return (
		<svg
			width="48"
			height="48"
			viewBox="0 0 48 48"
			fill="none"
			xmlns="http://www.w3.org/2000/svg"
		>
			<path
				d="M44 4H12C7.58172 4 4 7.58172 4 12V44"
				stroke="white"
				stroke-width="8"
				stroke-linecap="round"
			/>
		</svg>
	);
}

function TopLeft() {
	return (
		<svg
			width="48"
			height="48"
			viewBox="0 0 48 48"
			fill="none"
			xmlns="http://www.w3.org/2000/svg"
		>
			<path
				d="M4 4H36C40.4183 4 44 7.58172 44 12V44"
				stroke="white"
				stroke-width="8"
				stroke-linecap="round"
			/>
		</svg>
	);
}

function BottomLeft() {
	return (
		<svg
			width="48"
			height="48"
			viewBox="0 0 48 48"
			fill="none"
			xmlns="http://www.w3.org/2000/svg"
		>
			<path
				d="M44 44H12C7.58172 44 4 40.4183 4 36V4"
				stroke="white"
				stroke-width="8"
				stroke-linecap="round"
			/>
		</svg>
	);
}

function BottomRight() {
	return (
		<svg
			width="48"
			height="48"
			viewBox="0 0 48 48"
			fill="none"
			xmlns="http://www.w3.org/2000/svg"
		>
			<path
				d="M4 44H36C40.4183 44 44 40.4183 44 36V4"
				stroke="white"
				stroke-width="8"
				stroke-linecap="round"
			/>
		</svg>
	);
}

export default DropZone;