forked from aframevr/aframe-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassetsUtils.js
More file actions
73 lines (65 loc) · 1.82 KB
/
Copy pathassetsUtils.js
File metadata and controls
73 lines (65 loc) · 1.82 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
export function getUrlFromId(assetId) {
return (
assetId.length > 1 &&
document.querySelector(assetId) &&
document.querySelector(assetId).getAttribute('src')
);
}
export function getIdFromUrl(url) {
const escaped = url.replace(/'/g, "\\'");
return document.querySelector(`a-assets > [src='${escaped}']`)?.id;
}
export function getFilename(url, converted = false) {
var filename = url.split('/').pop();
if (converted) {
filename = getValidId(filename);
}
return filename;
}
export function isValidId(id) {
// The correct re should include : and . but A-frame seems to fail while accessing them
var re = /^[A-Za-z_]+[\w-]*$/;
return re.test(id);
}
export function getValidId(name) {
return (
name
.split('.')
.shift()
// Replace spaces with hyphens
.replace(/\s+/g, '-')
// Remove characters that are not letters (A–Z, a–z), numbers (0–9), underscore (_), and hyphen (-)
.replace(/[^\w-]+/g, '')
// Strip leading hyphens and digits
.replace(/^[-\d]+/, '')
.toLowerCase()
);
}
export function insertNewAsset(type, id, src, onLoadedCallback = undefined) {
let element;
switch (type) {
case 'img':
element = document.createElement('img');
element.id = id;
element.src = src;
element.crossOrigin = 'anonymous';
break;
}
if (element) {
element.onload = function () {
if (onLoadedCallback) {
onLoadedCallback();
}
};
let assetsEl = document.querySelector('a-assets');
if (!assetsEl) {
assetsEl = document.createElement('a-assets');
var sceneEl = document.querySelector('a-scene');
if (!sceneEl) {
throw new Error('No a-scene element found to append a-assets to');
}
sceneEl.appendChild(assetsEl);
}
assetsEl.appendChild(element);
}
}