notes.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. function openNotes() {
  14. var jsFileLocation = document.querySelector('script[src$="notes.js"]').src; // this js file path
  15. jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
  16. var notesPopup = window.open( jsFileLocation + 'notes.html', 'reveal.js - Notes', 'width=1100,height=700' );
  17. /**
  18. * Connect to the notes window through a postmessage handshake.
  19. * Using postmessage enables us to work in situations where the
  20. * origins differ, such as a presentation being opened from the
  21. * file system.
  22. */
  23. function connect() {
  24. // Keep trying to connect until we get a 'connected' message back
  25. var connectInterval = setInterval( function() {
  26. notesPopup.postMessage( JSON.stringify( {
  27. namespace: 'reveal-notes',
  28. type: 'connect',
  29. url: window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search,
  30. state: Reveal.getState()
  31. } ), '*' );
  32. }, 500 );
  33. window.addEventListener( 'message', function( event ) {
  34. var data = JSON.parse( event.data );
  35. if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
  36. clearInterval( connectInterval );
  37. onConnected();
  38. }
  39. } );
  40. }
  41. /**
  42. * Posts the current slide data to the notes window
  43. */
  44. function post() {
  45. var slideElement = Reveal.getCurrentSlide(),
  46. notesElement = slideElement.querySelector( 'aside.notes' );
  47. var messageData = {
  48. namespace: 'reveal-notes',
  49. type: 'state',
  50. notes: '',
  51. markdown: false,
  52. whitespace: 'normal',
  53. state: Reveal.getState()
  54. };
  55. // Look for notes defined in a slide attribute
  56. if( slideElement.hasAttribute( 'data-notes' ) ) {
  57. messageData.notes = slideElement.getAttribute( 'data-notes' );
  58. messageData.whitespace = 'pre-wrap';
  59. }
  60. // Look for notes defined in an aside element
  61. if( notesElement ) {
  62. messageData.notes = notesElement.innerHTML;
  63. messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
  64. }
  65. notesPopup.postMessage( JSON.stringify( messageData ), '*' );
  66. }
  67. /**
  68. * Called once we have established a connection to the notes
  69. * window.
  70. */
  71. function onConnected() {
  72. // Monitor events that trigger a change in state
  73. Reveal.addEventListener( 'slidechanged', post );
  74. Reveal.addEventListener( 'fragmentshown', post );
  75. Reveal.addEventListener( 'fragmenthidden', post );
  76. Reveal.addEventListener( 'overviewhidden', post );
  77. Reveal.addEventListener( 'overviewshown', post );
  78. Reveal.addEventListener( 'paused', post );
  79. Reveal.addEventListener( 'resumed', post );
  80. // Post the initial state
  81. post();
  82. }
  83. connect();
  84. }
  85. if( !/receiver/i.test( window.location.search ) ) {
  86. // If the there's a 'notes' query set, open directly
  87. if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
  88. openNotes();
  89. }
  90. // Open the notes when the 's' key is hit
  91. document.addEventListener( 'keydown', function( event ) {
  92. // Disregard the event if the target is editable or a
  93. // modifier is present
  94. if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  95. // Disregard the event if keyboard is disabled
  96. if ( Reveal.getConfig().keyboard === false ) return;
  97. if( event.keyCode === 83 ) {
  98. event.preventDefault();
  99. openNotes();
  100. }
  101. }, false );
  102. }
  103. return { open: openNotes };
  104. })();