Skip to content

Commit

Permalink
reveal.js can now be uninitialized by calling Reveal.destroy() #1145 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimel committed Feb 21, 2022
1 parent ff20051 commit 1e0cbe6
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 55 deletions.
2 changes: 1 addition & 1 deletion dist/reveal.esm.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions js/controllers/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ export default class Controls {
}
}

destroy() {

this.unbind();
this.element.remove();

}

/**
* Event handlers for navigation control buttons.
*/
Expand Down
13 changes: 13 additions & 0 deletions js/controllers/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

}

}
11 changes: 11 additions & 0 deletions js/controllers/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
91 changes: 65 additions & 26 deletions js/reveal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -2521,6 +2559,7 @@ export default function( revealElement, options ) {

initialize,
configure,
destroy,

sync,
syncSlide,
Expand Down
29 changes: 7 additions & 22 deletions plugin/zoom/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const Plugin = {
}
} );

},

destroy: () => {

zoom.reset();

}

};
Expand Down Expand Up @@ -52,19 +58,11 @@ var zoom = (function(){
panUpdateInterval = -1;

// Check for transform support so that we can fallback otherwise
var supportsTransforms = 'WebkitTransform' in document.body.style ||
'MozTransform' in document.body.style ||
'msTransform' in document.body.style ||
'OTransform' in document.body.style ||
'transform' in document.body.style;
var supportsTransforms = 'transform' in document.body.style;

if( supportsTransforms ) {
// The easing that will be applied when we zoom in/out
document.body.style.transition = 'transform 0.8s ease';
document.body.style.OTransition = '-o-transform 0.8s ease';
document.body.style.msTransition = '-ms-transform 0.8s ease';
document.body.style.MozTransition = '-moz-transform 0.8s ease';
document.body.style.WebkitTransition = '-webkit-transform 0.8s ease';
}

// Zoom out if the user hits escape
Expand Down Expand Up @@ -105,27 +103,14 @@ var zoom = (function(){
// Reset
if( scale === 1 ) {
document.body.style.transform = '';
document.body.style.OTransform = '';
document.body.style.msTransform = '';
document.body.style.MozTransform = '';
document.body.style.WebkitTransform = '';
}
// Scale
else {
var origin = scrollOffset.x +'px '+ scrollOffset.y +'px',
transform = 'translate('+ -rect.x +'px,'+ -rect.y +'px) scale('+ scale +')';

document.body.style.transformOrigin = origin;
document.body.style.OTransformOrigin = origin;
document.body.style.msTransformOrigin = origin;
document.body.style.MozTransformOrigin = origin;
document.body.style.WebkitTransformOrigin = origin;

document.body.style.transform = transform;
document.body.style.OTransform = transform;
document.body.style.msTransform = transform;
document.body.style.MozTransform = transform;
document.body.style.WebkitTransform = transform;
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion plugin/zoom/zoom.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions plugin/zoom/zoom.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e0cbe6

Please sign in to comment.