-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
126 lines (111 loc) · 3.53 KB
/
helpers.js
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
/**
* querySelector wrapper
*
* @param {string} selector Selector to query
* @param {Element} [scope] Optional scope element for the selector
*/
export function qs(selector, scope) {
return (scope || document).querySelector(selector);
}
/**
* addEventListener wrapper
*
* @param {Element|Window} target Target Element
* @param {string} type Event name to bind to
* @param {Function} callback Event callback
* @param {boolean} [capture] Capture the event
*/
export function $on(target, type, callback, capture) {
target.addEventListener(type, callback, !!capture);
}
/**
* Attach a handler to an event for all elements matching a selector.
*
* @param {Element} target Element which the event must bubble to
* @param {string} selector Selector to match
* @param {string} type Event name
* @param {Function} handler Function called when the event bubbles to target
* from an element matching selector
* @param {boolean} [capture] Capture the event
*/
export function $delegate(target, selector, type, handler, capture) {
const dispatchEvent = event => {
const targetElement = event.target;
const potentialElements = target.querySelectorAll(selector);
let i = potentialElements.length;
while (i--) {
if (potentialElements[i] === targetElement) {
handler.call(targetElement, event);
break;
}
}
};
$on(target, type, dispatchEvent, !!capture);
}
export function getCurrentUrl() {
let url = window.location.href;
return url;
};
export function getToto() {
return "toto";
};
export function getLastPastedText() {
return latestPaste;
};
export function getPromptText(message, placeholder) {
return prompt(message, placeholder);
};
export function isFirefox() {
return navigator.userAgent.indexOf("Firefox") != -1;
}
export function initFirefoxPasteWorkaround() {
if (navigator.userAgent.indexOf("Firefox") != -1) {
const avaloniaInputElement = document.querySelector("input.avalonia-input-element");
avaloniaInputElement.contentEditable = true;
//document.onpaste = (e) => {
// e.preventDefault();
// pastedText = (e.clipboardData || window.clipboardData).getData("text");
// console.log("pasted text:");
// console.log(pastedText);
//};
return true;
}
return false;
}
export async function openFilePicker() {
//return img src
return new Promise((resolve, reject) => {
var input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.onchange = (e) => {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = (e) => {
var img = document.createElement("img");
img.src = e.target.result;
document.body.appendChild(img);
resolve(e.target.result);
};
reader.readAsDataURL(file);
};
input.click();
});
}
export function saveFile(byteArray, mimeType, fileName) {
var blob = new Blob([byteArray], { type: mimeType });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
}
//export function () {
// readClipboardTextAsync(): Promise < string > {
// if(globalThis.navigator.clipboard.readText) {
// return await globalThis.navigator.clipboard.readText();
// }
// else {
// return this.latestPaste;
// }
//}
//}