reveal.js can now be uninitialized by calling Reveal.destroy() #1145 #3134

This commit is contained in:
hakimel
2022-02-21 13:41:36 +01:00
parent ff20051861
commit 1e0cbe6779
11 changed files with 110 additions and 55 deletions

View File

@ -188,6 +188,13 @@ export default class Controls {
}
}
destroy() {
this.unbind();
this.element.remove();
}
/**
* Event handlers for navigation control buttons.
*/

View File

@ -238,4 +238,17 @@ export default class Plugins {
}
destroy() {
Object.values( this.registeredPlugins ).forEach( plugin => {
if( typeof plugin.destroy === 'function' ) {
plugin.destroy();
}
} );
this.registeredPlugins = {};
this.asyncDependencies = [];
}
}

View File

@ -75,6 +75,17 @@ export default class Pointer {
}
destroy() {
this.showCursor();
document.removeEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false );
document.removeEventListener( 'mousewheel', this.onDocumentMouseScroll, false );
document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false );
document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false );
}
/**
* Called whenever there is mouse input at the document level
* to determine if the cursor is active or not.

View File

@ -405,32 +405,7 @@ export default function( revealElement, options ) {
function setupPostMessage() {
if( config.postMessage ) {
window.addEventListener( 'message', event => {
let data = event.data;
// Make sure we're dealing with JSON
if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
data = JSON.parse( data );
// Check if the requested method can be found
if( data.method && typeof Reveal[data.method] === 'function' ) {
if( POST_MESSAGE_METHOD_BLACKLIST.test( data.method ) === false ) {
const result = Reveal[data.method].apply( Reveal, data.args );
// Dispatch a postMessage event with the returned value from
// our method invocation for getter functions
dispatchPostMessage( 'callback', { method: data.method, result: result } );
}
else {
console.warn( 'reveal.js: "'+ data.method +'" is is blacklisted from the postMessage API' );
}
}
}
}, false );
window.addEventListener( 'message', onPostMessage, false );
}
}
@ -577,6 +552,37 @@ export default function( revealElement, options ) {
}
/**
* Uninitializes reveal.js by undoing changes made to the
* DOM and removing all event listeners.
*/
function destroy() {
removeEventListeners();
cancelAutoSlide();
disablePreviewLinks();
// Destroy controllers
plugins.destroy();
pointer.destroy();
controls.destroy();
// Remove event listeners
document.removeEventListener( 'fullscreenchange', onFullscreenChange );
document.removeEventListener( 'webkitfullscreenchange', onFullscreenChange );
document.removeEventListener( 'visibilitychange', onPageVisibilityChange, false );
window.removeEventListener( 'message', onPostMessage, false );
window.removeEventListener( 'load', layout, false );
// Undo DOM changes
dom.viewport.classList.remove( 'reveal-viewport' );
document.documentElement.classList.remove( 'reveal-full-page' );
dom.viewport.style.removeProperty( '--slide-width' );
dom.viewport.style.removeProperty( '--slide-height' );
}
/**
* Adds a listener to one of our custom reveal.js events,
* like slidechanged.
@ -2375,6 +2381,38 @@ export default function( revealElement, options ) {
}
/**
* Listener for post message events posted to this window.
*/
function onPostMessage( event ) {
let data = event.data;
// Make sure we're dealing with JSON
if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
data = JSON.parse( data );
// Check if the requested method can be found
if( data.method && typeof Reveal[data.method] === 'function' ) {
if( POST_MESSAGE_METHOD_BLACKLIST.test( data.method ) === false ) {
const result = Reveal[data.method].apply( Reveal, data.args );
// Dispatch a postMessage event with the returned value from
// our method invocation for getter functions
dispatchPostMessage( 'callback', { method: data.method, result: result } );
}
else {
console.warn( 'reveal.js: "'+ data.method +'" is is blacklisted from the postMessage API' );
}
}
}
}
/**
* Event listener for transition end on the current slide.
*
@ -2521,6 +2559,7 @@ export default function( revealElement, options ) {
initialize,
configure,
destroy,
sync,
syncSlide,