plugin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. notesElement = slideElement.querySelector( '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. notesElement = fragmentNotes;
  94. }
  95. else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
  96. messageData.notes = fragmentElement.getAttribute( 'data-notes' );
  97. messageData.whitespace = 'pre-wrap';
  98. // In case there are slide notes
  99. notesElement = null;
  100. }
  101. }
  102. // Look for notes defined in an aside element
  103. if( notesElement ) {
  104. messageData.notes = notesElement.innerHTML;
  105. messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
  106. }
  107. popup.postMessage( JSON.stringify( messageData ), '*' );
  108. }
  109. /**
  110. * Called once we have established a connection to the notes
  111. * window.
  112. */
  113. function onConnected() {
  114. // Monitor events that trigger a change in state
  115. deck.on( 'slidechanged', post );
  116. deck.on( 'fragmentshown', post );
  117. deck.on( 'fragmenthidden', post );
  118. deck.on( 'overviewhidden', post );
  119. deck.on( 'overviewshown', post );
  120. deck.on( 'paused', post );
  121. deck.on( 'resumed', post );
  122. // Post the initial state
  123. post();
  124. }
  125. connect();
  126. }
  127. return {
  128. id: 'notes',
  129. init: function( reveal ) {
  130. deck = reveal;
  131. if( !/receiver/i.test( window.location.search ) ) {
  132. // If the there's a 'notes' query set, open directly
  133. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  134. openNotes();
  135. }
  136. // Open the notes when the 's' key is hit
  137. deck.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
  138. openNotes();
  139. } );
  140. }
  141. },
  142. open: openNotes
  143. };
  144. };
  145. export default Plugin;