notes.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. var deck;
  15. function openNotes( notesFilePath ) {
  16. if (notesPopup && !notesPopup.closed) {
  17. notesPopup.focus();
  18. return;
  19. }
  20. if( !notesFilePath ) {
  21. var jsFileLocation = document.querySelector('script[src$="notes.js"]').src; // this js file path
  22. jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
  23. notesFilePath = jsFileLocation + 'notes.html';
  24. }
  25. notesPopup = window.open( notesFilePath, 'reveal.js - Notes', 'width=1100,height=700' );
  26. if( !notesPopup ) {
  27. alert( 'Speaker view popup failed to open. Please make sure popups are allowed and reopen the speaker view.' );
  28. return;
  29. }
  30. /**
  31. * Connect to the notes window through a postmessage handshake.
  32. * Using postmessage enables us to work in situations where the
  33. * origins differ, such as a presentation being opened from the
  34. * file system.
  35. */
  36. function connect() {
  37. // Keep trying to connect until we get a 'connected' message back
  38. var connectInterval = setInterval( function() {
  39. notesPopup.postMessage( JSON.stringify( {
  40. namespace: 'reveal-notes',
  41. type: 'connect',
  42. url: window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search,
  43. state: deck.getState()
  44. } ), '*' );
  45. }, 500 );
  46. window.addEventListener( 'message', function( event ) {
  47. var data = JSON.parse( event.data );
  48. if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
  49. clearInterval( connectInterval );
  50. onConnected();
  51. }
  52. if( data && data.namespace === 'reveal-notes' && data.type === 'call' ) {
  53. callRevealApi( data.methodName, data.arguments, data.callId );
  54. }
  55. } );
  56. }
  57. /**
  58. * Calls the specified Reveal.js method with the provided argument
  59. * and then pushes the result to the notes frame.
  60. */
  61. function callRevealApi( methodName, methodArguments, callId ) {
  62. var result = deck[methodName].apply( deck, methodArguments );
  63. notesPopup.postMessage( JSON.stringify( {
  64. namespace: 'reveal-notes',
  65. type: 'return',
  66. result: result,
  67. callId: callId
  68. } ), '*' );
  69. }
  70. /**
  71. * Posts the current slide data to the notes window
  72. */
  73. function post( event ) {
  74. var slideElement = deck.getCurrentSlide(),
  75. notesElement = slideElement.querySelector( 'aside.notes' ),
  76. fragmentElement = slideElement.querySelector( '.current-fragment' );
  77. var messageData = {
  78. namespace: 'reveal-notes',
  79. type: 'state',
  80. notes: '',
  81. markdown: false,
  82. whitespace: 'normal',
  83. state: deck.getState()
  84. };
  85. // Look for notes defined in a slide attribute
  86. if( slideElement.hasAttribute( 'data-notes' ) ) {
  87. messageData.notes = slideElement.getAttribute( 'data-notes' );
  88. messageData.whitespace = 'pre-wrap';
  89. }
  90. // Look for notes defined in a fragment
  91. if( fragmentElement ) {
  92. var fragmentNotes = fragmentElement.querySelector( 'aside.notes' );
  93. if( fragmentNotes ) {
  94. notesElement = fragmentNotes;
  95. }
  96. else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
  97. messageData.notes = fragmentElement.getAttribute( 'data-notes' );
  98. messageData.whitespace = 'pre-wrap';
  99. // In case there are slide notes
  100. notesElement = null;
  101. }
  102. }
  103. // Look for notes defined in an aside element
  104. if( notesElement ) {
  105. messageData.notes = notesElement.innerHTML;
  106. messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
  107. }
  108. notesPopup.postMessage( JSON.stringify( messageData ), '*' );
  109. }
  110. /**
  111. * Called once we have established a connection to the notes
  112. * window.
  113. */
  114. function onConnected() {
  115. // Monitor events that trigger a change in state
  116. deck.on( 'slidechanged', post );
  117. deck.on( 'fragmentshown', post );
  118. deck.on( 'fragmenthidden', post );
  119. deck.on( 'overviewhidden', post );
  120. deck.on( 'overviewshown', post );
  121. deck.on( 'paused', post );
  122. deck.on( 'resumed', post );
  123. // Post the initial state
  124. post();
  125. }
  126. connect();
  127. }
  128. return {
  129. id: 'notes',
  130. init: function( revealInstance ) {
  131. deck = revealInstance;
  132. if( !/receiver/i.test( window.location.search ) ) {
  133. // If the there's a 'notes' query set, open directly
  134. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  135. openNotes();
  136. }
  137. // Open the notes when the 's' key is hit
  138. deck.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
  139. openNotes();
  140. } );
  141. }
  142. },
  143. open: openNotes
  144. };
  145. })();
  146. Reveal.registerPlugin( RevealNotes );