-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplacemark.js
61 lines (48 loc) · 1.96 KB
/
placemark.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
var openSpan = '<span class="placemark">';
var closeSpan = '</span>';
function getWordAndIndex(range, node) {
// count back chars until you reach whitespace or beginning of element, set range start
while (range.toString().indexOf(' ') != 0 && range.startOffset != 0) {
range.setStart(node, (range.startOffset - 1));
}
// BUG: Clicking twice in the same p element is now wrapping the whole p element text in a span tag
// remove leading space if not first word in element
if (range.startOffset != 0) range.setStart(node, range.startOffset + 1);
// count forward chars until you reach whitespace, set range end
do {
range.setEnd(node, range.endOffset + 1);
} while (range.toString().indexOf(' ') == -1 && range.toString().trim() != '' && range.endOffset < node.length);
var str = range.toString().trim();
return [str, range.startOffset];
}
function handleClick(e) {
// remove span with class 'placemark', but leave innerText
spanList = document.querySelectorAll('.placemark');
spanList.forEach(function(span){
span.parentNode.innerHTML = removeSpan(span.parentNode.innerHTML);
})
// capture click location
var s = window.getSelection();
var range = s.getRangeAt(0);
var node = s.anchorNode;
// get word clicked on, and string index within element
var wordAndIndex = getWordAndIndex(range, node);
var word = wordAndIndex[0];
var index = wordAndIndex[1];
console.log(word);
// add span word clicked within element
var newhtml = e.target.innerHTML.replace(word, addSpan(word));
e.target.innerHTML = newhtml;
}
document.addEventListener('click', function(e){
var wordAndElement = handleClick(e);
})
function addSpan(str) {
// console.log(openSpan + str + closeSpan);
return openSpan + str + closeSpan;
}
function removeSpan(str) {
var str1 = str.replace(openSpan, "");
var str2 = str1.replace(closeSpan, "");
return str2;
}