Skip to content

Commit

Permalink
feat: create basic methods for previewScrubbing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bzizmo committed Oct 22, 2024
1 parent d2b9d5c commit b1f76c3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
81 changes: 79 additions & 2 deletions src/js/control-bar/progress-control/seek-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class SeekBar extends Slider {
constructor(player, options) {
super(player, options);
this.setEventHandlers_();

// Initialize the previewScrubbing option
this.previewScrubbing = this.player_.options_.previewScrubbing || false;
// Initialize properties for deferred seeking
this.desiredSeekPosition = null;
}

/**
Expand Down Expand Up @@ -392,14 +397,83 @@ class SeekBar extends Slider {
* Move more quickly fast forward for keyboard-only users
*/
stepForward() {
this.userSeek_(this.player_.currentTime() + STEP_SECONDS);
if (this.previewScrubbing) {
// Use desiredSeekPosition if set, else start from currentTime
const currentPos = this.desiredSeekPosition !== null ?
this.desiredSeekPosition :
this.player_.currentTime();

const newTime = currentPos + STEP_SECONDS;

this.updateDesiredPosition(newTime);
} else {
// Immediate seek when previewScrubbing is false
this.userSeek_(this.player_.currentTime() + STEP_SECONDS);
}
}

/**
* Move more quickly rewind for keyboard-only users
*/
stepBack() {
this.userSeek_(this.player_.currentTime() - STEP_SECONDS);
if (this.previewScrubbing) {
// Use desiredSeekPosition if set, else start from currentTime
const currentPos = this.desiredSeekPosition !== null ?
this.desiredSeekPosition :
this.player_.currentTime();

const newTime = currentPos - STEP_SECONDS;

this.updateDesiredPosition(newTime);
} else {
// Immediate seek when previewScrubbing is false
this.userSeek_(this.player_.currentTime() - STEP_SECONDS);
}
}

/**
* Update the desired seek position and refresh the UI
*
* @param {number} newTime - The desired time to seek to.
*/
updateDesiredPosition(newTime) {
if (!this.player_.paused()) {
this.player_.pause();
}
const duration = this.player_.duration();
// Clamp newTime between 0 and duration

newTime = Math.max(0, Math.min(newTime, duration));

this.desiredSeekPosition = newTime;
// Update the UI
this.updateProgressBarWithDesiredPosition(newTime);
}

updateProgressBarWithDesiredPosition(time) {
const duration = this.player_.duration();
const percent = duration ? (time / duration) : 0;

// Update the bar's width
if (this.bar) {
this.bar.el().style.width = (percent * 100).toFixed(2) + '%';
}

// Update ARIA attributes for accessibility
this.el_.setAttribute('aria-valuenow', (percent * 100).toFixed(2));
this.el_.setAttribute('aria-valuetext', `${Math.floor(this.desiredSeekPosition)} seconds`);
}

confirmSeek() {
this.userSeek_(this.desiredSeekPosition);
this.desiredSeekPosition = null;
}

cancelSeek() {
this.desiredSeekPosition = null;
const currentTime = this.player_.currentTime();

this.updateProgressBarWithDesiredPosition(currentTime);
}

/**
Expand All @@ -411,6 +485,9 @@ class SeekBar extends Slider {
*
*/
handleAction(event) {
if (this.desiredSeekPosition !== null) {
this.confirmSeek();
}
if (this.player_.paused()) {
this.player_.play();
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5567,7 +5567,9 @@ Player.prototype.options_ = {
horizontalSeek: false
},
// Default smooth seeking to false
enableSmoothSeeking: false
enableSmoothSeeking: false,
// Default preview scrubbing to false
previewScrubbing: true
};

TECH_EVENTS_RETRIGGER.forEach(function(event) {
Expand Down
3 changes: 3 additions & 0 deletions src/js/slider/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ class Slider extends Component {
event.stopPropagation();
this.stepForward();
} else {
if (this.desiredSeekPosition) {
this.cancelSeek();
}
super.handleKeyDown(event);
}

Expand Down

0 comments on commit b1f76c3

Please sign in to comment.