-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselection.js
158 lines (143 loc) · 4.78 KB
/
selection.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
document.addEventListener('mouseup', function(event) {
var userSelection = window.getSelection().getRangeAt(0)
highlightSelection(userSelection)
});
/**
* Create Element helper function
* @param {string} tagName - Name of element to create
* @param {string} text - Text to put inside of element
* @return {Element}
*/
function makeElement(tagName, text){
var element = document.createElement(tagName)
if (text){
element.innerHTML = text
}
return element
}
/**
* Starts at bottom of dom tree and creates an array of parentNodes
* until it reaches the commonContainer
* @param {Node} container - A Node to begin the traversal
* @param {Node} commonContainer - The base node to finish traversing at
* @param {Array} reversedTree - Array of nodes set into a tree
*/
function treeReversal(container, commonContainer, reversedTree){
return container != commonContainer ?
treeReversal(container.parentNode, commonContainer, reversedTree.concat(container)) : reversedTree;
}
/**
* If a user selects a range spanning multiple elements this function
* creates a safe range of elements that can be wrapped around
* @param {Range} userRange - user selected range
* @return {Array} array of safe ranges
*/
function getSafeRanges(userRange) {
var commonContainer = userRange.commonAncestorContainer;
// Starts -- Work inward from the start, selecting the largest safe range
var beginRanges = new Array(0),
sortedBegin = new Array(0);
if (userRange.startContainer != commonContainer){
beginRanges = treeReversal(userRange.startContainer, commonContainer, [])
}
if (0 < beginRanges.length){
for(var i = 0; i < beginRanges.length; i++) {
var currNode = beginRanges[i],
currRange = document.createRange();
if (i === 0){
currRange.setStart(currNode, userRange.startOffset);
currRange.setEndAfter( (currNode.nodeType == Node.TEXT_NODE) ? currNode : currNode.lastChild);
}
else {
currRange.setStartAfter(beginRanges[i-1]);
currRange.setEndAfter(currNode.lastChild);
}
sortedBegin.push(currRange);
}
}
// Ends -- basically the same code reversed
var endRanges = new Array(0),
sortedEnd = new Array(0);
if (userRange.endContainer != commonContainer){
endRanges = treeReversal(userRange.endContainer, commonContainer, [])
}
if (0 < endRanges.length){
for(var i = 0; i < endRanges.length; i++) {
var currentNode = endRanges[i],
currentRange = document.createRange()
if (i === 0) {
currentRange.setStartBefore( (currentNode.nodeType == Node.TEXT_NODE) ? currentNode : currentNode.firstChild );
currentRange.setEnd(currentNode, userRange.endOffset);
} else {
currentRange.setStartBefore(currentNode.firstChild);
currentRange.setEndBefore(endRanges[i-1]);
}
sortedEnd.unshift(currentRange);
}
}
// Middle -- the uncaptured middle
if ((0 < beginRanges.length) && (0 < endRanges.length)) {
var midRanges = document.createRange();
midRanges.setStartAfter(beginRanges[beginRanges.length - 1]);
midRanges.setEndBefore(endRanges[endRanges.length - 1]);
}
else {
return [userRange];
}
return sortedBegin.concat(midRanges, sortedEnd)
}
function safeHighlights (userSelection){
return new Promise(function(resolve, reject) {
var safeHighlights = getSafeRanges(userSelection).map( function(range) {highlightRange(range)})
return safeHighlights.length > 0 ? resolve(safeHighlights) : reject([])
});
}
/**
* Get all elements with highlightedText class
* and set background-color to be none as well as remove
* the class attached
*/
function removeHighlight(){
var highlights = document.getElementsByClassName("highlightedText")
Array.prototype.map.call(highlights, function(highlight){
highlight.setAttribute(
'style',
'background-color: none; display: inline;'
)
highlight.removeAttribute('class')
})
}
/**
* Only fire safe highlights if user selection
* contains text after the mouse event
* else if no text selected then unhighlight text
* @param {Range}
*/
function highlightSelection(userSelection) {
if (userSelection.toString().length > 0){
safeHighlights(userSelection).then(function(){
console.log('highlighted userSelection', userSelection);
}).catch( function(error){
console.log('error highlighting selection', error);
})
} else {
removeHighlight()
}
}
/**
* Create div with highlightedText class
* to wrap user selection range
* @param {Range}
*/
function highlightRange(range) {
var newNode = makeElement("div")
newNode.setAttribute(
"class",
"highlightedText"
)
newNode.setAttribute(
"style",
"background-color: yellow; display: inline;"
);
range.surroundContents(newNode);
}