-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (120 loc) · 3.88 KB
/
main.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
134
135
136
import restrictFocus, { getAllElements } from "./src/index";
window.restrictFocus = restrictFocus;
const template = document.createElement("template");
template.innerHTML = `
<button>Start: Web component button</button>
<slot></slot>
<button>End: Web component button</button>
`;
class WebComponentElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
}
if (!window.customElements.get("web-component-element")) {
window.customElements.define("web-component-element", WebComponentElement);
}
window.addEventListener("restrict-focus:added", (e) => {
e.detail.element.style.boxShadow = "0 0 0 3px lime";
});
window.addEventListener("restrict-focus:removed", (e) => {
e.detail.element.style.boxShadow = "";
});
const registeredHotKeys = new Map();
window.registeredHotkeys = registeredHotKeys;
function sortObject(obj) {
return Object.keys(obj)
.sort()
.reduce(function (result, key) {
result[key] = obj[key];
return result;
}, {});
}
class AhaHotkeyElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
const template = document.createElement("template");
template.innerHTML = `<script><slot></slot></script>`;
const fnStr = this.childNodes[0].textContent.trim();
this.fn = /^function|^\(\) =>/.test(fnStr)
? new Function(`return ${fnStr}`).bind(this)()
: new Function(fnStr).bind(this);
// this.fn = new Function(`return ${fnStr}`)();
// const fn = new Function(fnStr);
// debugger;
// this.shadowRoot.appendChild(template.content.cloneNode(true));
const meta = this.hasAttribute("meta");
const ctrl = this.hasAttribute("ctrl");
const alt = this.hasAttribute("alt");
const shift = this.hasAttribute("shift");
const key = this.getAttribute("key");
this.complex = JSON.stringify(sortObject({ meta, ctrl, alt, shift, key }));
this.symbol = Symbol();
if (registeredHotKeys.has(this.complex)) {
const map = registeredHotKeys.get(this.complex);
map.set(this, "alert(1)");
registeredHotKeys.set(this.complex, map);
} else {
const map = new WeakMap();
map.set(this, "alert()");
registeredHotKeys.set(this.complex, map);
}
}
}
if (!window.customElements.get("aha-hotkey")) {
window.customElements.define("aha-hotkey", AhaHotkeyElement);
}
window.addEventListener("keyup", (e) => {
if (!e.metaKey && !e.ctrlKey && !e.altKey && !e.shiftKey) return;
const key = e.key.trim() || e.code;
const complex = JSON.stringify(
sortObject({
meta: true,
ctrl: true,
alt: true,
shift: true,
key,
})
);
const actions = registeredHotKeys.get(complex);
if (actions) {
// debugger;
let hotkeyElement;
const allNodes = Array.from(getAllElements(restrictFocus.activeElement));
while (!hotkeyElement && allNodes.length) {
const node = allNodes.pop();
if (actions.has(node)) {
hotkeyElement = node;
}
}
if (hotkeyElement) {
var link = hotkeyElement.getAttribute("link");
if (link) {
fetch(link);
} else if (hotkeyElement.fn) {
hotkeyElement.fn();
}
}
}
});
document.querySelectorAll("button.toggleFocus").forEach((button) => {
button.addEventListener("click", (e) => {
const section = e.target.closest("div[id]");
if (restrictFocus?.activeElement === section) {
restrictFocus.remove(section);
} else {
restrictFocus.add(section);
}
});
});
setTimeout(() => {
// restrictFocus.add(document.getElementById("plain-div"));
restrictFocus.add(document.getElementById("third"));
// restrictFocus.add(document.getElementById("second"), {
// // Re-enable to test piercing the restriction via some event.
// allowedEvents: ["mousedown", "mouseup", "click"],
// });
}, 2000);