1
0
mirror of https://github.com/hakimel/reveal.js.git synced 2025-01-18 05:48:32 +01:00

329 lines
9.9 KiB
JavaScript
Raw Normal View History

2020-03-14 08:27:29 +01:00
import { SLIDES_SELECTOR } from '../utils/constants.js'
import { queryAll, createStyleSheet } from '../utils/util.js'
2020-03-14 08:27:29 +01:00
/**
* The reader mode lets you read a reveal.js presentation
* as a linear scrollable page.
2020-03-14 08:27:29 +01:00
*/
export default class Reader {
2020-03-14 08:27:29 +01:00
constructor( Reveal ) {
this.Reveal = Reveal;
this.active = false;
this.activatedCallbacks = [];
2020-03-14 08:27:29 +01:00
}
async activate() {
2020-03-14 08:27:29 +01:00
if( this.active ) return;
2020-03-14 08:27:29 +01:00
this.active = true;
2020-03-14 08:27:29 +01:00
this.slideHTMLBeforeActivation = this.Reveal.getSlidesElement().innerHTML;
const viewportElement = this.Reveal.getViewportElement();
const slides = queryAll( this.Reveal.getRevealElement(), SLIDES_SELECTOR );
2020-03-14 08:27:29 +01:00
viewportElement.classList.add( 'loading-scroll-mode', 'reveal-reader' );
viewportElement.addEventListener( 'scroll', this.onScroll.bind( this ) );
2020-03-14 08:27:29 +01:00
let presentationBackground;
if( viewportElement ) {
const viewportStyles = window.getComputedStyle( viewportElement );
if( viewportStyles && viewportStyles.background ) {
presentationBackground = viewportStyles.background;
}
}
const pageElements = [];
2020-11-15 22:08:13 +01:00
const pageContainer = slides[0].parentNode;
2020-03-14 08:27:29 +01:00
// Slide and slide background layout
slides.forEach( function( slide ) {
2020-03-14 08:27:29 +01:00
// Vertical stacks are not centred since their section
// children will be
if( slide.classList.contains( 'stack' ) === false ) {
// Wrap the slide in a page element and hide its overflow
// so that no page ever flows onto another
2020-11-15 22:06:48 +01:00
const page = document.createElement( 'div' );
page.className = 'reader-page';
pageElements.push( page );
// Copy the presentation-wide background to each individual
// page when printing
if( presentationBackground ) {
page.style.background = presentationBackground;
}
const stickyContainer = document.createElement( 'div' );
stickyContainer.className = 'reader-page-sticky';
page.appendChild( stickyContainer );
const contentContainer = document.createElement( 'div' );
contentContainer.className = 'reader-page-content';
stickyContainer.appendChild( contentContainer );
contentContainer.appendChild( slide );
2020-03-14 08:27:29 +01:00
slide.classList.remove( 'past', 'future' );
2020-03-14 08:27:29 +01:00
if( slide.slideBackgroundElement ) {
slide.slideBackgroundElement.remove( 'past', 'future' );
contentContainer.insertBefore( slide.slideBackgroundElement, slide );
2020-03-14 08:27:29 +01:00
}
}
2020-03-14 08:27:29 +01:00
}, this );
2020-03-14 08:27:29 +01:00
// Remove leftover stacks
queryAll( this.Reveal.getRevealElement(), '.stack' ).forEach( stack => stack.remove() );
2020-03-14 08:27:29 +01:00
await new Promise( requestAnimationFrame );
2020-03-14 08:27:29 +01:00
pageElements.forEach( page => pageContainer.appendChild( page ) );
2020-03-14 08:27:29 +01:00
// Re-run JS-based content layout after the slide is added to page DOM
this.Reveal.slideContent.layout( this.Reveal.getSlidesElement() );
2020-03-14 08:27:29 +01:00
this.Reveal.layout();
2020-03-14 08:27:29 +01:00
viewportElement.classList.remove( 'loading-scroll-mode' );
this.activatedCallbacks.forEach( callback => callback() );
this.activatedCallbacks = [];
}
2020-03-14 08:27:29 +01:00
deactivate() {
if( !this.active ) return;
this.active = false;
this.Reveal.getViewportElement().classList.remove( 'reveal-reader' );
this.Reveal.getSlidesElement().innerHTML = this.slideHTMLBeforeActivation;
this.Reveal.sync();
// TODO Navigate to the slide that is currently scrolled into view
this.Reveal.slide( 0 );
}
toggle() {
if( this.active === true ) {
this.deactivate();
}
else {
this.activate();
}
}
/**
* Checks if the reader mode is currently active.
*/
isActive() {
2020-03-14 08:27:29 +01:00
return this.active;
2020-03-14 08:27:29 +01:00
}
2020-03-14 08:27:29 +01:00
/**
* Updates our reader pages to match the latest configuration and
* presentation size.
*/
sync() {
const config = this.Reveal.getConfig();
const slideSize = this.Reveal.getComputedSlideSize( window.innerWidth, window.innerHeight );
const scale = this.Reveal.getScale();
const readerLayout = config.readerLayout;
const viewportElement = this.Reveal.getViewportElement();
const viewportHeight = viewportElement.offsetHeight;
const compactHeight = slideSize.height * scale;
const pageHeight = readerLayout === 'full' ? viewportHeight : compactHeight;
// The height that needs to be scrolled between scroll triggers
const scrollTriggerHeight = viewportHeight / 2;
2020-03-14 08:27:29 +01:00
viewportElement.style.setProperty( '--page-height', pageHeight + 'px' );
viewportElement.style.scrollSnapType = typeof config.readerScrollSnap === 'string' ?
`y ${config.readerScrollSnap}` : '';
2020-03-14 08:27:29 +01:00
const pageElements = Array.from( this.Reveal.getRevealElement().querySelectorAll( '.reader-page' ) );
this.pages = pageElements.map( pageElement => {
const page = {
pageElement: pageElement,
stickyElement: pageElement.querySelector( '.reader-page-sticky' ),
slideElement: pageElement.querySelector( 'section' ),
backgroundElement: pageElement.querySelector( '.slide-background' ),
top: pageElement.offsetTop,
scrollTriggers: []
};
2020-03-14 08:27:29 +01:00
page.slideElement.style.width = slideSize.width + 'px';
page.slideElement.style.height = config.center === true ? '' : slideSize.height + 'px';
// Each fragment 'group' is an array containing one or more
// fragments. Multiple fragments that appear at the same time
// are part of the same group.
page.fragments = this.Reveal.fragments.sort( pageElement.querySelectorAll( '.fragment:not(.disabled)' ) );
page.fragmentGroups = this.Reveal.fragments.sort( pageElement.querySelectorAll( '.fragment' ), true );
2020-03-14 08:27:29 +01:00
// Create scroll triggers that show/hide fragments
if( page.fragmentGroups.length ) {
const segmentSize = 1 / ( page.fragmentGroups.length + 1 );
page.scrollTriggers.push(
// Trigger for the initial state with no fragments visible
{ range: [ 0, segmentSize ], fragmentIndex: -1 },
2020-11-15 22:08:13 +01:00
// Triggers for each fragment group
...page.fragmentGroups.map( ( fragments, i ) => ({
range: [ segmentSize * ( i + 1 ), segmentSize * ( i + 2 ) ],
fragmentIndex: i
}))
);
}
// Add scroll padding based on how many scroll triggers we have
page.scrollPadding = scrollTriggerHeight * page.scrollTriggers.length;
// In the compact layout, only slides with scroll triggers cover the
// full viewport height. This helps avoid empty gaps before or after
// a sticky slide.
if( readerLayout === 'compact' && page.scrollTriggers.length > 0 ) {
page.pageHeight = viewportHeight;
page.pageElement.style.setProperty( '--page-height', viewportHeight + 'px' );
}
else {
page.pageHeight = pageHeight;
page.pageElement.style.removeProperty( '--page-height' );
}
page.pageElement.style.scrollSnapAlign = page.pageHeight < viewportHeight ? 'center' : 'start';
// This variable is used to pad the height of our page in CSS
page.pageElement.style.setProperty( '--page-scroll-padding', page.scrollPadding + 'px' );
// The total height including scrollable space
page.totalHeight = page.pageHeight + page.scrollPadding;
page.bottom = page.top + page.totalHeight;
// If this is a sticky page, stick it to the vertical center
if( page.scrollTriggers.length > 0 ) {
page.stickyElement.style.position = 'sticky';
page.stickyElement.style.top = Math.max( ( viewportHeight - page.pageHeight ) / 2, 0 ) + 'px';
// Make this page freeze at the vertical center of the viewport
page.top -= ( viewportHeight - page.pageHeight ) / 2;
}
else {
page.stickyElement.style.position = 'relative';
}
return page;
} );
2020-03-14 08:27:29 +01:00
}
layout() {
this.sync();
this.onScroll();
}
scrollToSlide( slideElement ) {
if( !this.active ) {
this.activatedCallbacks.push( () => this.scrollToSlide( slideElement ) );
}
else {
slideElement.parentNode.scrollIntoView();
}
}
onScroll() {
const viewportElement = this.Reveal.getViewportElement();
2023-09-14 15:17:07 +02:00
const viewportHeight = viewportElement.offsetHeight;
const scrollTop = viewportElement.scrollTop;
this.pages.forEach( ( page, pageIndex ) => {
2023-09-14 15:17:07 +02:00
const isWithinPreloadRange = scrollTop + viewportHeight >= page.top - viewportHeight && scrollTop < page.top + page.bottom + viewportHeight;
const isPartiallyVisible = scrollTop + viewportHeight >= page.top && scrollTop < page.top + page.bottom;
const isMostlyVisible = scrollTop + viewportHeight >= page.top + viewportHeight / 2 && scrollTop < page.top + page.bottom - viewportHeight / 2;
2023-09-14 15:17:07 +02:00
// Preload content when it appears within range
if( isWithinPreloadRange ) {
if( !page.preloaded ) {
page.preloaded = true;
this.Reveal.slideContent.load( page.slideElement );
2023-09-14 15:17:07 +02:00
}
}
else if( page.preloaded ) {
page.preloaded = false;
this.Reveal.slideContent.unload( page.slideElement );
}
// Play slide content when the slide becomes visible
if( isMostlyVisible ) {
2023-09-22 10:31:34 +02:00
if( !page.active ) {
page.active = true;
page.pageElement.classList.add( 'present' );
page.slideElement.classList.add( 'present' );
this.Reveal.setCurrentReaderPage( pageIndex, page.pageElement );
this.Reveal.slideContent.startEmbeddedContent( page.slideElement );
if( page.backgroundElement ) {
this.Reveal.slideContent.startEmbeddedContent( page.backgroundElement );
}
}
2023-09-14 15:17:07 +02:00
}
2023-09-22 10:31:34 +02:00
else if( page.active ) {
page.active = false;
page.pageElement.classList.remove( 'present' );
page.slideElement.classList.remove( 'present' );
2023-09-14 15:17:07 +02:00
this.Reveal.slideContent.stopEmbeddedContent( page.slideElement );
if( page.backgroundElement ) {
this.Reveal.slideContent.stopEmbeddedContent( page.backgroundElement );
}
2023-09-14 15:17:07 +02:00
}
2023-09-14 15:17:07 +02:00
// Handle scroll freezing and triggers for slides in view
if( isPartiallyVisible && page.totalHeight > page.pageHeight ) {
let scrollProgress = ( scrollTop - page.top ) / page.scrollPadding;
2023-09-14 15:17:07 +02:00
scrollProgress = Math.max( Math.min( scrollProgress, 1 ), 0 );
2023-09-14 15:17:07 +02:00
page.scrollTriggers.forEach( trigger => {
if( scrollProgress >= trigger.range[0] && scrollProgress < trigger.range[1] ) {
if( !trigger.active ) {
trigger.active = true;
this.Reveal.fragments.update( trigger.fragmentIndex, page.fragments, page.slideElement );
}
2023-09-14 15:17:07 +02:00
}
else {
trigger.active = false;
}
} );
}
} );
2020-03-14 08:27:29 +01:00
}
2020-11-15 22:08:13 +01:00
}