-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
167 lines (137 loc) · 3.44 KB
/
utils.ts
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
import qs from "qs";
import jsPDF from "jspdf";
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { CircleIcon, ImageIcon, PentagonIcon, SlashIcon, SquareIcon, TriangleIcon, TypeIcon } from "lucide-react";
const adjectives = [
"Happy",
"Creative",
"Energetic",
"Lively",
"Dynamic",
"Radiant",
"Joyful",
"Vibrant",
"Cheerful",
"Sunny",
"Sparkling",
"Bright",
"Shining",
];
const animals = [
"Dolphin",
"Tiger",
"Elephant",
"Penguin",
"Kangaroo",
"Panther",
"Lion",
"Cheetah",
"Giraffe",
"Hippopotamus",
"Monkey",
"Panda",
"Crocodile",
];
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function generateRandomName(): string {
const randomAdjective = adjectives[Math.floor(Math.random() * adjectives.length)];
const randomAnimal = animals[Math.floor(Math.random() * animals.length)];
return `${randomAdjective} ${randomAnimal}`;
}
export function getShapeInfo(shapeType: string) {
switch (shapeType) {
case "rect":
return {
icon: SquareIcon,
name: "Rectangle",
};
case "circle":
return {
icon: CircleIcon,
name: "Circle",
};
case "triangle":
return {
icon: TriangleIcon,
name: "Triangle",
};
case "line":
return {
icon: SlashIcon,
name: "Line",
};
case "i-text":
return {
icon: TypeIcon,
name: "Text",
};
case "image":
return {
icon: ImageIcon,
name: "Image",
};
case "freeform":
return {
icon: PentagonIcon,
name: "Free Drawing",
};
default:
return {
icon: SquareIcon,
name: shapeType,
};
}
}
export function exportToPDF() {
const canvas = document.querySelector("canvas");
if (!canvas) return;
const doc = new jsPDF({
orientation: "landscape",
unit: "px",
format: [canvas.width, canvas.height],
});
const data = canvas.toDataURL();
doc.addImage(data, "png", 0, 0, canvas.width, canvas.height);
doc.save("canvas.pdf");
}
export function formUrlQuery(queryString: string, key: string, value: string | number | null) {
const params = { ...qs.parse(queryString.toString()), [key]: value };
return `?${qs.stringify(params, {
skipNulls: true,
})}`;
}
export function removeKeysFromQuery(queryString: string, keysToRemove: string[]) {
const currentUrl = qs.parse(queryString);
keysToRemove.forEach((key) => {
delete currentUrl[key];
});
// Remove null or undefined values
Object.keys(currentUrl).forEach((key) => currentUrl[key] == null && delete currentUrl[key]);
return `?${qs.stringify(currentUrl)}`;
}
export function elapsedTime(date: Date) {
const now = new Date();
const inputDate = new Date(date);
const diffInSeconds = Math.floor((now.getTime() - inputDate.getTime()) / 1000);
const times = [
{ unit: "year", seconds: 31536000 },
{ unit: "month", seconds: 2592000 },
{ unit: "week", seconds: 604800 },
{ unit: "day", seconds: 86400 },
{ unit: "hour", seconds: 3600 },
{ unit: "minute", seconds: 60 },
{ unit: "second", seconds: 1 },
];
for (const time of times) {
const interval = Math.floor(diffInSeconds / time.seconds);
if (interval > 1) {
return `${interval} ${time.unit}s ago`;
} else if (interval === 1) {
return `1 ${time.unit} ago`;
}
}
return "just now";
}