Skip to content

Commit 31d2370

Browse files
authored
Merge pull request #208 from hyperaudio/207-fix-autoscroll
207 fix autoscroll
2 parents 5bad952 + 11f0fa4 commit 31d2370

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

js/hyperaudio-lite.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*! (C) The Hyperaudio Project. MIT @license: en.wikipedia.org/wiki/MIT_License. */
2-
/*! Version 2.3.1 */
2+
/*! Version 2.3.2 */
33

44
'use strict';
55

@@ -305,9 +305,11 @@ class HyperaudioLite {
305305
this.setupTranscriptWords();
306306
this.setupEventListeners(doubleClick, playOnClick);
307307
this.setupInitialPlayHead();
308+
this.setupAutoScroll(autoscroll);
308309
this.minimizedMode = minimizedMode;
309310
this.autoscroll = autoscroll;
310311
this.webMonetization = webMonetization;
312+
this.scrollerContainer = this.transcript;
311313
}
312314

313315
// Setup hash for transcript selection
@@ -389,10 +391,31 @@ class HyperaudioLite {
389391
setupTranscriptWords() {
390392
const words = this.transcript.querySelectorAll('[data-m]');
391393
this.wordArr = this.createWordArray(words);
392-
this.parentTag = words[0].parentElement.tagName;
394+
395+
// check for elements with data-m attributes that are not directly below <section>
396+
// these will contain <p> or similar that we can scroll to
397+
398+
for (const word of words) {
399+
let parentTagName = word.parentElement.tagName;
400+
if (parentTagName !== "SECTION") {
401+
this.parentTag = parentTagName;
402+
break;
403+
}
404+
}
405+
393406
this.parentElements = this.transcript.getElementsByTagName(this.parentTag);
394407
}
395408

409+
setupAutoScroll(autoscroll) {
410+
if (autoscroll) {
411+
if (window.Velocity) {
412+
this.scroller = window.Velocity;
413+
} else if (window.jQuery && window.jQuery.Velocity) {
414+
this.scroller = window.jQuery.Velocity;
415+
}
416+
}
417+
}
418+
396419
// Setup event listeners for interactions
397420
setupEventListeners(doubleClick, playOnClick) {
398421
this.minimizedMode = false;
@@ -403,10 +426,6 @@ class HyperaudioLite {
403426
this.highlightedText = false;
404427
this.start = null;
405428

406-
if (this.autoscroll) {
407-
this.scroller = window.Velocity || window.jQuery.Velocity;
408-
}
409-
410429
const playHeadEvent = doubleClick ? 'dblclick' : 'click';
411430
this.transcript.addEventListener(playHeadEvent, this.setPlayHead.bind(this), false);
412431
this.transcript.addEventListener(playHeadEvent, this.checkPlayHead.bind(this), false);
@@ -418,7 +437,7 @@ class HyperaudioLite {
418437
if (!isNaN(parseFloat(this.start))) {
419438
this.highlightedText = true;
420439
let indices = this.updateTranscriptVisualState(this.start);
421-
if (indices.currentWordIndex > 0) {
440+
if (indices.currentWordIndex > 0 && this.autoscroll) {
422441
this.scrollToParagraph(indices.currentParentElementIndex, indices.currentWordIndex);
423442
}
424443
}
@@ -591,6 +610,7 @@ class HyperaudioLite {
591610

592611
// Scroll to the paragraph containing the current word
593612
scrollToParagraph(currentParentElementIndex, index) {
613+
594614
const scrollNode = this.wordArr[index - 1].n.closest('p') || this.wordArr[index - 1].n;
595615

596616
if (currentParentElementIndex !== this.parentElementIndex) {
@@ -620,7 +640,8 @@ class HyperaudioLite {
620640
} else {
621641
const indices = this.updateTranscriptVisualState(this.currentTime);
622642
const index = indices.currentWordIndex;
623-
if (index > 0) {
643+
644+
if (index > 0 && this.autoscroll) {
624645
this.scrollToParagraph(indices.currentParentElementIndex, index);
625646
}
626647

@@ -739,7 +760,7 @@ class HyperaudioLite {
739760
}
740761
this.wordArr[index - 1].n.parentNode.classList.add('active');
741762
}
742-
763+
743764
const currentParentElementIndex = Array.from(this.parentElements).findIndex(el => el.classList.contains('active'));
744765

745766
return {

0 commit comments

Comments
 (0)