notes.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Handles opening of and synchronization with the reveal.js
  3. * notes window.
  4. *
  5. * Handshake process:
  6. * 1. This window posts 'connect' to notes window
  7. * - Includes URL of presentation to show
  8. * 2. Notes window responds with 'connected' when it is available
  9. * 3. This window proceeds to send the current presentation state
  10. * to the notes window
  11. */
  12. var RevealNotes = (function() {
  13. var notesPopup = null;
  14. function openNotes( notesFilePath ) {
  15. if (notesPopup && !notesPopup.closed) {
  16. notesPopup.focus();
  17. return;
  18. }
  19. if( !notesFilePath ) {
  20. var jsFileLocation = document.querySelector('script[src$="notes.js"]').src; // this js file path
  21. jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
  22. notesFilePath = jsFileLocation + 'notes.html';
  23. }
  24. notesPopup = window.open( notesFilePath, 'reveal.js - Notes', 'width=1100,height=700' );
  25. if( !notesPopup ) {
  26. alert( 'Speaker view popup failed to open. Please make sure popups are allowed and reopen the speaker view.' );
  27. return;
  28. }
  29. // Allow popup window access to Reveal API
  30. notesPopup.Reveal = window.Reveal;
  31. /**
  32. * Connect to the notes window through a postmessage handshake.
  33. * Using postmessage enables us to work in situations where the
  34. * origins differ, such as a presentation being opened from the
  35. * file system.
  36. */
  37. function connect() {
  38. // Keep trying to connect until we get a 'connected' message back
  39. var connectInterval = setInterval( function() {
  40. notesPopup.postMessage( JSON.stringify( {
  41. namespace: 'reveal-notes',
  42. type: 'connect',
  43. url: window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search,
  44. state: Reveal.getState()
  45. } ), '*' );
  46. }, 500 );
  47. window.addEventListener( 'message', function( event ) {
  48. var data = JSON.parse( event.data );
  49. if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
  50. clearInterval( connectInterval );
  51. onConnected();
  52. }
  53. } );
  54. }
  55. /**
  56. * Posts the current slide data to the notes window
  57. */
  58. function post( event ) {
  59. var slideElement = Reveal.getCurrentSlide(),
  60. notesElement = slideElement.querySelector( 'aside.notes' ),
  61. fragmentElement = slideElement.querySelector( '.current-fragment' );
  62. var messageData = {
  63. namespace: 'reveal-notes',
  64. type: 'state',
  65. notes: '',
  66. markdown: false,
  67. whitespace: 'normal',
  68. state: Reveal.getState()
  69. };
  70. // Look for notes defined in a slide attribute
  71. if( slideElement.hasAttribute( 'data-notes' ) ) {
  72. messageData.notes = slideElement.getAttribute( 'data-notes' );
  73. messageData.whitespace = 'pre-wrap';
  74. }
  75. // Look for notes defined in a fragment
  76. if( fragmentElement ) {
  77. var fragmentNotes = fragmentElement.querySelector( 'aside.notes' );
  78. if( fragmentNotes ) {
  79. notesElement = fragmentNotes;
  80. }
  81. else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
  82. messageData.notes = fragmentElement.getAttribute( 'data-notes' );
  83. messageData.whitespace = 'pre-wrap';
  84. // In case there are slide notes
  85. notesElement = null;
  86. }
  87. }
  88. // Look for notes defined in an aside element
  89. if( notesElement ) {
  90. messageData.notes = notesElement.innerHTML;
  91. messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
  92. }
  93. notesPopup.postMessage( JSON.stringify( messageData ), '*' );
  94. }
  95. /**
  96. * Called once we have established a connection to the notes
  97. * window.
  98. */
  99. function onConnected() {
  100. // Monitor events that trigger a change in state
  101. Reveal.addEventListener( 'slidechanged', post );
  102. Reveal.addEventListener( 'fragmentshown', post );
  103. Reveal.addEventListener( 'fragmenthidden', post );
  104. Reveal.addEventListener( 'overviewhidden', post );
  105. Reveal.addEventListener( 'overviewshown', post );
  106. Reveal.addEventListener( 'paused', post );
  107. Reveal.addEventListener( 'resumed', post );
  108. // Post the initial state
  109. post();
  110. }
  111. connect();
  112. }
  113. if( !/receiver/i.test( window.location.search ) ) {
  114. // If the there's a 'notes' query set, open directly
  115. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  116. openNotes();
  117. }
  118. // Open the notes when the 's' key is hit
  119. Reveal.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
  120. openNotes();
  121. } );
  122. }
  123. return { open: openNotes };
  124. })();