blob: 1585affbd84b4bbd2f600bb749f7714ff556020f [file] [log] [blame]
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001import SlideContent from './controllers/slidecontent.js'
2import SlideNumber from './controllers/slidenumber.js'
Marc Kupietz09b75752023-10-07 09:32:19 +02003import JumpToSlide from './controllers/jumptoslide.js'
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02004import Backgrounds from './controllers/backgrounds.js'
5import AutoAnimate from './controllers/autoanimate.js'
Marc Kupietz9c036a42024-05-14 13:17:25 +02006import ScrollView from './controllers/scrollview.js'
7import PrintView from './controllers/printview.js'
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02008import Fragments from './controllers/fragments.js'
9import Overview from './controllers/overview.js'
10import Keyboard from './controllers/keyboard.js'
11import Location from './controllers/location.js'
12import Controls from './controllers/controls.js'
13import Progress from './controllers/progress.js'
14import Pointer from './controllers/pointer.js'
15import Plugins from './controllers/plugins.js'
Marc Kupietzcf6e9982026-08-15 15:37:40 +020016import Overlay from './controllers/overlay.js'
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020017import Touch from './controllers/touch.js'
18import Focus from './controllers/focus.js'
19import Notes from './controllers/notes.js'
20import Playback from './components/playback.js'
21import defaultConfig from './config.js'
22import * as Util from './utils/util.js'
23import * as Device from './utils/device.js'
24import {
25 SLIDES_SELECTOR,
26 HORIZONTAL_SLIDES_SELECTOR,
27 VERTICAL_SLIDES_SELECTOR,
28 POST_MESSAGE_METHOD_BLACKLIST
29} from './utils/constants.js'
30
31// The reveal.js version
Marc Kupietzcf6e9982026-08-15 15:37:40 +020032export const VERSION = '5.2.1';
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020033
34/**
35 * reveal.js
36 * https://revealjs.com
37 * MIT licensed
38 *
Marc Kupietz09b75752023-10-07 09:32:19 +020039 * Copyright (C) 2011-2022 Hakim El Hattab, https://hakim.se
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020040 */
41export default function( revealElement, options ) {
42
43 // Support initialization with no args, one arg
44 // [options] or two args [revealElement, options]
45 if( arguments.length < 2 ) {
46 options = arguments[0];
47 revealElement = document.querySelector( '.reveal' );
48 }
49
50 const Reveal = {};
51
52 // Configuration defaults, can be overridden at initialization time
53 let config = {},
54
Marc Kupietz9c036a42024-05-14 13:17:25 +020055 // Flags if initialize() has been invoked for this reveal instance
56 initialized = false,
57
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020058 // Flags if reveal.js is loaded (has dispatched the 'ready' event)
59 ready = false,
60
61 // The horizontal and vertical index of the currently active slide
62 indexh,
63 indexv,
64
65 // The previous and current slide HTML elements
66 previousSlide,
67 currentSlide,
68
69 // Remember which directions that the user has navigated towards
70 navigationHistory = {
71 hasNavigatedHorizontally: false,
72 hasNavigatedVertically: false
73 },
74
75 // Slides may have a data-state attribute which we pick up and apply
76 // as a class to the body. This list contains the combined state of
77 // all current slides.
78 state = [],
79
80 // The current scale of the presentation (see width/height config)
81 scale = 1,
82
83 // CSS transform that is currently applied to the slides container,
84 // split into two groups
85 slidesTransform = { layout: '', overview: '' },
86
87 // Cached references to DOM elements
88 dom = {},
89
90 // Flags if the interaction event listeners are bound
91 eventsAreBound = false,
92
93 // The current slide transition state; idle or running
94 transition = 'idle',
95
96 // The current auto-slide duration
97 autoSlide = 0,
98
99 // Auto slide properties
100 autoSlidePlayer,
101 autoSlideTimeout = 0,
102 autoSlideStartTime = -1,
103 autoSlidePaused = false,
104
105 // Controllers for different aspects of our presentation. They're
106 // all given direct references to this Reveal instance since there
107 // may be multiple presentations running in parallel.
108 slideContent = new SlideContent( Reveal ),
109 slideNumber = new SlideNumber( Reveal ),
Marc Kupietz09b75752023-10-07 09:32:19 +0200110 jumpToSlide = new JumpToSlide( Reveal ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200111 autoAnimate = new AutoAnimate( Reveal ),
112 backgrounds = new Backgrounds( Reveal ),
Marc Kupietz9c036a42024-05-14 13:17:25 +0200113 scrollView = new ScrollView( Reveal ),
114 printView = new PrintView( Reveal ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200115 fragments = new Fragments( Reveal ),
116 overview = new Overview( Reveal ),
117 keyboard = new Keyboard( Reveal ),
118 location = new Location( Reveal ),
119 controls = new Controls( Reveal ),
120 progress = new Progress( Reveal ),
121 pointer = new Pointer( Reveal ),
122 plugins = new Plugins( Reveal ),
Marc Kupietzcf6e9982026-08-15 15:37:40 +0200123 overlay = new Overlay( Reveal ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200124 focus = new Focus( Reveal ),
125 touch = new Touch( Reveal ),
126 notes = new Notes( Reveal );
127
128 /**
129 * Starts up the presentation.
130 */
131 function initialize( initOptions ) {
132
Christophe Dervieux8afae132021-12-06 15:16:42 +0100133 if( !revealElement ) throw 'Unable to find presentation root (<div class="reveal">).';
134
Marc Kupietzcf6e9982026-08-15 15:37:40 +0200135 if( initialized ) throw 'Reveal.js has already been initialized.';
136
Marc Kupietz9c036a42024-05-14 13:17:25 +0200137 initialized = true;
138
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200139 // Cache references to key DOM elements
140 dom.wrapper = revealElement;
141 dom.slides = revealElement.querySelector( '.slides' );
142
Christophe Dervieux8afae132021-12-06 15:16:42 +0100143 if( !dom.slides ) throw 'Unable to find slides container (<div class="slides">).';
144
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200145 // Compose our config object in order of increasing precedence:
146 // 1. Default reveal.js options
147 // 2. Options provided via Reveal.configure() prior to
148 // initialization
149 // 3. Options passed to the Reveal constructor
150 // 4. Options passed to Reveal.initialize
151 // 5. Query params
152 config = { ...defaultConfig, ...config, ...options, ...initOptions, ...Util.getQueryHash() };
153
Marc Kupietz9c036a42024-05-14 13:17:25 +0200154 // Legacy support for the ?print-pdf query
155 if( /print-pdf/gi.test( window.location.search ) ) {
156 config.view = 'print';
157 }
158
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200159 setViewport();
160
161 // Force a layout when the whole page, incl fonts, has loaded
162 window.addEventListener( 'load', layout, false );
163
164 // Register plugins and load dependencies, then move on to #start()
165 plugins.load( config.plugins, config.dependencies ).then( start );
166
167 return new Promise( resolve => Reveal.on( 'ready', resolve ) );
168
169 }
170
171 /**
172 * Encase the presentation in a reveal.js viewport. The
173 * extent of the viewport differs based on configuration.
174 */
175 function setViewport() {
176
177 // Embedded decks use the reveal element as their viewport
178 if( config.embedded === true ) {
179 dom.viewport = Util.closest( revealElement, '.reveal-viewport' ) || revealElement;
180 }
181 // Full-page decks use the body as their viewport
182 else {
183 dom.viewport = document.body;
184 document.documentElement.classList.add( 'reveal-full-page' );
185 }
186
187 dom.viewport.classList.add( 'reveal-viewport' );
188
189 }
190
191 /**
192 * Starts up reveal.js by binding input events and navigating
193 * to the current URL deeplink if there is one.
194 */
195 function start() {
196
Marc Kupietzcf6e9982026-08-15 15:37:40 +0200197 // Don't proceed if this instance has been destroyed
198 if( initialized === false ) return;
199
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200200 ready = true;
201
202 // Remove slides hidden with data-visibility
203 removeHiddenSlides();
204
205 // Make sure we've got all the DOM elements we need
206 setupDOM();
207
208 // Listen to messages posted to this window
209 setupPostMessage();
210
211 // Prevent the slides from being scrolled out of view
212 setupScrollPrevention();
213
Marc Kupietz09b75752023-10-07 09:32:19 +0200214 // Adds bindings for fullscreen mode
215 setupFullscreen();
216
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200217 // Resets all vertical slides so that only the first is visible
218 resetVerticalSlides();
219
220 // Updates the presentation to match the current configuration values
221 configure();
222
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200223 // Create slide backgrounds
224 backgrounds.update( true );
225
Marc Kupietz9c036a42024-05-14 13:17:25 +0200226 // Activate the print/scroll view if configured
227 activateInitialView();
228
229 // Read the initial hash
230 location.readURL();
231
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200232 // Notify listeners that the presentation is ready but use a 1ms
233 // timeout to ensure it's not fired synchronously after #initialize()
234 setTimeout( () => {
235 // Enable transitions now that we're loaded
236 dom.slides.classList.remove( 'no-transition' );
237
238 dom.wrapper.classList.add( 'ready' );
239
240 dispatchEvent({
241 type: 'ready',
242 data: {
243 indexh,
244 indexv,
245 currentSlide
246 }
247 });
248 }, 1 );
249
Marc Kupietz9c036a42024-05-14 13:17:25 +0200250 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200251
Marc Kupietz9c036a42024-05-14 13:17:25 +0200252 /**
253 * Activates the correct reveal.js view based on our config.
254 * This is only invoked once during initialization.
255 */
256 function activateInitialView() {
257
258 const activatePrintView = config.view === 'print';
259 const activateScrollView = config.view === 'scroll' || config.view === 'reader';
260
261 if( activatePrintView || activateScrollView ) {
262
263 if( activatePrintView ) {
264 removeEventListeners();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200265 }
266 else {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200267 touch.unbind();
268 }
269
270 // Avoid content flickering during layout
271 dom.viewport.classList.add( 'loading-scroll-mode' );
272
273 if( activatePrintView ) {
274 // The document needs to have loaded for the PDF layout
275 // measurements to be accurate
276 if( document.readyState === 'complete' ) {
277 printView.activate();
278 }
279 else {
280 window.addEventListener( 'load', () => printView.activate() );
281 }
282 }
283 else {
284 scrollView.activate();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200285 }
286 }
287
288 }
289
290 /**
291 * Removes all slides with data-visibility="hidden". This
292 * is done right before the rest of the presentation is
293 * initialized.
294 *
295 * If you want to show all hidden slides, initialize
296 * reveal.js with showHiddenSlides set to true.
297 */
298 function removeHiddenSlides() {
299
300 if( !config.showHiddenSlides ) {
301 Util.queryAll( dom.wrapper, 'section[data-visibility="hidden"]' ).forEach( slide => {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200302 const parent = slide.parentNode;
303
304 // If this slide is part of a stack and that stack will be
305 // empty after removing the hidden slide, remove the entire
306 // stack
307 if( parent.childElementCount === 1 && /section/i.test( parent.nodeName ) ) {
308 parent.remove();
309 }
310 else {
311 slide.remove();
312 }
313
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200314 } );
315 }
316
317 }
318
319 /**
320 * Finds and stores references to DOM elements which are
321 * required by the presentation. If a required element is
322 * not found, it is created.
323 */
324 function setupDOM() {
325
326 // Prevent transitions while we're loading
327 dom.slides.classList.add( 'no-transition' );
328
329 if( Device.isMobile ) {
330 dom.wrapper.classList.add( 'no-hover' );
331 }
332 else {
333 dom.wrapper.classList.remove( 'no-hover' );
334 }
335
336 backgrounds.render();
337 slideNumber.render();
Marc Kupietz09b75752023-10-07 09:32:19 +0200338 jumpToSlide.render();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200339 controls.render();
340 progress.render();
341 notes.render();
342
343 // Overlay graphic which is displayed during the paused mode
344 dom.pauseOverlay = Util.createSingletonNode( dom.wrapper, 'div', 'pause-overlay', config.controls ? '<button class="resume-button">Resume presentation</button>' : null );
345
346 dom.statusElement = createStatusElement();
347
348 dom.wrapper.setAttribute( 'role', 'application' );
349 }
350
351 /**
352 * Creates a hidden div with role aria-live to announce the
353 * current slide content. Hide the div off-screen to make it
354 * available only to Assistive Technologies.
355 *
356 * @return {HTMLElement}
357 */
358 function createStatusElement() {
359
360 let statusElement = dom.wrapper.querySelector( '.aria-status' );
361 if( !statusElement ) {
362 statusElement = document.createElement( 'div' );
363 statusElement.style.position = 'absolute';
364 statusElement.style.height = '1px';
365 statusElement.style.width = '1px';
366 statusElement.style.overflow = 'hidden';
367 statusElement.style.clip = 'rect( 1px, 1px, 1px, 1px )';
368 statusElement.classList.add( 'aria-status' );
369 statusElement.setAttribute( 'aria-live', 'polite' );
370 statusElement.setAttribute( 'aria-atomic','true' );
371 dom.wrapper.appendChild( statusElement );
372 }
373 return statusElement;
374
375 }
376
377 /**
378 * Announces the given text to screen readers.
379 */
380 function announceStatus( value ) {
381
382 dom.statusElement.textContent = value;
383
384 }
385
386 /**
387 * Converts the given HTML element into a string of text
388 * that can be announced to a screen reader. Hidden
389 * elements are excluded.
390 */
391 function getStatusText( node ) {
392
393 let text = '';
394
395 // Text node
396 if( node.nodeType === 3 ) {
397 text += node.textContent;
398 }
399 // Element node
400 else if( node.nodeType === 1 ) {
401
402 let isAriaHidden = node.getAttribute( 'aria-hidden' );
403 let isDisplayHidden = window.getComputedStyle( node )['display'] === 'none';
404 if( isAriaHidden !== 'true' && !isDisplayHidden ) {
405
406 Array.from( node.childNodes ).forEach( child => {
407 text += getStatusText( child );
408 } );
409
410 }
411
412 }
413
414 text = text.trim();
415
416 return text === '' ? '' : text + ' ';
417
418 }
419
420 /**
421 * This is an unfortunate necessity. Some actions – such as
422 * an input field being focused in an iframe or using the
423 * keyboard to expand text selection beyond the bounds of
424 * a slide – can trigger our content to be pushed out of view.
425 * This scrolling can not be prevented by hiding overflow in
426 * CSS (we already do) so we have to resort to repeatedly
427 * checking if the slides have been offset :(
428 */
429 function setupScrollPrevention() {
430
431 setInterval( () => {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200432 if( !scrollView.isActive() && dom.wrapper.scrollTop !== 0 || dom.wrapper.scrollLeft !== 0 ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200433 dom.wrapper.scrollTop = 0;
434 dom.wrapper.scrollLeft = 0;
435 }
436 }, 1000 );
437
438 }
439
440 /**
Marc Kupietz09b75752023-10-07 09:32:19 +0200441 * After entering fullscreen we need to force a layout to
442 * get our presentations to scale correctly. This behavior
443 * is inconsistent across browsers but a force layout seems
444 * to normalize it.
445 */
446 function setupFullscreen() {
447
448 document.addEventListener( 'fullscreenchange', onFullscreenChange );
449 document.addEventListener( 'webkitfullscreenchange', onFullscreenChange );
450
451 }
452
453 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200454 * Registers a listener to postMessage events, this makes it
455 * possible to call all reveal.js API methods from another
456 * window. For example:
457 *
458 * revealWindow.postMessage( JSON.stringify({
459 * method: 'slide',
460 * args: [ 2 ]
461 * }), '*' );
462 */
463 function setupPostMessage() {
464
465 if( config.postMessage ) {
Marc Kupietz09b75752023-10-07 09:32:19 +0200466 window.addEventListener( 'message', onPostMessage, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200467 }
468
469 }
470
471 /**
472 * Applies the configuration settings from the config
473 * object. May be called multiple times.
474 *
475 * @param {object} options
476 */
477 function configure( options ) {
478
479 const oldConfig = { ...config }
480
481 // New config options may be passed when this method
482 // is invoked through the API after initialization
483 if( typeof options === 'object' ) Util.extend( config, options );
484
485 // Abort if reveal.js hasn't finished loading, config
486 // changes will be applied automatically once ready
487 if( Reveal.isReady() === false ) return;
488
489 const numberOfSlides = dom.wrapper.querySelectorAll( SLIDES_SELECTOR ).length;
490
491 // The transition is added as a class on the .reveal element
492 dom.wrapper.classList.remove( oldConfig.transition );
493 dom.wrapper.classList.add( config.transition );
494
495 dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
496 dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
497
498 // Expose our configured slide dimensions as custom props
Marc Kupietz9c036a42024-05-14 13:17:25 +0200499 dom.viewport.style.setProperty( '--slide-width', typeof config.width === 'string' ? config.width : config.width + 'px' );
500 dom.viewport.style.setProperty( '--slide-height', typeof config.height === 'string' ? config.height : config.height + 'px' );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200501
502 if( config.shuffle ) {
503 shuffle();
504 }
505
506 Util.toggleClass( dom.wrapper, 'embedded', config.embedded );
507 Util.toggleClass( dom.wrapper, 'rtl', config.rtl );
508 Util.toggleClass( dom.wrapper, 'center', config.center );
509
510 // Exit the paused mode if it was configured off
511 if( config.pause === false ) {
512 resume();
513 }
514
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200515 // Reset all changes made by auto-animations
516 autoAnimate.reset();
517
518 // Remove existing auto-slide controls
519 if( autoSlidePlayer ) {
520 autoSlidePlayer.destroy();
521 autoSlidePlayer = null;
522 }
523
524 // Generate auto-slide controls if needed
525 if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable ) {
526 autoSlidePlayer = new Playback( dom.wrapper, () => {
527 return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
528 } );
529
530 autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
531 autoSlidePaused = false;
532 }
533
534 // Add the navigation mode to the DOM so we can adjust styling
535 if( config.navigationMode !== 'default' ) {
536 dom.wrapper.setAttribute( 'data-navigation-mode', config.navigationMode );
537 }
538 else {
539 dom.wrapper.removeAttribute( 'data-navigation-mode' );
540 }
541
542 notes.configure( config, oldConfig );
543 focus.configure( config, oldConfig );
544 pointer.configure( config, oldConfig );
545 controls.configure( config, oldConfig );
546 progress.configure( config, oldConfig );
547 keyboard.configure( config, oldConfig );
548 fragments.configure( config, oldConfig );
549 slideNumber.configure( config, oldConfig );
550
551 sync();
552
553 }
554
555 /**
556 * Binds all event listeners.
557 */
558 function addEventListeners() {
559
560 eventsAreBound = true;
561
562 window.addEventListener( 'resize', onWindowResize, false );
563
564 if( config.touch ) touch.bind();
565 if( config.keyboard ) keyboard.bind();
566 if( config.progress ) progress.bind();
567 if( config.respondToHashChanges ) location.bind();
568 controls.bind();
569 focus.bind();
570
Christophe Dervieux8afae132021-12-06 15:16:42 +0100571 dom.slides.addEventListener( 'click', onSlidesClicked, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200572 dom.slides.addEventListener( 'transitionend', onTransitionEnd, false );
573 dom.pauseOverlay.addEventListener( 'click', resume, false );
574
575 if( config.focusBodyOnPageVisibilityChange ) {
576 document.addEventListener( 'visibilitychange', onPageVisibilityChange, false );
577 }
578
579 }
580
581 /**
582 * Unbinds all event listeners.
583 */
584 function removeEventListeners() {
585
586 eventsAreBound = false;
587
588 touch.unbind();
589 focus.unbind();
590 keyboard.unbind();
591 controls.unbind();
592 progress.unbind();
593 location.unbind();
594
595 window.removeEventListener( 'resize', onWindowResize, false );
596
Christophe Dervieux8afae132021-12-06 15:16:42 +0100597 dom.slides.removeEventListener( 'click', onSlidesClicked, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200598 dom.slides.removeEventListener( 'transitionend', onTransitionEnd, false );
599 dom.pauseOverlay.removeEventListener( 'click', resume, false );
600
601 }
602
603 /**
Marc Kupietz09b75752023-10-07 09:32:19 +0200604 * Uninitializes reveal.js by undoing changes made to the
605 * DOM and removing all event listeners.
606 */
607 function destroy() {
608
Marc Kupietzcf6e9982026-08-15 15:37:40 +0200609 initialized = false;
610
611 // There's nothing to destroy if this instance hasn't finished
612 // initializing
613 if( ready === false ) return;
Marc Kupietz9c036a42024-05-14 13:17:25 +0200614
Marc Kupietz09b75752023-10-07 09:32:19 +0200615 removeEventListeners();
616 cancelAutoSlide();
Marc Kupietz09b75752023-10-07 09:32:19 +0200617
618 // Destroy controllers
619 notes.destroy();
620 focus.destroy();
Marc Kupietzcf6e9982026-08-15 15:37:40 +0200621 overlay.destroy();
Marc Kupietz09b75752023-10-07 09:32:19 +0200622 plugins.destroy();
623 pointer.destroy();
624 controls.destroy();
625 progress.destroy();
626 backgrounds.destroy();
627 slideNumber.destroy();
628 jumpToSlide.destroy();
629
630 // Remove event listeners
631 document.removeEventListener( 'fullscreenchange', onFullscreenChange );
632 document.removeEventListener( 'webkitfullscreenchange', onFullscreenChange );
633 document.removeEventListener( 'visibilitychange', onPageVisibilityChange, false );
634 window.removeEventListener( 'message', onPostMessage, false );
635 window.removeEventListener( 'load', layout, false );
636
637 // Undo DOM changes
638 if( dom.pauseOverlay ) dom.pauseOverlay.remove();
639 if( dom.statusElement ) dom.statusElement.remove();
640
641 document.documentElement.classList.remove( 'reveal-full-page' );
642
643 dom.wrapper.classList.remove( 'ready', 'center', 'has-horizontal-slides', 'has-vertical-slides' );
644 dom.wrapper.removeAttribute( 'data-transition-speed' );
645 dom.wrapper.removeAttribute( 'data-background-transition' );
646
647 dom.viewport.classList.remove( 'reveal-viewport' );
648 dom.viewport.style.removeProperty( '--slide-width' );
649 dom.viewport.style.removeProperty( '--slide-height' );
650
651 dom.slides.style.removeProperty( 'width' );
652 dom.slides.style.removeProperty( 'height' );
653 dom.slides.style.removeProperty( 'zoom' );
654 dom.slides.style.removeProperty( 'left' );
655 dom.slides.style.removeProperty( 'top' );
656 dom.slides.style.removeProperty( 'bottom' );
657 dom.slides.style.removeProperty( 'right' );
658 dom.slides.style.removeProperty( 'transform' );
659
660 Array.from( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( slide => {
661 slide.style.removeProperty( 'display' );
662 slide.style.removeProperty( 'top' );
663 slide.removeAttribute( 'hidden' );
664 slide.removeAttribute( 'aria-hidden' );
665 } );
666
667 }
668
669 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200670 * Adds a listener to one of our custom reveal.js events,
671 * like slidechanged.
672 */
673 function on( type, listener, useCapture ) {
674
675 revealElement.addEventListener( type, listener, useCapture );
676
677 }
678
679 /**
680 * Unsubscribes from a reveal.js event.
681 */
682 function off( type, listener, useCapture ) {
683
684 revealElement.removeEventListener( type, listener, useCapture );
685
686 }
687
688 /**
689 * Applies CSS transforms to the slides container. The container
690 * is transformed from two separate sources: layout and the overview
691 * mode.
692 *
693 * @param {object} transforms
694 */
695 function transformSlides( transforms ) {
696
697 // Pick up new transforms from arguments
698 if( typeof transforms.layout === 'string' ) slidesTransform.layout = transforms.layout;
699 if( typeof transforms.overview === 'string' ) slidesTransform.overview = transforms.overview;
700
701 // Apply the transforms to the slides container
702 if( slidesTransform.layout ) {
703 Util.transformElement( dom.slides, slidesTransform.layout + ' ' + slidesTransform.overview );
704 }
705 else {
706 Util.transformElement( dom.slides, slidesTransform.overview );
707 }
708
709 }
710
711 /**
712 * Dispatches an event of the specified type from the
713 * reveal DOM element.
714 */
715 function dispatchEvent({ target=dom.wrapper, type, data, bubbles=true }) {
716
717 let event = document.createEvent( 'HTMLEvents', 1, 2 );
718 event.initEvent( type, bubbles, true );
719 Util.extend( event, data );
720 target.dispatchEvent( event );
721
722 if( target === dom.wrapper ) {
723 // If we're in an iframe, post each reveal.js event to the
724 // parent window. Used by the notes plugin
725 dispatchPostMessage( type );
726 }
727
Christophe Dervieux8afae132021-12-06 15:16:42 +0100728 return event;
729
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200730 }
731
732 /**
Marc Kupietz9c036a42024-05-14 13:17:25 +0200733 * Dispatches a slidechanged event.
734 *
735 * @param {string} origin Used to identify multiplex clients
736 */
737 function dispatchSlideChanged( origin ) {
738
739 dispatchEvent({
740 type: 'slidechanged',
741 data: {
742 indexh,
743 indexv,
744 previousSlide,
745 currentSlide,
746 origin
747 }
748 });
749
750 }
751
752 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200753 * Dispatched a postMessage of the given type from our window.
754 */
755 function dispatchPostMessage( type, data ) {
756
757 if( config.postMessageEvents && window.parent !== window.self ) {
758 let message = {
759 namespace: 'reveal',
760 eventName: type,
761 state: getState()
762 };
763
764 Util.extend( message, data );
765
766 window.parent.postMessage( JSON.stringify( message ), '*' );
767 }
768
769 }
770
771 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200772 * Applies JavaScript-controlled layout rules to the
773 * presentation.
774 */
775 function layout() {
776
Marc Kupietz9c036a42024-05-14 13:17:25 +0200777 if( dom.wrapper && !printView.isActive() ) {
778
779 const viewportWidth = dom.viewport.offsetWidth;
780 const viewportHeight = dom.viewport.offsetHeight;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200781
782 if( !config.disableLayout ) {
783
784 // On some mobile devices '100vh' is taller than the visible
785 // viewport which leads to part of the presentation being
786 // cut off. To work around this we define our own '--vh' custom
787 // property where 100x adds up to the correct height.
788 //
789 // https://css-tricks.com/the-trick-to-viewport-units-on-mobile/
790 if( Device.isMobile && !config.embedded ) {
791 document.documentElement.style.setProperty( '--vh', ( window.innerHeight * 0.01 ) + 'px' );
792 }
793
Marc Kupietz9c036a42024-05-14 13:17:25 +0200794 const size = scrollView.isActive() ?
795 getComputedSlideSize( viewportWidth, viewportHeight ) :
796 getComputedSlideSize();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200797
798 const oldScale = scale;
799
800 // Layout the contents of the slides
801 layoutSlideContents( config.width, config.height );
802
803 dom.slides.style.width = size.width + 'px';
804 dom.slides.style.height = size.height + 'px';
805
806 // Determine scale of content to fit within available space
807 scale = Math.min( size.presentationWidth / size.width, size.presentationHeight / size.height );
808
809 // Respect max/min scale settings
810 scale = Math.max( scale, config.minScale );
811 scale = Math.min( scale, config.maxScale );
812
Marc Kupietz9c036a42024-05-14 13:17:25 +0200813 // Don't apply any scaling styles if scale is 1 or we're
814 // in the scroll view
815 if( scale === 1 || scrollView.isActive() ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200816 dom.slides.style.zoom = '';
817 dom.slides.style.left = '';
818 dom.slides.style.top = '';
819 dom.slides.style.bottom = '';
820 dom.slides.style.right = '';
821 transformSlides( { layout: '' } );
822 }
823 else {
Marc Kupietz09b75752023-10-07 09:32:19 +0200824 dom.slides.style.zoom = '';
825 dom.slides.style.left = '50%';
826 dom.slides.style.top = '50%';
827 dom.slides.style.bottom = 'auto';
828 dom.slides.style.right = 'auto';
829 transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200830 }
831
832 // Select all slides, vertical and horizontal
833 const slides = Array.from( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
834
835 for( let i = 0, len = slides.length; i < len; i++ ) {
836 const slide = slides[ i ];
837
838 // Don't bother updating invisible slides
839 if( slide.style.display === 'none' ) {
840 continue;
841 }
842
Marc Kupietz9c036a42024-05-14 13:17:25 +0200843 if( ( config.center || slide.classList.contains( 'center' ) ) ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200844 // Vertical stacks are not centred since their section
845 // children will be
846 if( slide.classList.contains( 'stack' ) ) {
847 slide.style.top = 0;
848 }
849 else {
850 slide.style.top = Math.max( ( size.height - slide.scrollHeight ) / 2, 0 ) + 'px';
851 }
852 }
853 else {
854 slide.style.top = '';
855 }
856
857 }
858
859 if( oldScale !== scale ) {
860 dispatchEvent({
861 type: 'resize',
862 data: {
863 oldScale,
864 scale,
865 size
866 }
867 });
868 }
869 }
870
Marc Kupietz9c036a42024-05-14 13:17:25 +0200871 checkResponsiveScrollView();
872
Marc Kupietz09b75752023-10-07 09:32:19 +0200873 dom.viewport.style.setProperty( '--slide-scale', scale );
Marc Kupietz9c036a42024-05-14 13:17:25 +0200874 dom.viewport.style.setProperty( '--viewport-width', viewportWidth + 'px' );
875 dom.viewport.style.setProperty( '--viewport-height', viewportHeight + 'px' );
876
877 scrollView.layout();
Marc Kupietz09b75752023-10-07 09:32:19 +0200878
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200879 progress.update();
880 backgrounds.updateParallax();
881
882 if( overview.isActive() ) {
883 overview.update();
884 }
885
886 }
887
888 }
889
890 /**
891 * Applies layout logic to the contents of all slides in
892 * the presentation.
893 *
894 * @param {string|number} width
895 * @param {string|number} height
896 */
897 function layoutSlideContents( width, height ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200898 // Handle sizing of elements with the 'r-stretch' class
899 Util.queryAll( dom.slides, 'section > .stretch, section > .r-stretch' ).forEach( element => {
900
901 // Determine how much vertical space we can use
902 let remainingHeight = Util.getRemainingHeight( element, height );
903
904 // Consider the aspect ratio of media elements
905 if( /(img|video)/gi.test( element.nodeName ) ) {
906 const nw = element.naturalWidth || element.videoWidth,
907 nh = element.naturalHeight || element.videoHeight;
908
909 const es = Math.min( width / nw, remainingHeight / nh );
910
911 element.style.width = ( nw * es ) + 'px';
912 element.style.height = ( nh * es ) + 'px';
913
914 }
915 else {
916 element.style.width = width + 'px';
917 element.style.height = remainingHeight + 'px';
918 }
919
920 } );
921
922 }
923
924 /**
Marc Kupietz9c036a42024-05-14 13:17:25 +0200925 * Responsively activates the scroll mode when we reach the configured
926 * activation width.
927 */
928 function checkResponsiveScrollView() {
929
930 // Only proceed if...
931 // 1. The DOM is ready
932 // 2. Layouts aren't disabled via config
933 // 3. We're not currently printing
934 // 4. There is a scrollActivationWidth set
935 // 5. The deck isn't configured to always use the scroll view
936 if(
937 dom.wrapper &&
938 !config.disableLayout &&
939 !printView.isActive() &&
940 typeof config.scrollActivationWidth === 'number' &&
941 config.view !== 'scroll'
942 ) {
943 const size = getComputedSlideSize();
944
945 if( size.presentationWidth > 0 && size.presentationWidth <= config.scrollActivationWidth ) {
946 if( !scrollView.isActive() ) {
947 backgrounds.create();
948 scrollView.activate()
949 };
950 }
951 else {
952 if( scrollView.isActive() ) scrollView.deactivate();
953 }
954 }
955
956 }
957
958 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200959 * Calculates the computed pixel size of our slides. These
960 * values are based on the width and height configuration
961 * options.
962 *
963 * @param {number} [presentationWidth=dom.wrapper.offsetWidth]
964 * @param {number} [presentationHeight=dom.wrapper.offsetHeight]
965 */
966 function getComputedSlideSize( presentationWidth, presentationHeight ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +0200967
Marc Kupietz09b75752023-10-07 09:32:19 +0200968 let width = config.width;
969 let height = config.height;
970
971 if( config.disableLayout ) {
972 width = dom.slides.offsetWidth;
973 height = dom.slides.offsetHeight;
974 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200975
976 const size = {
977 // Slide size
Marc Kupietz09b75752023-10-07 09:32:19 +0200978 width: width,
979 height: height,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +0200980
981 // Presentation size
982 presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
983 presentationHeight: presentationHeight || dom.wrapper.offsetHeight
984 };
985
986 // Reduce available space by margin
987 size.presentationWidth -= ( size.presentationWidth * config.margin );
988 size.presentationHeight -= ( size.presentationHeight * config.margin );
989
990 // Slide width may be a percentage of available width
991 if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
992 size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
993 }
994
995 // Slide height may be a percentage of available height
996 if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
997 size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
998 }
999
1000 return size;
1001
1002 }
1003
1004 /**
1005 * Stores the vertical index of a stack so that the same
1006 * vertical slide can be selected when navigating to and
1007 * from the stack.
1008 *
1009 * @param {HTMLElement} stack The vertical stack element
1010 * @param {string|number} [v=0] Index to memorize
1011 */
1012 function setPreviousVerticalIndex( stack, v ) {
1013
1014 if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
1015 stack.setAttribute( 'data-previous-indexv', v || 0 );
1016 }
1017
1018 }
1019
1020 /**
1021 * Retrieves the vertical index which was stored using
1022 * #setPreviousVerticalIndex() or 0 if no previous index
1023 * exists.
1024 *
1025 * @param {HTMLElement} stack The vertical stack element
1026 */
1027 function getPreviousVerticalIndex( stack ) {
1028
1029 if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
1030 // Prefer manually defined start-indexv
1031 const attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
1032
1033 return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
1034 }
1035
1036 return 0;
1037
1038 }
1039
1040 /**
1041 * Checks if the current or specified slide is vertical
1042 * (nested within another slide).
1043 *
1044 * @param {HTMLElement} [slide=currentSlide] The slide to check
1045 * orientation of
1046 * @return {Boolean}
1047 */
1048 function isVerticalSlide( slide = currentSlide ) {
1049
1050 return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
1051
1052 }
1053
1054 /**
Marc Kupietz9c036a42024-05-14 13:17:25 +02001055 * Checks if the current or specified slide is a stack containing
1056 * vertical slides.
1057 *
1058 * @param {HTMLElement} [slide=currentSlide]
1059 * @return {Boolean}
1060 */
1061 function isVerticalStack( slide = currentSlide ) {
1062
1063 return slide.classList.contains( '.stack' ) || slide.querySelector( 'section' ) !== null;
1064
1065 }
1066
1067 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001068 * Returns true if we're on the last slide in the current
1069 * vertical stack.
1070 */
1071 function isLastVerticalSlide() {
1072
1073 if( currentSlide && isVerticalSlide( currentSlide ) ) {
1074 // Does this slide have a next sibling?
1075 if( currentSlide.nextElementSibling ) return false;
1076
1077 return true;
1078 }
1079
1080 return false;
1081
1082 }
1083
1084 /**
1085 * Returns true if we're currently on the first slide in
1086 * the presentation.
1087 */
1088 function isFirstSlide() {
1089
1090 return indexh === 0 && indexv === 0;
1091
1092 }
1093
1094 /**
1095 * Returns true if we're currently on the last slide in
Marc Kupietzcf6e9982026-08-15 15:37:40 +02001096 * the presentation. If the last slide is a stack, we only
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001097 * consider this the last slide if it's at the end of the
1098 * stack.
1099 */
1100 function isLastSlide() {
1101
1102 if( currentSlide ) {
1103 // Does this slide have a next sibling?
1104 if( currentSlide.nextElementSibling ) return false;
1105
1106 // If it's vertical, does its parent have a next sibling?
1107 if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
1108
1109 return true;
1110 }
1111
1112 return false;
1113
1114 }
1115
1116 /**
1117 * Enters the paused mode which fades everything on screen to
1118 * black.
1119 */
1120 function pause() {
1121
1122 if( config.pause ) {
1123 const wasPaused = dom.wrapper.classList.contains( 'paused' );
1124
1125 cancelAutoSlide();
1126 dom.wrapper.classList.add( 'paused' );
1127
1128 if( wasPaused === false ) {
1129 dispatchEvent({ type: 'paused' });
1130 }
1131 }
1132
1133 }
1134
1135 /**
1136 * Exits from the paused mode.
1137 */
1138 function resume() {
1139
1140 const wasPaused = dom.wrapper.classList.contains( 'paused' );
1141 dom.wrapper.classList.remove( 'paused' );
1142
1143 cueAutoSlide();
1144
1145 if( wasPaused ) {
1146 dispatchEvent({ type: 'resumed' });
1147 }
1148
1149 }
1150
1151 /**
1152 * Toggles the paused mode on and off.
1153 */
1154 function togglePause( override ) {
1155
1156 if( typeof override === 'boolean' ) {
1157 override ? pause() : resume();
1158 }
1159 else {
1160 isPaused() ? resume() : pause();
1161 }
1162
1163 }
1164
1165 /**
1166 * Checks if we are currently in the paused mode.
1167 *
1168 * @return {Boolean}
1169 */
1170 function isPaused() {
1171
1172 return dom.wrapper.classList.contains( 'paused' );
1173
1174 }
1175
1176 /**
Marc Kupietz09b75752023-10-07 09:32:19 +02001177 * Toggles visibility of the jump-to-slide UI.
1178 */
1179 function toggleJumpToSlide( override ) {
1180
1181 if( typeof override === 'boolean' ) {
1182 override ? jumpToSlide.show() : jumpToSlide.hide();
1183 }
1184 else {
1185 jumpToSlide.isVisible() ? jumpToSlide.hide() : jumpToSlide.show();
1186 }
1187
1188 }
1189
1190 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001191 * Toggles the auto slide mode on and off.
1192 *
1193 * @param {Boolean} [override] Flag which sets the desired state.
1194 * True means autoplay starts, false means it stops.
1195 */
1196
1197 function toggleAutoSlide( override ) {
1198
1199 if( typeof override === 'boolean' ) {
1200 override ? resumeAutoSlide() : pauseAutoSlide();
1201 }
1202
1203 else {
1204 autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
1205 }
1206
1207 }
1208
1209 /**
1210 * Checks if the auto slide mode is currently on.
1211 *
1212 * @return {Boolean}
1213 */
1214 function isAutoSliding() {
1215
1216 return !!( autoSlide && !autoSlidePaused );
1217
1218 }
1219
1220 /**
1221 * Steps from the current point in the presentation to the
1222 * slide which matches the specified horizontal and vertical
1223 * indices.
1224 *
1225 * @param {number} [h=indexh] Horizontal index of the target slide
1226 * @param {number} [v=indexv] Vertical index of the target slide
1227 * @param {number} [f] Index of a fragment within the
1228 * target slide to activate
Christophe Dervieux8afae132021-12-06 15:16:42 +01001229 * @param {number} [origin] Origin for use in multimaster environments
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001230 */
Christophe Dervieux8afae132021-12-06 15:16:42 +01001231 function slide( h, v, f, origin ) {
1232
Marc Kupietz09b75752023-10-07 09:32:19 +02001233 // Dispatch an event before the slide
Christophe Dervieux8afae132021-12-06 15:16:42 +01001234 const slidechange = dispatchEvent({
1235 type: 'beforeslidechange',
1236 data: {
1237 indexh: h === undefined ? indexh : h,
1238 indexv: v === undefined ? indexv : v,
1239 origin
1240 }
1241 });
1242
1243 // Abort if this slide change was prevented by an event listener
1244 if( slidechange.defaultPrevented ) return;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001245
1246 // Remember where we were at before
1247 previousSlide = currentSlide;
1248
1249 // Query all horizontal slides in the deck
1250 const horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
1251
Marc Kupietz9c036a42024-05-14 13:17:25 +02001252 // If we're in scroll mode, we scroll the target slide into view
1253 // instead of running our standard slide transition
1254 if( scrollView.isActive() ) {
1255 const scrollToSlide = scrollView.getSlideByIndices( h, v );
1256 if( scrollToSlide ) scrollView.scrollToSlide( scrollToSlide );
1257 return;
1258 }
1259
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001260 // Abort if there are no slides
1261 if( horizontalSlides.length === 0 ) return;
1262
1263 // If no vertical index is specified and the upcoming slide is a
1264 // stack, resume at its previous vertical index
1265 if( v === undefined && !overview.isActive() ) {
1266 v = getPreviousVerticalIndex( horizontalSlides[ h ] );
1267 }
1268
1269 // If we were on a vertical stack, remember what vertical index
1270 // it was on so we can resume at the same position when returning
1271 if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
1272 setPreviousVerticalIndex( previousSlide.parentNode, indexv );
1273 }
1274
1275 // Remember the state before this slide
1276 const stateBefore = state.concat();
1277
1278 // Reset the state array
1279 state.length = 0;
1280
1281 let indexhBefore = indexh || 0,
1282 indexvBefore = indexv || 0;
1283
1284 // Activate and transition to the new slide
1285 indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
1286 indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
1287
1288 // Dispatch an event if the slide changed
1289 let slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
1290
1291 // Ensure that the previous slide is never the same as the current
1292 if( !slideChanged ) previousSlide = null;
1293
1294 // Find the current horizontal slide and any possible vertical slides
1295 // within it
1296 let currentHorizontalSlide = horizontalSlides[ indexh ],
1297 currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
1298
Marc Kupietz9c036a42024-05-14 13:17:25 +02001299 // Indicate when we're on a vertical slide
1300 revealElement.classList.toggle( 'is-vertical-slide', currentVerticalSlides.length > 1 );
1301
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001302 // Store references to the previous and current slides
1303 currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
1304
1305 let autoAnimateTransition = false;
1306
1307 // Detect if we're moving between two auto-animated slides
1308 if( slideChanged && previousSlide && currentSlide && !overview.isActive() ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +02001309 transition = 'running';
1310
1311 autoAnimateTransition = shouldAutoAnimateBetween( previousSlide, currentSlide, indexhBefore, indexvBefore );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001312
1313 // If this is an auto-animated transition, we disable the
1314 // regular slide transition
1315 //
1316 // Note 20-03-2020:
1317 // This needs to happen before we update slide visibility,
1318 // otherwise transitions will still run in Safari.
Marc Kupietz9c036a42024-05-14 13:17:25 +02001319 if( autoAnimateTransition ) {
1320 dom.slides.classList.add( 'disable-slide-transitions' )
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001321 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001322 }
1323
1324 // Update the visibility of slides now that the indices have changed
1325 updateSlidesVisibility();
1326
1327 layout();
1328
1329 // Update the overview if it's currently active
1330 if( overview.isActive() ) {
1331 overview.update();
1332 }
1333
1334 // Show fragment, if specified
1335 if( typeof f !== 'undefined' ) {
1336 fragments.goto( f );
1337 }
1338
1339 // Solves an edge case where the previous slide maintains the
1340 // 'present' class when navigating between adjacent vertical
1341 // stacks
1342 if( previousSlide && previousSlide !== currentSlide ) {
1343 previousSlide.classList.remove( 'present' );
1344 previousSlide.setAttribute( 'aria-hidden', 'true' );
1345
1346 // Reset all slides upon navigate to home
1347 if( isFirstSlide() ) {
1348 // Launch async task
1349 setTimeout( () => {
1350 getVerticalStacks().forEach( slide => {
1351 setPreviousVerticalIndex( slide, 0 );
1352 } );
1353 }, 0 );
1354 }
1355 }
1356
1357 // Apply the new state
1358 stateLoop: for( let i = 0, len = state.length; i < len; i++ ) {
1359 // Check if this state existed on the previous slide. If it
1360 // did, we will avoid adding it repeatedly
1361 for( let j = 0; j < stateBefore.length; j++ ) {
1362 if( stateBefore[j] === state[i] ) {
1363 stateBefore.splice( j, 1 );
1364 continue stateLoop;
1365 }
1366 }
1367
1368 dom.viewport.classList.add( state[i] );
1369
1370 // Dispatch custom event matching the state's name
1371 dispatchEvent({ type: state[i] });
1372 }
1373
1374 // Clean up the remains of the previous state
1375 while( stateBefore.length ) {
1376 dom.viewport.classList.remove( stateBefore.pop() );
1377 }
1378
1379 if( slideChanged ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +02001380 dispatchSlideChanged( origin );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001381 }
1382
1383 // Handle embedded content
1384 if( slideChanged || !previousSlide ) {
1385 slideContent.stopEmbeddedContent( previousSlide );
1386 slideContent.startEmbeddedContent( currentSlide );
1387 }
1388
1389 // Announce the current slide contents to screen readers
1390 // Use animation frame to prevent getComputedStyle in getStatusText
1391 // from triggering layout mid-frame
1392 requestAnimationFrame( () => {
1393 announceStatus( getStatusText( currentSlide ) );
1394 });
1395
1396 progress.update();
1397 controls.update();
1398 notes.update();
1399 backgrounds.update();
1400 backgrounds.updateParallax();
1401 slideNumber.update();
1402 fragments.update();
1403
1404 // Update the URL hash
1405 location.writeURL();
1406
1407 cueAutoSlide();
1408
1409 // Auto-animation
1410 if( autoAnimateTransition ) {
1411
1412 setTimeout( () => {
1413 dom.slides.classList.remove( 'disable-slide-transitions' );
1414 }, 0 );
1415
1416 if( config.autoAnimate ) {
1417 // Run the auto-animation between our slides
1418 autoAnimate.run( previousSlide, currentSlide );
1419 }
1420
1421 }
1422
1423 }
1424
1425 /**
Marc Kupietz9c036a42024-05-14 13:17:25 +02001426 * Checks whether or not an auto-animation should take place between
1427 * the two given slides.
1428 *
1429 * @param {HTMLElement} fromSlide
1430 * @param {HTMLElement} toSlide
1431 * @param {number} indexhBefore
1432 * @param {number} indexvBefore
1433 *
1434 * @returns {boolean}
1435 */
1436 function shouldAutoAnimateBetween( fromSlide, toSlide, indexhBefore, indexvBefore ) {
1437
1438 return fromSlide.hasAttribute( 'data-auto-animate' ) && toSlide.hasAttribute( 'data-auto-animate' ) &&
1439 fromSlide.getAttribute( 'data-auto-animate-id' ) === toSlide.getAttribute( 'data-auto-animate-id' ) &&
1440 !( ( indexh > indexhBefore || indexv > indexvBefore ) ? toSlide : fromSlide ).hasAttribute( 'data-auto-animate-restart' );
1441
1442 }
1443
1444 /**
1445 * Called anytime a new slide should be activated while in the scroll
1446 * view. The active slide is the page that occupies the most space in
1447 * the scrollable viewport.
1448 *
1449 * @param {number} pageIndex
1450 * @param {HTMLElement} slideElement
1451 */
1452 function setCurrentScrollPage( slideElement, h, v ) {
1453
1454 let indexhBefore = indexh || 0;
1455
1456 indexh = h;
1457 indexv = v;
1458
1459 const slideChanged = currentSlide !== slideElement;
1460
1461 previousSlide = currentSlide;
1462 currentSlide = slideElement;
1463
1464 if( currentSlide && previousSlide ) {
1465 if( config.autoAnimate && shouldAutoAnimateBetween( previousSlide, currentSlide, indexhBefore, indexv ) ) {
1466 // Run the auto-animation between our slides
1467 autoAnimate.run( previousSlide, currentSlide );
1468 }
1469 }
1470
1471 // Start or stop embedded content like videos and iframes
1472 if( slideChanged ) {
1473 if( previousSlide ) {
1474 slideContent.stopEmbeddedContent( previousSlide );
1475 slideContent.stopEmbeddedContent( previousSlide.slideBackgroundElement );
1476 }
1477
1478 slideContent.startEmbeddedContent( currentSlide );
1479 slideContent.startEmbeddedContent( currentSlide.slideBackgroundElement );
1480 }
1481
1482 requestAnimationFrame( () => {
1483 announceStatus( getStatusText( currentSlide ) );
1484 });
1485
1486 dispatchSlideChanged();
1487
1488 }
1489
1490 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001491 * Syncs the presentation with the current DOM. Useful
1492 * when new slides or control elements are added or when
1493 * the configuration has changed.
1494 */
1495 function sync() {
1496
1497 // Subscribe to input
1498 removeEventListeners();
1499 addEventListeners();
1500
1501 // Force a layout to make sure the current config is accounted for
1502 layout();
1503
1504 // Reflect the current autoSlide value
1505 autoSlide = config.autoSlide;
1506
1507 // Start auto-sliding if it's enabled
1508 cueAutoSlide();
1509
1510 // Re-create all slide backgrounds
1511 backgrounds.create();
1512
1513 // Write the current hash to the URL
1514 location.writeURL();
1515
Marc Kupietz09b75752023-10-07 09:32:19 +02001516 if( config.sortFragmentsOnSync === true ) {
1517 fragments.sortAll();
1518 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001519
1520 controls.update();
1521 progress.update();
1522
1523 updateSlidesVisibility();
1524
1525 notes.update();
1526 notes.updateVisibility();
Marc Kupietzcf6e9982026-08-15 15:37:40 +02001527 overlay.update();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001528 backgrounds.update( true );
1529 slideNumber.update();
1530 slideContent.formatEmbeddedContent();
1531
1532 // Start or stop embedded content depending on global config
1533 if( config.autoPlayMedia === false ) {
1534 slideContent.stopEmbeddedContent( currentSlide, { unloadIframes: false } );
1535 }
1536 else {
1537 slideContent.startEmbeddedContent( currentSlide );
1538 }
1539
1540 if( overview.isActive() ) {
1541 overview.layout();
1542 }
1543
1544 }
1545
1546 /**
1547 * Updates reveal.js to keep in sync with new slide attributes. For
1548 * example, if you add a new `data-background-image` you can call
1549 * this to have reveal.js render the new background image.
1550 *
1551 * Similar to #sync() but more efficient when you only need to
1552 * refresh a specific slide.
1553 *
1554 * @param {HTMLElement} slide
1555 */
1556 function syncSlide( slide = currentSlide ) {
1557
1558 backgrounds.sync( slide );
1559 fragments.sync( slide );
1560
1561 slideContent.load( slide );
1562
1563 backgrounds.update();
1564 notes.update();
1565
1566 }
1567
1568 /**
1569 * Resets all vertical slides so that only the first
1570 * is visible.
1571 */
1572 function resetVerticalSlides() {
1573
1574 getHorizontalSlides().forEach( horizontalSlide => {
1575
1576 Util.queryAll( horizontalSlide, 'section' ).forEach( ( verticalSlide, y ) => {
1577
1578 if( y > 0 ) {
1579 verticalSlide.classList.remove( 'present' );
1580 verticalSlide.classList.remove( 'past' );
1581 verticalSlide.classList.add( 'future' );
1582 verticalSlide.setAttribute( 'aria-hidden', 'true' );
1583 }
1584
1585 } );
1586
1587 } );
1588
1589 }
1590
1591 /**
1592 * Randomly shuffles all slides in the deck.
1593 */
1594 function shuffle( slides = getHorizontalSlides() ) {
1595
1596 slides.forEach( ( slide, i ) => {
1597
1598 // Insert the slide next to a randomly picked sibling slide
1599 // slide. This may cause the slide to insert before itself,
1600 // but that's not an issue.
1601 let beforeSlide = slides[ Math.floor( Math.random() * slides.length ) ];
1602 if( beforeSlide.parentNode === slide.parentNode ) {
1603 slide.parentNode.insertBefore( slide, beforeSlide );
1604 }
1605
1606 // Randomize the order of vertical slides (if there are any)
1607 let verticalSlides = slide.querySelectorAll( 'section' );
1608 if( verticalSlides.length ) {
1609 shuffle( verticalSlides );
1610 }
1611
1612 } );
1613
1614 }
1615
1616 /**
1617 * Updates one dimension of slides by showing the slide
1618 * with the specified index.
1619 *
1620 * @param {string} selector A CSS selector that will fetch
1621 * the group of slides we are working with
1622 * @param {number} index The index of the slide that should be
1623 * shown
1624 *
1625 * @return {number} The index of the slide that is now shown,
1626 * might differ from the passed in index if it was out of
1627 * bounds.
1628 */
1629 function updateSlides( selector, index ) {
1630
1631 // Select all slides and convert the NodeList result to
1632 // an array
1633 let slides = Util.queryAll( dom.wrapper, selector ),
1634 slidesLength = slides.length;
1635
Marc Kupietz9c036a42024-05-14 13:17:25 +02001636 let printMode = scrollView.isActive() || printView.isActive();
Marc Kupietz09b75752023-10-07 09:32:19 +02001637 let loopedForwards = false;
1638 let loopedBackwards = false;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001639
1640 if( slidesLength ) {
1641
1642 // Should the index loop?
1643 if( config.loop ) {
Marc Kupietz09b75752023-10-07 09:32:19 +02001644 if( index >= slidesLength ) loopedForwards = true;
1645
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001646 index %= slidesLength;
1647
1648 if( index < 0 ) {
1649 index = slidesLength + index;
Marc Kupietz09b75752023-10-07 09:32:19 +02001650 loopedBackwards = true;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001651 }
1652 }
1653
1654 // Enforce max and minimum index bounds
1655 index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
1656
1657 for( let i = 0; i < slidesLength; i++ ) {
1658 let element = slides[i];
1659
1660 let reverse = config.rtl && !isVerticalSlide( element );
1661
1662 // Avoid .remove() with multiple args for IE11 support
1663 element.classList.remove( 'past' );
1664 element.classList.remove( 'present' );
1665 element.classList.remove( 'future' );
1666
1667 // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
1668 element.setAttribute( 'hidden', '' );
1669 element.setAttribute( 'aria-hidden', 'true' );
1670
1671 // If this element contains vertical slides
1672 if( element.querySelector( 'section' ) ) {
1673 element.classList.add( 'stack' );
1674 }
1675
1676 // If we're printing static slides, all slides are "present"
1677 if( printMode ) {
1678 element.classList.add( 'present' );
1679 continue;
1680 }
1681
1682 if( i < index ) {
1683 // Any element previous to index is given the 'past' class
1684 element.classList.add( reverse ? 'future' : 'past' );
1685
1686 if( config.fragments ) {
1687 // Show all fragments in prior slides
Marc Kupietz09b75752023-10-07 09:32:19 +02001688 showFragmentsIn( element );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001689 }
1690 }
1691 else if( i > index ) {
1692 // Any element subsequent to index is given the 'future' class
1693 element.classList.add( reverse ? 'past' : 'future' );
1694
1695 if( config.fragments ) {
1696 // Hide all fragments in future slides
Marc Kupietz09b75752023-10-07 09:32:19 +02001697 hideFragmentsIn( element );
1698 }
1699 }
1700 // Update the visibility of fragments when a presentation loops
1701 // in either direction
1702 else if( i === index && config.fragments ) {
1703 if( loopedForwards ) {
1704 hideFragmentsIn( element );
1705 }
1706 else if( loopedBackwards ) {
1707 showFragmentsIn( element );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001708 }
1709 }
1710 }
1711
1712 let slide = slides[index];
1713 let wasPresent = slide.classList.contains( 'present' );
1714
1715 // Mark the current slide as present
1716 slide.classList.add( 'present' );
1717 slide.removeAttribute( 'hidden' );
1718 slide.removeAttribute( 'aria-hidden' );
1719
1720 if( !wasPresent ) {
1721 // Dispatch an event indicating the slide is now visible
1722 dispatchEvent({
1723 target: slide,
1724 type: 'visible',
1725 bubbles: false
1726 });
1727 }
1728
1729 // If this slide has a state associated with it, add it
1730 // onto the current state of the deck
1731 let slideState = slide.getAttribute( 'data-state' );
1732 if( slideState ) {
1733 state = state.concat( slideState.split( ' ' ) );
1734 }
1735
1736 }
1737 else {
1738 // Since there are no slides we can't be anywhere beyond the
1739 // zeroth index
1740 index = 0;
1741 }
1742
1743 return index;
1744
1745 }
1746
1747 /**
Marc Kupietz9c036a42024-05-14 13:17:25 +02001748 * Shows all fragment elements within the given container.
Marc Kupietz09b75752023-10-07 09:32:19 +02001749 */
1750 function showFragmentsIn( container ) {
1751
1752 Util.queryAll( container, '.fragment' ).forEach( fragment => {
1753 fragment.classList.add( 'visible' );
1754 fragment.classList.remove( 'current-fragment' );
1755 } );
1756
1757 }
1758
1759 /**
Marc Kupietz9c036a42024-05-14 13:17:25 +02001760 * Hides all fragment elements within the given container.
Marc Kupietz09b75752023-10-07 09:32:19 +02001761 */
1762 function hideFragmentsIn( container ) {
1763
1764 Util.queryAll( container, '.fragment.visible' ).forEach( fragment => {
1765 fragment.classList.remove( 'visible', 'current-fragment' );
1766 } );
1767
1768 }
1769
1770 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001771 * Optimization method; hide all slides that are far away
1772 * from the present slide.
1773 */
1774 function updateSlidesVisibility() {
1775
1776 // Select all slides and convert the NodeList result to
1777 // an array
1778 let horizontalSlides = getHorizontalSlides(),
1779 horizontalSlidesLength = horizontalSlides.length,
1780 distanceX,
1781 distanceY;
1782
1783 if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
1784
1785 // The number of steps away from the present slide that will
1786 // be visible
1787 let viewDistance = overview.isActive() ? 10 : config.viewDistance;
1788
1789 // Shorten the view distance on devices that typically have
1790 // less resources
1791 if( Device.isMobile ) {
1792 viewDistance = overview.isActive() ? 6 : config.mobileViewDistance;
1793 }
1794
1795 // All slides need to be visible when exporting to PDF
Marc Kupietz9c036a42024-05-14 13:17:25 +02001796 if( printView.isActive() ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001797 viewDistance = Number.MAX_VALUE;
1798 }
1799
1800 for( let x = 0; x < horizontalSlidesLength; x++ ) {
1801 let horizontalSlide = horizontalSlides[x];
1802
1803 let verticalSlides = Util.queryAll( horizontalSlide, 'section' ),
1804 verticalSlidesLength = verticalSlides.length;
1805
1806 // Determine how far away this slide is from the present
1807 distanceX = Math.abs( ( indexh || 0 ) - x ) || 0;
1808
1809 // If the presentation is looped, distance should measure
1810 // 1 between the first and last slides
1811 if( config.loop ) {
1812 distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
1813 }
1814
1815 // Show the horizontal slide if it's within the view distance
1816 if( distanceX < viewDistance ) {
1817 slideContent.load( horizontalSlide );
1818 }
1819 else {
1820 slideContent.unload( horizontalSlide );
1821 }
1822
1823 if( verticalSlidesLength ) {
1824
1825 let oy = getPreviousVerticalIndex( horizontalSlide );
1826
1827 for( let y = 0; y < verticalSlidesLength; y++ ) {
1828 let verticalSlide = verticalSlides[y];
1829
1830 distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
1831
1832 if( distanceX + distanceY < viewDistance ) {
1833 slideContent.load( verticalSlide );
1834 }
1835 else {
1836 slideContent.unload( verticalSlide );
1837 }
1838 }
1839
1840 }
1841 }
1842
1843 // Flag if there are ANY vertical slides, anywhere in the deck
1844 if( hasVerticalSlides() ) {
1845 dom.wrapper.classList.add( 'has-vertical-slides' );
1846 }
1847 else {
1848 dom.wrapper.classList.remove( 'has-vertical-slides' );
1849 }
1850
1851 // Flag if there are ANY horizontal slides, anywhere in the deck
1852 if( hasHorizontalSlides() ) {
1853 dom.wrapper.classList.add( 'has-horizontal-slides' );
1854 }
1855 else {
1856 dom.wrapper.classList.remove( 'has-horizontal-slides' );
1857 }
1858
1859 }
1860
1861 }
1862
1863 /**
1864 * Determine what available routes there are for navigation.
1865 *
1866 * @return {{left: boolean, right: boolean, up: boolean, down: boolean}}
1867 */
1868 function availableRoutes({ includeFragments = false } = {}) {
1869
1870 let horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
1871 verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
1872
1873 let routes = {
1874 left: indexh > 0,
1875 right: indexh < horizontalSlides.length - 1,
1876 up: indexv > 0,
1877 down: indexv < verticalSlides.length - 1
1878 };
1879
1880 // Looped presentations can always be navigated as long as
1881 // there are slides available
1882 if( config.loop ) {
1883 if( horizontalSlides.length > 1 ) {
1884 routes.left = true;
1885 routes.right = true;
1886 }
1887
1888 if( verticalSlides.length > 1 ) {
1889 routes.up = true;
1890 routes.down = true;
1891 }
1892 }
1893
1894 if ( horizontalSlides.length > 1 && config.navigationMode === 'linear' ) {
1895 routes.right = routes.right || routes.down;
1896 routes.left = routes.left || routes.up;
1897 }
1898
1899 // If includeFragments is set, a route will be considered
Marc Kupietz09b75752023-10-07 09:32:19 +02001900 // available if either a slid OR fragment is available in
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001901 // the given direction
1902 if( includeFragments === true ) {
1903 let fragmentRoutes = fragments.availableRoutes();
1904 routes.left = routes.left || fragmentRoutes.prev;
1905 routes.up = routes.up || fragmentRoutes.prev;
1906 routes.down = routes.down || fragmentRoutes.next;
1907 routes.right = routes.right || fragmentRoutes.next;
1908 }
1909
1910 // Reverse horizontal controls for rtl
1911 if( config.rtl ) {
1912 let left = routes.left;
1913 routes.left = routes.right;
1914 routes.right = left;
1915 }
1916
1917 return routes;
1918
1919 }
1920
1921 /**
1922 * Returns the number of past slides. This can be used as a global
1923 * flattened index for slides.
1924 *
1925 * @param {HTMLElement} [slide=currentSlide] The slide we're counting before
1926 *
1927 * @return {number} Past slide count
1928 */
1929 function getSlidePastCount( slide = currentSlide ) {
1930
1931 let horizontalSlides = getHorizontalSlides();
1932
1933 // The number of past slides
1934 let pastCount = 0;
1935
1936 // Step through all slides and count the past ones
1937 mainLoop: for( let i = 0; i < horizontalSlides.length; i++ ) {
1938
1939 let horizontalSlide = horizontalSlides[i];
1940 let verticalSlides = horizontalSlide.querySelectorAll( 'section' );
1941
1942 for( let j = 0; j < verticalSlides.length; j++ ) {
1943
1944 // Stop as soon as we arrive at the present
1945 if( verticalSlides[j] === slide ) {
1946 break mainLoop;
1947 }
1948
1949 // Don't count slides with the "uncounted" class
1950 if( verticalSlides[j].dataset.visibility !== 'uncounted' ) {
1951 pastCount++;
1952 }
1953
1954 }
1955
1956 // Stop as soon as we arrive at the present
1957 if( horizontalSlide === slide ) {
1958 break;
1959 }
1960
1961 // Don't count the wrapping section for vertical slides and
1962 // slides marked as uncounted
1963 if( horizontalSlide.classList.contains( 'stack' ) === false && horizontalSlide.dataset.visibility !== 'uncounted' ) {
1964 pastCount++;
1965 }
1966
1967 }
1968
1969 return pastCount;
1970
1971 }
1972
1973 /**
1974 * Returns a value ranging from 0-1 that represents
1975 * how far into the presentation we have navigated.
1976 *
1977 * @return {number}
1978 */
1979 function getProgress() {
1980
1981 // The number of past and total slides
1982 let totalCount = getTotalSlides();
1983 let pastCount = getSlidePastCount();
1984
1985 if( currentSlide ) {
1986
1987 let allFragments = currentSlide.querySelectorAll( '.fragment' );
1988
1989 // If there are fragments in the current slide those should be
1990 // accounted for in the progress.
1991 if( allFragments.length > 0 ) {
1992 let visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
1993
1994 // This value represents how big a portion of the slide progress
1995 // that is made up by its fragments (0-1)
1996 let fragmentWeight = 0.9;
1997
1998 // Add fragment progress to the past slide count
1999 pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
2000 }
2001
2002 }
2003
2004 return Math.min( pastCount / ( totalCount - 1 ), 1 );
2005
2006 }
2007
2008 /**
2009 * Retrieves the h/v location and fragment of the current,
2010 * or specified, slide.
2011 *
2012 * @param {HTMLElement} [slide] If specified, the returned
2013 * index will be for this slide rather than the currently
2014 * active one
2015 *
2016 * @return {{h: number, v: number, f: number}}
2017 */
2018 function getIndices( slide ) {
2019
2020 // By default, return the current indices
2021 let h = indexh,
2022 v = indexv,
2023 f;
2024
2025 // If a slide is specified, return the indices of that slide
2026 if( slide ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +02002027 // In scroll mode the original h/x index is stored on the slide
2028 if( scrollView.isActive() ) {
2029 h = parseInt( slide.getAttribute( 'data-index-h' ), 10 );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002030
Marc Kupietz9c036a42024-05-14 13:17:25 +02002031 if( slide.getAttribute( 'data-index-v' ) ) {
2032 v = parseInt( slide.getAttribute( 'data-index-v' ), 10 );
2033 }
2034 }
2035 else {
2036 let isVertical = isVerticalSlide( slide );
2037 let slideh = isVertical ? slide.parentNode : slide;
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002038
Marc Kupietz9c036a42024-05-14 13:17:25 +02002039 // Select all horizontal slides
2040 let horizontalSlides = getHorizontalSlides();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002041
Marc Kupietz9c036a42024-05-14 13:17:25 +02002042 // Now that we know which the horizontal slide is, get its index
2043 h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002044
Marc Kupietz9c036a42024-05-14 13:17:25 +02002045 // Assume we're not vertical
2046 v = undefined;
2047
2048 // If this is a vertical slide, grab the vertical index
2049 if( isVertical ) {
2050 v = Math.max( Util.queryAll( slide.parentNode, 'section' ).indexOf( slide ), 0 );
2051 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002052 }
2053 }
2054
2055 if( !slide && currentSlide ) {
2056 let hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
2057 if( hasFragments ) {
2058 let currentFragment = currentSlide.querySelector( '.current-fragment' );
2059 if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
2060 f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
2061 }
2062 else {
2063 f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
2064 }
2065 }
2066 }
2067
2068 return { h, v, f };
2069
2070 }
2071
2072 /**
2073 * Retrieves all slides in this presentation.
2074 */
2075 function getSlides() {
2076
2077 return Util.queryAll( dom.wrapper, SLIDES_SELECTOR + ':not(.stack):not([data-visibility="uncounted"])' );
2078
2079 }
2080
2081 /**
2082 * Returns a list of all horizontal slides in the deck. Each
2083 * vertical stack is included as one horizontal slide in the
2084 * resulting array.
2085 */
2086 function getHorizontalSlides() {
2087
2088 return Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR );
2089
2090 }
2091
2092 /**
2093 * Returns all vertical slides that exist within this deck.
2094 */
2095 function getVerticalSlides() {
2096
2097 return Util.queryAll( dom.wrapper, '.slides>section>section' );
2098
2099 }
2100
2101 /**
2102 * Returns all vertical stacks (each stack can contain multiple slides).
2103 */
2104 function getVerticalStacks() {
2105
2106 return Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR + '.stack');
2107
2108 }
2109
2110 /**
2111 * Returns true if there are at least two horizontal slides.
2112 */
2113 function hasHorizontalSlides() {
2114
2115 return getHorizontalSlides().length > 1;
2116 }
2117
2118 /**
2119 * Returns true if there are at least two vertical slides.
2120 */
2121 function hasVerticalSlides() {
2122
2123 return getVerticalSlides().length > 1;
2124
2125 }
2126
2127 /**
2128 * Returns an array of objects where each object represents the
2129 * attributes on its respective slide.
2130 */
2131 function getSlidesAttributes() {
2132
2133 return getSlides().map( slide => {
2134
2135 let attributes = {};
2136 for( let i = 0; i < slide.attributes.length; i++ ) {
2137 let attribute = slide.attributes[ i ];
2138 attributes[ attribute.name ] = attribute.value;
2139 }
2140 return attributes;
2141
2142 } );
2143
2144 }
2145
2146 /**
2147 * Retrieves the total number of slides in this presentation.
2148 *
2149 * @return {number}
2150 */
2151 function getTotalSlides() {
2152
2153 return getSlides().length;
2154
2155 }
2156
2157 /**
2158 * Returns the slide element matching the specified index.
2159 *
2160 * @return {HTMLElement}
2161 */
2162 function getSlide( x, y ) {
2163
2164 let horizontalSlide = getHorizontalSlides()[ x ];
2165 let verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
2166
2167 if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
2168 return verticalSlides ? verticalSlides[ y ] : undefined;
2169 }
2170
2171 return horizontalSlide;
2172
2173 }
2174
2175 /**
2176 * Returns the background element for the given slide.
2177 * All slides, even the ones with no background properties
2178 * defined, have a background element so as long as the
2179 * index is valid an element will be returned.
2180 *
2181 * @param {mixed} x Horizontal background index OR a slide
2182 * HTML element
2183 * @param {number} y Vertical background index
2184 * @return {(HTMLElement[]|*)}
2185 */
2186 function getSlideBackground( x, y ) {
2187
2188 let slide = typeof x === 'number' ? getSlide( x, y ) : x;
2189 if( slide ) {
2190 return slide.slideBackgroundElement;
2191 }
2192
2193 return undefined;
2194
2195 }
2196
2197 /**
2198 * Retrieves the current state of the presentation as
2199 * an object. This state can then be restored at any
2200 * time.
2201 *
2202 * @return {{indexh: number, indexv: number, indexf: number, paused: boolean, overview: boolean}}
2203 */
2204 function getState() {
2205
2206 let indices = getIndices();
2207
2208 return {
2209 indexh: indices.h,
2210 indexv: indices.v,
2211 indexf: indices.f,
2212 paused: isPaused(),
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002213 overview: overview.isActive(),
2214 ...overlay.getState()
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002215 };
2216
2217 }
2218
2219 /**
2220 * Restores the presentation to the given state.
2221 *
2222 * @param {object} state As generated by getState()
2223 * @see {@link getState} generates the parameter `state`
2224 */
2225 function setState( state ) {
2226
2227 if( typeof state === 'object' ) {
2228 slide( Util.deserialize( state.indexh ), Util.deserialize( state.indexv ), Util.deserialize( state.indexf ) );
2229
2230 let pausedFlag = Util.deserialize( state.paused ),
2231 overviewFlag = Util.deserialize( state.overview );
2232
2233 if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
2234 togglePause( pausedFlag );
2235 }
2236
2237 if( typeof overviewFlag === 'boolean' && overviewFlag !== overview.isActive() ) {
2238 overview.toggle( overviewFlag );
2239 }
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002240
2241 overlay.setState( state );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002242 }
2243
2244 }
2245
2246 /**
2247 * Cues a new automated slide if enabled in the config.
2248 */
2249 function cueAutoSlide() {
2250
2251 cancelAutoSlide();
2252
2253 if( currentSlide && config.autoSlide !== false ) {
2254
Marc Kupietz09b75752023-10-07 09:32:19 +02002255 let fragment = currentSlide.querySelector( '.current-fragment[data-autoslide]' );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002256
2257 let fragmentAutoSlide = fragment ? fragment.getAttribute( 'data-autoslide' ) : null;
2258 let parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
2259 let slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
2260
2261 // Pick value in the following priority order:
2262 // 1. Current fragment's data-autoslide
2263 // 2. Current slide's data-autoslide
2264 // 3. Parent slide's data-autoslide
2265 // 4. Global autoSlide setting
2266 if( fragmentAutoSlide ) {
2267 autoSlide = parseInt( fragmentAutoSlide, 10 );
2268 }
2269 else if( slideAutoSlide ) {
2270 autoSlide = parseInt( slideAutoSlide, 10 );
2271 }
2272 else if( parentAutoSlide ) {
2273 autoSlide = parseInt( parentAutoSlide, 10 );
2274 }
2275 else {
2276 autoSlide = config.autoSlide;
2277
2278 // If there are media elements with data-autoplay,
2279 // automatically set the autoSlide duration to the
2280 // length of that media. Not applicable if the slide
2281 // is divided up into fragments.
2282 // playbackRate is accounted for in the duration.
2283 if( currentSlide.querySelectorAll( '.fragment' ).length === 0 ) {
2284 Util.queryAll( currentSlide, 'video, audio' ).forEach( el => {
2285 if( el.hasAttribute( 'data-autoplay' ) ) {
2286 if( autoSlide && (el.duration * 1000 / el.playbackRate ) > autoSlide ) {
2287 autoSlide = ( el.duration * 1000 / el.playbackRate ) + 1000;
2288 }
2289 }
2290 } );
2291 }
2292 }
2293
2294 // Cue the next auto-slide if:
2295 // - There is an autoSlide value
2296 // - Auto-sliding isn't paused by the user
2297 // - The presentation isn't paused
2298 // - The overview isn't active
2299 // - The presentation isn't over
2300 if( autoSlide && !autoSlidePaused && !isPaused() && !overview.isActive() && ( !isLastSlide() || fragments.availableRoutes().next || config.loop === true ) ) {
2301 autoSlideTimeout = setTimeout( () => {
2302 if( typeof config.autoSlideMethod === 'function' ) {
2303 config.autoSlideMethod()
2304 }
2305 else {
2306 navigateNext();
2307 }
2308 cueAutoSlide();
2309 }, autoSlide );
2310 autoSlideStartTime = Date.now();
2311 }
2312
2313 if( autoSlidePlayer ) {
2314 autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
2315 }
2316
2317 }
2318
2319 }
2320
2321 /**
2322 * Cancels any ongoing request to auto-slide.
2323 */
2324 function cancelAutoSlide() {
2325
2326 clearTimeout( autoSlideTimeout );
2327 autoSlideTimeout = -1;
2328
2329 }
2330
2331 function pauseAutoSlide() {
2332
2333 if( autoSlide && !autoSlidePaused ) {
2334 autoSlidePaused = true;
2335 dispatchEvent({ type: 'autoslidepaused' });
2336 clearTimeout( autoSlideTimeout );
2337
2338 if( autoSlidePlayer ) {
2339 autoSlidePlayer.setPlaying( false );
2340 }
2341 }
2342
2343 }
2344
2345 function resumeAutoSlide() {
2346
2347 if( autoSlide && autoSlidePaused ) {
2348 autoSlidePaused = false;
2349 dispatchEvent({ type: 'autoslideresumed' });
2350 cueAutoSlide();
2351 }
2352
2353 }
2354
Christophe Dervieux8afae132021-12-06 15:16:42 +01002355 function navigateLeft({skipFragments=false}={}) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002356
2357 navigationHistory.hasNavigatedHorizontally = true;
2358
Marc Kupietz9c036a42024-05-14 13:17:25 +02002359 // Scroll view navigation is handled independently
2360 if( scrollView.isActive() ) return scrollView.prev();
2361
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002362 // Reverse for RTL
2363 if( config.rtl ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +01002364 if( ( overview.isActive() || skipFragments || fragments.next() === false ) && availableRoutes().left ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002365 slide( indexh + 1, config.navigationMode === 'grid' ? indexv : undefined );
2366 }
2367 }
2368 // Normal navigation
Christophe Dervieux8afae132021-12-06 15:16:42 +01002369 else if( ( overview.isActive() || skipFragments || fragments.prev() === false ) && availableRoutes().left ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002370 slide( indexh - 1, config.navigationMode === 'grid' ? indexv : undefined );
2371 }
2372
2373 }
2374
Christophe Dervieux8afae132021-12-06 15:16:42 +01002375 function navigateRight({skipFragments=false}={}) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002376
2377 navigationHistory.hasNavigatedHorizontally = true;
2378
Marc Kupietz9c036a42024-05-14 13:17:25 +02002379 // Scroll view navigation is handled independently
2380 if( scrollView.isActive() ) return scrollView.next();
2381
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002382 // Reverse for RTL
2383 if( config.rtl ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +01002384 if( ( overview.isActive() || skipFragments || fragments.prev() === false ) && availableRoutes().right ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002385 slide( indexh - 1, config.navigationMode === 'grid' ? indexv : undefined );
2386 }
2387 }
2388 // Normal navigation
Christophe Dervieux8afae132021-12-06 15:16:42 +01002389 else if( ( overview.isActive() || skipFragments || fragments.next() === false ) && availableRoutes().right ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002390 slide( indexh + 1, config.navigationMode === 'grid' ? indexv : undefined );
2391 }
2392
2393 }
2394
Christophe Dervieux8afae132021-12-06 15:16:42 +01002395 function navigateUp({skipFragments=false}={}) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002396
Marc Kupietz9c036a42024-05-14 13:17:25 +02002397 // Scroll view navigation is handled independently
2398 if( scrollView.isActive() ) return scrollView.prev();
2399
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002400 // Prioritize hiding fragments
Christophe Dervieux8afae132021-12-06 15:16:42 +01002401 if( ( overview.isActive() || skipFragments || fragments.prev() === false ) && availableRoutes().up ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002402 slide( indexh, indexv - 1 );
2403 }
2404
2405 }
2406
Christophe Dervieux8afae132021-12-06 15:16:42 +01002407 function navigateDown({skipFragments=false}={}) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002408
2409 navigationHistory.hasNavigatedVertically = true;
2410
Marc Kupietz9c036a42024-05-14 13:17:25 +02002411 // Scroll view navigation is handled independently
2412 if( scrollView.isActive() ) return scrollView.next();
2413
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002414 // Prioritize revealing fragments
Christophe Dervieux8afae132021-12-06 15:16:42 +01002415 if( ( overview.isActive() || skipFragments || fragments.next() === false ) && availableRoutes().down ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002416 slide( indexh, indexv + 1 );
2417 }
2418
2419 }
2420
2421 /**
2422 * Navigates backwards, prioritized in the following order:
2423 * 1) Previous fragment
2424 * 2) Previous vertical slide
2425 * 3) Previous horizontal slide
2426 */
Christophe Dervieux8afae132021-12-06 15:16:42 +01002427 function navigatePrev({skipFragments=false}={}) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002428
Marc Kupietz9c036a42024-05-14 13:17:25 +02002429 // Scroll view navigation is handled independently
2430 if( scrollView.isActive() ) return scrollView.prev();
2431
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002432 // Prioritize revealing fragments
Christophe Dervieux8afae132021-12-06 15:16:42 +01002433 if( skipFragments || fragments.prev() === false ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002434 if( availableRoutes().up ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +01002435 navigateUp({skipFragments});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002436 }
2437 else {
2438 // Fetch the previous horizontal slide, if there is one
2439 let previousSlide;
2440
2441 if( config.rtl ) {
2442 previousSlide = Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR + '.future' ).pop();
2443 }
2444 else {
2445 previousSlide = Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR + '.past' ).pop();
2446 }
2447
Christophe Dervieux8afae132021-12-06 15:16:42 +01002448 // When going backwards and arriving on a stack we start
2449 // at the bottom of the stack
2450 if( previousSlide && previousSlide.classList.contains( 'stack' ) ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002451 let v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
2452 let h = indexh - 1;
2453 slide( h, v );
2454 }
Marc Kupietz9c036a42024-05-14 13:17:25 +02002455 else if( config.rtl ) {
2456 navigateRight({skipFragments});
2457 }
Christophe Dervieux8afae132021-12-06 15:16:42 +01002458 else {
2459 navigateLeft({skipFragments});
2460 }
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002461 }
2462 }
2463
2464 }
2465
2466 /**
2467 * The reverse of #navigatePrev().
2468 */
Christophe Dervieux8afae132021-12-06 15:16:42 +01002469 function navigateNext({skipFragments=false}={}) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002470
2471 navigationHistory.hasNavigatedHorizontally = true;
2472 navigationHistory.hasNavigatedVertically = true;
2473
Marc Kupietz9c036a42024-05-14 13:17:25 +02002474 // Scroll view navigation is handled independently
2475 if( scrollView.isActive() ) return scrollView.next();
2476
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002477 // Prioritize revealing fragments
Christophe Dervieux8afae132021-12-06 15:16:42 +01002478 if( skipFragments || fragments.next() === false ) {
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002479
2480 let routes = availableRoutes();
2481
2482 // When looping is enabled `routes.down` is always available
2483 // so we need a separate check for when we've reached the
2484 // end of a stack and should move horizontally
2485 if( routes.down && routes.right && config.loop && isLastVerticalSlide() ) {
2486 routes.down = false;
2487 }
2488
2489 if( routes.down ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +01002490 navigateDown({skipFragments});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002491 }
2492 else if( config.rtl ) {
Christophe Dervieux8afae132021-12-06 15:16:42 +01002493 navigateLeft({skipFragments});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002494 }
2495 else {
Christophe Dervieux8afae132021-12-06 15:16:42 +01002496 navigateRight({skipFragments});
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002497 }
2498 }
2499
2500 }
2501
2502
2503 // --------------------------------------------------------------------//
2504 // ----------------------------- EVENTS -------------------------------//
2505 // --------------------------------------------------------------------//
2506
2507 /**
2508 * Called by all event handlers that are based on user
2509 * input.
2510 *
2511 * @param {object} [event]
2512 */
2513 function onUserInput( event ) {
2514
2515 if( config.autoSlideStoppable ) {
2516 pauseAutoSlide();
2517 }
2518
2519 }
2520
2521 /**
Marc Kupietz09b75752023-10-07 09:32:19 +02002522 * Listener for post message events posted to this window.
2523 */
2524 function onPostMessage( event ) {
2525
2526 let data = event.data;
2527
2528 // Make sure we're dealing with JSON
2529 if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
2530 data = JSON.parse( data );
2531
2532 // Check if the requested method can be found
2533 if( data.method && typeof Reveal[data.method] === 'function' ) {
2534
2535 if( POST_MESSAGE_METHOD_BLACKLIST.test( data.method ) === false ) {
2536
2537 const result = Reveal[data.method].apply( Reveal, data.args );
2538
2539 // Dispatch a postMessage event with the returned value from
2540 // our method invocation for getter functions
2541 dispatchPostMessage( 'callback', { method: data.method, result: result } );
2542
2543 }
2544 else {
2545 console.warn( 'reveal.js: "'+ data.method +'" is is blacklisted from the postMessage API' );
2546 }
2547
2548 }
2549 }
2550
2551 }
2552
2553 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002554 * Event listener for transition end on the current slide.
2555 *
2556 * @param {object} [event]
2557 */
2558 function onTransitionEnd( event ) {
2559
2560 if( transition === 'running' && /section/gi.test( event.target.nodeName ) ) {
2561 transition = 'idle';
2562 dispatchEvent({
2563 type: 'slidetransitionend',
2564 data: { indexh, indexv, previousSlide, currentSlide }
2565 });
2566 }
2567
2568 }
2569
2570 /**
Christophe Dervieux8afae132021-12-06 15:16:42 +01002571 * A global listener for all click events inside of the
2572 * .slides container.
2573 *
2574 * @param {object} [event]
2575 */
2576 function onSlidesClicked( event ) {
2577
2578 const anchor = Util.closest( event.target, 'a[href^="#"]' );
2579
2580 // If a hash link is clicked, we find the target slide
2581 // and navigate to it. We previously relied on 'hashchange'
2582 // for links like these but that prevented media with
2583 // audio tracks from playing in mobile browsers since it
2584 // wasn't considered a direct interaction with the document.
2585 if( anchor ) {
2586 const hash = anchor.getAttribute( 'href' );
2587 const indices = location.getIndicesFromHash( hash );
2588
2589 if( indices ) {
2590 Reveal.slide( indices.h, indices.v, indices.f );
2591 event.preventDefault();
2592 }
2593 }
2594
2595 }
2596
2597 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002598 * Handler for the window level 'resize' event.
2599 *
2600 * @param {object} [event]
2601 */
2602 function onWindowResize( event ) {
2603
2604 layout();
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002605 }
2606
2607 /**
2608 * Handle for the window level 'visibilitychange' event.
2609 *
2610 * @param {object} [event]
2611 */
2612 function onPageVisibilityChange( event ) {
2613
2614 // If, after clicking a link or similar and we're coming back,
2615 // focus the document.body to ensure we can use keyboard shortcuts
2616 if( document.hidden === false && document.activeElement !== document.body ) {
2617 // Not all elements support .blur() - SVGs among them.
2618 if( typeof document.activeElement.blur === 'function' ) {
2619 document.activeElement.blur();
2620 }
2621 document.body.focus();
2622 }
2623
2624 }
2625
2626 /**
Marc Kupietz09b75752023-10-07 09:32:19 +02002627 * Handler for the document level 'fullscreenchange' event.
2628 *
2629 * @param {object} [event]
2630 */
2631 function onFullscreenChange( event ) {
2632
2633 let element = document.fullscreenElement || document.webkitFullscreenElement;
2634 if( element === dom.wrapper ) {
2635 event.stopImmediatePropagation();
2636
2637 // Timeout to avoid layout shift in Safari
2638 setTimeout( () => {
2639 Reveal.layout();
2640 Reveal.focus.focus(); // focus.focus :'(
2641 }, 1 );
2642 }
2643
2644 }
2645
2646 /**
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002647 * Handles click on the auto-sliding controls element.
2648 *
2649 * @param {object} [event]
2650 */
2651 function onAutoSlidePlayerClick( event ) {
2652
2653 // Replay
2654 if( isLastSlide() && config.loop === false ) {
2655 slide( 0, 0 );
2656 resumeAutoSlide();
2657 }
2658 // Resume
2659 else if( autoSlidePaused ) {
2660 resumeAutoSlide();
2661 }
2662 // Pause
2663 else {
2664 pauseAutoSlide();
2665 }
2666
2667 }
2668
2669
2670 // --------------------------------------------------------------------//
2671 // ------------------------------- API --------------------------------//
2672 // --------------------------------------------------------------------//
2673
2674 // The public reveal.js API
2675 const API = {
2676 VERSION,
2677
2678 initialize,
2679 configure,
Marc Kupietz09b75752023-10-07 09:32:19 +02002680 destroy,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002681
2682 sync,
2683 syncSlide,
2684 syncFragments: fragments.sync.bind( fragments ),
2685
2686 // Navigation methods
2687 slide,
2688 left: navigateLeft,
2689 right: navigateRight,
2690 up: navigateUp,
2691 down: navigateDown,
2692 prev: navigatePrev,
2693 next: navigateNext,
2694
2695 // Navigation aliases
2696 navigateLeft, navigateRight, navigateUp, navigateDown, navigatePrev, navigateNext,
2697
2698 // Fragment methods
2699 navigateFragment: fragments.goto.bind( fragments ),
2700 prevFragment: fragments.prev.bind( fragments ),
2701 nextFragment: fragments.next.bind( fragments ),
2702
2703 // Event binding
2704 on,
2705 off,
2706
2707 // Legacy event binding methods left in for backwards compatibility
2708 addEventListener: on,
2709 removeEventListener: off,
2710
2711 // Forces an update in slide layout
2712 layout,
2713
2714 // Randomizes the order of slides
2715 shuffle,
2716
2717 // Returns an object with the available routes as booleans (left/right/top/bottom)
2718 availableRoutes,
2719
2720 // Returns an object with the available fragments as booleans (prev/next)
2721 availableFragments: fragments.availableRoutes.bind( fragments ),
2722
2723 // Toggles a help overlay with keyboard shortcuts
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002724 toggleHelp: overlay.toggleHelp.bind( overlay ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002725
2726 // Toggles the overview mode on/off
2727 toggleOverview: overview.toggle.bind( overview ),
2728
Marc Kupietz9c036a42024-05-14 13:17:25 +02002729 // Toggles the scroll view on/off
2730 toggleScrollView: scrollView.toggle.bind( scrollView ),
2731
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002732 // Toggles the "black screen" mode on/off
2733 togglePause,
2734
2735 // Toggles the auto slide mode on/off
2736 toggleAutoSlide,
2737
Marc Kupietz09b75752023-10-07 09:32:19 +02002738 // Toggles visibility of the jump-to-slide UI
2739 toggleJumpToSlide,
2740
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002741 // Slide navigation checks
2742 isFirstSlide,
2743 isLastSlide,
2744 isLastVerticalSlide,
2745 isVerticalSlide,
Marc Kupietz9c036a42024-05-14 13:17:25 +02002746 isVerticalStack,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002747
2748 // State checks
2749 isPaused,
2750 isAutoSliding,
2751 isSpeakerNotes: notes.isSpeakerNotesWindow.bind( notes ),
2752 isOverview: overview.isActive.bind( overview ),
2753 isFocused: focus.isFocused.bind( focus ),
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002754 isOverlayOpen: overlay.isOpen.bind( overlay ),
Marc Kupietz9c036a42024-05-14 13:17:25 +02002755 isScrollView: scrollView.isActive.bind( scrollView ),
2756 isPrintView: printView.isActive.bind( printView ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002757
2758 // Checks if reveal.js has been loaded and is ready for use
2759 isReady: () => ready,
2760
2761 // Slide preloading
2762 loadSlide: slideContent.load.bind( slideContent ),
2763 unloadSlide: slideContent.unload.bind( slideContent ),
2764
Marc Kupietz9c036a42024-05-14 13:17:25 +02002765 // Start/stop all media inside of the current slide
Marc Kupietz09b75752023-10-07 09:32:19 +02002766 startEmbeddedContent: () => slideContent.startEmbeddedContent( currentSlide ),
2767 stopEmbeddedContent: () => slideContent.stopEmbeddedContent( currentSlide, { unloadIframes: false } ),
2768
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002769 // Lightbox previews
2770 previewIframe: overlay.previewIframe.bind( overlay ),
2771 previewImage: overlay.previewImage.bind( overlay ),
2772 previewVideo: overlay.previewVideo.bind( overlay ),
2773
2774 showPreview: overlay.previewIframe.bind( overlay ), // deprecated in favor of showIframeLightbox
2775 hidePreview: overlay.close.bind( overlay ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002776
2777 // Adds or removes all internal event listeners
2778 addEventListeners,
2779 removeEventListeners,
2780 dispatchEvent,
2781
2782 // Facility for persisting and restoring the presentation state
2783 getState,
2784 setState,
2785
2786 // Presentation progress on range of 0-1
2787 getProgress,
2788
2789 // Returns the indices of the current, or specified, slide
2790 getIndices,
2791
2792 // Returns an Array of key:value maps of the attributes of each
2793 // slide in the deck
2794 getSlidesAttributes,
2795
2796 // Returns the number of slides that we have passed
2797 getSlidePastCount,
2798
2799 // Returns the total number of slides
2800 getTotalSlides,
2801
2802 // Returns the slide element at the specified index
2803 getSlide,
2804
2805 // Returns the previous slide element, may be null
2806 getPreviousSlide: () => previousSlide,
2807
2808 // Returns the current slide element
2809 getCurrentSlide: () => currentSlide,
2810
2811 // Returns the slide background element at the specified index
2812 getSlideBackground,
2813
2814 // Returns the speaker notes string for a slide, or null
2815 getSlideNotes: notes.getSlideNotes.bind( notes ),
2816
2817 // Returns an Array of all slides
2818 getSlides,
2819
2820 // Returns an array with all horizontal/vertical slides in the deck
2821 getHorizontalSlides,
2822 getVerticalSlides,
2823
2824 // Checks if the presentation contains two or more horizontal
2825 // and vertical slides
2826 hasHorizontalSlides,
2827 hasVerticalSlides,
2828
2829 // Checks if the deck has navigated on either axis at least once
2830 hasNavigatedHorizontally: () => navigationHistory.hasNavigatedHorizontally,
2831 hasNavigatedVertically: () => navigationHistory.hasNavigatedVertically,
2832
Marc Kupietz9c036a42024-05-14 13:17:25 +02002833 shouldAutoAnimateBetween,
2834
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002835 // Adds/removes a custom key binding
2836 addKeyBinding: keyboard.addKeyBinding.bind( keyboard ),
2837 removeKeyBinding: keyboard.removeKeyBinding.bind( keyboard ),
2838
2839 // Programmatically triggers a keyboard event
2840 triggerKey: keyboard.triggerKey.bind( keyboard ),
2841
2842 // Registers a new shortcut to include in the help overlay
2843 registerKeyboardShortcut: keyboard.registerKeyboardShortcut.bind( keyboard ),
2844
2845 getComputedSlideSize,
Marc Kupietz9c036a42024-05-14 13:17:25 +02002846 setCurrentScrollPage,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002847
2848 // Returns the current scale of the presentation content
2849 getScale: () => scale,
2850
2851 // Returns the current configuration object
2852 getConfig: () => config,
2853
2854 // Helper method, retrieves query string as a key:value map
2855 getQueryHash: Util.getQueryHash,
2856
Marc Kupietz09b75752023-10-07 09:32:19 +02002857 // Returns the path to the current slide as represented in the URL
2858 getSlidePath: location.getHash.bind( location ),
2859
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002860 // Returns reveal.js DOM elements
2861 getRevealElement: () => revealElement,
2862 getSlidesElement: () => dom.slides,
2863 getViewportElement: () => dom.viewport,
2864 getBackgroundsElement: () => backgrounds.element,
2865
2866 // API for registering and retrieving plugins
2867 registerPlugin: plugins.registerPlugin.bind( plugins ),
2868 hasPlugin: plugins.hasPlugin.bind( plugins ),
2869 getPlugin: plugins.getPlugin.bind( plugins ),
2870 getPlugins: plugins.getRegisteredPlugins.bind( plugins )
2871
2872 };
2873
2874 // Our internal API which controllers have access to
2875 Util.extend( Reveal, {
2876 ...API,
2877
2878 // Methods for announcing content to screen readers
2879 announceStatus,
2880 getStatusText,
2881
2882 // Controllers
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002883 focus,
Marc Kupietz9c036a42024-05-14 13:17:25 +02002884 scroll: scrollView,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002885 progress,
2886 controls,
2887 location,
2888 overview,
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002889 keyboard,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002890 fragments,
Marc Kupietz9c036a42024-05-14 13:17:25 +02002891 backgrounds,
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002892 slideContent,
2893 slideNumber,
2894
2895 onUserInput,
Marc Kupietzcf6e9982026-08-15 15:37:40 +02002896 closeOverlay: overlay.close.bind( overlay ),
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02002897 updateSlidesVisibility,
2898 layoutSlideContents,
2899 transformSlides,
2900 cueAutoSlide,
2901 cancelAutoSlide
2902 } );
2903
2904 return API;
2905
2906};