Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 1 | import { |
| 2 | SLIDE_NUMBER_FORMAT_CURRENT, |
| 3 | SLIDE_NUMBER_FORMAT_CURRENT_SLASH_TOTAL, |
| 4 | SLIDE_NUMBER_FORMAT_HORIZONTAL_DOT_VERTICAL, |
| 5 | SLIDE_NUMBER_FORMAT_HORIZONTAL_SLASH_VERTICAL |
| 6 | } from "../utils/constants"; |
| 7 | |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 8 | /** |
| 9 | * Handles the display of reveal.js' optional slide number. |
| 10 | */ |
| 11 | export default class SlideNumber { |
| 12 | |
| 13 | constructor( Reveal ) { |
| 14 | |
| 15 | this.Reveal = Reveal; |
| 16 | |
| 17 | } |
| 18 | |
| 19 | render() { |
| 20 | |
| 21 | this.element = document.createElement( 'div' ); |
| 22 | this.element.className = 'slide-number'; |
| 23 | this.Reveal.getRevealElement().appendChild( this.element ); |
| 24 | |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Called when the reveal.js config is updated. |
| 29 | */ |
| 30 | configure( config, oldConfig ) { |
| 31 | |
| 32 | let slideNumberDisplay = 'none'; |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 33 | if( config.slideNumber && !this.Reveal.isPrintView() ) { |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 34 | if( config.showSlideNumber === 'all' ) { |
| 35 | slideNumberDisplay = 'block'; |
| 36 | } |
| 37 | else if( config.showSlideNumber === 'speaker' && this.Reveal.isSpeakerNotes() ) { |
| 38 | slideNumberDisplay = 'block'; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | this.element.style.display = slideNumberDisplay; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Updates the slide number to match the current slide. |
| 48 | */ |
| 49 | update() { |
| 50 | |
| 51 | // Update slide number if enabled |
| 52 | if( this.Reveal.getConfig().slideNumber && this.element ) { |
| 53 | this.element.innerHTML = this.getSlideNumber(); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the HTML string corresponding to the current slide |
| 60 | * number, including formatting. |
| 61 | */ |
| 62 | getSlideNumber( slide = this.Reveal.getCurrentSlide() ) { |
| 63 | |
| 64 | let config = this.Reveal.getConfig(); |
| 65 | let value; |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 66 | let format = SLIDE_NUMBER_FORMAT_HORIZONTAL_DOT_VERTICAL; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 67 | |
| 68 | if ( typeof config.slideNumber === 'function' ) { |
| 69 | value = config.slideNumber( slide ); |
| 70 | } else { |
| 71 | // Check if a custom number format is available |
| 72 | if( typeof config.slideNumber === 'string' ) { |
| 73 | format = config.slideNumber; |
| 74 | } |
| 75 | |
| 76 | // If there are ONLY vertical slides in this deck, always use |
| 77 | // a flattened slide number |
| 78 | if( !/c/.test( format ) && this.Reveal.getHorizontalSlides().length === 1 ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 79 | format = SLIDE_NUMBER_FORMAT_CURRENT; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Offset the current slide number by 1 to make it 1-indexed |
| 83 | let horizontalOffset = slide && slide.dataset.visibility === 'uncounted' ? 0 : 1; |
| 84 | |
| 85 | value = []; |
| 86 | switch( format ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 87 | case SLIDE_NUMBER_FORMAT_CURRENT: |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 88 | value.push( this.Reveal.getSlidePastCount( slide ) + horizontalOffset ); |
| 89 | break; |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 90 | case SLIDE_NUMBER_FORMAT_CURRENT_SLASH_TOTAL: |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 91 | value.push( this.Reveal.getSlidePastCount( slide ) + horizontalOffset, '/', this.Reveal.getTotalSlides() ); |
| 92 | break; |
| 93 | default: |
| 94 | let indices = this.Reveal.getIndices( slide ); |
| 95 | value.push( indices.h + horizontalOffset ); |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame] | 96 | let sep = format === SLIDE_NUMBER_FORMAT_HORIZONTAL_SLASH_VERTICAL ? '/' : '.'; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 97 | if( this.Reveal.isVerticalSlide( slide ) ) value.push( sep, indices.v + 1 ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | let url = '#' + this.Reveal.location.getHash( slide ); |
| 102 | return this.formatNumber( value[0], value[1], value[2], url ); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Applies HTML formatting to a slide number before it's |
| 108 | * written to the DOM. |
| 109 | * |
| 110 | * @param {number} a Current slide |
| 111 | * @param {string} delimiter Character to separate slide numbers |
| 112 | * @param {(number|*)} b Total slides |
| 113 | * @param {HTMLElement} [url='#'+locationHash()] The url to link to |
| 114 | * @return {string} HTML string fragment |
| 115 | */ |
| 116 | formatNumber( a, delimiter, b, url = '#' + this.Reveal.location.getHash() ) { |
| 117 | |
| 118 | if( typeof b === 'number' && !isNaN( b ) ) { |
| 119 | return `<a href="${url}"> |
| 120 | <span class="slide-number-a">${a}</span> |
| 121 | <span class="slide-number-delimiter">${delimiter}</span> |
| 122 | <span class="slide-number-b">${b}</span> |
| 123 | </a>`; |
| 124 | } |
| 125 | else { |
| 126 | return `<a href="${url}"> |
| 127 | <span class="slide-number-a">${a}</span> |
| 128 | </a>`; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 133 | destroy() { |
| 134 | |
| 135 | this.element.remove(); |
| 136 | |
| 137 | } |
| 138 | |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 139 | } |