Skip to content

Commit

Permalink
fix previous bg video playing in background
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimel committed Mar 12, 2024
1 parent 62b1ea3 commit 421da63
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dist/reveal.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/reveal.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/reveal.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/reveal.js.map

Large diffs are not rendered by default.

69 changes: 36 additions & 33 deletions js/controllers/backgrounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,47 @@ export default class Backgrounds {

} );

// The previous background may refer to a DOM element that has
// been removed after a presentation is synced & bgs are recreated
if( this.previousBackground && !this.previousBackground.closest( 'body' ) ) {
this.previousBackground = null;
}

if( currentBackground && this.previousBackground ) {

// Don't transition between identical backgrounds. This
// prevents unwanted flicker.
let previousBackgroundHash = this.previousBackground.getAttribute( 'data-background-hash' );
let currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );

if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== this.previousBackground ) {
this.element.classList.add( 'no-transition' );

// If multiple slides have the same background video, carry
// the <video> element forward so that it plays continuously
// across multiple slides
const currentVideo = currentBackground.querySelector( 'video' );
const previousVideo = this.previousBackground.querySelector( 'video' );

if( currentVideo && previousVideo ) {

const currentVideoParent = currentVideo.parentNode;
const previousVideoParent = previousVideo.parentNode;

// Swap the two videos
previousVideoParent.appendChild( currentVideo );
currentVideoParent.appendChild( previousVideo );

}
}

}

// Stop content inside of previous backgrounds
if( this.previousBackground ) {

this.Reveal.slideContent.stopEmbeddedContent( this.previousBackground, { unloadIframes: !this.Reveal.slideContent.shouldPreload( this.previousBackground ) } );

// Clear the previous background if it was removed from DOM
if( !this.previousBackground.closest( 'body' ) ) {
this.previousBackground = null;
}

}

// Start content in the current background
Expand All @@ -353,34 +384,6 @@ export default class Backgrounds {

}

// Don't transition between identical backgrounds. This
// prevents unwanted flicker.
let previousBackgroundHash = this.previousBackground ? this.previousBackground.getAttribute( 'data-background-hash' ) : null;
let currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== this.previousBackground ) {
this.element.classList.add( 'no-transition' );

// If multiple slides have the same background video, we carry
// the <video> element forward so that it doesn't restart
const currentVideo = currentBackground.querySelector( 'video' );
if( currentVideo && this.previousBackground ) {
const previousVideo = this.previousBackground.querySelector( 'video' );

if( previousVideo ) {
const currentVideoParent = currentVideo.parentNode;
const previousVideoParent = previousVideo.parentNode;

// Swap the two videos
previousVideoParent.appendChild( currentVideo );
currentVideoParent.appendChild( previousVideo );

if( config.autoPlayMedia !== false ) {
previousVideo.play();
}
}
}
}

this.previousBackground = currentBackground;

}
Expand Down

0 comments on commit 421da63

Please sign in to comment.