add slidenumber & location controllers

This commit is contained in:
Hakim El Hattab
2020-03-10 21:08:11 +01:00
parent ac15678dea
commit 3683ad255d
6 changed files with 240 additions and 184 deletions

View File

@ -263,7 +263,7 @@ export default class Keyboard {
}
// N, PAGE DOWN
else if( keyCode === 78 || keyCode === 34 ) {
this.Review.next();
this.Reveal.next();
}
// H, LEFT
else if( keyCode === 72 || keyCode === 37 ) {
@ -283,7 +283,7 @@ export default class Keyboard {
this.Reveal.slide( Number.MAX_VALUE );
}
else if( !this.Reveal.overview.isActive() && useLinearMode ) {
this.Review.next();
this.Reveal.next();
}
else {
this.Reveal.right();
@ -301,7 +301,7 @@ export default class Keyboard {
// J, DOWN
else if( keyCode === 74 || keyCode === 40 ) {
if( !this.Reveal.overview.isActive() && useLinearMode ) {
this.Review.next();
this.Reveal.next();
}
else {
this.Reveal.down();
@ -324,7 +324,7 @@ export default class Keyboard {
this.Reveal.prev();
}
else {
this.Review.next();
this.Reveal.next();
}
}
// TWO-SPOT, SEMICOLON, B, V, PERIOD, LOGITECH PRESENTER TOOLS "BLACK SCREEN" BUTTON

View File

@ -0,0 +1,51 @@
import { enterFullscreen } from '../utils/util.js'
/**
* Handles all reveal.js keyboard interactions.
*/
export default class Location {
constructor( Reveal ) {
this.Reveal = Reveal;
}
/**
* Return a hash URL that will resolve to the given slide location.
*
* @param {HTMLElement} [slide=currentSlide] The slide to link to
*/
getHash( slide = this.Reveal.getCurrentSlide() ) {
let url = '/';
// Attempt to create a named link based on the slide's ID
let id = slide ? slide.getAttribute( 'id' ) : null;
if( id ) {
id = encodeURIComponent( id );
}
let index = this.Reveal.getIndices( slide );
if( !this.Reveal.getConfig().fragmentInURL ) {
index.f = undefined;
}
// If the current slide has an ID, use that as a named link,
// but we don't support named links with a fragment index
if( typeof id === 'string' && id.length && index.f === undefined ) {
url = '/' + id;
}
// Otherwise use the /h/v index
else {
let hashIndexBase = this.Reveal.getConfig().hashOneBasedIndex ? 1 : 0;
if( index.h > 0 || index.v > 0 || index.f !== undefined ) url += index.h + hashIndexBase;
if( index.v > 0 || index.f !== undefined ) url += '/' + (index.v + hashIndexBase );
if( index.f !== undefined ) url += '/' + index.f;
}
return url;
}
}

View File

@ -0,0 +1,128 @@
import { enterFullscreen } from '../utils/util.js'
/**
* Handles all reveal.js keyboard interactions.
*/
export default class SlideNumber {
constructor( Reveal ) {
this.Reveal = Reveal;
}
createElement() {
this.element = document.createElement( 'div' );
this.element.className = 'slide-number';
this.Reveal.getRevealElement().appendChild( this.element );
}
/**
* Shows or hides the slide number depending on the
* current config and state.
*/
refreshVisibility() {
let config = this.Reveal.getConfig();
let slideNumberDisplay = 'none';
if( config.slideNumber && !this.Reveal.isPrintingPDF() ) {
if( config.showSlideNumber === 'all' ) {
slideNumberDisplay = 'block';
}
else if( config.showSlideNumber === 'speaker' && this.Reveal.isSpeakerNotes() ) {
slideNumberDisplay = 'block';
}
}
this.element.style.display = slideNumberDisplay;
}
/**
* Updates the slide number to match the current slide.
*/
update() {
// Update slide number if enabled
if( this.Reveal.getConfig().slideNumber && this.element ) {
this.element.innerHTML = this.getSlideNumber();
}
}
/**
* Returns the HTML string corresponding to the current slide
* number, including formatting.
*/
getSlideNumber( slide = this.Reveal.getCurrentSlide() ) {
let config = this.Reveal.getConfig();
let value;
let format = 'h.v';
if ( typeof config.slideNumber === 'function' ) {
value = config.slideNumber( slide );
} else {
// Check if a custom number format is available
if( typeof config.slideNumber === 'string' ) {
format = config.slideNumber;
}
// If there are ONLY vertical slides in this deck, always use
// a flattened slide number
if( !/c/.test( format ) && this.Reveal.getHorizontalSlides().length === 1 ) {
format = 'c';
}
value = [];
switch( format ) {
case 'c':
value.push( this.Reveal.getSlidePastCount( slide ) + 1 );
break;
case 'c/t':
value.push( this.Reveal.getSlidePastCount( slide ) + 1, '/', this.Reveal.getTotalSlides() );
break;
default:
let indices = this.Reveal.getIndices( slide );
value.push( indices.h + 1 );
let sep = format === 'h/v' ? '/' : '.';
if( this.Reveal.isVerticalSlide( slide ) ) value.push( sep, indices.v + 1 );
}
}
let url = '#' + this.Reveal.location.getHash( slide );
return this.formatNumber( value[0], value[1], value[2], url );
}
/**
* Applies HTML formatting to a slide number before it's
* written to the DOM.
*
* @param {number} a Current slide
* @param {string} delimiter Character to separate slide numbers
* @param {(number|*)} b Total slides
* @param {HTMLElement} [url='#'+locationHash()] The url to link to
* @return {string} HTML string fragment
*/
formatNumber( a, delimiter, b, url = '#' + this.Reveal.location.getHash() ) {
if( typeof b === 'number' && !isNaN( b ) ) {
return `<a href="${url}">
<span class="slide-number-a">${a}</span>
<span class="slide-number-delimiter">${delimiter}</span>
<span class="slide-number-b">${b}</span>
</a>`;
}
else {
return `<a href="${url}">
<span class="slide-number-a">${a}</span>
</a>`;
}
}
}