-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathsvgAssetSelector.js
More file actions
265 lines (225 loc) · 10.3 KB
/
svgAssetSelector.js
File metadata and controls
265 lines (225 loc) · 10.3 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
/**
* SVG Asset Selector
*
* Provides a modal UI for selecting built-in SVG images from the
* project's asset directories (images/ and mouse-art/) or uploading
* from the local file system.
*
* @module svgAssetSelector
* @see {@link https://github.com/sugarlabs/musicblocks/issues/1291}
*/
// Built-in SVG assets are loaded dynamically from js/svgAssets.json
/**
* Opens the SVG asset selector modal.
*
* Presents two tabs:
* 1. "Built-in Images" — a grid of selectable SVG previews
* 2. "Upload from Device" — triggers the existing file-upload flow
*
* @param {Function} onSelectBuiltIn - Called with the data-URL of the
* chosen built-in SVG when the user confirms a selection.
* @param {Function} onUploadFromDevice - Called (with no args) when
* the user chooses to upload a local file instead.
*/
function openSvgAssetSelector(onSelectBuiltIn, onUploadFromDevice) {
// Prevent duplicate modals
const existing = document.getElementById("svgAssetSelectorOverlay");
if (existing) existing.remove();
fetch("./js/svgAssets.json")
.then(function (response) {
if (!response.ok) {
throw new Error("HTTP " + response.status);
}
return response.json();
})
.then(function (data) {
const BUILTIN_SVG_ASSETS = data.svgs;
// ── Build DOM ────────────────────────────────────────────────
const overlay = document.createElement("div");
overlay.id = "svgAssetSelectorOverlay";
const modal = document.createElement("div");
modal.id = "svgAssetSelectorModal";
// Header
const header = document.createElement("div");
header.className = "svg-selector-header";
header.innerHTML =
"<h3>Choose an Image</h3>" +
'<button class="svg-selector-close" aria-label="Close">×</button>';
modal.appendChild(header);
// Tabs
const tabBar = document.createElement("div");
tabBar.className = "svg-selector-tabs";
const tabBuiltIn = document.createElement("button");
tabBuiltIn.className = "svg-selector-tab active";
tabBuiltIn.id = "svgTabBuiltIn";
tabBuiltIn.innerHTML =
'<span class="tab-icon material-icons">collections</span> Built-in Images';
const tabUpload = document.createElement("button");
tabUpload.className = "svg-selector-tab";
tabUpload.id = "svgTabUpload";
tabUpload.innerHTML =
'<span class="tab-icon material-icons">file_upload</span> Upload from Device';
tabBar.appendChild(tabBuiltIn);
tabBar.appendChild(tabUpload);
modal.appendChild(tabBar);
// ── Built-in panel ──────────────────────────────────────────
const gridContainer = document.createElement("div");
gridContainer.className = "svg-selector-grid-container";
gridContainer.id = "svgGridPanel";
const grid = document.createElement("div");
grid.className = "svg-selector-grid";
let selectedAssetPath = null;
BUILTIN_SVG_ASSETS.forEach(function (asset) {
const item = document.createElement("div");
item.className = "svg-asset-item";
item.dataset.path = asset.path;
const img = document.createElement("img");
img.src = asset.path;
img.alt = asset.name;
img.loading = "lazy";
// Graceful fallback for missing images
img.onerror = function () {
this.style.display = "none";
};
const label = document.createElement("span");
label.className = "asset-name";
label.textContent = asset.name;
item.appendChild(img);
item.appendChild(label);
item.addEventListener("click", function () {
// Deselect previous
const prev = grid.querySelector(".svg-asset-item.selected");
if (prev) prev.classList.remove("selected");
item.classList.add("selected");
selectedAssetPath = asset.path;
applyBtn.disabled = false;
});
grid.appendChild(item);
});
gridContainer.appendChild(grid);
modal.appendChild(gridContainer);
// ── Upload panel (hidden by default) ────────────────────────
const uploadPanel = document.createElement("div");
uploadPanel.className = "svg-selector-upload";
uploadPanel.id = "svgUploadPanel";
uploadPanel.style.display = "none";
const uploadBtn = document.createElement("button");
uploadBtn.className = "svg-upload-btn";
uploadBtn.innerHTML = '<span class="material-icons">cloud_upload</span> Choose File';
const uploadHint = document.createElement("span");
uploadHint.className = "svg-upload-hint";
uploadHint.textContent = "Supports PNG, JPG, SVG, GIF and other image formats";
uploadPanel.appendChild(uploadBtn);
uploadPanel.appendChild(uploadHint);
modal.appendChild(uploadPanel);
// ── Footer ──────────────────────────────────────────────────
const footer = document.createElement("div");
footer.className = "svg-selector-footer";
const cancelBtn = document.createElement("button");
cancelBtn.className = "svg-cancel-btn";
cancelBtn.textContent = "Cancel";
const applyBtn = document.createElement("button");
applyBtn.className = "svg-apply-btn";
applyBtn.textContent = "Apply";
applyBtn.disabled = true;
footer.appendChild(cancelBtn);
footer.appendChild(applyBtn);
modal.appendChild(footer);
overlay.appendChild(modal);
document.body.appendChild(overlay);
// Trigger enter animation
requestAnimationFrame(function () {
overlay.classList.add("visible");
});
// ── Event handlers ──────────────────────────────────────────
function closeModal() {
overlay.classList.remove("visible");
setTimeout(function () {
overlay.remove();
}, 250);
}
// Close button
header.querySelector(".svg-selector-close").addEventListener("click", closeModal);
cancelBtn.addEventListener("click", closeModal);
// Click outside modal to close
overlay.addEventListener("click", function (e) {
if (e.target === overlay) closeModal();
});
// Tab switching
tabBuiltIn.addEventListener("click", function () {
tabBuiltIn.classList.add("active");
tabUpload.classList.remove("active");
gridContainer.style.display = "";
uploadPanel.style.display = "none";
footer.style.display = "";
});
tabUpload.addEventListener("click", function () {
tabUpload.classList.add("active");
tabBuiltIn.classList.remove("active");
gridContainer.style.display = "none";
uploadPanel.style.display = "";
footer.style.display = "none";
});
// Upload button → close modal and delegate to existing upload flow
uploadBtn.addEventListener("click", function () {
closeModal();
if (typeof onUploadFromDevice === "function") {
onUploadFromDevice();
}
});
// Apply selected built-in image
applyBtn.addEventListener("click", function () {
if (!selectedAssetPath) return;
// Fetch the SVG/image and convert to data URL
_fetchAsDataURL(selectedAssetPath, function (dataURL) {
closeModal();
if (typeof onSelectBuiltIn === "function") {
onSelectBuiltIn(dataURL);
}
});
});
})
.catch(function (error) {
// eslint-disable-next-line no-console
console.error("Failed to load SVG assets:", error);
// Fallback to upload from device if built-in fails to load
if (typeof onUploadFromDevice === "function") {
onUploadFromDevice();
}
});
}
/**
* Fetches an asset file and converts it to a data-URL string.
*
* Works entirely offline because it reads from the project's own
* static assets via a relative path.
*
* @param {string} path - Relative path to the asset file.
* @param {Function} callback - Called with the resulting data-URL.
* @private
*/
function _fetchAsDataURL(path, callback) {
const xhr = new XMLHttpRequest();
xhr.open("GET", path, true);
xhr.responseType = "blob";
xhr.onload = function () {
if (xhr.status === 200 || xhr.status === 0) {
var reader = new FileReader();
reader.onloadend = function () {
callback(reader.result);
};
reader.readAsDataURL(xhr.response);
} else {
// eslint-disable-next-line no-console
console.error("Failed to load built-in asset: " + path);
}
};
xhr.onerror = function () {
// eslint-disable-next-line no-console
console.error("Network error loading built-in asset: " + path);
};
xhr.send();
}
if (typeof module !== "undefined" && module.exports) {
module.exports = { openSvgAssetSelector };
}