Update transcript-tracer.js
Browse files- transcript-tracer.js +123 -110
transcript-tracer.js
CHANGED
@@ -21,6 +21,8 @@ var ttTimeOffset = 0;
|
|
21 |
var ttAutoScroll = null;
|
22 |
var ttClickable = false;
|
23 |
|
|
|
|
|
24 |
|
25 |
// Prepare transcripts and media players
|
26 |
function loadTranscriptTracer(options=null) {
|
@@ -31,7 +33,7 @@ function loadTranscriptTracer(options=null) {
|
|
31 |
});
|
32 |
return;
|
33 |
}
|
34 |
-
|
35 |
// Save user-provided options to configuration variables
|
36 |
if (options) {
|
37 |
if ('blockSelector' in options) ttBlockSelector = options.blockSelector;
|
@@ -41,7 +43,7 @@ function loadTranscriptTracer(options=null) {
|
|
41 |
if ('autoScroll' in options) ttAutoScroll = options.autoScroll;
|
42 |
if ('clickable' in options) ttClickable = options.clickable;
|
43 |
}
|
44 |
-
|
45 |
// Reset Transcript Tracer to prevent unexpected behavior if loadTranscriptTracer() is called more than once
|
46 |
if (ttIsInitialized) {
|
47 |
if (ttTranscripts) for (const transcript of ttTranscripts) {
|
@@ -56,7 +58,7 @@ function loadTranscriptTracer(options=null) {
|
|
56 |
element.outerHTML = element.innerHTML;
|
57 |
});
|
58 |
}
|
59 |
-
|
60 |
// Set a few global state variables
|
61 |
ttIsInitialized = true;
|
62 |
ttTranscripts = document.getElementsByClassName('tt-transcript');
|
@@ -67,9 +69,9 @@ function loadTranscriptTracer(options=null) {
|
|
67 |
var transcript = ttTranscripts[t];
|
68 |
// Skip transcript if it's not linked to any media files
|
69 |
if (!transcript.dataset.ttMediaUrls) continue;
|
70 |
-
|
71 |
transcript.dataset.ttTranscript = t;
|
72 |
-
|
73 |
// Loop through text nodes to add spans (tt-word, tt-whitespace)
|
74 |
// Method of iterating text nodes thanks to https://stackoverflow.com/a/34700627/1349044
|
75 |
var iter = document.createNodeIterator(transcript, NodeFilter.SHOW_TEXT);
|
@@ -79,7 +81,7 @@ function loadTranscriptTracer(options=null) {
|
|
79 |
if (text.replace(/\s/g, '').length != 0) {
|
80 |
var spannedText = '<span class="tt-word">' + text.replace(/(\s+)/g, '</span><span class="tt-whitespace">$1</span><span class="tt-word">') + '</span>';
|
81 |
spannedText = spannedText.replaceAll('<span class="tt-word"></span>', '');
|
82 |
-
|
83 |
// Replace text node with spanned text
|
84 |
const template = document.createElement('template');
|
85 |
template.innerHTML = spannedText;
|
@@ -88,11 +90,11 @@ function loadTranscriptTracer(options=null) {
|
|
88 |
}
|
89 |
}
|
90 |
}
|
91 |
-
|
92 |
for (const mediaPlayer of ttMediaPlayers) {
|
93 |
// Link related transcript(s)
|
94 |
linkTranscripts(mediaPlayer);
|
95 |
-
|
96 |
// Add event listener to media player
|
97 |
mediaPlayer.addEventListener('play', function(e) {
|
98 |
if (ttActivePlayer != e.currentTarget) {
|
@@ -113,11 +115,29 @@ function loadTranscriptTracer(options=null) {
|
|
113 |
if (ttCurrentEvent) ttCurrentEvent = null;
|
114 |
});
|
115 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
}
|
117 |
|
118 |
|
119 |
// Link media player to relevant transcripts
|
120 |
function linkTranscripts(mediaPlayer) {
|
|
|
121 |
var trackElement = mediaPlayer.querySelector('track[kind="metadata"]')
|
122 |
|
123 |
var mediaPlayerSourceUrls = [];
|
@@ -125,12 +145,12 @@ function linkTranscripts(mediaPlayer) {
|
|
125 |
var mediaPlayerSourceElements = mediaPlayer.querySelectorAll('source');
|
126 |
if (mediaPlayerSrc) mediaPlayerSourceUrls.push(mediaPlayerSrc);
|
127 |
if (mediaPlayerSourceElements) for (const s of mediaPlayerSourceElements) mediaPlayerSourceUrls.push(s.src);
|
128 |
-
|
129 |
// If there's nothing to link, return
|
130 |
if (!trackElement || !trackElement.getAttribute('src') || mediaPlayerSourceUrls.length == 0) return;
|
131 |
-
|
132 |
// Fetch WebVTT content and link related transcripts
|
133 |
-
for (const transcript of ttTranscripts) {
|
134 |
for (const mediaUrl of mediaPlayerSourceUrls) {
|
135 |
if (transcript.dataset.ttMediaUrls.includes(mediaUrl)) {
|
136 |
mediaPlayer.dataset.ttLinkedMediaUrl = mediaUrl;
|
@@ -146,26 +166,26 @@ function linkTranscripts(mediaPlayer) {
|
|
146 |
function linkTranscript(mediaPlayer, vttContent, transcript) {
|
147 |
var wordTimings = parseVttToWordTimings(vttContent);
|
148 |
transcript.dataset.ttCurrentMediaUrl = mediaPlayer.dataset.ttLinkedMediaUrl;
|
149 |
-
|
150 |
function normalizedWord(word) {
|
151 |
// Convert to lowercase, normalize, and remove anything that's not a letter or number
|
152 |
return word.toLowerCase().normalize('NFD').replace(/[^\p{L}\p{N}]/gu, '');
|
153 |
}
|
154 |
-
|
155 |
// Add metadata to block and phrase containers (if ttBlockSelector and ttPhraseSelector are defined)
|
156 |
var blockContainers = ttBlockSelector ? transcript.querySelectorAll(ttBlockSelector) : [];
|
157 |
for (let c = 0; c < blockContainers.length; c++) blockContainers[c].dataset.ttBlock = c;
|
158 |
var phraseContainers = ttPhraseSelector ? transcript.querySelectorAll(ttPhraseSelector) : [];
|
159 |
for (let c = 0; c < phraseContainers.length; c++) phraseContainers[c].dataset.ttPhrase = c;
|
160 |
-
|
161 |
// Add metadata to each word span, and build timed events list
|
162 |
var timedEvents = [];
|
163 |
var wordTimingsIndex = 0;
|
164 |
var wordSpans = transcript.getElementsByClassName('tt-word');
|
165 |
for (let s = 0; s < wordSpans.length; s++) {
|
166 |
var span = wordSpans[s];
|
167 |
-
|
168 |
-
// Find the next word timing object that matches the current span's text
|
169 |
var initialWordTimingsIndex = wordTimingsIndex;
|
170 |
var maxFuzzyWordTimingsIndex = Math.min(wordTimingsIndex + ttAlignmentFuzziness, wordTimings.length - 1);
|
171 |
while (normalizedWord(span.innerText) != normalizedWord(wordTimings[wordTimingsIndex].text) && wordTimingsIndex <= maxFuzzyWordTimingsIndex) {
|
@@ -176,17 +196,20 @@ function linkTranscripts(mediaPlayer) {
|
|
176 |
wordTimingsIndex = initialWordTimingsIndex;
|
177 |
continue;
|
178 |
}
|
179 |
-
|
180 |
// Get the block, phrase, and word index
|
181 |
blockIndex = ttBlockSelector ? (span.closest(ttBlockSelector)?.dataset?.ttBlock ?? null) : wordTimings[wordTimingsIndex].blockIndex;
|
182 |
phraseIndex = ttPhraseSelector ? (span.closest(ttPhraseSelector)?.dataset?.ttPhrase ?? null) : wordTimings[wordTimingsIndex].phraseIndex;
|
183 |
wordIndex = wordTimings[wordTimingsIndex].wordIndex;
|
184 |
-
|
185 |
// Add block, phrase, and word index as metadata on the span
|
186 |
span.dataset.ttBlock = blockIndex;
|
187 |
span.dataset.ttPhrase = phraseIndex;
|
188 |
span.dataset.ttWord = wordIndex;
|
189 |
|
|
|
|
|
|
|
190 |
// Add timed event to timed events list
|
191 |
if (timedEvents.length != 0 && wordTimings[wordTimingsIndex].startSeconds == timedEvents[timedEvents.length-1].seconds) {
|
192 |
timedEvents[timedEvents.length-1].currentWordIndexes.push(wordIndex);
|
@@ -198,7 +221,7 @@ function linkTranscripts(mediaPlayer) {
|
|
198 |
'blockIndex': blockIndex,
|
199 |
});
|
200 |
}
|
201 |
-
|
202 |
wordTimingsIndex += 1;
|
203 |
}
|
204 |
|
@@ -219,7 +242,7 @@ function linkTranscripts(mediaPlayer) {
|
|
219 |
}
|
220 |
return null;
|
221 |
}
|
222 |
-
|
223 |
// Add metadata to block and phrase containers (if ttBlockSelector and ttPhraseSelector aren't defined)
|
224 |
if (!ttBlockSelector) {
|
225 |
var count = wordTimings[wordTimings.length-1].blockIndex + 1;
|
@@ -237,12 +260,12 @@ function linkTranscripts(mediaPlayer) {
|
|
237 |
if (phraseContainer) phraseContainer.dataset.ttPhrase = c;
|
238 |
}
|
239 |
}
|
240 |
-
|
241 |
// Sort timed events list by time
|
242 |
-
timedEvents = timedEvents.sort(function(a, b) {
|
243 |
return a.seconds - b.seconds;
|
244 |
})
|
245 |
-
|
246 |
// Add reference data to ttLinkedDataByMediaUrl
|
247 |
var transcriptIndex = parseInt(transcript.dataset.ttTranscript);
|
248 |
ttLinkedDataByMediaUrl[mediaPlayer.dataset.ttLinkedMediaUrl] = {
|
@@ -271,20 +294,20 @@ function linkTranscripts(mediaPlayer) {
|
|
271 |
// Unlink transcript from previous VTT
|
272 |
function unlinkTranscript(transcript) {
|
273 |
clearHighlightedWords(transcript);
|
274 |
-
|
275 |
var ttLinkedElements = transcript.querySelectorAll('[data-tt-word]');
|
276 |
for (const element of ttLinkedElements) {
|
277 |
element.dataset.ttWord = '';
|
278 |
element.dataset.ttPhrase = '';
|
279 |
element.dataset.ttBlock = '';
|
280 |
}
|
281 |
-
|
282 |
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
283 |
if (mediaUrl) {
|
284 |
delete ttLinkedDataByMediaUrl[mediaUrl]
|
285 |
transcript.dataset.ttCurrentMediaUrl = '';
|
286 |
}
|
287 |
-
|
288 |
for (const word of document.querySelectorAll('.tt-word')) {
|
289 |
word.removeEventListener('click', handleWordClick);
|
290 |
}
|
@@ -299,14 +322,14 @@ function unlinkTranscript(transcript) {
|
|
299 |
// Convert WebVTT into a wordTimings list
|
300 |
function parseVttToWordTimings(vttContent) {
|
301 |
var wordTimings = [];
|
302 |
-
|
303 |
// Split WebVTT at each double line break
|
304 |
var vttSegments = vttContent.split(/\r?\n\r?\n/);
|
305 |
if (vttSegments.length == 0 || !vttSegments[0].startsWith('WEBVTT')) {
|
306 |
console.error('Error: Invalid VTT file. See https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API');
|
307 |
return;
|
308 |
}
|
309 |
-
|
310 |
// Loop through each segment of the WebVTT
|
311 |
// WebVTT documentation: https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API
|
312 |
var cueWordCounter = 0;
|
@@ -314,10 +337,10 @@ function parseVttToWordTimings(vttContent) {
|
|
314 |
var cueBlockCounter = 0;
|
315 |
for (let s = 0; s < vttSegments.length; s++) {
|
316 |
var segment = vttSegments[s];
|
317 |
-
|
318 |
// Skip segment if it's not a cue
|
319 |
if (segment.startsWith('WEBVTT') || /^STYLE\s/.test(segment) || /^NOTE\s/.test(segment)) continue;
|
320 |
-
|
321 |
// Convert VTT timestamps to seconds
|
322 |
function convertVttTimestampToSeconds(vttTimestamp) {
|
323 |
var parts = vttTimestamp.trim().split(':');
|
@@ -329,7 +352,7 @@ function parseVttToWordTimings(vttContent) {
|
|
329 |
}
|
330 |
return seconds;
|
331 |
}
|
332 |
-
|
333 |
// Parse cue
|
334 |
var cueIdentifier = cuePayload = '';
|
335 |
var cueStartSeconds = cueEndSeconds = null;
|
@@ -354,7 +377,7 @@ function parseVttToWordTimings(vttContent) {
|
|
354 |
wordStartSeconds = convertVttTimestampToSeconds(match[1].replace('<', '').replace('>', ''));
|
355 |
word = match[2];
|
356 |
}
|
357 |
-
|
358 |
// Push word to wordTimings list
|
359 |
if (word) {
|
360 |
wordTimings.push({
|
@@ -373,7 +396,7 @@ function parseVttToWordTimings(vttContent) {
|
|
373 |
}
|
374 |
cueBlockCounter += 1;
|
375 |
}
|
376 |
-
|
377 |
return wordTimings;
|
378 |
}
|
379 |
|
@@ -386,42 +409,42 @@ var ttNextEvent = null;
|
|
386 |
function ttTimeUpdate(e) {
|
387 |
// If the current player isn't active or doesn't have data, return
|
388 |
if (!ttActivePlayer || e.currentTarget != ttActivePlayer || !(ttActivePlayer.dataset.ttLinkedMediaUrl in ttLinkedDataByMediaUrl)) return;
|
389 |
-
|
390 |
var adjustedCurrentTime = ttActivePlayer.currentTime + (ttTimeOffset * -1);
|
391 |
var ttData = ttLinkedDataByMediaUrl[ttActivePlayer.dataset.ttLinkedMediaUrl];
|
392 |
-
|
393 |
// Make sure the correct transcript is selected
|
394 |
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
|
395 |
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
|
396 |
}
|
397 |
-
|
398 |
// If before the first event, after the last event, or within the range of the current event, return
|
399 |
if (ttCurrentEvent && (ttCurrentEvent.seconds < ttData.timedEvents[0].seconds || ttCurrentEvent.seconds > ttData.timedEvents[ttData.timedEvents.length-1].seconds)) return;
|
400 |
if (ttCurrentEvent && ttNextEvent && ttCurrentEvent.seconds <= adjustedCurrentTime && ttNextEvent.seconds > adjustedCurrentTime) return;
|
401 |
-
|
402 |
// Clear words that were highlighted from the previous event
|
403 |
clearHighlightedWords(ttCurrentTranscript);
|
404 |
-
|
405 |
// Add highlights for the current event
|
406 |
for (let t = 0; t < ttData.timedEvents.length; t++) {
|
407 |
if (ttData.timedEvents[t].seconds <= adjustedCurrentTime && (!ttData.timedEvents[t+1] || adjustedCurrentTime < ttData.timedEvents[t+1]?.seconds)) {
|
408 |
-
|
409 |
ttPreviousEvent = ttData.timedEvents[t-1] || null;
|
410 |
ttCurrentEvent = ttData.timedEvents[t];
|
411 |
ttNextEvent = ttData.timedEvents[t+1] || null;
|
412 |
-
|
413 |
// Mark blocks
|
414 |
if (ttCurrentEvent.blockIndex != null) {
|
415 |
var blockElements = ttCurrentTranscript.querySelectorAll(`[data-tt-block="${ttCurrentEvent.blockIndex}"]`);
|
416 |
for (let b = 0; b < blockElements.length; b++) blockElements[b].classList.add((b==0 && !blockElements[b].classList.contains('tt-word')) ? 'tt-current-block-container' : 'tt-current-block');
|
417 |
}
|
418 |
-
|
419 |
// Mark phrases
|
420 |
if (ttCurrentEvent.phraseIndex != null) {
|
421 |
var phraseElements = ttCurrentTranscript.querySelectorAll(`[data-tt-phrase="${ttCurrentEvent.phraseIndex}"]`);
|
422 |
for (let p = 0; p < phraseElements.length; p++) phraseElements[p].classList.add((p==0 && !phraseElements[p].classList.contains('tt-word')) ? 'tt-current-phrase-container' : 'tt-current-phrase');
|
423 |
}
|
424 |
-
|
425 |
// Mark words
|
426 |
if (ttCurrentEvent.currentWordIndexes.length > 0) {
|
427 |
for (const wordIndex of ttCurrentEvent.currentWordIndexes) {
|
@@ -433,7 +456,7 @@ function ttTimeUpdate(e) {
|
|
433 |
wordElement.classList.add('tt-previous-word');
|
434 |
}
|
435 |
}
|
436 |
-
|
437 |
// Auto-scroll to the highlighted text
|
438 |
if (ttAutoScroll && !ttActivePlayer.paused && ttActivePlayer.duration != 0) {
|
439 |
var scrollOptions = { behavior: 'smooth', block: 'nearest', inline: 'nearest' }
|
@@ -443,9 +466,39 @@ function ttTimeUpdate(e) {
|
|
443 |
document.querySelector('.tt-current-phrase-container').scrollIntoView(scrollOptions);
|
444 |
} else if (ttAutoScroll == 'word') {
|
445 |
document.querySelector('.tt-current-word').scrollIntoView(scrollOptions);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
446 |
}
|
447 |
}
|
448 |
-
|
449 |
break;
|
450 |
}
|
451 |
}
|
@@ -467,16 +520,16 @@ function handleWordClick(e) {
|
|
467 |
var wordElement = e.currentTarget;
|
468 |
var wordIndex = wordElement.dataset.ttWord;
|
469 |
|
470 |
-
if (wordElement.className == "tt-whitespace" || wordIndex === undefined) return;
|
471 |
-
|
472 |
var transcript = wordElement.closest('.tt-transcript');
|
473 |
-
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
474 |
var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds
|
475 |
|
476 |
ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds;
|
477 |
|
478 |
var ttData = ttLinkedDataByMediaUrl[mediaUrl];
|
479 |
-
|
480 |
// Make sure the correct transcript is selected
|
481 |
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
|
482 |
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
|
@@ -484,7 +537,7 @@ function handleWordClick(e) {
|
|
484 |
|
485 |
// Clear words that were highlighted from the previous event
|
486 |
clearHighlightedWords(ttCurrentTranscript);
|
487 |
-
|
488 |
// Mark words
|
489 |
wordElement.classList.add('tt-current-word');
|
490 |
|
@@ -492,7 +545,7 @@ function handleWordClick(e) {
|
|
492 |
if (wordElement.classList.contains('tt-current-word')) break;
|
493 |
wordElement.classList.add('tt-previous-word');
|
494 |
}
|
495 |
-
|
496 |
}
|
497 |
|
498 |
// Handle when a word in the transcript with an event listener is clicked
|
@@ -500,8 +553,8 @@ function handleWordDBClick(e) {
|
|
500 |
var wordElement = e.currentTarget;
|
501 |
var wordIndex = wordElement.dataset.ttWord;
|
502 |
|
503 |
-
if (wordElement.className == "tt-whitespace" || wordIndex === undefined) return;
|
504 |
-
|
505 |
var transcript = wordElement.closest('.tt-transcript');
|
506 |
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
507 |
var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds
|
@@ -509,7 +562,7 @@ function handleWordDBClick(e) {
|
|
509 |
ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds;
|
510 |
|
511 |
var ttData = ttLinkedDataByMediaUrl[mediaUrl];
|
512 |
-
|
513 |
// Make sure the correct transcript is selected
|
514 |
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
|
515 |
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
|
@@ -517,7 +570,7 @@ function handleWordDBClick(e) {
|
|
517 |
|
518 |
// Clear words that were highlighted from the previous event
|
519 |
clearHighlightedWords(ttCurrentTranscript);
|
520 |
-
|
521 |
// Mark words
|
522 |
wordElement.classList.add('tt-current-word');
|
523 |
|
@@ -532,61 +585,39 @@ function handleWordDBClick(e) {
|
|
532 |
}
|
533 |
}
|
534 |
|
535 |
-
|
536 |
function handleSelection() {
|
537 |
|
538 |
// Get selection
|
539 |
var selection = document.getSelection();
|
540 |
-
if (selection.type != "Range") return;
|
541 |
|
542 |
// ====================================================================================================
|
543 |
-
|
544 |
// These can be obtained simply with `startWordElement`
|
545 |
var transcript = selection.baseNode.parentNode.closest('.tt-transcript');
|
546 |
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
547 |
-
|
548 |
-
// ====================================================================================================
|
549 |
-
|
550 |
-
var dict = {};
|
551 |
-
var nodes = [];
|
552 |
-
var listIndex = -1;
|
553 |
-
for (const x of transcript.children) {
|
554 |
-
for (const wordElement of x.children) {
|
555 |
-
listIndex = listIndex + 1;
|
556 |
-
// Note: manually add extra information
|
557 |
-
wordElement.listIndex = listIndex;
|
558 |
-
nodes.push(wordElement);
|
559 |
-
if (wordElement.className != "tt-whitespace") {
|
560 |
-
wordIndex = wordElement.dataset.ttWord;
|
561 |
-
// TODO: We no longer need this after we inject the list index information into `wordElement`.
|
562 |
-
dict[wordIndex] = listIndex;
|
563 |
-
}
|
564 |
-
}
|
565 |
-
}
|
566 |
-
console.log(nodes);
|
567 |
-
console.log(dict);
|
568 |
|
569 |
// ====================================================================================================
|
570 |
-
|
571 |
var startNode = selection.baseNode;
|
572 |
var startWordElement = startNode.parentNode;
|
573 |
-
|
574 |
-
// Used for highlight
|
575 |
var startWordElementForHighlight = startWordElement
|
576 |
-
|
577 |
while (startWordElementForHighlight.className == "tt-whitespace") {
|
578 |
var index = startWordElementForHighlight.listIndex + 1;
|
579 |
if (index >= nodes.length) break;
|
580 |
startWordElementForHighlight = nodes[index];
|
581 |
}
|
582 |
-
|
583 |
// This could be `undefined`!
|
584 |
var startWordIndexForHighlight = startWordElementForHighlight.dataset.ttWord;
|
585 |
// This is always defined.
|
586 |
var startWordListIndexForHighlight = startWordElementForHighlight.listIndex;
|
587 |
-
|
588 |
// Used for computing the start seconds
|
589 |
-
var startWordElementForTime = startWordElementForHighlight;
|
590 |
|
591 |
// No time specified for some words --> we compute the starting time with the first element where the time information is provided.
|
592 |
// (This could go beyond the selected religon!)
|
@@ -606,20 +637,11 @@ function handleSelection() {
|
|
606 |
startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[startWordIndexForTime].startSeconds;
|
607 |
}
|
608 |
|
609 |
-
console.log(startWordElement);
|
610 |
-
console.log(startWordElementForHighlight);
|
611 |
-
console.log(startWordIndexForHighlight);
|
612 |
-
console.log(startWordListIndexForHighlight);
|
613 |
-
console.log(startWordElementForTime);
|
614 |
-
console.log(startWordIndexForTime);
|
615 |
-
console.log(startWordListIndexForTime);
|
616 |
-
console.log(startSeconds);
|
617 |
-
|
618 |
// ====================================================================================================
|
619 |
|
620 |
var endNode = selection.extentNode;
|
621 |
var endWordElement = endNode.parentNode;
|
622 |
-
|
623 |
// Used for highlight
|
624 |
var endWordElementForHighlight = endWordElement;
|
625 |
|
@@ -639,13 +661,13 @@ function handleSelection() {
|
|
639 |
|
640 |
// Forward to the next element (if possible)
|
641 |
if ((endWordElementForTime.listIndex + 1) < nodes.length) {
|
642 |
-
endWordElementForTime = nodes[endWordElementForTime.listIndex + 1];
|
643 |
}
|
644 |
-
|
645 |
while (endWordElementForTime.className == "tt-whitespace" || (endWordElementForTime.dataset.ttWord === undefined)) {
|
646 |
var index = endWordElementForTime.listIndex + 1;
|
647 |
if (index >= nodes.length) break;
|
648 |
-
endWordElementForTime = nodes[index];
|
649 |
}
|
650 |
|
651 |
// This could still be `undefined`.
|
@@ -653,7 +675,7 @@ function handleSelection() {
|
|
653 |
// Let's get the `listIndex`.
|
654 |
var endWordListIndexForTime = endWordElementForTime.listIndex;
|
655 |
|
656 |
-
var endSeconds = null;
|
657 |
if (endWordIndexForTime !== undefined) {
|
658 |
endSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[endWordIndexForTime].startSeconds;
|
659 |
if (endWordElement.listIndex == nodes.length - 1) {
|
@@ -661,18 +683,9 @@ function handleSelection() {
|
|
661 |
}
|
662 |
}
|
663 |
|
664 |
-
console.log(endWordElement);
|
665 |
-
console.log(endWordElementForHighlight);
|
666 |
-
console.log(endWordIndexForHighlight);
|
667 |
-
console.log(endWordListIndexForHighlight);
|
668 |
-
console.log(endWordElementForTime);
|
669 |
-
console.log(endWordIndexForTime);
|
670 |
-
console.log(endWordListIndexForTime);
|
671 |
-
console.log(endSeconds);
|
672 |
-
|
673 |
return
|
674 |
-
|
675 |
-
// ====================================================================================================
|
676 |
// Mark selection
|
677 |
|
678 |
|
|
|
21 |
var ttAutoScroll = null;
|
22 |
var ttClickable = false;
|
23 |
|
24 |
+
var dict = {};
|
25 |
+
var nodes = [];
|
26 |
|
27 |
// Prepare transcripts and media players
|
28 |
function loadTranscriptTracer(options=null) {
|
|
|
33 |
});
|
34 |
return;
|
35 |
}
|
36 |
+
|
37 |
// Save user-provided options to configuration variables
|
38 |
if (options) {
|
39 |
if ('blockSelector' in options) ttBlockSelector = options.blockSelector;
|
|
|
43 |
if ('autoScroll' in options) ttAutoScroll = options.autoScroll;
|
44 |
if ('clickable' in options) ttClickable = options.clickable;
|
45 |
}
|
46 |
+
|
47 |
// Reset Transcript Tracer to prevent unexpected behavior if loadTranscriptTracer() is called more than once
|
48 |
if (ttIsInitialized) {
|
49 |
if (ttTranscripts) for (const transcript of ttTranscripts) {
|
|
|
58 |
element.outerHTML = element.innerHTML;
|
59 |
});
|
60 |
}
|
61 |
+
|
62 |
// Set a few global state variables
|
63 |
ttIsInitialized = true;
|
64 |
ttTranscripts = document.getElementsByClassName('tt-transcript');
|
|
|
69 |
var transcript = ttTranscripts[t];
|
70 |
// Skip transcript if it's not linked to any media files
|
71 |
if (!transcript.dataset.ttMediaUrls) continue;
|
72 |
+
|
73 |
transcript.dataset.ttTranscript = t;
|
74 |
+
|
75 |
// Loop through text nodes to add spans (tt-word, tt-whitespace)
|
76 |
// Method of iterating text nodes thanks to https://stackoverflow.com/a/34700627/1349044
|
77 |
var iter = document.createNodeIterator(transcript, NodeFilter.SHOW_TEXT);
|
|
|
81 |
if (text.replace(/\s/g, '').length != 0) {
|
82 |
var spannedText = '<span class="tt-word">' + text.replace(/(\s+)/g, '</span><span class="tt-whitespace">$1</span><span class="tt-word">') + '</span>';
|
83 |
spannedText = spannedText.replaceAll('<span class="tt-word"></span>', '');
|
84 |
+
|
85 |
// Replace text node with spanned text
|
86 |
const template = document.createElement('template');
|
87 |
template.innerHTML = spannedText;
|
|
|
90 |
}
|
91 |
}
|
92 |
}
|
93 |
+
|
94 |
for (const mediaPlayer of ttMediaPlayers) {
|
95 |
// Link related transcript(s)
|
96 |
linkTranscripts(mediaPlayer);
|
97 |
+
|
98 |
// Add event listener to media player
|
99 |
mediaPlayer.addEventListener('play', function(e) {
|
100 |
if (ttActivePlayer != e.currentTarget) {
|
|
|
115 |
if (ttCurrentEvent) ttCurrentEvent = null;
|
116 |
});
|
117 |
}
|
118 |
+
|
119 |
+
// ====================================================================================================
|
120 |
+
|
121 |
+
if (nodes.length > 0) nodes = []; dict = {};
|
122 |
+
var transcript = ttTranscripts[0];
|
123 |
+
var listIndex= -1;
|
124 |
+
for (const x of transcript.children) {
|
125 |
+
for (const wordElement of x.children) {
|
126 |
+
listIndex = listIndex + 1;
|
127 |
+
// Note: manually add extra information
|
128 |
+
wordElement.listIndex = listIndex;
|
129 |
+
nodes.push(wordElement);
|
130 |
+
}
|
131 |
+
}
|
132 |
+
|
133 |
+
// ====================================================================================================
|
134 |
+
|
135 |
}
|
136 |
|
137 |
|
138 |
// Link media player to relevant transcripts
|
139 |
function linkTranscripts(mediaPlayer) {
|
140 |
+
|
141 |
var trackElement = mediaPlayer.querySelector('track[kind="metadata"]')
|
142 |
|
143 |
var mediaPlayerSourceUrls = [];
|
|
|
145 |
var mediaPlayerSourceElements = mediaPlayer.querySelectorAll('source');
|
146 |
if (mediaPlayerSrc) mediaPlayerSourceUrls.push(mediaPlayerSrc);
|
147 |
if (mediaPlayerSourceElements) for (const s of mediaPlayerSourceElements) mediaPlayerSourceUrls.push(s.src);
|
148 |
+
|
149 |
// If there's nothing to link, return
|
150 |
if (!trackElement || !trackElement.getAttribute('src') || mediaPlayerSourceUrls.length == 0) return;
|
151 |
+
|
152 |
// Fetch WebVTT content and link related transcripts
|
153 |
+
for (const transcript of ttTranscripts) {
|
154 |
for (const mediaUrl of mediaPlayerSourceUrls) {
|
155 |
if (transcript.dataset.ttMediaUrls.includes(mediaUrl)) {
|
156 |
mediaPlayer.dataset.ttLinkedMediaUrl = mediaUrl;
|
|
|
166 |
function linkTranscript(mediaPlayer, vttContent, transcript) {
|
167 |
var wordTimings = parseVttToWordTimings(vttContent);
|
168 |
transcript.dataset.ttCurrentMediaUrl = mediaPlayer.dataset.ttLinkedMediaUrl;
|
169 |
+
|
170 |
function normalizedWord(word) {
|
171 |
// Convert to lowercase, normalize, and remove anything that's not a letter or number
|
172 |
return word.toLowerCase().normalize('NFD').replace(/[^\p{L}\p{N}]/gu, '');
|
173 |
}
|
174 |
+
|
175 |
// Add metadata to block and phrase containers (if ttBlockSelector and ttPhraseSelector are defined)
|
176 |
var blockContainers = ttBlockSelector ? transcript.querySelectorAll(ttBlockSelector) : [];
|
177 |
for (let c = 0; c < blockContainers.length; c++) blockContainers[c].dataset.ttBlock = c;
|
178 |
var phraseContainers = ttPhraseSelector ? transcript.querySelectorAll(ttPhraseSelector) : [];
|
179 |
for (let c = 0; c < phraseContainers.length; c++) phraseContainers[c].dataset.ttPhrase = c;
|
180 |
+
|
181 |
// Add metadata to each word span, and build timed events list
|
182 |
var timedEvents = [];
|
183 |
var wordTimingsIndex = 0;
|
184 |
var wordSpans = transcript.getElementsByClassName('tt-word');
|
185 |
for (let s = 0; s < wordSpans.length; s++) {
|
186 |
var span = wordSpans[s];
|
187 |
+
|
188 |
+
// Find the next word timing object that matches the current span's text
|
189 |
var initialWordTimingsIndex = wordTimingsIndex;
|
190 |
var maxFuzzyWordTimingsIndex = Math.min(wordTimingsIndex + ttAlignmentFuzziness, wordTimings.length - 1);
|
191 |
while (normalizedWord(span.innerText) != normalizedWord(wordTimings[wordTimingsIndex].text) && wordTimingsIndex <= maxFuzzyWordTimingsIndex) {
|
|
|
196 |
wordTimingsIndex = initialWordTimingsIndex;
|
197 |
continue;
|
198 |
}
|
199 |
+
|
200 |
// Get the block, phrase, and word index
|
201 |
blockIndex = ttBlockSelector ? (span.closest(ttBlockSelector)?.dataset?.ttBlock ?? null) : wordTimings[wordTimingsIndex].blockIndex;
|
202 |
phraseIndex = ttPhraseSelector ? (span.closest(ttPhraseSelector)?.dataset?.ttPhrase ?? null) : wordTimings[wordTimingsIndex].phraseIndex;
|
203 |
wordIndex = wordTimings[wordTimingsIndex].wordIndex;
|
204 |
+
|
205 |
// Add block, phrase, and word index as metadata on the span
|
206 |
span.dataset.ttBlock = blockIndex;
|
207 |
span.dataset.ttPhrase = phraseIndex;
|
208 |
span.dataset.ttWord = wordIndex;
|
209 |
|
210 |
+
// We no longer needs this!
|
211 |
+
dict[wordIndex] = span.listIndex;
|
212 |
+
|
213 |
// Add timed event to timed events list
|
214 |
if (timedEvents.length != 0 && wordTimings[wordTimingsIndex].startSeconds == timedEvents[timedEvents.length-1].seconds) {
|
215 |
timedEvents[timedEvents.length-1].currentWordIndexes.push(wordIndex);
|
|
|
221 |
'blockIndex': blockIndex,
|
222 |
});
|
223 |
}
|
224 |
+
|
225 |
wordTimingsIndex += 1;
|
226 |
}
|
227 |
|
|
|
242 |
}
|
243 |
return null;
|
244 |
}
|
245 |
+
|
246 |
// Add metadata to block and phrase containers (if ttBlockSelector and ttPhraseSelector aren't defined)
|
247 |
if (!ttBlockSelector) {
|
248 |
var count = wordTimings[wordTimings.length-1].blockIndex + 1;
|
|
|
260 |
if (phraseContainer) phraseContainer.dataset.ttPhrase = c;
|
261 |
}
|
262 |
}
|
263 |
+
|
264 |
// Sort timed events list by time
|
265 |
+
timedEvents = timedEvents.sort(function(a, b) {
|
266 |
return a.seconds - b.seconds;
|
267 |
})
|
268 |
+
|
269 |
// Add reference data to ttLinkedDataByMediaUrl
|
270 |
var transcriptIndex = parseInt(transcript.dataset.ttTranscript);
|
271 |
ttLinkedDataByMediaUrl[mediaPlayer.dataset.ttLinkedMediaUrl] = {
|
|
|
294 |
// Unlink transcript from previous VTT
|
295 |
function unlinkTranscript(transcript) {
|
296 |
clearHighlightedWords(transcript);
|
297 |
+
|
298 |
var ttLinkedElements = transcript.querySelectorAll('[data-tt-word]');
|
299 |
for (const element of ttLinkedElements) {
|
300 |
element.dataset.ttWord = '';
|
301 |
element.dataset.ttPhrase = '';
|
302 |
element.dataset.ttBlock = '';
|
303 |
}
|
304 |
+
|
305 |
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
306 |
if (mediaUrl) {
|
307 |
delete ttLinkedDataByMediaUrl[mediaUrl]
|
308 |
transcript.dataset.ttCurrentMediaUrl = '';
|
309 |
}
|
310 |
+
|
311 |
for (const word of document.querySelectorAll('.tt-word')) {
|
312 |
word.removeEventListener('click', handleWordClick);
|
313 |
}
|
|
|
322 |
// Convert WebVTT into a wordTimings list
|
323 |
function parseVttToWordTimings(vttContent) {
|
324 |
var wordTimings = [];
|
325 |
+
|
326 |
// Split WebVTT at each double line break
|
327 |
var vttSegments = vttContent.split(/\r?\n\r?\n/);
|
328 |
if (vttSegments.length == 0 || !vttSegments[0].startsWith('WEBVTT')) {
|
329 |
console.error('Error: Invalid VTT file. See https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API');
|
330 |
return;
|
331 |
}
|
332 |
+
|
333 |
// Loop through each segment of the WebVTT
|
334 |
// WebVTT documentation: https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API
|
335 |
var cueWordCounter = 0;
|
|
|
337 |
var cueBlockCounter = 0;
|
338 |
for (let s = 0; s < vttSegments.length; s++) {
|
339 |
var segment = vttSegments[s];
|
340 |
+
|
341 |
// Skip segment if it's not a cue
|
342 |
if (segment.startsWith('WEBVTT') || /^STYLE\s/.test(segment) || /^NOTE\s/.test(segment)) continue;
|
343 |
+
|
344 |
// Convert VTT timestamps to seconds
|
345 |
function convertVttTimestampToSeconds(vttTimestamp) {
|
346 |
var parts = vttTimestamp.trim().split(':');
|
|
|
352 |
}
|
353 |
return seconds;
|
354 |
}
|
355 |
+
|
356 |
// Parse cue
|
357 |
var cueIdentifier = cuePayload = '';
|
358 |
var cueStartSeconds = cueEndSeconds = null;
|
|
|
377 |
wordStartSeconds = convertVttTimestampToSeconds(match[1].replace('<', '').replace('>', ''));
|
378 |
word = match[2];
|
379 |
}
|
380 |
+
|
381 |
// Push word to wordTimings list
|
382 |
if (word) {
|
383 |
wordTimings.push({
|
|
|
396 |
}
|
397 |
cueBlockCounter += 1;
|
398 |
}
|
399 |
+
|
400 |
return wordTimings;
|
401 |
}
|
402 |
|
|
|
409 |
function ttTimeUpdate(e) {
|
410 |
// If the current player isn't active or doesn't have data, return
|
411 |
if (!ttActivePlayer || e.currentTarget != ttActivePlayer || !(ttActivePlayer.dataset.ttLinkedMediaUrl in ttLinkedDataByMediaUrl)) return;
|
412 |
+
|
413 |
var adjustedCurrentTime = ttActivePlayer.currentTime + (ttTimeOffset * -1);
|
414 |
var ttData = ttLinkedDataByMediaUrl[ttActivePlayer.dataset.ttLinkedMediaUrl];
|
415 |
+
|
416 |
// Make sure the correct transcript is selected
|
417 |
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
|
418 |
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
|
419 |
}
|
420 |
+
|
421 |
// If before the first event, after the last event, or within the range of the current event, return
|
422 |
if (ttCurrentEvent && (ttCurrentEvent.seconds < ttData.timedEvents[0].seconds || ttCurrentEvent.seconds > ttData.timedEvents[ttData.timedEvents.length-1].seconds)) return;
|
423 |
if (ttCurrentEvent && ttNextEvent && ttCurrentEvent.seconds <= adjustedCurrentTime && ttNextEvent.seconds > adjustedCurrentTime) return;
|
424 |
+
|
425 |
// Clear words that were highlighted from the previous event
|
426 |
clearHighlightedWords(ttCurrentTranscript);
|
427 |
+
|
428 |
// Add highlights for the current event
|
429 |
for (let t = 0; t < ttData.timedEvents.length; t++) {
|
430 |
if (ttData.timedEvents[t].seconds <= adjustedCurrentTime && (!ttData.timedEvents[t+1] || adjustedCurrentTime < ttData.timedEvents[t+1]?.seconds)) {
|
431 |
+
|
432 |
ttPreviousEvent = ttData.timedEvents[t-1] || null;
|
433 |
ttCurrentEvent = ttData.timedEvents[t];
|
434 |
ttNextEvent = ttData.timedEvents[t+1] || null;
|
435 |
+
|
436 |
// Mark blocks
|
437 |
if (ttCurrentEvent.blockIndex != null) {
|
438 |
var blockElements = ttCurrentTranscript.querySelectorAll(`[data-tt-block="${ttCurrentEvent.blockIndex}"]`);
|
439 |
for (let b = 0; b < blockElements.length; b++) blockElements[b].classList.add((b==0 && !blockElements[b].classList.contains('tt-word')) ? 'tt-current-block-container' : 'tt-current-block');
|
440 |
}
|
441 |
+
|
442 |
// Mark phrases
|
443 |
if (ttCurrentEvent.phraseIndex != null) {
|
444 |
var phraseElements = ttCurrentTranscript.querySelectorAll(`[data-tt-phrase="${ttCurrentEvent.phraseIndex}"]`);
|
445 |
for (let p = 0; p < phraseElements.length; p++) phraseElements[p].classList.add((p==0 && !phraseElements[p].classList.contains('tt-word')) ? 'tt-current-phrase-container' : 'tt-current-phrase');
|
446 |
}
|
447 |
+
|
448 |
// Mark words
|
449 |
if (ttCurrentEvent.currentWordIndexes.length > 0) {
|
450 |
for (const wordIndex of ttCurrentEvent.currentWordIndexes) {
|
|
|
456 |
wordElement.classList.add('tt-previous-word');
|
457 |
}
|
458 |
}
|
459 |
+
|
460 |
// Auto-scroll to the highlighted text
|
461 |
if (ttAutoScroll && !ttActivePlayer.paused && ttActivePlayer.duration != 0) {
|
462 |
var scrollOptions = { behavior: 'smooth', block: 'nearest', inline: 'nearest' }
|
|
|
466 |
document.querySelector('.tt-current-phrase-container').scrollIntoView(scrollOptions);
|
467 |
} else if (ttAutoScroll == 'word') {
|
468 |
document.querySelector('.tt-current-word').scrollIntoView(scrollOptions);
|
469 |
+
|
470 |
+
word = document.querySelector('.tt-current-word');
|
471 |
+
|
472 |
+
var current_word = word;
|
473 |
+
var current_index = word.listIndex;
|
474 |
+
var current_rect = word.getBoundingClientRect();
|
475 |
+
var current_bottom = current_rect.bottom;
|
476 |
+
|
477 |
+
var index = current_index;
|
478 |
+
|
479 |
+
var rect = word.getBoundingClientRect();
|
480 |
+
var bottom = rect.bottom;
|
481 |
+
|
482 |
+
while (index < nodes.length) {
|
483 |
+
word = nodes[index];
|
484 |
+
rect = word.getBoundingClientRect();
|
485 |
+
bottom = rect.bottom;
|
486 |
+
if (current_bottom < bottom) break;
|
487 |
+
index = index + 1;
|
488 |
+
}
|
489 |
+
word.scrollIntoView(scrollOptions);
|
490 |
+
|
491 |
+
var end_bottom = nodes[nodes.length - 1].getBoundingClientRect().bottom;
|
492 |
+
|
493 |
+
if (current_bottom == end_bottom) {
|
494 |
+
var objDiv = document.getElementById("transcript");
|
495 |
+
objDiv.scrollTop = objDiv.scrollHeight;
|
496 |
+
|
497 |
+
}
|
498 |
+
|
499 |
}
|
500 |
}
|
501 |
+
|
502 |
break;
|
503 |
}
|
504 |
}
|
|
|
520 |
var wordElement = e.currentTarget;
|
521 |
var wordIndex = wordElement.dataset.ttWord;
|
522 |
|
523 |
+
if (wordElement.className == "tt-whitespace" || wordIndex === undefined) return;
|
524 |
+
|
525 |
var transcript = wordElement.closest('.tt-transcript');
|
526 |
+
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
527 |
var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds
|
528 |
|
529 |
ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds;
|
530 |
|
531 |
var ttData = ttLinkedDataByMediaUrl[mediaUrl];
|
532 |
+
|
533 |
// Make sure the correct transcript is selected
|
534 |
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
|
535 |
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
|
|
|
537 |
|
538 |
// Clear words that were highlighted from the previous event
|
539 |
clearHighlightedWords(ttCurrentTranscript);
|
540 |
+
|
541 |
// Mark words
|
542 |
wordElement.classList.add('tt-current-word');
|
543 |
|
|
|
545 |
if (wordElement.classList.contains('tt-current-word')) break;
|
546 |
wordElement.classList.add('tt-previous-word');
|
547 |
}
|
548 |
+
|
549 |
}
|
550 |
|
551 |
// Handle when a word in the transcript with an event listener is clicked
|
|
|
553 |
var wordElement = e.currentTarget;
|
554 |
var wordIndex = wordElement.dataset.ttWord;
|
555 |
|
556 |
+
if (wordElement.className == "tt-whitespace" || wordIndex === undefined) return;
|
557 |
+
|
558 |
var transcript = wordElement.closest('.tt-transcript');
|
559 |
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
560 |
var startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[wordIndex].startSeconds
|
|
|
562 |
ttLinkedDataByMediaUrl[mediaUrl].mediaElement.currentTime = startSeconds;
|
563 |
|
564 |
var ttData = ttLinkedDataByMediaUrl[mediaUrl];
|
565 |
+
|
566 |
// Make sure the correct transcript is selected
|
567 |
if (!ttCurrentTranscript || ttCurrentTranscript.dataset.ttTranscript != ttData.transcriptIndex) {
|
568 |
ttCurrentTranscript = document.querySelector(`[data-tt-transcript="${ttData.transcriptIndex}"]`);
|
|
|
570 |
|
571 |
// Clear words that were highlighted from the previous event
|
572 |
clearHighlightedWords(ttCurrentTranscript);
|
573 |
+
|
574 |
// Mark words
|
575 |
wordElement.classList.add('tt-current-word');
|
576 |
|
|
|
585 |
}
|
586 |
}
|
587 |
|
|
|
588 |
function handleSelection() {
|
589 |
|
590 |
// Get selection
|
591 |
var selection = document.getSelection();
|
592 |
+
if (selection.type != "Range") return;
|
593 |
|
594 |
// ====================================================================================================
|
595 |
+
|
596 |
// These can be obtained simply with `startWordElement`
|
597 |
var transcript = selection.baseNode.parentNode.closest('.tt-transcript');
|
598 |
var mediaUrl = transcript.dataset.ttCurrentMediaUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
599 |
|
600 |
// ====================================================================================================
|
601 |
+
|
602 |
var startNode = selection.baseNode;
|
603 |
var startWordElement = startNode.parentNode;
|
604 |
+
|
605 |
+
// Used for highlight
|
606 |
var startWordElementForHighlight = startWordElement
|
607 |
+
|
608 |
while (startWordElementForHighlight.className == "tt-whitespace") {
|
609 |
var index = startWordElementForHighlight.listIndex + 1;
|
610 |
if (index >= nodes.length) break;
|
611 |
startWordElementForHighlight = nodes[index];
|
612 |
}
|
613 |
+
|
614 |
// This could be `undefined`!
|
615 |
var startWordIndexForHighlight = startWordElementForHighlight.dataset.ttWord;
|
616 |
// This is always defined.
|
617 |
var startWordListIndexForHighlight = startWordElementForHighlight.listIndex;
|
618 |
+
|
619 |
// Used for computing the start seconds
|
620 |
+
var startWordElementForTime = startWordElementForHighlight;
|
621 |
|
622 |
// No time specified for some words --> we compute the starting time with the first element where the time information is provided.
|
623 |
// (This could go beyond the selected religon!)
|
|
|
637 |
startSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[startWordIndexForTime].startSeconds;
|
638 |
}
|
639 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
640 |
// ====================================================================================================
|
641 |
|
642 |
var endNode = selection.extentNode;
|
643 |
var endWordElement = endNode.parentNode;
|
644 |
+
|
645 |
// Used for highlight
|
646 |
var endWordElementForHighlight = endWordElement;
|
647 |
|
|
|
661 |
|
662 |
// Forward to the next element (if possible)
|
663 |
if ((endWordElementForTime.listIndex + 1) < nodes.length) {
|
664 |
+
endWordElementForTime = nodes[endWordElementForTime.listIndex + 1];
|
665 |
}
|
666 |
+
|
667 |
while (endWordElementForTime.className == "tt-whitespace" || (endWordElementForTime.dataset.ttWord === undefined)) {
|
668 |
var index = endWordElementForTime.listIndex + 1;
|
669 |
if (index >= nodes.length) break;
|
670 |
+
endWordElementForTime = nodes[index];
|
671 |
}
|
672 |
|
673 |
// This could still be `undefined`.
|
|
|
675 |
// Let's get the `listIndex`.
|
676 |
var endWordListIndexForTime = endWordElementForTime.listIndex;
|
677 |
|
678 |
+
var endSeconds = null;
|
679 |
if (endWordIndexForTime !== undefined) {
|
680 |
endSeconds = ttLinkedDataByMediaUrl[mediaUrl].wordTimings[endWordIndexForTime].startSeconds;
|
681 |
if (endWordElement.listIndex == nodes.length - 1) {
|
|
|
683 |
}
|
684 |
}
|
685 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
686 |
return
|
687 |
+
|
688 |
+
// ====================================================================================================
|
689 |
// Mark selection
|
690 |
|
691 |
|