-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlighter.es6.js
199 lines (166 loc) · 6.46 KB
/
highlighter.es6.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* Created by Leroy on 2/5/2016.
*/
// IIFE void function because this isn't fucking LISP
void function (window, document, undefined) {
"use strict";
//TODO: overlay highlight
// create less file.
// .edge()
// @border = @nindigreen @nindpurple
const _window = window;
const highlighter = (() => {
let initialized = false;
return {
border: [
document.createElement('div'),
document.createElement('div'),
document.createElement('div'),
document.createElement('div')
],
on
};
/**
*
* @param elem {DOM Element} to attach border to
*/
function on(elem) {
// TODO: init should run when highlighter is init'd.
// TODO: highlighter.border is not in scope yet so the whole thing needs to be redone.
// TODO: this will be fixed once highlighter is its own file
if (!initialized) init();
return elem.isEdge || new Promise(function () {
let bounds = elem.getBoundingClientRect(); // Maybe I'm jaded but somehow I feel like this won't work for all cases
highlighter.attachedTo = elem;
/**
* 0 = n
* 1 = s
* 2 = e
* 3 = w
**/
highlighter.border.forEach((edge, iedge, border) => {
edge.style.top = bounds.top + 'px';
edge.style.left = bounds.left + 'px';
edge.style.display = 'block';
edge.style.zIndex = 1 + (elem.zIndex || 0); // 1 + elem.z-index //TODO: fix me
edge.style.zIndex = 20; // this seems to work. why doesn't z=1?
// no longer TODO: check es6 spec. I think this should work
// this did work, I just got the cardinal directions wrong. TODO: fix cardinal directions
switch (iedge) {
case 1: //s
edge.style.left = bounds.left + bounds.width - 4 + 'px'; //TODO make border width configurable
case 0: //n
edge.style.height = bounds.height + 'px';
break;
case 3: //w
edge.style.top = bounds.top + bounds.height - 4 + 'px'; //TODO make border width configurable
case 2: //e
edge.style.width = bounds.width + 'px';
}
})
});
}
/**
* attach borders to body
*/
function init() {
if (initialized) return;
// TODO: edge needs to be a css class as well
highlighter.border.forEach(edge => {
edge.class="edge";
edge.style.position = 'absolute';
edge.style.height = '4px'; //TODO make border width configurable
edge.style.width = '4px'; //TODO make border width configurable
edge.style.display = 'none';
edge.isEdge = true; //window.hasEdge = true; //there can be only one
edge.style.background = 'purple'; // TODO: make border color configurable
document.body.appendChild(edge);
});
initialized = true;
}
})();
const tool = {noop, init, enableFinder, disableFinder, finder};
function noop() {
console.log(arguments, 'noop');
}
//discuss amongst yourselves ... I'm just concerned about possible memory leaks, performance issues
//TODO: Should we remove keydown on execution and reattach on keyup?
//TODO: Same for keyup, reattach on keydown...
//TODO: mousemove only on finder on
//TODO: wheel --"--
function init() {
// enable tool when alt press on window
var _keydown = _e => _e.repeat || !_e.altKey || tool.enableFinder();
// disable tool when alt released
var _keyup = _e => _e.keyCode === 18 && tool.disableFinder();
// find and highlight on mouse move
var _mousemove = () => tool.finder(_mousemove);
// traverse current DOM branch on wheel
var _wheel = () => {
// tool.noop;
};
_window.addEventListener('keydown', _keydown, true);
_window.addEventListener('keyup', _keyup, true);
_window.addEventListener('mousemove', _mousemove, true);
_window.addEventListener('wheel', _wheel, true);
}
function enableFinder() {
if (tool.finderEnabled) {
return;
}
tool.finderEnabled = true;
}
function disableFinder() {
tool.finderEnabled = false;
}
let finderPath = [],
current = _window;
function finder(_mousemove) {
!tool.finderEnabled || //
current == _mousemove.target || // /short circuits
new Promise((resolve, rej) => { //
// TODO remove class
//current.removeClass('tool-found')
setCurrent(_mousemove.target).then(highlighter.on);
resolve(current);
});
function setCurrent(found) {
current = found;
return Promise.resolve(found);
}
// if (
// !tool.finderEnabled
// || current == _mousemove.target) {
// return;
// }
// TOOO: find and save direct path to root/null
//
// _mouseScroll =>
// let elem = _mousemove.target;
// do {
// console.log(elem);
// console.log(_mousemove)
// finderPath.push(elem);
// // console.log(finderPath);
// } while (elem = elem.parentElement)
//
// finderPath.map(elem => elem.)
// or
// let finderPath = [];
// function finder(_mousemove) {
// if (!tool.finderEnabled) {
// return;
// }
// console.log(_mousemove);
// let elem = _mousemove.target;
// console.log(elem);
// while (elem !== _window) {
// console.log(elem);
// finderPath.push(elem);
// // console.log(finderPath);
// elem = elem.parent;
// }
// }
}
tool.init();
}(window, document, undefined);