2023-10-10 14:47:08 +02:00
|
|
|
import { HORIZONTAL_SLIDES_SELECTOR } from '../utils/constants.js'
|
|
|
|
import { queryAll } from '../utils/util.js'
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 13:34:33 +02:00
|
|
|
const HIDE_SCROLLBAR_TIMEOUT = 500;
|
2023-10-10 14:47:08 +02:00
|
|
|
const PROGRESS_SPACING = 4;
|
|
|
|
const MIN_PROGRESS_SEGMENT_HEIGHT = 6;
|
|
|
|
const MIN_PLAYHEAD_HEIGHT = 18;
|
2023-10-10 13:34:33 +02:00
|
|
|
|
2020-03-14 08:27:29 +01:00
|
|
|
/**
|
2023-09-14 13:00:31 +02:00
|
|
|
* The reader mode lets you read a reveal.js presentation
|
|
|
|
* as a linear scrollable page.
|
2020-03-14 08:27:29 +01:00
|
|
|
*/
|
2023-09-12 17:00:56 +02:00
|
|
|
export default class Reader {
|
2020-03-14 08:27:29 +01:00
|
|
|
|
|
|
|
constructor( Reveal ) {
|
|
|
|
|
|
|
|
this.Reveal = Reveal;
|
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
this.active = false;
|
2023-09-14 15:03:23 +02:00
|
|
|
this.activatedCallbacks = [];
|
|
|
|
|
2023-10-06 09:14:23 +02:00
|
|
|
this.onScroll = this.onScroll.bind( this );
|
|
|
|
|
2020-03-14 08:27:29 +01:00
|
|
|
}
|
|
|
|
|
2023-10-05 14:06:06 +02:00
|
|
|
/**
|
|
|
|
* Activates the reader mode. This rearranges the presentation DOM
|
|
|
|
* by—among other things—wrapping each slide in a page element.
|
|
|
|
*/
|
2023-10-06 09:52:21 +02:00
|
|
|
activate() {
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
if( this.active ) return;
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
const state = this.Reveal.getState();
|
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
this.active = true;
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
this.slideHTMLBeforeActivation = this.Reveal.getSlidesElement().innerHTML;
|
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
const horizontalSlides = queryAll( this.Reveal.getRevealElement(), HORIZONTAL_SLIDES_SELECTOR );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
this.viewportElement.classList.add( 'loading-scroll-mode', 'reveal-reader' );
|
2023-10-10 14:47:08 +02:00
|
|
|
this.viewportElement.addEventListener( 'scroll', this.onScroll, { passive: true } );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2021-11-16 14:10:29 +01:00
|
|
|
let presentationBackground;
|
2023-10-06 10:07:19 +02:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
const viewportStyles = window.getComputedStyle( this.viewportElement );
|
2023-10-06 10:07:19 +02:00
|
|
|
if( viewportStyles && viewportStyles.background ) {
|
|
|
|
presentationBackground = viewportStyles.background;
|
2021-11-16 14:10:29 +01:00
|
|
|
}
|
|
|
|
|
2023-09-20 15:00:15 +02:00
|
|
|
const pageElements = [];
|
2023-10-06 11:37:58 +02:00
|
|
|
const pageContainer = horizontalSlides[0].parentNode;
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
let previousSlide;
|
2021-11-16 14:10:29 +01:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
const createPage = ( slide, h, v ) => {
|
2023-09-20 15:00:15 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
let contentContainer;
|
|
|
|
|
|
|
|
if( previousSlide && this.Reveal.shouldAutoAnimateBetween( previousSlide, slide ) ) {
|
|
|
|
contentContainer = document.createElement( 'div' );
|
|
|
|
contentContainer.className = 'reader-page-content reader-auto-animate-page';
|
|
|
|
previousSlide.closest( '.reader-page-content' ).parentNode.appendChild( contentContainer );
|
2023-10-06 11:37:58 +02:00
|
|
|
}
|
2023-10-20 10:18:34 +02:00
|
|
|
else {
|
|
|
|
// Wrap the slide in a page element and hide its overflow
|
|
|
|
// so that no page ever flows onto another
|
|
|
|
const page = document.createElement( 'div' );
|
|
|
|
page.className = 'reader-page';
|
|
|
|
pageElements.push( page );
|
|
|
|
|
|
|
|
// Copy the presentation-wide background to each page
|
|
|
|
if( presentationBackground ) {
|
|
|
|
page.style.background = presentationBackground;
|
|
|
|
}
|
2023-09-20 15:00:15 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
const stickyContainer = document.createElement( 'div' );
|
|
|
|
stickyContainer.className = 'reader-page-sticky';
|
|
|
|
page.appendChild( stickyContainer );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
contentContainer = document.createElement( 'div' );
|
|
|
|
contentContainer.className = 'reader-page-content';
|
|
|
|
stickyContainer.appendChild( contentContainer );
|
|
|
|
}
|
2023-09-25 12:32:46 +02:00
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
contentContainer.appendChild( slide );
|
|
|
|
|
|
|
|
slide.classList.remove( 'past', 'future' );
|
|
|
|
|
|
|
|
if( typeof h === 'number' ) slide.setAttribute( 'data-index-h', h );
|
|
|
|
if( typeof v === 'number' ) slide.setAttribute( 'data-index-v', v );
|
|
|
|
|
|
|
|
if( slide.slideBackgroundElement ) {
|
|
|
|
slide.slideBackgroundElement.remove( 'past', 'future' );
|
|
|
|
contentContainer.insertBefore( slide.slideBackgroundElement, slide );
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
previousSlide = slide;
|
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
}
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
// Slide and slide background layout
|
|
|
|
horizontalSlides.forEach( ( horizontalSlide, h ) => {
|
|
|
|
|
|
|
|
if( this.Reveal.isVerticalStack( horizontalSlide ) ) {
|
|
|
|
horizontalSlide.querySelectorAll( 'section' ).forEach( ( verticalSlide, v ) => {
|
|
|
|
createPage( verticalSlide, h, v );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
createPage( horizontalSlide, h, 0 );
|
2023-09-14 13:00:31 +02:00
|
|
|
}
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
}, this );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
this.createProgressBar();
|
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
// Remove leftover stacks
|
|
|
|
queryAll( this.Reveal.getRevealElement(), '.stack' ).forEach( stack => stack.remove() );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-20 15:00:15 +02:00
|
|
|
pageElements.forEach( page => pageContainer.appendChild( page ) );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-14 13:00:31 +02: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
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
this.Reveal.layout();
|
2023-10-06 11:37:58 +02:00
|
|
|
this.Reveal.setState( state );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
this.viewportElement.classList.remove( 'loading-scroll-mode' );
|
2023-09-19 11:52:54 +02:00
|
|
|
|
2023-09-14 15:03:23 +02:00
|
|
|
this.activatedCallbacks.forEach( callback => callback() );
|
|
|
|
this.activatedCallbacks = [];
|
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
}
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-05 14:06:06 +02:00
|
|
|
/**
|
|
|
|
* Deactivates the reader mode and restores the standard slide-based
|
|
|
|
* presentation.
|
|
|
|
*/
|
2023-09-19 11:52:54 +02:00
|
|
|
deactivate() {
|
|
|
|
|
|
|
|
if( !this.active ) return;
|
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
const state = this.Reveal.getState();
|
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
this.active = false;
|
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
this.viewportElement.removeEventListener( 'scroll', this.onScroll );
|
|
|
|
this.viewportElement.classList.remove( 'reveal-reader' );
|
2023-10-06 09:14:23 +02:00
|
|
|
|
2023-10-10 13:34:33 +02:00
|
|
|
this.removeProgressBar();
|
2023-10-06 09:14:23 +02:00
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
this.Reveal.getSlidesElement().innerHTML = this.slideHTMLBeforeActivation;
|
|
|
|
this.Reveal.sync();
|
2023-10-06 11:37:58 +02:00
|
|
|
this.Reveal.setState( state );
|
2023-09-19 11:52:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-06 09:52:21 +02:00
|
|
|
toggle( override ) {
|
2023-09-19 11:52:54 +02:00
|
|
|
|
2023-10-06 09:52:21 +02:00
|
|
|
if( typeof override === 'boolean' ) {
|
|
|
|
override ? this.activate() : this.deactivate();
|
2023-09-19 11:52:54 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-10-06 09:52:21 +02:00
|
|
|
this.isActive() ? this.deactivate() : this.activate();
|
2023-09-19 11:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
/**
|
2023-09-19 11:52:54 +02:00
|
|
|
* Checks if the reader mode is currently active.
|
2023-09-14 13:00:31 +02:00
|
|
|
*/
|
|
|
|
isActive() {
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
return this.active;
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
}
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a slide by its original h/v index (i.e. the indices the
|
|
|
|
* slide had before being linearized).
|
|
|
|
*
|
|
|
|
* @param {number} h
|
|
|
|
* @param {number} v
|
|
|
|
* @returns {HTMLElement}
|
|
|
|
*/
|
2023-10-06 11:37:58 +02:00
|
|
|
getSlideByIndices( h, v ) {
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
const page = this.getAllPages().find( page => {
|
|
|
|
return page.indexh === h && page.indexv === v;
|
|
|
|
} );
|
2023-10-06 11:37:58 +02:00
|
|
|
|
|
|
|
return page ? page.slideElement : null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
getScrollTriggerBySlide( slide ) {
|
|
|
|
|
|
|
|
return this.slideTriggers.find( trigger => trigger.page.slideElement === slide );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllPages() {
|
|
|
|
|
|
|
|
return this.pages.flatMap( page => [page, ...(page.autoAnimatePages || [])] );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
/**
|
|
|
|
* Renders the progress bar component.
|
|
|
|
*/
|
|
|
|
createProgressBar() {
|
|
|
|
|
|
|
|
this.progressBar = document.createElement( 'div' );
|
|
|
|
this.progressBar.className = 'reader-progress';
|
|
|
|
|
|
|
|
this.progressBarInner = document.createElement( 'div' );
|
|
|
|
this.progressBarInner.className = 'reader-progress-inner';
|
|
|
|
this.progressBar.appendChild( this.progressBarInner );
|
|
|
|
|
|
|
|
this.progressBarPlayhead = document.createElement( 'div' );
|
|
|
|
this.progressBarPlayhead.className = 'reader-progress-playhead';
|
|
|
|
this.progressBarInner.appendChild( this.progressBarPlayhead );
|
|
|
|
|
|
|
|
this.viewportElement.insertBefore( this.progressBar, this.viewportElement.firstChild );
|
|
|
|
|
|
|
|
const handleDocumentMouseMove = ( event ) => {
|
|
|
|
|
|
|
|
let progress = ( event.clientY - this.progressBarInner.getBoundingClientRect().top ) / this.progressBarHeight;
|
|
|
|
|
|
|
|
progress = Math.max( Math.min( progress, 1 ), 0 );
|
|
|
|
|
|
|
|
this.viewportElement.scrollTop = progress * ( this.viewportElement.scrollHeight - this.viewportElement.offsetHeight );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDocumentMouseUp = ( event ) => {
|
|
|
|
|
|
|
|
this.draggingProgressBar = false;
|
|
|
|
this.showProgressBar();
|
|
|
|
|
|
|
|
document.removeEventListener( 'mousemove', handleDocumentMouseMove );
|
|
|
|
document.removeEventListener( 'mouseup', handleDocumentMouseUp );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-10-11 10:42:47 +02:00
|
|
|
const handleMouseDown = ( event ) => {
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.draggingProgressBar = true;
|
|
|
|
|
|
|
|
document.addEventListener( 'mousemove', handleDocumentMouseMove );
|
|
|
|
document.addEventListener( 'mouseup', handleDocumentMouseUp );
|
|
|
|
|
|
|
|
handleDocumentMouseMove( event );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
this.progressBarInner.addEventListener( 'mousedown', handleMouseDown );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
removeProgressBar() {
|
|
|
|
|
|
|
|
if( this.progressBar ) {
|
|
|
|
this.progressBar.remove();
|
|
|
|
this.progressBar = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
layout() {
|
|
|
|
|
|
|
|
if( this.isActive() ) {
|
|
|
|
this.syncPages();
|
|
|
|
this.onScroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-21 13:35:49 +02:00
|
|
|
/**
|
|
|
|
* Updates our reader pages to match the latest configuration and
|
|
|
|
* presentation size.
|
|
|
|
*/
|
2023-10-10 14:47:08 +02:00
|
|
|
syncPages() {
|
2022-04-04 15:33:44 +02:00
|
|
|
|
2023-09-21 13:35:49 +02:00
|
|
|
const config = this.Reveal.getConfig();
|
2023-09-14 15:03:23 +02:00
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
const slideSize = this.Reveal.getComputedSlideSize( window.innerWidth, window.innerHeight );
|
|
|
|
const scale = this.Reveal.getScale();
|
2023-09-21 13:35:49 +02:00
|
|
|
const readerLayout = config.readerLayout;
|
2023-09-20 15:00:15 +02:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
const viewportHeight = this.viewportElement.offsetHeight;
|
2023-09-21 13:35:49 +02:00
|
|
|
const compactHeight = slideSize.height * scale;
|
|
|
|
const pageHeight = readerLayout === 'full' ? viewportHeight : compactHeight;
|
2022-04-04 15:33:44 +02:00
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
// The height that needs to be scrolled between scroll triggers
|
2023-10-18 11:16:06 +02:00
|
|
|
const scrollTriggerHeight = viewportHeight;
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
this.viewportElement.style.setProperty( '--page-height', pageHeight + 'px' );
|
|
|
|
this.viewportElement.style.scrollSnapType = typeof config.readerScrollSnap === 'string' ?
|
2023-09-21 13:35:49 +02:00
|
|
|
`y ${config.readerScrollSnap}` : '';
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
// This will hold all scroll triggers used to show/hide slides
|
|
|
|
this.slideTriggers = [];
|
|
|
|
|
2023-09-20 15:00:15 +02:00
|
|
|
const pageElements = Array.from( this.Reveal.getRevealElement().querySelectorAll( '.reader-page' ) );
|
2023-09-14 15:03:23 +02:00
|
|
|
|
2023-09-20 15:00:15 +02:00
|
|
|
this.pages = pageElements.map( pageElement => {
|
2023-09-14 13:00:31 +02:00
|
|
|
const page = {
|
|
|
|
pageElement: pageElement,
|
|
|
|
slideElement: pageElement.querySelector( 'section' ),
|
2023-10-20 10:18:34 +02:00
|
|
|
stickyElement: pageElement.querySelector( '.reader-page-sticky' ),
|
|
|
|
contentElement: pageElement.querySelector( '.reader-page-content' ),
|
2023-09-20 15:00:15 +02:00
|
|
|
backgroundElement: pageElement.querySelector( '.slide-background' ),
|
2023-10-20 10:18:34 +02:00
|
|
|
autoAnimateElements: pageElement.querySelectorAll( '.reader-auto-animate-page' ),
|
|
|
|
|
2023-09-20 15:00:15 +02:00
|
|
|
top: pageElement.offsetTop,
|
2023-10-20 10:18:34 +02:00
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
scrollTriggers: [],
|
2023-10-20 10:18:34 +02:00
|
|
|
scrollTriggerHeight,
|
|
|
|
scrollTriggerCount: 0
|
2023-09-14 13:00:31 +02:00
|
|
|
};
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
let additionalScrollTriggerCount = 0;
|
|
|
|
|
2023-10-06 11:37:58 +02:00
|
|
|
page.indexh = parseInt( page.slideElement.getAttribute( 'data-index-h' ), 10 );
|
|
|
|
page.indexv = parseInt( page.slideElement.getAttribute( 'data-index-v' ), 10 );
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
page.pageElement.style.setProperty( '--slide-height', config.center === true ? 'auto' : slideSize.height + 'px' );
|
2023-09-21 13:35:49 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
this.slideTriggers.push({
|
|
|
|
page: page,
|
|
|
|
activate: () => this.activatePage( page ),
|
|
|
|
deactivate: () => this.deactivatePage( page )
|
|
|
|
});
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
// Create scroll triggers that show/hide fragments
|
2023-10-20 10:18:34 +02:00
|
|
|
page.scrollTriggerCount += this.createFragmentTriggersForPage( page );
|
|
|
|
|
|
|
|
if( page.autoAnimateElements.length > 0 ) {
|
|
|
|
// Create scroll triggers for triggering auto-animate steps
|
|
|
|
page.autoAnimatePages = this.createAutoAnimateTriggersForPage( page );
|
|
|
|
|
|
|
|
additionalScrollTriggerCount = page.autoAnimatePages.reduce( ( total, page ) => {
|
|
|
|
return total + Math.max( page.scrollTriggerCount - 1, 0 );
|
|
|
|
}, page.autoAnimateElements.length );
|
|
|
|
}
|
|
|
|
|
|
|
|
const totalScrollTriggerCount = Math.max( page.scrollTriggerCount - 1, 0 ) + additionalScrollTriggerCount;
|
|
|
|
|
|
|
|
// Clean up from previous renders
|
|
|
|
page.pageElement.querySelectorAll( '.reader-snap-point' ).forEach( el => el.remove() );
|
|
|
|
|
|
|
|
// Create snap points for all scroll triggers
|
|
|
|
// - Can't be absolute in FF
|
|
|
|
// - Can't be 0-height in Safari
|
|
|
|
for( let i = 0; i < totalScrollTriggerCount + 1; i++ ) {
|
|
|
|
const triggerStick = document.createElement( 'div' );
|
|
|
|
triggerStick.className = 'reader-snap-point';
|
|
|
|
triggerStick.style.height = scrollTriggerHeight + 'px';
|
|
|
|
page.pageElement.appendChild( triggerStick );
|
|
|
|
|
|
|
|
if( i === 0 ) {
|
|
|
|
triggerStick.style.marginTop = -scrollTriggerHeight + 'px';
|
|
|
|
}
|
2023-09-21 13:35:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add scroll padding based on how many scroll triggers we have
|
2023-10-20 10:18:34 +02:00
|
|
|
page.scrollPadding = scrollTriggerHeight * totalScrollTriggerCount;
|
2023-09-21 13:35:49 +02:00
|
|
|
|
|
|
|
// 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.
|
2023-10-20 10:18:34 +02:00
|
|
|
if( readerLayout === 'compact' && page.scrollTriggerCount > 0 ) {
|
2023-09-21 13:35:49 +02:00
|
|
|
page.pageHeight = viewportHeight;
|
|
|
|
page.pageElement.style.setProperty( '--page-height', viewportHeight + 'px' );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
page.pageHeight = pageHeight;
|
|
|
|
page.pageElement.style.removeProperty( '--page-height' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2023-10-20 10:18:34 +02:00
|
|
|
if( totalScrollTriggerCount > 0 ) {
|
2023-09-21 13:35:49 +02:00
|
|
|
page.stickyElement.style.position = 'sticky';
|
|
|
|
page.stickyElement.style.top = Math.max( ( viewportHeight - page.pageHeight ) / 2, 0 ) + 'px';
|
2023-09-14 15:03:23 +02:00
|
|
|
|
|
|
|
// Make this page freeze at the vertical center of the viewport
|
|
|
|
page.top -= ( viewportHeight - page.pageHeight ) / 2;
|
2023-09-14 13:00:31 +02:00
|
|
|
}
|
2023-09-21 13:35:49 +02:00
|
|
|
else {
|
|
|
|
page.stickyElement.style.position = 'relative';
|
2023-10-20 10:18:34 +02:00
|
|
|
page.pageElement.style.scrollSnapAlign = page.pageHeight < viewportHeight ? 'center' : 'start';
|
2023-09-21 13:35:49 +02:00
|
|
|
}
|
2022-05-31 11:50:26 +02:00
|
|
|
|
2023-09-14 13:00:31 +02:00
|
|
|
return page;
|
|
|
|
} );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
this.progressBarSegmentCount = this.slideTriggers.reduce( ( total, trigger ) => total + Math.max( trigger.page.scrollTriggerCount, 1 ), 0 );
|
|
|
|
let rangeStart = 0;
|
|
|
|
|
|
|
|
this.slideTriggers.forEach( ( trigger, i ) => {
|
|
|
|
const rangeEnd = rangeStart + Math.max( trigger.page.scrollTriggerCount, 1 ) / this.progressBarSegmentCount;
|
|
|
|
trigger.range = [rangeStart, rangeEnd];
|
|
|
|
|
|
|
|
const slideRange = trigger.range[1] - trigger.range[0];
|
|
|
|
trigger.page.scrollTriggers.forEach( scrollTrigger => {
|
|
|
|
scrollTrigger.globalRange = [
|
|
|
|
rangeStart + scrollTrigger.range[0] * slideRange,
|
|
|
|
rangeStart + scrollTrigger.range[1] * slideRange
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
rangeStart = rangeEnd;
|
|
|
|
} );
|
|
|
|
|
|
|
|
/*
|
|
|
|
console.log(this.slideTriggers.map( t => {
|
|
|
|
return {
|
|
|
|
range: `${t.range[0].toFixed(2)}-${t.range[1].toFixed(2)}`,
|
|
|
|
triggers: t.page.scrollTriggers.map( t => {
|
|
|
|
return `${t.globalRange[0].toFixed(2)}-${t.globalRange[1].toFixed(2)}`
|
|
|
|
}).join( ', ' ),
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.viewportElement.setAttribute( 'data-reader-scroll-bar', config.readerScrollbar );
|
2023-10-10 14:47:08 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
if( config.readerScrollbar && this.progressBarSegmentCount > 1 ) {
|
2023-10-10 14:47:08 +02:00
|
|
|
// Create the progress bar if it doesn't already exist
|
|
|
|
if( !this.progressBar ) this.createProgressBar();
|
|
|
|
|
|
|
|
this.syncProgressBar();
|
2023-10-10 13:34:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.removeProgressBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
/**
|
|
|
|
* Creates scroll triggers for the fragments in the given page.
|
|
|
|
*
|
|
|
|
* @param {*} page
|
|
|
|
*/
|
|
|
|
createFragmentTriggersForPage( page, slideElement ) {
|
|
|
|
|
|
|
|
slideElement = slideElement || page.slideElement;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
const fragmentGroups = this.Reveal.fragments.sort( slideElement.querySelectorAll( '.fragment' ), true );
|
|
|
|
|
|
|
|
// Create scroll triggers that show/hide fragments
|
|
|
|
if( fragmentGroups.length ) {
|
|
|
|
page.fragments = this.Reveal.fragments.sort( slideElement.querySelectorAll( '.fragment:not(.disabled)' ) );
|
|
|
|
page.scrollTriggers.push(
|
|
|
|
// Trigger for the initial state with no fragments visible
|
|
|
|
{
|
|
|
|
activate: () => {
|
|
|
|
this.Reveal.fragments.update( -1, page.fragments, slideElement );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Triggers for each fragment group
|
|
|
|
...fragmentGroups.map( ( fragments, i ) => ({
|
|
|
|
activate: () => {
|
|
|
|
this.Reveal.fragments.update( i, page.fragments, slideElement );
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const scrollTriggerRangeSegmentSize = 1 / page.scrollTriggers.length;
|
|
|
|
page.scrollTriggers.forEach( ( trigger, i ) => {
|
|
|
|
trigger.range = [i * scrollTriggerRangeSegmentSize, ( i + 1 ) * scrollTriggerRangeSegmentSize];
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return page.scrollTriggers.length;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates scroll triggers for the auto-animate steps in the
|
|
|
|
* given page.
|
|
|
|
*
|
|
|
|
* @param {*} page
|
|
|
|
*/
|
|
|
|
createAutoAnimateTriggersForPage( page ) {
|
|
|
|
|
|
|
|
let pages = [];
|
|
|
|
|
|
|
|
if( page.autoAnimateElements.length > 0 ) {
|
|
|
|
|
|
|
|
// Triggers for each subsequent auto-animate slide
|
|
|
|
this.slideTriggers.push( ...Array.from( page.autoAnimateElements ).map( ( autoAnimateElement, i ) => {
|
|
|
|
const slideElement = autoAnimateElement.querySelector( 'section' );
|
|
|
|
|
|
|
|
let autoAnimatePage = {
|
|
|
|
slideElement,
|
|
|
|
backgroundElement: autoAnimateElement.querySelector( '.slide-background' ),
|
|
|
|
contentElement: autoAnimateElement,
|
|
|
|
scrollTriggers: [],
|
|
|
|
scrollTriggerCount: 0,
|
|
|
|
scrollTriggerHeight: page.scrollTriggerHeight,
|
|
|
|
top: page.top,
|
|
|
|
bottom: page.bottom,
|
|
|
|
indexh: parseInt( slideElement.getAttribute( 'data-index-h' ), 10 ),
|
|
|
|
indexv: parseInt( slideElement.getAttribute( 'data-index-v' ), 10 )
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create fragment scroll triggers for the auto-animate slide
|
|
|
|
autoAnimatePage.scrollTriggerCount = this.createFragmentTriggersForPage( autoAnimatePage, slideElement );
|
|
|
|
|
|
|
|
pages.push( autoAnimatePage );
|
|
|
|
|
|
|
|
return {
|
|
|
|
page: autoAnimatePage,
|
|
|
|
activate: () => this.activatePage( autoAnimatePage ),
|
|
|
|
deactivate: () => this.deactivatePage( autoAnimatePage )
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
/**
|
|
|
|
* Rerenders progress bar segments so that they match the current
|
|
|
|
* reveal.js config and size.
|
|
|
|
*/
|
|
|
|
syncProgressBar() {
|
2023-10-10 10:24:02 +02:00
|
|
|
|
|
|
|
this.progressBarInner.querySelectorAll( '.reader-progress-slide' ).forEach( slide => slide.remove() );
|
|
|
|
|
|
|
|
const viewportHeight = this.viewportElement.offsetHeight;
|
|
|
|
const scrollHeight = this.viewportElement.scrollHeight;
|
|
|
|
|
|
|
|
this.progressBarHeight = this.progressBarInner.offsetHeight;
|
2023-10-20 10:18:34 +02:00
|
|
|
this.playheadHeight = Math.max( 1 / this.progressBarSegmentCount * this.progressBarHeight, MIN_PLAYHEAD_HEIGHT );
|
2023-10-10 10:24:02 +02:00
|
|
|
this.progressBarScrollableHeight = this.progressBarHeight - this.playheadHeight;
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
this.progressBarPlayhead.style.height = this.playheadHeight - PROGRESS_SPACING + 'px';
|
2023-10-10 10:24:02 +02:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
const progressSegmentHeight = viewportHeight / scrollHeight * this.progressBarHeight;
|
2023-10-10 10:24:02 +02:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
// Don't show individual segments if they're too small
|
|
|
|
if( progressSegmentHeight > MIN_PROGRESS_SEGMENT_HEIGHT ) {
|
2023-10-10 10:24:02 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
this.slideTriggers.forEach( trigger => {
|
|
|
|
|
|
|
|
const { page } = trigger;
|
2023-10-10 11:16:31 +02:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
page.progressBarSlide = document.createElement( 'div' );
|
|
|
|
page.progressBarSlide.className = 'reader-progress-slide';
|
2023-10-20 10:18:34 +02:00
|
|
|
page.progressBarSlide.style.top = trigger.range[0] * this.progressBarHeight + 'px';
|
|
|
|
page.progressBarSlide.style.height = ( trigger.range[1] - trigger.range[0] ) * this.progressBarHeight - PROGRESS_SPACING + 'px';
|
2023-10-10 14:47:08 +02:00
|
|
|
page.progressBarSlide.classList.toggle( 'has-triggers', page.scrollTriggers.length > 0 );
|
|
|
|
this.progressBarInner.appendChild( page.progressBarSlide );
|
2023-10-10 11:16:31 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
const scrollBarSlideHeight = ( trigger.range[1] - trigger.range[0] ) * this.progressBarHeight;
|
2023-10-12 11:09:47 +02:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
// Create visual representations for each scroll trigger
|
2023-10-20 10:18:34 +02:00
|
|
|
trigger.page.scrollTriggerElements = page.scrollTriggers.map( ( trigger, i ) => {
|
2023-10-10 11:16:31 +02:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
const triggerElement = document.createElement( 'div' );
|
|
|
|
triggerElement.className = 'reader-progress-trigger';
|
2023-10-18 11:16:06 +02:00
|
|
|
triggerElement.style.top = trigger.range[0] * scrollBarSlideHeight + 'px';
|
2023-10-20 10:18:34 +02:00
|
|
|
triggerElement.style.height = ( trigger.range[1] - trigger.range[0] ) * scrollBarSlideHeight - PROGRESS_SPACING*2 + 'px';
|
2023-10-10 14:47:08 +02:00
|
|
|
page.progressBarSlide.appendChild( triggerElement );
|
2023-10-10 10:24:02 +02:00
|
|
|
|
2023-10-12 13:39:44 +02:00
|
|
|
if( i === 0 ) triggerElement.style.display = 'none';
|
|
|
|
|
|
|
|
return triggerElement;
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
} );
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
this.pages.forEach( page => page.progressBarSlide = null );
|
2023-09-12 17:00:56 +02:00
|
|
|
|
2023-10-10 10:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
/**
|
|
|
|
* Moves the progress bar playhead to the specified position.
|
|
|
|
*
|
|
|
|
* @param {number} progress 0-1
|
|
|
|
*/
|
|
|
|
setProgressBarValue( progress ) {
|
2023-10-10 10:24:02 +02:00
|
|
|
|
2023-10-10 13:34:33 +02:00
|
|
|
if( this.progressBar ) {
|
2023-10-10 10:24:02 +02:00
|
|
|
|
2023-10-10 13:34:33 +02:00
|
|
|
this.progressBarPlayhead.style.transform = `translateY(${progress * this.progressBarScrollableHeight}px)`;
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
this.getAllPages()
|
2023-10-10 14:47:08 +02:00
|
|
|
.filter( page => page.progressBarSlide )
|
|
|
|
.forEach( ( page ) => {
|
|
|
|
page.progressBarSlide.classList.toggle( 'active', !!page.active );
|
2023-10-12 13:39:44 +02:00
|
|
|
|
|
|
|
page.scrollTriggers.forEach( ( trigger, i ) => {
|
|
|
|
page.scrollTriggerElements[i].classList.toggle( 'active', page.active && trigger.active );
|
|
|
|
} );
|
2023-10-10 14:47:08 +02:00
|
|
|
} );
|
2023-10-10 13:34:33 +02:00
|
|
|
|
|
|
|
this.showProgressBar();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
/**
|
|
|
|
* Show the progress bar and, if configured, automatically hide
|
|
|
|
* it after a delay.
|
|
|
|
*/
|
2023-10-10 13:34:33 +02:00
|
|
|
showProgressBar() {
|
|
|
|
|
|
|
|
this.progressBar.classList.add( 'visible' );
|
|
|
|
|
|
|
|
clearTimeout( this.hideProgressBarTimeout );
|
|
|
|
|
2023-10-12 14:03:28 +02:00
|
|
|
if( this.Reveal.getConfig().readerScrollbar === 'auto' && !this.draggingProgressBar ) {
|
2023-10-10 13:34:33 +02:00
|
|
|
|
|
|
|
this.hideProgressBarTimeout = setTimeout( () => {
|
|
|
|
this.progressBar.classList.remove( 'visible' );
|
|
|
|
}, HIDE_SCROLLBAR_TIMEOUT );
|
|
|
|
|
|
|
|
}
|
2023-09-12 17:00:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
/**
|
|
|
|
* Scrolls the given slide element into view.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} slideElement
|
|
|
|
*/
|
2023-09-14 15:03:23 +02:00
|
|
|
scrollToSlide( slideElement ) {
|
|
|
|
|
2023-09-19 11:52:54 +02:00
|
|
|
if( !this.active ) {
|
2023-09-14 15:03:23 +02:00
|
|
|
this.activatedCallbacks.push( () => this.scrollToSlide( slideElement ) );
|
|
|
|
}
|
|
|
|
else {
|
2023-10-20 10:18:34 +02:00
|
|
|
// Find the trigger for this slide
|
|
|
|
const trigger = this.getScrollTriggerBySlide( slideElement );
|
|
|
|
|
|
|
|
if( trigger ) {
|
|
|
|
// Use the trigger's range to calculate the scroll position
|
|
|
|
this.viewportElement.scrollTop = trigger.range[0] * ( this.viewportElement.scrollHeight - this.viewportElement.offsetHeight );
|
|
|
|
}
|
2023-09-14 15:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
activatePage( page ) {
|
2023-09-12 17:00:56 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
if( !page.active ) {
|
2023-09-12 17:00:56 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
page.active = true;
|
2023-10-10 14:47:08 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
const { slideElement, backgroundElement, contentElement, indexh, indexv } = page;
|
|
|
|
|
|
|
|
contentElement.style.display = 'block';
|
|
|
|
|
|
|
|
slideElement.classList.add( 'present' );
|
|
|
|
backgroundElement.classList.add( 'present' );
|
2023-10-10 14:47:08 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
this.Reveal.setCurrentReaderPage( slideElement, indexh, indexv );
|
|
|
|
this.Reveal.slideContent.startEmbeddedContent( slideElement );
|
|
|
|
this.Reveal.backgrounds.bubbleSlideContrastClassToElement( slideElement, this.viewportElement );
|
2023-10-10 14:47:08 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
if( backgroundElement ) {
|
|
|
|
this.Reveal.slideContent.startEmbeddedContent( backgroundElement );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide all content element siblings
|
|
|
|
Array.from( contentElement.parentNode.children ).forEach( sibling => {
|
|
|
|
if( sibling !== contentElement ) {
|
|
|
|
sibling.style.display = 'none';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-10-05 14:06:06 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
deactivatePage( page ) {
|
|
|
|
|
|
|
|
if( page.active ) {
|
|
|
|
|
|
|
|
page.active = false;
|
|
|
|
|
|
|
|
const { slideElement, backgroundElement } = page;
|
|
|
|
|
|
|
|
slideElement.classList.remove( 'present' );
|
|
|
|
backgroundElement.classList.remove( 'present' );
|
|
|
|
|
|
|
|
this.Reveal.slideContent.stopEmbeddedContent( slideElement );
|
|
|
|
|
|
|
|
if( backgroundElement ) {
|
|
|
|
this.Reveal.slideContent.stopEmbeddedContent( backgroundElement );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
onScroll() {
|
|
|
|
|
|
|
|
const viewportHeight = this.viewportElement.offsetHeight;
|
|
|
|
const scrollTop = this.viewportElement.scrollTop;
|
|
|
|
const scrollHeight = this.viewportElement.scrollHeight - viewportHeight
|
|
|
|
const scrollProgress = Math.max( Math.min( scrollTop / scrollHeight, 1 ), 0 );
|
|
|
|
const scrollProgressMid = Math.max( Math.min( ( scrollTop + viewportHeight / 2 ) / this.viewportElement.scrollHeight, 1 ), 0 );
|
|
|
|
|
|
|
|
let activePage;
|
|
|
|
|
|
|
|
this.slideTriggers.forEach( ( trigger, i ) => {
|
|
|
|
const { page } = trigger;
|
|
|
|
const isWithinPreloadRange = scrollProgress >= trigger.range[0] - 2 && scrollProgress <= trigger.range[1] + 2;
|
2023-09-12 17:00:56 +02:00
|
|
|
|
2023-09-14 15:17:07 +02:00
|
|
|
if( isWithinPreloadRange ) {
|
|
|
|
if( !page.preloaded ) {
|
|
|
|
page.preloaded = true;
|
2023-09-14 13:00:31 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
if( scrollProgress >= trigger.range[0] && scrollProgress <= trigger.range[1] ) {
|
|
|
|
if( !trigger.active ) {
|
|
|
|
trigger.active = true;
|
|
|
|
trigger.activate();
|
2023-09-14 13:00:31 +02:00
|
|
|
}
|
2023-10-20 10:18:34 +02:00
|
|
|
|
|
|
|
activePage = trigger.page;
|
2023-09-14 15:17:07 +02:00
|
|
|
}
|
2023-10-20 10:18:34 +02:00
|
|
|
else if( trigger.activate ) {
|
|
|
|
trigger.active = false;
|
|
|
|
if( trigger.deactivate ) trigger.deactivate();
|
2023-09-14 15:17:07 +02:00
|
|
|
}
|
2023-10-20 10:18:34 +02:00
|
|
|
} );
|
2023-09-14 13:00:31 +02:00
|
|
|
|
2023-10-20 10:18:34 +02:00
|
|
|
if( activePage ) {
|
|
|
|
activePage.scrollTriggers.forEach( trigger => {
|
|
|
|
if( scrollProgressMid >= trigger.globalRange[0] && scrollProgressMid <= trigger.globalRange[1] ) {
|
|
|
|
if( !trigger.active ) {
|
|
|
|
trigger.active = true;
|
|
|
|
trigger.activate();
|
2023-09-14 15:17:07 +02:00
|
|
|
}
|
2023-10-20 10:18:34 +02:00
|
|
|
}
|
|
|
|
else if( trigger.activate ) {
|
|
|
|
trigger.active = false;
|
|
|
|
if( trigger.deactivate ) trigger.deactivate();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2020-03-14 08:27:29 +01:00
|
|
|
|
2023-10-10 14:47:08 +02:00
|
|
|
this.setProgressBarValue( scrollTop / ( this.viewportElement.scrollHeight - viewportHeight ) );
|
2023-10-10 10:24:02 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
get viewportElement() {
|
|
|
|
|
|
|
|
return this.Reveal.getViewportElement();
|
|
|
|
|
2020-03-14 08:27:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 22:08:13 +01:00
|
|
|
}
|