-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
405 lines (364 loc) · 14.3 KB
/
example.html
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
display: flex;
/* align-items: center; */
flex-direction: column;
justify-content: center;
background-color: gainsboro;
}
.relative{
position: relative;
padding: 10px;
margin: 30px 20px;
background-color: white;
}
</style>
</head>
<body>
<h1>12:15pm EDT</h1>
<h2>23:00 PT</h2>
<b>11:00pm GST</b>
<h6>11:00pm PT</h6>
<div id="time-test">
The time is 11:00pm IST now
</div>
<div id="target" class="text-dark-gray font-size-6">
<span>10:00AM</span>
<meta itemprop="startDate" content="2021-05-14 14:00:00 UTC">
<span> to </span>
<i>May 16, IST 2:00PM</i>
<meta itemprop="endDate" content="2021-05-16 18:00:00 UTC">
<b>IST, it's IST got it!?</b>
</div>
<p style="width: 75px">2:30 PM IST</p>
<div class="relative">
<p >
The <i>blue</i> bird <b>eats</b> up the <i>wriggly</i> worm
</p>
<p id="test">
A <i>blue</i> bird is a red stone
</p>
<p>
The blue bird red stone
</p>
</div>
<div id="dummy"></div>
<button onclick="highlightText()">Highlight Text</button>
<button onclick="iterate()">Iterate</button>
<script>
function iterate(){
const node = document.querySelector("#target");
var regex = /\s?((1[0-2]|0?[0-9]):([0-5]?[0-9]?)\s?([AaPp][Mm])|(2[0-3]|[0-1][0-9]):?([0-5][0-9]))\s+([A-Z]{2,4})/gm
const groups = regex.exec(node.textContent)
if(groups){
const hour = groups[2] ?? groups[5];
const minute = groups[3] ?? groups[6];
const meridian = groups[4];
const timezone = groups[7];
const dynamicRegex = new RegExp(`(${hour}:${minute})|(${meridian})|(${timezone})`, "gm")
const nodes = iterateThroughNode(node, dynamicRegex)
let range = document.createRange();
let startFound = false;
for(let node of nodes){
if(node.value === `${hour}:${minute}`){
range.setStart(node.node, node.index)
startFound = true
}
if(startFound && node.value.trim() === timezone){
range.setEnd(node.node, node.index + node.value.length)
break;
}
}
// range.setEnd(nodes[nodes.length-1].node, nodes[nodes.length-1].index + nodes[nodes.length-1].value.length);
let sel = document.getSelection();
sel.addRange(range)
}
// var sel = window.getSelection();
// let range = document.createRange();
// range.setStart(matches[0].node, matches[0].index);
// range.setEnd(matches[0].node, matches[0].index + matches[0].value.length);
// console.log(range)
// sel.addRange(range)
// console.log(range.toString());
// for(let match of matches){
// const {node: childNode} = match;
// if(node === childNode){
// range.setStart
// }
// }
}
function conjoinNodes(nodes){
let prevValidValue = ""
return nodes.map((item, index) => {
const {textContent} = item
if(textContent){
prevValidValue = textContent
}
})
}
function iterateThroughNode(node, regex){
let indexes = []
if(node.nodeType == Node.TEXT_NODE){
const results = []
while(match = regex.exec(node.textContent)){
console.log(match)
if(match && match.length > 0){
results.push({node: node, index: match.index, value: match[0]})
}
}
return results;
}
node.childNodes.forEach((item, index) => {
const result = (iterateThroughNode(item, regex))
if(result){
indexes = indexes.concat(result)
}
})
return indexes;
}
function highlightText(){
const sel = document.getSelection();
const container = document.querySelector("#test")
// textRange.moveToElement(container)
const regex = /blue|red/igm
const highlighted = document.createElement("span");
highlighted.setAttribute("style", "background-color: yellow; display: inline");
console.log(container)
while(match = regex.exec(sel.toString())){
const textRange = document.createRange();
const word = match[0]
console.log(match.index);
textRange.setStart(container, match.index);
textRange.setEnd(container, match.index + word.length);
console.log(textRange)
console.log(textRange.toString())
}
}
</script>
<!-- <script>
const _timezones = [
{
"value": "India Standard Time",
"abbr": "IST",
"offset": 5.5,
"isdst": false,
"text": "(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi",
"utc": [
"Asia/Kolkata"
]
},
{
"value": "Singapore Standard Time",
"abbr": "MPST",
"offset": 8,
"isdst": false,
"text": "(UTC+08:00) Kuala Lumpur, Singapore",
"utc": [
"Asia/Brunei",
"Asia/Kuala_Lumpur",
"Asia/Kuching",
"Asia/Makassar",
"Asia/Manila",
"Asia/Singapore",
"Etc/GMT-8"
]
},
]
document.onmouseup = (e) => {
const sel = document.getSelection();
const range = sel.getRangeAt(0)
var regex = /(?<!\S)((1[0-2]|0?[0-9]):([0-5]?[0-9]?)([AaPp][Mm])|(2[0-3]|[0-1][0-9]):?([0-5][0-9]))\s?([A-Z]{2,4})/gm
if(e.target.classList.contains("timezonify-popover")) return;
else if(sel.isCollapsed){ //isCollapsed is to check whether or not the user simply clicked out and not selecting something
const popovers = document.querySelectorAll(".timezonify-popover:not(.timezonify-time-popover)");
for(let popover of popovers){
popover.remove();
}
return;
}
const popovers = document.querySelectorAll(".timezonify-popover:not(.timezonify-time-popover)");
if(popovers.length < 1){
const rect = range.getBoundingClientRect()
const parentNode = getRangeParentNode(range);
createTimezonifyPopover(parentNode, rect, range.toString())
}
}
function getRangeParentNode(range){
const rangeNode = range.startContainer
if(rangeNode.nodeType == Node.TEXT_NODE){
return rangeNode.parentNode
}
return rangeNode.parentNode
}
function createTimezonifyPopover(parentNode, rect, text){
const sharedID = "babasquat"
let button = document.createElement("button");
button.style.top = rect.top > rect.height ? (rect.top - rect.height - 6 + "px") : (rect.top + rect.height + 3 + "px");
// button.style.top = rect.top - rect.height / 2 + "px"
button.style.left = rect.left + "px"
button.className = "timezonify-popover"
button.innerText = "Timezonify";
button.dataset.text = text;
button.dataset.parentID = sharedID
button.onclick = (e) => {
const timezonified = timezonify(text);
if(!timezonified){
console.error("Not a valid time")
button.innerText = "Not a valid time"
setTimeout(() => {
console.log(e.target)
e.target.parentNode.removeChild(e.target)
}, 1500)
return;
}
button.classList.add("timezonify-time-popover")
button.style.top = rect.top - rect.height / 2 + "px"
button.style.left = rect.left + "px"
button.innerText = timezonified
}
// parentNode.style.position = "relative"
parentNode.id = sharedID
parentNode.append(button)
}
function createTimePopover(parentNode, rect, replacement){
let button = document.createElement("button");
button.style.top = rect.top > rect.height ? (rect.top - rect.height - 6 + "px") : (rect.top + rect.height + 3 + "px");
button.style.left = rect.left + "px"
button.className = "timezonify-time-popover"
button.innerText = replacement;
// button.onclick = (e) => {
// createTimePopover(parentNode, rect, timezonified)
// }
// parentNode.style.position = "relative"
parentNode.append(button)
}
function timezonify(text){
var regex = /\s?((1[0-2]|0?[0-9]):([0-5]?[0-9]?)\s?([AaPp][Mm])|(2[0-3]|[0-1][0-9]):?([0-5][0-9]))\s+([A-Z]{2,4})/gm
const match = text.match(regex)
const replacement = replaceTime(text, regex)
return replacement
}
function replaceTime(text, regex){
const matches = text.match(regex)
if(matches){
for(let match of matches){
const convertedTime = convertTime(regex, match)
return convertedTime
}
}
}
function convertTime(regex, string){
const groups = regex.exec(string);
const hour = groups[2] ?? groups[5];
const minute = groups[3] ?? groups[6];
const meridian = groups[4];
const timezone = groups[7];
const sourceTimezoneData = findTimezoneDataFromTimezone(timezone);
const clientTimezoneData = getClientTimezoneData();
if(sourceTimezoneData){
const {
hour: targetHour,
minute: targetMinute,
meridian: targetMeridian
} = getOffsetTime(
hour,
minute,
sourceTimezoneData.offset,
clientTimezoneData.offset,
meridian
);
const targetTimeString =
targetHour +
":" +
targetMinute +
targetMeridian +
" " +
clientTimezoneData.abbr;
return targetTimeString
} else {
throw `Exception: ${timezone} is not a timezone!`
}
}
function getClientTimezoneData() {
// const offset = new Date().getTimezoneOffset() / 60;
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const clientTimezoneData = findTimezoneDataFromTimezoneUtc(timezone);
return clientTimezoneData;
}
function findTimezoneDataFromTimezoneUtc(timezoneUtc) {
return _timezones.find((timezone) => {
const containsUtc =
timezone.utc.filter((utc) => {
return utc === timezoneUtc;
}).length > 0;
return containsUtc;
});
}
function getOffsetTime(hour, minute, sourceOffset, clientOffset, meridian) {
const { hour: cleanedClientOffset, minute: clientMinute } = cleanTimeOffset(
clientOffset
);
const { hour: cleanedSourceOffset, minute: sourceMinute } = cleanTimeOffset(
sourceOffset
);
// console.log(hour);
// console.log(cleanedSourceOffset);
// console.log(cleanedClientOffset);
hour = hour - cleanedSourceOffset + cleanedClientOffset;
minute = minute - sourceMinute + clientMinute;
return formatTime(hour, minute, meridian, meridian == null);
}
function formatTime(hour, minute, meridian, is24hr) {
if(is24hr){
if (minute >= 60) {
minute -= 60;
hour += 1;
} else if (minute < 0) {
minute += 60;
hour -= 1;
}
if(minute === 0){
minute = "00"
}
if(hour > 24){
hour -= 24;
}
if(hour < 10){
hour = "0" + hour
}
return {hour, minute, meridian: ""}
}
if (minute >= 60) {
minute -= 60;
hour += 1;
} else if (minute < 0) {
minute += 60;
hour -= 1;
}
if(minute === 0){
minute = "00"
}
if (hour > 12) {
hour = hour - 12;
meridian = meridian ? (meridian === "am" ? "pm" : "am") : "";
}
return { hour, minute, meridian };
}
function findTimezoneDataFromTimezone(timezone) {
return _timezones.find((timezoneData) => timezoneData.abbr === timezone);
}
function cleanTimeOffset(offset) {
let decimalPlace = offset % 1;
let hour = offset - decimalPlace;
return { hour, minute: decimalPlace * 60 };
}
</script> -->
</body>
</html>