notes.js 2.8 KB

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