forked from b0o/surfingkeys-conf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
133 lines (113 loc) · 3.62 KB
/
util.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
127
128
129
130
131
132
133
const { categories } = require("./help")
const util = {}
util.getCurrentLocation = (prop = "href") => {
if (typeof window === "undefined") {
return ""
}
return window.location[prop]
}
util.escape = (str) => String(str).replace(/[&<>"'`=/]/g, (s) => ({
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'",
"/": "/",
"`": "`",
"=": "=",
}[s]))
util.createSuggestionItem = (html, props = {}) => {
const li = document.createElement("li")
li.innerHTML = html
return { html: li.outerHTML, props }
}
util.createURLItem = (title, url, sanitize = true) => {
let t = title
let u = url
if (sanitize) {
t = util.escape(t)
u = new URL(u).toString()
}
return util.createSuggestionItem(`
<div class="title">${t}</div>
<div class="url">${u}</div>
`, { url: u })
}
// Determine if the given rect is visible in the viewport
util.isRectVisibleInViewport = (rect) =>
rect.height > 0
&& rect.width > 0
&& rect.bottom >= 0
&& rect.right >= 0
&& rect.top <= (window.innerHeight || document.documentElement.clientHeight)
&& rect.left <= (window.innerWidth || document.documentElement.clientWidth)
// Determine if the given element is visible in the viewport
util.isElementInViewport = (e) =>
e.offsetHeight > 0 && e.offsetWidth > 0
&& !e.getAttribute("disabled")
&& util.isRectVisibleInViewport(e.getBoundingClientRect())
// Process Unmaps
util.rmMaps = (a) => {
if (typeof unmap === "undefined") {
return
}
a.forEach((u) => unmap(u))
}
util.rmSearchAliases = (a) => Object.entries(a).forEach(([leader, items]) => {
if (typeof removeSearchAliasX === "undefined") {
return
}
items.forEach((v) => removeSearchAliasX(v, leader))
})
// Process Mappings
util.processMaps = (maps, aliases, siteleader) => {
if (typeof map === "undefined" || typeof mapkey === "undefined") {
return
}
const hydratedAliases = Object.entries(aliases)
.flatMap(([baseDomain, aliasDomains]) =>
aliasDomains.flatMap((a) => ({ [a]: maps[baseDomain] })))
const mapsAndAliases = Object.assign({}, maps, ...hydratedAliases)
Object.entries(mapsAndAliases).forEach(([domain, domainMaps]) => domainMaps.forEach(((mapObj) => {
const {
alias,
callback,
leader = (domain === "global") ? "" : siteleader,
category = categories.misc,
description = "",
} = mapObj
const opts = {}
const key = `${leader}${alias}`
// Determine if it's a site-specific mapping
if (domain !== "global") {
const d = domain.replace(".", "\\.")
opts.domain = new RegExp(`^http(s)?://(([a-zA-Z0-9-_]+\\.)*)(${d})(/.*)?`)
}
const fullDescription = `#${category} ${description}`
if (mapObj.map !== undefined) {
map(alias, mapObj.map)
} else {
mapkey(key, fullDescription, callback, opts)
}
})))
}
// process completions
util.processCompletions = (completions, searchleader) => Object.values(completions).forEach((s) => {
if (typeof Front === "undefined" || typeof addSearchAliasX === "undefined" || typeof mapkey === "undefined") {
return
}
addSearchAliasX(s.alias, s.name, s.search, searchleader, s.compl, s.callback)
mapkey(`${searchleader}${s.alias}`, `#8Search ${s.name}`, () => Front.openOmnibar({ type: "SearchEngine", extra: s.alias }))
mapkey(`c${searchleader}${s.alias}`, `#8Search ${s.name} with clipboard contents`, () => {
Clipboard.read((c) => {
Front.openOmnibar({ type: "SearchEngine", pref: c.data, extra: s.alias })
})
})
})
util.addSettings = (s) => {
if (typeof settings === "undefined") {
return
}
Object.assign(settings, s)
}
module.exports = util