Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions __TEST__/hyperaudio-lite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,43 @@ test("smoothScrollTo jumps directly under prefers-reduced-motion (#259)", () =>
delete window.matchMedia;
});

test("user input cancels an active transcript scroll animation (#266)", () => {
const inst = new HyperaudioLite({
transcript: "hypertranscript",
player: "hyperplayer",
});
const originalRequestAnimationFrame = global.requestAnimationFrame;
const originalCancelAnimationFrame = global.cancelAnimationFrame;
let nextAnimationId = 1;
global.requestAnimationFrame = jest.fn(() => nextAnimationId++);
global.cancelAnimationFrame = jest.fn();

try {
for (const eventName of ["wheel", "touchstart", "keydown"]) {
inst.scrollContainer.scrollTop = 0;
inst.smoothScrollTo(inst.scrollContainer, 500, 800);
const animationId = inst.scrollAnimationId;

inst.scrollContainer.dispatchEvent(new Event(eventName, { bubbles: true }));

expect(global.cancelAnimationFrame).toHaveBeenLastCalledWith(animationId);
expect(inst.scrollAnimationId).toBeNull();
}
} finally {
inst.destroy();
if (originalRequestAnimationFrame === undefined) {
delete global.requestAnimationFrame;
} else {
global.requestAnimationFrame = originalRequestAnimationFrame;
}
if (originalCancelAnimationFrame === undefined) {
delete global.cancelAnimationFrame;
} else {
global.cancelAnimationFrame = originalCancelAnimationFrame;
}
}
});

test("registerPlayer() adds a custom player type (#253)", () => {
class FakePlayer {
constructor(instance) {
Expand Down
18 changes: 14 additions & 4 deletions js/hyperaudio-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ class HyperaudioLite {
} else {
this.scrollContainer = this.transcript;
}

const cancelAutoscroll = () => this.cancelScrollAnimation();
const listenerOpts = { signal: this.listenerController.signal };
['wheel', 'touchstart', 'keydown'].forEach(eventName => {
this.scrollContainer.addEventListener(eventName, cancelAutoscroll, listenerOpts);
});
}

// Setup event listeners for interactions
Expand Down Expand Up @@ -790,6 +796,13 @@ class HyperaudioLite {
this.autoscroll = true;
}

cancelScrollAnimation() {
if (this.scrollAnimationId != null) {
cancelAnimationFrame(this.scrollAnimationId);
this.scrollAnimationId = null;
}
}

// Tear down the instance: stop the polling loop, cancel any in-flight
// scroll animation and remove every DOM listener this instance added
// (transcript, native player, popover). Embed players (YouTube, Vimeo,
Expand All @@ -798,10 +811,7 @@ class HyperaudioLite {
destroy() {
this.destroyed = true;
this.clearTimer();
if (this.scrollAnimationId) {
cancelAnimationFrame(this.scrollAnimationId);
this.scrollAnimationId = null;
}
this.cancelScrollAnimation();
this.listenerController.abort();
}

Expand Down
18 changes: 14 additions & 4 deletions js/hyperaudio-lite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ class HyperaudioLite {
} else {
this.scrollContainer = this.transcript;
}

const cancelAutoscroll = () => this.cancelScrollAnimation();
const listenerOpts = { signal: this.listenerController.signal };
['wheel', 'touchstart', 'keydown'].forEach(eventName => {
this.scrollContainer.addEventListener(eventName, cancelAutoscroll, listenerOpts);
});
}

// Setup event listeners for interactions
Expand Down Expand Up @@ -790,6 +796,13 @@ class HyperaudioLite {
this.autoscroll = true;
}

cancelScrollAnimation() {
if (this.scrollAnimationId != null) {
cancelAnimationFrame(this.scrollAnimationId);
this.scrollAnimationId = null;
}
}

// Tear down the instance: stop the polling loop, cancel any in-flight
// scroll animation and remove every DOM listener this instance added
// (transcript, native player, popover). Embed players (YouTube, Vimeo,
Expand All @@ -798,10 +811,7 @@ class HyperaudioLite {
destroy() {
this.destroyed = true;
this.clearTimer();
if (this.scrollAnimationId) {
cancelAnimationFrame(this.scrollAnimationId);
this.scrollAnimationId = null;
}
this.cancelScrollAnimation();
this.listenerController.abort();
}

Expand Down
Loading