add 'slidechange' event that can prevent navigation #3003

This commit is contained in:
Hakim El Hattab 2021-09-16 11:15:41 +02:00
parent 2aed592d1f
commit ad50dd513f
4 changed files with 45 additions and 5 deletions

2
dist/reveal.esm.js vendored

File diff suppressed because one or more lines are too long

2
dist/reveal.js vendored

File diff suppressed because one or more lines are too long

View File

@ -618,6 +618,8 @@ export default function( revealElement, options ) {
dispatchPostMessage( type ); dispatchPostMessage( type );
} }
return event;
} }
/** /**
@ -1192,9 +1194,22 @@ export default function( revealElement, options ) {
* @param {number} [v=indexv] Vertical index of the target slide * @param {number} [v=indexv] Vertical index of the target slide
* @param {number} [f] Index of a fragment within the * @param {number} [f] Index of a fragment within the
* target slide to activate * target slide to activate
* @param {number} [o] Origin for use in multimaster environments * @param {number} [origin] Origin for use in multimaster environments
*/ */
function slide( h, v, f, o ) { function slide( h, v, f, origin ) {
// Dispatch an event before hte slide
const slidechange = dispatchEvent({
type: 'slidechange',
data: {
indexh: h === undefined ? indexh : h,
indexv: v === undefined ? indexv : v,
origin
}
});
// Abort if this slide change was prevented by an event listener
if( slidechange.defaultPrevented ) return;
// Remember where we were at before // Remember where we were at before
previousSlide = currentSlide; previousSlide = currentSlide;
@ -1330,7 +1345,7 @@ export default function( revealElement, options ) {
indexv, indexv,
previousSlide, previousSlide,
currentSlide, currentSlide,
origin: o origin
} }
}); });
} }

View File

@ -781,6 +781,31 @@
QUnit.module( 'Events' ); QUnit.module( 'Events' );
QUnit.test( 'slidechange', function( assert ) {
var done = assert.async( 2 );
var normalEvent = function( event ) {
assert.ok( true, 'event fired' );
done();
}
var blockingEvent = function( event ) {
event.preventDefault();
assert.ok( true, 'event fired' );
done();
}
Reveal.on( 'slidechange', normalEvent );
Reveal.slide( 2, 0 );
Reveal.off( 'slidechange', normalEvent );
Reveal.on( 'slidechange', blockingEvent );
Reveal.slide( 3, 0 );
Reveal.off( 'slidechange', blockingEvent );
assert.strictEqual( Reveal.getIndices().h, 2, 'preventing "slidechange" blocks navigation ' );
});
QUnit.test( 'slidechanged', function( assert ) { QUnit.test( 'slidechanged', function( assert ) {
assert.expect( 3 ); assert.expect( 3 );
var done = assert.async( 3 ); var done = assert.async( 3 );