notes.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { extend, toArray } from '../utils/util.js'
  2. /**
  3. *
  4. */
  5. export default class Notes {
  6. constructor( Reveal ) {
  7. this.Reveal = Reveal;
  8. }
  9. render() {
  10. this.element = document.createElement( 'div' );
  11. this.element.className = 'speaker-notes';
  12. this.element.setAttribute( 'data-prevent-swipe', '' );
  13. this.element.setAttribute( 'tabindex', '0' );
  14. this.Reveal.getRevealElement().appendChild( this.element );
  15. }
  16. /**
  17. * Called when the reveal.js config is updated.
  18. */
  19. configure( config, oldConfig ) {
  20. if( config.showNotes ) {
  21. this.element.setAttribute( 'data-layout', typeof config.showNotes === 'string' ? config.showNotes : 'inline' );
  22. }
  23. }
  24. /**
  25. * Pick up notes from the current slide and display them
  26. * to the viewer.
  27. *
  28. * @see {@link config.showNotes}
  29. */
  30. update() {
  31. if( this.Reveal.getConfig().showNotes && this.element && this.Reveal.getCurrentSlide() && !this.Reveal.print.isPrintingPDF() ) {
  32. this.element.innerHTML = this.getSlideNotes() || '<span class="notes-placeholder">No notes on this slide.</span>';
  33. }
  34. }
  35. /**
  36. * Updates the visibility of the speaker notes sidebar that
  37. * is used to share annotated slides. The notes sidebar is
  38. * only visible if showNotes is true and there are notes on
  39. * one or more slides in the deck.
  40. */
  41. updateVisibility() {
  42. if( this.Reveal.getConfig().showNotes && this.hasNotes() ) {
  43. this.Reveal.getRevealElement().classList.add( 'show-notes' );
  44. }
  45. else {
  46. this.Reveal.getRevealElement().classList.remove( 'show-notes' );
  47. }
  48. }
  49. /**
  50. * Checks if there are speaker notes for ANY slide in the
  51. * presentation.
  52. */
  53. hasNotes() {
  54. return this.Reveal.getSlidesElement().querySelectorAll( '[data-notes], aside.notes' ).length > 0;
  55. }
  56. /**
  57. * Checks if this presentation is running inside of the
  58. * speaker notes window.
  59. *
  60. * @return {boolean}
  61. */
  62. isSpeakerNotes() {
  63. return !!window.location.search.match( /receiver/gi );
  64. }
  65. /**
  66. * Retrieves the speaker notes from a slide. Notes can be
  67. * defined in two ways:
  68. * 1. As a data-notes attribute on the slide <section>
  69. * 2. As an <aside class="notes"> inside of the slide
  70. *
  71. * @param {HTMLElement} [slide=currentSlide]
  72. * @return {(string|null)}
  73. */
  74. getSlideNotes( slide = this.Reveal.getCurrentSlide() ) {
  75. // Notes can be specified via the data-notes attribute...
  76. if( slide.hasAttribute( 'data-notes' ) ) {
  77. return slide.getAttribute( 'data-notes' );
  78. }
  79. // ... or using an <aside class="notes"> element
  80. let notesElement = slide.querySelector( 'aside.notes' );
  81. if( notesElement ) {
  82. return notesElement.innerHTML;
  83. }
  84. return null;
  85. }
  86. }