2023-01-16 11:41:19 +01:00
|
|
|
/**
|
|
|
|
* Makes it possble to jump to a slide by entering its
|
|
|
|
* slide number or id.
|
|
|
|
*/
|
|
|
|
export default class JumpToSlide {
|
|
|
|
|
|
|
|
constructor( Reveal ) {
|
|
|
|
|
|
|
|
this.Reveal = Reveal;
|
|
|
|
|
|
|
|
this.onInput = this.onInput.bind( this );
|
|
|
|
this.onBlur = this.onBlur.bind( this );
|
|
|
|
this.onKeyDown = this.onKeyDown.bind( this );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
this.element = document.createElement( 'div' );
|
|
|
|
this.element.className = 'jump-to-slide';
|
|
|
|
|
|
|
|
this.jumpInput = document.createElement( 'input' );
|
|
|
|
this.jumpInput.type = 'text';
|
|
|
|
this.jumpInput.className = 'jump-to-slide-input';
|
|
|
|
this.jumpInput.placeholder = 'Jump to slide';
|
|
|
|
this.jumpInput.addEventListener( 'input', this.onInput );
|
|
|
|
this.jumpInput.addEventListener( 'keydown', this.onKeyDown );
|
|
|
|
this.jumpInput.addEventListener( 'blur', this.onBlur );
|
|
|
|
|
|
|
|
this.element.appendChild( this.jumpInput );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
|
|
|
|
this.indicesOnShow = this.Reveal.getIndices();
|
|
|
|
|
|
|
|
this.Reveal.getRevealElement().appendChild( this.element );
|
|
|
|
this.jumpInput.focus();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
|
|
|
|
if( this.isVisible() ) {
|
|
|
|
this.element.remove();
|
|
|
|
this.jumpInput.value = '';
|
2023-01-16 14:43:50 +01:00
|
|
|
|
|
|
|
clearTimeout( this.jumpTimeout );
|
|
|
|
delete this.jumpTimeout;
|
2023-01-16 11:41:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
isVisible() {
|
|
|
|
|
|
|
|
return !!this.element.parentNode;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-16 12:33:37 +01:00
|
|
|
/**
|
|
|
|
* Parses the current input and jumps to the given slide.
|
|
|
|
*/
|
2023-01-16 11:41:19 +01:00
|
|
|
jump() {
|
|
|
|
|
2023-01-16 14:43:50 +01:00
|
|
|
clearTimeout( this.jumpTimeout );
|
|
|
|
delete this.jumpTimeout;
|
|
|
|
|
2023-01-17 09:49:49 +01:00
|
|
|
const query = this.jumpInput.value.trim( '' );
|
|
|
|
let indices = this.Reveal.location.getIndicesFromHash( query, { oneBasedIndex: true } );
|
2023-01-16 11:41:19 +01:00
|
|
|
|
2023-01-17 09:49:49 +01:00
|
|
|
// If no valid index was found and the input query is a
|
|
|
|
// string, fall back on a simple search
|
|
|
|
if( !indices && /\S+/i.test( query ) ) {
|
|
|
|
indices = this.search( query );
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:30:35 +01:00
|
|
|
if( indices && query !== '' && query.length > 1 ) {
|
2023-01-16 11:41:19 +01:00
|
|
|
this.Reveal.slide( indices.h, indices.v, indices.f );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.Reveal.slide( this.indicesOnShow.h, this.indicesOnShow.v, this.indicesOnShow.f );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:43:50 +01:00
|
|
|
jumpAfter( delay ) {
|
|
|
|
|
|
|
|
clearTimeout( this.jumpTimeout );
|
|
|
|
this.jumpTimeout = setTimeout( () => this.jump(), delay );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-17 09:49:49 +01:00
|
|
|
/**
|
|
|
|
* A lofi search that looks for the given query in all
|
|
|
|
* of our slides and returns the first match.
|
|
|
|
*/
|
|
|
|
search( query ) {
|
|
|
|
|
|
|
|
const regex = new RegExp( '\\b' + query.trim() + '\\b', 'i' );
|
|
|
|
|
|
|
|
const slide = this.Reveal.getSlides().find( ( slide ) => {
|
|
|
|
return regex.test( slide.innerText );
|
|
|
|
} );
|
|
|
|
|
|
|
|
if( slide ) {
|
|
|
|
return this.Reveal.getIndices( slide );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:41:19 +01:00
|
|
|
/**
|
|
|
|
* Reverts back to the slide we were on when jump to slide was
|
|
|
|
* invoked.
|
|
|
|
*/
|
|
|
|
cancel() {
|
|
|
|
|
|
|
|
this.Reveal.slide( this.indicesOnShow.h, this.indicesOnShow.v, this.indicesOnShow.f );
|
|
|
|
this.hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
confirm() {
|
|
|
|
|
2023-01-17 09:49:49 +01:00
|
|
|
this.jump();
|
2023-01-16 11:41:19 +01:00
|
|
|
this.hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
|
|
|
|
this.jumpInput.removeEventListener( 'input', this.onInput );
|
|
|
|
this.jumpInput.removeEventListener( 'keydown', this.onKeyDown );
|
|
|
|
this.jumpInput.removeEventListener( 'blur', this.onBlur );
|
|
|
|
|
|
|
|
this.element.remove();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyDown( event ) {
|
|
|
|
|
|
|
|
if( event.keyCode === 13 ) {
|
|
|
|
this.confirm();
|
|
|
|
}
|
|
|
|
else if( event.keyCode === 27 ) {
|
|
|
|
this.cancel();
|
|
|
|
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
onInput( event ) {
|
|
|
|
|
2023-01-16 14:43:50 +01:00
|
|
|
this.jumpAfter( 200 );
|
2023-01-16 11:41:19 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlur() {
|
|
|
|
|
|
|
|
setTimeout( () => this.hide(), 1 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|