forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
61 lines (51 loc) · 2.05 KB
/
events.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
import { dispatch } from "./dispatch.js";
import { addDropUpload } from "./events/addDropUpload.js";
import { addVerticalBar } from "./events/addVerticalBar.js";
const trigger = (e) => e.composedPath()[0];
const matchesTrigger = (e, selectorString) =>
trigger(e).matches(selectorString);
// create on listener
export const createListener =
(target) => (eventName, selectorString, event) => {
// focus doesn't work with this, focus doesn't bubble, need focusin
target.addEventListener(eventName, (e) => {
e.trigger = trigger(e); // Do I need this? e.target seems to work in many (all?) cases
if (selectorString === "" || matchesTrigger(e, selectorString)) event(e);
});
};
function hasSomeParentTheClass(element, classname) {
if (element.className === "") return false;
if (element.className.split(' ').indexOf(classname)>=0) return true;
return element.parentNode && hasSomeParentTheClass(element.parentNode, classname);
}
export function addEvents(state) {
const bodyListener = createListener(document.body);
bodyListener("keydown", "", function (event) {
const code = event.code;
const mod = navigator.platform.startsWith("Mac")
? event.metaKey && !event.ctrlKey
: event.ctrlKey && !event.metaKey;
const active = document.activeElement;
const isCM = active ? hasSomeParentTheClass(active, "code-container") : false;
if (isCM && code === "KeyS" && !event.shiftKey && mod) {
event.preventDefault();
dispatch("SAVE");
}
if (code === "Enter" && (event.shiftKey || event.ctrlKey || event.metaKey)) {
event.preventDefault();
dispatch("RUN");
}
});
bodyListener("click", ".docs-trigger", function (event) {
dispatch("DOC_OPEN");
});
window.addEventListener("beforeunload", (event) => {
if (state.stale) {
event.preventDefault();
return event.returnValue = "You have unsaved changes. Are you sure you want to leave?";
}
});
addVerticalBar(state, bodyListener);
// addNumberDragging(state, bodyListener);
addDropUpload(state, bodyListener);
}