plugin.js 5.0 KB

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