-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyllables.js
63 lines (53 loc) · 2.05 KB
/
syllables.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
function get_selection()
{
var html = "";
var color_list = [ "Red", "DodgerBlue", "Orange", "LimeGreen", "DarkSalmon", "MediumOrchid" ];
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
if (sel.rangeCount != 1)
console.log("Warning : multiple selections not supported");
range = sel.getRangeAt(0);
// get text under selection
container.appendChild(range.cloneContents());
html = container.innerHTML;
// Change color of text
if (range.startOffset != range.endOffset)
{
document.designMode = "on";
sel.removeAllRanges();
sel.addRange(range);
// Use HiliteColor since some browsers apply BackColor to the whole block
if (!document.execCommand("HiliteColor", false, color_list[window.color_counter])) {
document.execCommand("BackColor", false, color_list[window.color_counter]);
}
document.designMode = "off";
}
// // remove selection
// if (sel.empty) { // Chrome
// sel.empty();
// } else if (sel.removeAllRanges) { // Firefox
// sel.removeAllRanges();
// }
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
if (html == "")
return;
var div = document.createElement("div");
div.innerHTML = html;
$(div).addClass("syllable");
$(div).css("color", color_list[window.color_counter]);
$(div).draggable();
$('body').append(div);
// finally update global counter
window.color_counter = (window.color_counter + 1) % color_list.length;
}
$(function() {
window.color_counter = 0;
$( "#text-written" ).mouseup(get_selection);
});