forked from github/turbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
108 lines (91 loc) · 2.95 KB
/
util.ts
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
export type DispatchOptions = {
target: EventTarget
cancelable: boolean
detail: any
}
export function dispatch(eventName: string, { target, cancelable, detail }: Partial<DispatchOptions> = {}) {
const event = new CustomEvent(eventName, {
cancelable,
bubbles: true,
detail,
})
if (target && (target as Element).isConnected) {
target.dispatchEvent(event)
} else {
document.documentElement.dispatchEvent(event)
}
return event
}
export function nextAnimationFrame() {
return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
}
export function nextEventLoopTick() {
return new Promise<void>((resolve) => setTimeout(() => resolve(), 0))
}
export function nextMicrotask() {
return Promise.resolve()
}
export function parseHTMLDocument(html = "") {
return new DOMParser().parseFromString(html, "text/html")
}
export function unindent(strings: TemplateStringsArray, ...values: any[]): string {
const lines = interpolate(strings, values).replace(/^\n/, "").split("\n")
const match = lines[0].match(/^\s+/)
const indent = match ? match[0].length : 0
return lines.map((line) => line.slice(indent)).join("\n")
}
function interpolate(strings: TemplateStringsArray, values: any[]) {
return strings.reduce((result, string, i) => {
const value = values[i] == undefined ? "" : values[i]
return result + string + value
}, "")
}
export function uuid() {
return Array.from({ length: 36 })
.map((_, i) => {
if (i == 8 || i == 13 || i == 18 || i == 23) {
return "-"
} else if (i == 14) {
return "4"
} else if (i == 19) {
return (Math.floor(Math.random() * 4) + 8).toString(16)
} else {
return Math.floor(Math.random() * 15).toString(16)
}
})
.join("")
}
export function getAttribute(attributeName: string, ...elements: (Element | undefined)[]): string | null {
for (const value of elements.map((element) => element?.getAttribute(attributeName))) {
if (typeof value == "string") return value
}
return null
}
export function markAsBusy(...elements: Element[]) {
for (const element of elements) {
if (element.localName == "turbo-frame") {
element.setAttribute("busy", "")
}
element.setAttribute("aria-busy", "true")
}
}
export function clearBusyState(...elements: Element[]) {
for (const element of elements) {
if (element.localName == "turbo-frame") {
element.removeAttribute("busy")
}
element.removeAttribute("aria-busy")
}
}
export function waitForLoad(element: HTMLLinkElement, timeoutInMilliseconds = 2000): Promise<void> {
return new Promise((resolve) => {
const onComplete = () => {
element.removeEventListener("error", onComplete)
element.removeEventListener("load", onComplete)
resolve()
}
element.addEventListener("load", onComplete, { once: true })
element.addEventListener("error", onComplete, { once: true })
setTimeout(resolve, timeoutInMilliseconds)
})
}