This repository was archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.js
More file actions
146 lines (135 loc) · 3.93 KB
/
Copy pathutil.js
File metadata and controls
146 lines (135 loc) · 3.93 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
export function jsonParse(s, d = null) {
try {
return JSON.parse(s);
} catch (err) {
return d;
}
}
export function parseQuery(queryString) {
const query = {};
const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split('=');
const k = decodeURIComponent(pair[0]);
if (k) {
const v = decodeURIComponent(pair[1] || '');
query[k] = v;
}
}
return query;
}
export function getRandomString() {
return Math.random().toString(36).substring(7);
}
export function hex2Uint8Array(hex) {
return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16)))
}
export function uint8Array2hex(uint8Array) {
return Array.prototype.map.call(uint8Array, x => ('00' + x.toString(16)).slice(-2)).join('');
}
export function getExt(fileName) {
const match = fileName
.replace(/^[a-z]+:\/\/[^\/]+\//, '')
.match(/\.([^\.]+|t\.js|rtf\.js)(?:\?.*)?$/);
return match ? match[1].toLowerCase() : '';
}
export function downloadFile(file, filename) {
const blobURL = URL.createObjectURL(file);
const tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', filename);
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
}
export function readFile(file) {
return new Promise((accept, reject) => {
const fr = new FileReader();
fr.onload = () => {
accept(new Uint8Array(fr.result));
};
fr.onerror = reject;
fr.readAsArrayBuffer(file);
});
}
export function bindUploadFileButton(inputFileEl, handleUpload) {
function change(e) {
inputFileEl.removeEventListener('change', change);
const {files} = e.target;
if (inputFileEl.multiple) {
handleUpload(Array.from(files));
} else {
const [file = null] = files;
handleUpload(file);
}
const {parentNode} = inputFileEl;
parentNode.removeChild(inputFileEl);
const newInputFileEl = inputFileEl.ownerDocument.createElement('input');
newInputFileEl.type = 'file';
newInputFileEl.id = inputFileEl.id;
// newInputFileEl.id = 'upload-file-button';
// newInputFileEl.style.display = 'none';
newInputFileEl.classList.add('hidden');
parentNode.appendChild(newInputFileEl);
bindUploadFileButton(newInputFileEl, handleUpload);
}
inputFileEl.addEventListener('change', change);
}
export function snapPosition(o, positionSnap) {
if (positionSnap > 0) {
o.position.x = Math.round((o.position.x + positionSnap/2) / positionSnap) * positionSnap - positionSnap/2;
o.position.y = Math.round((o.position.y + positionSnap/2) / positionSnap) * positionSnap - positionSnap/2;
o.position.z = Math.round((o.position.z + positionSnap/2) / positionSnap) * positionSnap - positionSnap/2;
}
}
export function snapRotation(o, rotationSnap) {
o.rotation.x = Math.round(o.rotation.x / rotationSnap) * rotationSnap;
o.rotation.y = Math.round(o.rotation.y / rotationSnap) * rotationSnap;
o.rotation.z = Math.round(o.rotation.z / rotationSnap) * rotationSnap;
}
export function makePromise() {
let accept, reject;
const p = new Promise((a, r) => {
accept = a;
reject = r;
});
p.accept = accept;
p.reject = reject;
return p;
}
export function clone(o) {
return JSON.parse(JSON.stringify(o));
}
export class WaitQueue {
constructor() {
this.locked = false;
this.waiterCbs = [];
}
async lock() {
if (!this.locked) {
this.locked = true;
} else {
const p = makePromise();
this.waiterCbs.push(p.accept);
await p;
}
}
async unlock() {
if (this.waiterCbs.length > 0) {
this.waiterCbs.pop()();
} else {
this.locked = false;
}
}
clearQueue() {
this.waiterCbs.length = 0;
}
}
export function isInIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}