-
Notifications
You must be signed in to change notification settings - Fork 412
/
util.ts
139 lines (113 loc) · 3.55 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
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
import { Action, isAction } from "./core/types"
export type DispatchOptions<T extends CustomEvent> = {
target: EventTarget
cancelable: boolean
detail: T["detail"]
}
export function dispatch<T extends CustomEvent>(
eventName: string,
{ target, cancelable, detail }: Partial<DispatchOptions<T>> = {}
) {
const event = new CustomEvent<T["detail"]>(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 getHistoryMethodForAction(action: Action) {
switch (action) {
case "replace":
return history.replaceState
case "advance":
case "restore":
return history.pushState
}
}
export function getVisitAction(...elements: (Element | undefined)[]): Action | null {
const action = getAttribute("data-turbo-action", ...elements)
return isAction(action) ? action : null
}
export function getMetaElement(name: string): HTMLMetaElement | null {
return document.querySelector(`meta[name="${name}"]`)
}
export function getMetaContent(name: string) {
const element = getMetaElement(name)
return element && element.content
}
export function setMetaContent(name: string, content: string) {
let element = getMetaElement(name)
if (!element) {
element = document.createElement("meta")
element.setAttribute("name", name)
document.head.appendChild(element)
}
element.setAttribute("content", content)
return element
}