forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken-events.js
More file actions
94 lines (72 loc) · 2.3 KB
/
token-events.js
File metadata and controls
94 lines (72 loc) · 2.3 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import { getTokenLocation } from ".";
import { isEqual } from "lodash";
function isInvalidTarget(target: HTMLElement) {
if (!target || !target.innerText) {
return true;
}
const tokenText = target.innerText.trim();
const cursorPos = target.getBoundingClientRect();
// exclude literal tokens where it does not make sense to show a preview
const invalidType = ["cm-atom", ""].includes(target.className);
// exclude syntax where the expression would be a syntax error
const invalidToken =
tokenText === "" || tokenText.match(/^[(){}\|&%,.;=<>\+-/\*\s](?=)/);
// exclude codemirror elements that are not tokens
const invalidTarget =
(target.parentElement &&
!target.parentElement.closest(".CodeMirror-line")) ||
cursorPos.top == 0;
const invalidClasses = ["editor-mount"];
if (invalidClasses.some(className => target.classList.contains(className))) {
return true;
}
if (target.closest(".popover")) {
return true;
}
return invalidTarget || invalidToken || invalidType;
}
function dispatch(codeMirror, eventName, data) {
codeMirror.constructor.signal(codeMirror, eventName, data);
}
function invalidLeaveTarget(target: ?HTMLElement) {
if (!target || target.closest(".popover")) {
return true;
}
return false;
}
export function onMouseOver(codeMirror: any) {
let prevTokenPos = null;
function onMouseLeave(event) {
if (invalidLeaveTarget(event.relatedTarget)) {
return addMouseLeave(event.target);
}
prevTokenPos = null;
dispatch(codeMirror, "tokenleave", event);
}
function addMouseLeave(target) {
target.addEventListener("mouseleave", onMouseLeave, {
capture: true,
once: true
});
}
return (enterEvent: any) => {
const { target } = enterEvent;
if (isInvalidTarget(target)) {
return;
}
const tokenPos = getTokenLocation(codeMirror, target);
if (!isEqual(prevTokenPos, tokenPos)) {
addMouseLeave(target);
dispatch(codeMirror, "tokenenter", {
event: enterEvent,
target,
tokenPos
});
prevTokenPos = tokenPos;
}
};
}