-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquoteShare.js
168 lines (147 loc) · 4.87 KB
/
quoteShare.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
var isMobile = true;
function textSelected() {
var selectedText = getSelectionText();
changeVisability(selectedText != "" && selectedText != " ");
}
window.addEventListener('load', function () {
changeVisability(false);
// desktop
document.body.addEventListener('mouseup', function (e) {
if (document.getElementById("tooltip").className == "tooltipGeneral tooltip1") {
document.getElementById("tooltip").className = "tooltipGeneral tooltip2";
}
isMobile = false;
textSelected()
}, false)
// mobile
document.body.addEventListener('touchend', function (e) {
if (document.getElementById("tooltip").className == "tooltipGeneral tooltip2") {
document.getElementById("tooltip").className = "tooltipGeneral tooltip1";
}
isMobile = true;
textSelected()
}, false)
document.getElementById("svgMark").addEventListener('mouseover', function () {
markText("1");
}, false)
document.getElementById("svgMark").addEventListener('mousedown', function () {
markText("2");
}, false)
}, false)
// from http://stackoverflow.com/a/5379408/5044231
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function getSelectionCoords() {
var selection = document.getSelection(),
range = selection.getRangeAt(0),
clientRects = range.getClientRects()
var rect = clientRects[0];
var x = rect.left,
y = rect.top;
if (rect.width > 1) {
x = x + (rect.width / 2);
}
return {
x: x,
y: y
};
}
function changeVisability(visible) {
var tooltip = document.getElementById('tooltip');
if (visible == true) {
var coords = getSelectionCoords();
setLinks();
setTimeout(function () {
tooltip.style.left = coords.x - 53 + 'px';
if (isMobile) {
tooltip.style.top = coords.y + window.pageYOffset + 24 + 'px';
} else {
tooltip.style.top = coords.y + window.pageYOffset - 46 + 'px';
}
tooltip.style.display = 'block';
fadeIn(tooltip);
}, 50);
} else {
tooltip.style.display = 'none';
}
}
function setLinks() {
var text = getSelectionText(),
url = document.URL;
document.getElementById("svgFacebook").href = getLink("facebook", text, url);
document.getElementById("svgTwitter").href = getLink("twitter", text, url);
document.getElementById("svgWhatsapp").href = getLink("whatsapp", text, url);
}
function getLink(network, text, url) {
if (network == "facebook") {
return "String";
}
else if (network == "twitter") {
var canvas = document.getElementById('twitterCanvas');
var context = canvas.getContext('2d');
var maxWidth = 400;
var lineHeight = 25;
var x = (canvas.width - maxWidth) / 2;
var y = 40;
context.font = '16pt Calibri';
context.fillStyle = '#333';
context.background = '#fbfbfb';
wrapText(context, text, x, y, maxWidth, lineHeight);
var canvas = document.getElementById("twitterCanvas");
var img = canvas.toDataURL("image/png");
return "String";
} else if (network == "whatsapp") {
return "whatsapp://send?text=\"" + encodeURI(text) + "\" - " + encodeURI(url);
}
}
var selection;
function markText(type) {
if (type == "1") {
selection = window.getSelection().getRangeAt(0);
}
if (type == "2") {
var selectedText = selection.extractContents();
var span = document.createElement("span");
span.style.backgroundColor = "yellow";
span.appendChild(selectedText);
selection.insertNode(span);
}
}
// from http://youmightnotneedjquery.com/#fade_in
function fadeIn(el) {
el.style.opacity = 0;
var last = +new Date();
var tick = function () {
el.style.opacity = +el.style.opacity + (new Date() - last) / 150;
last = +new Date();
if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
};
tick();
}
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for (var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}