-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_caret.js
235 lines (170 loc) · 4.84 KB
/
smart_caret.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//todo: make nodes selectable by range by using str.split(":"), and changing make selection to use start and stopping nodes
//expand selection function which goes to next sibling node until some ending is met.
let textNodeArr = [];
let hintMap = {};
let alphabet = "abcdefghijklmnopqrstuvwxyz".split('');
let range = document.createRange();
let displayHints = false;
let index = "";
let selectedIndex = 0;
let scrollWindowTop = 0;
let scrollWindowBottom = $( window ).height() + scrollWindowTop;
let selectedNode;
$(document.body).append('<div id="selectionHintMarkerContainer"></div>');
let markerClassName = "selectionHintMarker";
let markerContainer = $("#selectionHintMarkerContainer");
function makeSelectionHints(){
console.log("making selecions");
let i = 0,
j = 0;
let r = document.createRange();
textNodeArr.forEach(function(d){
r.selectNodeContents(d);
let rects = r.getClientRects();
if (rects.length > 0) {
if(rects[0].top >= scrollWindowTop && rects[0].top <= scrollWindowBottom){
let hint = newHint(j);
hintMap[hint] = i;
selectionHint(rects[0].left, rects[0].top, hint);
j++;
}
}
i++;
});
}
function selectionHint(x, y, hint) {
markerContainer.append('<div class="'+markerClassName+'" id="'+hint+'" style="left: '+x+'px; top: '+y+'px;">'+hint+'</div>');
}
//produces base 26 number
function newHint(j){
let n = j;
let digits = [];
n += 1;
while (n > 0){
digits.push(alphabet[((n-1) % 26)]);
n = Math.floor(n/26);
}
return digits.join("");
}
//find all nodes with text and append them to nodeArr.
function getTextNodes(from, to){
$("body").find(":not(iframe)").addBack().contents().filter(function() {
//if text node
if (this.nodeType == 3){
let s = this.textContent;
//if node isnt just whitespace
if ( /\S/.test(s)) {
textNodeArr.push(this);
}
}
})
}
//set area for marker hints to be drawn
function setViewArea(){
scrollWindowTop = $(document).scrollTop();
scrollWindowBottom = $( window ).height() + scrollWindowTop;
}
function makeSelection( node ){
selection = window.getSelection();
selection.removeAllRanges();
range.selectNodeContents(node);
selection.addRange(range);
}
function clearSelectionHints(){
selectedIndex = 0;
textNodeArr = [];
selectedNode = undefined;
markerContainer.empty();
}
// function stealFocus(){
// $("#selectionHintMarkerContainer").focus();
// console.log("stole focus");
// $(document).keyup(function(event)){
// if(event.keycode >= 48 && event.keycode <= 57){
// console.log(event.keycode - 48);
// }
// }
// }
function markerMode() {
if(textNodeArr.length == 0 || scrollWindowTop != $(document).scrollTop()){
//clear out old hint markers
clearSelectionHints();
//get area for visible space to place hints
setViewArea();
//fill textNodeArr with new nodes
getTextNodes();
//make new hint markers to reflect contents of textNodeArr
makeSelectionHints();
}
//toggle displaying hint markers
if(displayHints == false){
$("."+markerClassName).css("display", "inline");
displayHints = true;
}else{
$("."+markerClassName).css("display", "none");
displayHints = false;
}
}
function enterSelection(){
//if hints are showing, set selectedIndex to index, and hide hints then reset index
if(displayHints){
selectedIndex = hintMap[index];
$("."+markerClassName).css("display", "none");
displayHints = false;
index = "";
}
//select node at selected index
selectedNode = textNodeArr[selectedIndex];
makeSelection(selectedNode);
//iterate for next call
selectedIndex++;
}
function expandSelection(){
console.log("+");
if(!selectedNode) {
selectedNode = textNodeArr[selectedIndex];
}
selectedNode = selectedNode.parentElement;
makeSelection(selectedNode);
}
function inputAlpha(keycode) {
if(displayHints){
index = index + String.fromCharCode(keycode).toLowerCase();
//show only hints that match input
$("."+markerClassName).not('div[id ^= "'+index+'"]').css("display", "none");
console.log(index);
}
}
function backspace() {
input = input.substring(0, str.length - 1);
}
//start listening for keypress, and iterate over selections
// if(!displayHints){
// $(document).keyup(function(event){
// });
// }else{
// }
$(document).keyup(function(event){
let keycode = (event.keyCode ? event.keyCode : event.which);
console.log(keycode);
//switch true allows us to do comarisons to keycode instead of checking equality
switch(true){
//keycode is numeric
case (keycode >= 65 && keycode <= 90):
inputAlpha(keycode);
break;
//keycode = enter
case (keycode == 13):
enterSelection();
break;
//keycode = ctrl + space
case (event.ctrlKey && ( event.which === 32 )):
markerMode();
break;
//keycode = equals sign
case (keycode == 61 ):
expandSelection();
break;
};
//todo: else if in keycode select mode goto selection
});