forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.js
More file actions
61 lines (48 loc) · 1.57 KB
/
expression.js
File metadata and controls
61 lines (48 loc) · 1.57 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
// @flow
import { isEqual } from "lodash";
export function getTokenLocation(codeMirror: any, tokenEl: HTMLElement) {
const { left, top, width, height } = tokenEl.getBoundingClientRect();
const { line, ch } = codeMirror.coordsChar({
left: left + width / 2,
top: top + height / 2
});
return {
line: line + 1,
column: ch
};
}
export function updatePreview(
target: HTMLElement,
editor: any,
{ linesInScope, preview, setPreview, clearPreview }: any
) {
const location = getTokenLocation(editor.codeMirror, target);
const tokenText = target.innerText ? target.innerText.trim() : "";
const cursorPos = target.getBoundingClientRect();
if (preview) {
// We are mousing over the same token as before
if (isEqual(preview.tokenPos, location)) {
return;
}
// We are mousing over a new token that is not in the preview
if (!target.classList.contains("debug-expression")) {
clearPreview();
}
}
const invalidToken =
tokenText === "" || tokenText.match(/[(){}\|&%,.;=<>\+-/\*\s]/);
const invalidTarget =
(target.parentElement &&
!target.parentElement.closest(".CodeMirror-line")) ||
cursorPos.top == 0;
const isUpdating = preview && preview.updating;
const inScope = linesInScope && linesInScope.includes(location.line);
const invaildType =
target.className === "cm-string" ||
target.className === "cm-number" ||
target.className === "cm-atom";
if (invalidTarget || !inScope || isUpdating || invalidToken || invaildType) {
return;
}
setPreview(tokenText, location, cursorPos);
}