notes.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. let Plugin = (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. notesFilePath = 'plugin/notes/notes.html'
  25. }
  26. notesPopup = window.open( notesFilePath, 'reveal.js - Notes', 'width=1100,height=700' );
  27. if( !notesPopup ) {
  28. alert( 'Speaker view popup failed to open. Please make sure popups are allowed and reopen the speaker view.' );
  29. return;
  30. }
  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: deck.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. if( data && data.namespace === 'reveal-notes' && data.type === 'call' ) {
  54. callRevealApi( data.methodName, data.arguments, data.callId );
  55. }
  56. } );
  57. }
  58. /**
  59. * Calls the specified Reveal.js method with the provided argument
  60. * and then pushes the result to the notes frame.
  61. */
  62. function callRevealApi( methodName, methodArguments, callId ) {
  63. var result = deck[methodName].apply( deck, methodArguments );
  64. notesPopup.postMessage( JSON.stringify( {
  65. namespace: 'reveal-notes',
  66. type: 'return',
  67. result: result,
  68. callId: callId
  69. } ), '*' );
  70. }
  71. /**
  72. * Posts the current slide data to the notes window
  73. */
  74. function post( event ) {
  75. var slideElement = deck.getCurrentSlide(),
  76. notesElement = slideElement.querySelector( 'aside.notes' ),
  77. fragmentElement = slideElement.querySelector( '.current-fragment' );
  78. var messageData = {
  79. namespace: 'reveal-notes',
  80. type: 'state',
  81. notes: '',
  82. markdown: false,
  83. whitespace: 'normal',
  84. state: deck.getState()
  85. };
  86. // Look for notes defined in a slide attribute
  87. if( slideElement.hasAttribute( 'data-notes' ) ) {
  88. messageData.notes = slideElement.getAttribute( 'data-notes' );
  89. messageData.whitespace = 'pre-wrap';
  90. }
  91. // Look for notes defined in a fragment
  92. if( fragmentElement ) {
  93. var fragmentNotes = fragmentElement.querySelector( 'aside.notes' );
  94. if( fragmentNotes ) {
  95. notesElement = fragmentNotes;
  96. }
  97. else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
  98. messageData.notes = fragmentElement.getAttribute( 'data-notes' );
  99. messageData.whitespace = 'pre-wrap';
  100. // In case there are slide notes
  101. notesElement = null;
  102. }
  103. }
  104. // Look for notes defined in an aside element
  105. if( notesElement ) {
  106. messageData.notes = notesElement.innerHTML;
  107. messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
  108. }
  109. notesPopup.postMessage( JSON.stringify( messageData ), '*' );
  110. }
  111. /**
  112. * Called once we have established a connection to the notes
  113. * window.
  114. */
  115. function onConnected() {
  116. // Monitor events that trigger a change in state
  117. deck.on( 'slidechanged', post );
  118. deck.on( 'fragmentshown', post );
  119. deck.on( 'fragmenthidden', post );
  120. deck.on( 'overviewhidden', post );
  121. deck.on( 'overviewshown', post );
  122. deck.on( 'paused', post );
  123. deck.on( 'resumed', post );
  124. // Post the initial state
  125. post();
  126. }
  127. connect();
  128. }
  129. return {
  130. id: 'notes',
  131. init: function( reveal ) {
  132. deck = reveal;
  133. if( !/receiver/i.test( window.location.search ) ) {
  134. // If the there's a 'notes' query set, open directly
  135. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  136. openNotes();
  137. }
  138. // Open the notes when the 's' key is hit
  139. deck.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
  140. openNotes();
  141. } );
  142. }
  143. },
  144. open: openNotes
  145. };
  146. })();
  147. export default () => Plugin;