-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
266 lines (217 loc) · 8.44 KB
/
script.js
File metadata and controls
266 lines (217 loc) · 8.44 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
document.addEventListener("DOMContentLoaded", () => {
const copySvgCodeBtn = document.getElementById("copySvgCodeBtn");
const svgCodePreview = document.getElementById("svgCodePreview");
const tooltipElement = copySvgCodeBtn.querySelector(".tooltip");
const tickIcon = copySvgCodeBtn.querySelector(".tick-icon");
copySvgCodeBtn.addEventListener("click", () => {
// Show the tick icon
tickIcon.style.display = "inline-block";
// Create a temporary textarea to copy text
const tempTextArea = document.createElement("textarea");
tempTextArea.value = svgCodePreview.textContent;
document.body.appendChild(tempTextArea);
// Select and copy the text
tempTextArea.select();
document.execCommand("copy");
// Remove the temporary textarea
document.body.removeChild(tempTextArea);
// Animate the copy button
copySvgCodeBtn.classList.add("copied");
tooltipElement.textContent = "Copied!";
// Reset the animation after 2 seconds
setTimeout(() => {
copySvgCodeBtn.classList.remove("copied");
tooltipElement.textContent = "Copy SVG";
tickIcon.style.display = "none";
}, 2000);
});
// Reset tooltip on mouseout
copySvgCodeBtn.addEventListener("mouseout", () => {
tooltipElement.textContent = "Copy SVG";
});
});
const searchInput = document.getElementById("iconSearch");
const searchBtn = document.getElementById("searchBtn");
const iconResults = document.getElementById("iconResults");
const loadingIndicator = document.getElementById("loading");
// const prevButton = document.getElementById("prevButton");
// const nextButton = document.getElementById("nextButton");
// const pageInfo = document.getElementById("pageInfo");
let currentStart = 0;
let totalIcons = 0;
let currentLimit = 12;
// Icon Preview Modal Functionality
const iconPreviewModal = document.getElementById("iconPreviewModal");
const closePreviewModal = document.getElementById("closePreviewModal");
const modalIconName = document.getElementById("modalIconName");
const modalIconPreview = document.getElementById("modalIconPreview");
const svgCodePreview = document.getElementById("svgCodePreview");
const downloadIcon = document.getElementById("downloadIcon");
let currentSelectedIcon = null;
function openIconPreview(iconName) {
const iconPreviewModal = document.getElementById("iconPreviewModal");
const modalIconName = document.getElementById("modalIconName");
const modalIconPreview = document.getElementById("modalIconIcon");
const svgCodePreview = document.getElementById("svgCodePreview");
currentSelectedIcon = iconName;
// Set modal content
modalIconName.textContent = iconName;
// Use Iconify to render the icon
modalIconPreview.setAttribute("icon", iconName);
// Fetch SVG content
const iconElement = document.querySelector(`[data-icon="${iconName}"]`);
const svgContent = iconElement
? `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">${iconElement.innerHTML}</svg>`
: "SVG not available"; // Fallback if no SVG is found
svgCodePreview.textContent = svgContent;
// Show modal with animation
iconPreviewModal.classList.add("visible");
}
// Event Listeners
document.getElementById("closePreviewModal").addEventListener("click", () => {
const iconPreviewModal = document.getElementById("iconPreviewModal");
iconPreviewModal.classList.remove("visible");
});
// Close modal when clicking outside
document.getElementById("iconPreviewModal").addEventListener("click", (e) => {
if (e.target === e.currentTarget) {
e.currentTarget.classList.remove("visible");
}
});
closePreviewModal.addEventListener("click", () => {
iconPreviewModal.classList.remove("visible");
});
downloadIcon.addEventListener("click", () => {
const iconName = currentSelectedIcon;
const iconElement = document.querySelector(`[data-icon="${iconName}"]`);
if (!iconElement) {
console.error("Icon not found");
return;
}
// Wrap the path with the proper <svg> structure
const svgString = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">${iconElement.innerHTML}</svg>`;
// Convert SVG string to an SVG Blob and create a URL for it
const svgBlob = new Blob([svgString], { type: "image/svg+xml" });
const svgUrl = URL.createObjectURL(svgBlob);
// Create an image element
const img = new Image();
img.src = svgUrl;
img.onload = () => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${iconName.replace(/:/g, "_")}.png`;
a.click();
}, "image/png");
};
// Error handling for image loading
img.onerror = (e) => {
console.error(e);
console.error("Error loading the image");
};
});
async function searchIcons() {
const query = searchInput.value.trim();
if (!query) {
alert("Please enter a search query");
return;
}
loadingIndicator.style.display = "block";
iconResults.innerHTML = "";
try {
// Construct the search URL with parameters
const searchUrl = new URL("https://api.iconify.design/search");
searchUrl.searchParams.set("query", query);
searchUrl.searchParams.set("start", currentStart);
const response = await fetch(searchUrl);
if (!response.ok) {
throw new Error("Search request failed");
}
const data = await response.json();
// Update total icons and pagination
totalIcons = data.total;
// updatePagination();
// Render icons
if (data.icons && data.icons.length > 0) {
data.icons.forEach(async (icon) => {
const card = await createIconCard(icon);
iconResults.appendChild(card);
});
} else {
iconResults.innerHTML =
'<p style="width:100%; text-align:center;">No icons found</p>';
}
} catch (error) {
console.error("Search error:", error);
iconResults.innerHTML = `<p style="width:100%; text-align:center;">Error: ${error.message}</p>`;
} finally {
loadingIndicator.style.display = "none";
}
}
// function updatePagination() {
// const lastIcon = Math.min(currentStart + currentLimit, totalIcons);
// pageInfo.textContent = `${currentStart + 1} - ${lastIcon} of ${totalIcons}`;
// prevButton.disabled = currentStart === 0;
// nextButton.disabled = currentStart + currentLimit >= totalIcons;
// }
async function createIconCard(icon) {
const card = document.createElement("div");
card.className = "icon-card";
const preview = document.createElement("div");
preview.className = "icon-preview";
const iconElement = document.createElement("span");
iconElement.className = "iconify";
iconElement.setAttribute("data-icon", icon);
iconElement.setAttribute("width", "64");
iconElement.setAttribute("height", "64");
preview.appendChild(iconElement);
const nameDisplay = document.createElement("div");
nameDisplay.className = "icon-name";
nameDisplay.textContent = icon;
card.appendChild(preview);
card.appendChild(nameDisplay);
card.addEventListener("click", () => openIconPreview(icon));
return card;
}
function createDownloadButton(iconName, type, iconClass) {
const btn = document.createElement("button");
btn.className = "download-btn";
btn.innerHTML = `<i class="${iconClass}"></i> ${type}`;
btn.onclick = () => downloadIcon(iconName, type);
return btn;
}
// Event Listeners
searchBtn.addEventListener("click", () => {
currentStart = 0;
searchIcons();
});
searchInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
currentStart = 0;
searchIcons();
}
});
// // Pagination Buttons
// prevButton.addEventListener("click", () => {
// currentStart = Math.max(0, currentStart - currentLimit);
// searchIcons();
// // Scroll to top
// window.scrollTo({ top: 0, behavior: "smooth" });
// });
// nextButton.addEventListener("click", () => {
// currentStart += currentLimit;
// searchIcons();
// // Scroll to top
// window.scrollTo({ top: 0, behavior: "smooth" });
// });
// Initial load
window.addEventListener("load", () => {
searchInput.value = "home";
searchIcons();
});