aboutsummaryrefslogtreecommitdiff
path: root/apps/web/lib/ExternalDroppedContent.ts
blob: 1333cf20b6ab9c314d8cc88d9c5f309617be61e7 (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
import { Editor } from "tldraw";
import createEmbedsFromUrl from "./createEmbeds";

function processURL(input: string): string | null {
	let str = input.trim();
	if (!/^(?:f|ht)tps?:\/\//i.test(str)) {
		str = "http://" + str;
	}
	try {
		const url = new URL(str);
		return url.href;
	} catch {
		return str.match(
			/^(https?:\/\/)?(www\.)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(\/.*)?$/i,
		)
			? str
			: null;
	}
}

function formatContent(title: string, content: string): string {
	const totalLength = title.length + content.length;
	const totalHeight = Math.ceil(totalLength * (2 / 3));
	const titleLines = Math.ceil(totalHeight * (2 / 5));
	const contentLines = totalHeight - titleLines;

	const titleWithNewLines = title + "\n".repeat(titleLines);
	const contentWithNewLines = content + "\n".repeat(contentLines);

	return `${titleWithNewLines.trim()}\n\n${contentWithNewLines.trim()}`;
}

function formatTextToRatio(text: string): { height: number; width: number } {
	const RATIO = 4 / 3;
	const FONT_SIZE = 15;
	const CHAR_WIDTH = FONT_SIZE * 0.6;
	const LINE_HEIGHT = FONT_SIZE * 1.2;
	const MIN_WIDTH = 200;

	let width = Math.min(
		800,
		Math.max(MIN_WIDTH, Math.ceil(text.length * CHAR_WIDTH)),
	);

	width = Math.ceil(width / 4) * 4;

	const maxLineWidth = Math.floor(width / CHAR_WIDTH);

	const words = text.split(" ");
	let lines: string[] = [];
	let currentLine = "";

	words.forEach((word) => {
		if ((currentLine + word).length <= maxLineWidth) {
			currentLine += (currentLine ? " " : "") + word;
		} else {
			lines.push(currentLine);
			currentLine = word;
		}
	});
	if (currentLine) {
		lines.push(currentLine);
	}

	let height = Math.ceil(lines.length * LINE_HEIGHT);

	if (width / height > RATIO) {
		width = Math.ceil(height * RATIO);
	} else {
		height = Math.ceil(width / RATIO);
	}

	return { height, width };
}

type CardData = {
	title: string;
	type: string;
	content: string;
	text: boolean;
};

type DroppedData = CardData | string | { imageUrl: string };

export function handleExternalDroppedContent({
	droppedData,
	editor,
}: {
	droppedData: DroppedData;
	editor: Editor;
}) {
	const position = editor.inputs.shiftKey
		? editor.inputs.currentPagePoint
		: editor.getViewportPageBounds().center;

	if (typeof droppedData === "string") {
		const processedURL = processURL(droppedData);
		if (processedURL) {
			createEmbedsFromUrl({ editor, url: processedURL });
		} else {
			createTextCard(editor, position, droppedData, "String Content");
		}
	} else if ("imageUrl" in droppedData) {
	} else {
		const { content, title, type, text } = droppedData;
		if (text) {
			const { height, width } = formatTextToRatio(content);
			editor.createShape({
				type: "text",
				x: position.x - width / 2,
				y: position.y - height / 2,
				props: { text: formatContent(title, content) },
			});
		} else {
			createTextCard(editor, position, title, type, content);
		}
	}
}

function createTextCard(
	editor: Editor,
	position: { x: number; y: number },
	content: string,
	type: string,
	extraInfo: string = "",
) {
	const { height, width } = formatTextToRatio(content);
	editor.createShape({
		type: "Textcard",
		x: position.x - width / 2,
		y: position.y - height / 2,
		props: { content, type, extrainfo: extraInfo, h: 200, w: 400 },
	});
}