2 * Handles opening of and synchronization with the reveal.js
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
12 var RevealNotes = (function() {
14 function openNotes() {
15 var jsFileLocation = document.querySelector('script[src$="notes.js"]').src; // this js file path
16 jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
17 var notesPopup = window.open( jsFileLocation + 'notes.html', 'reveal.js - Notes', 'width=1100,height=700' );
20 * Connect to the notes window through a postmessage handshake.
21 * Using postmessage enables us to work in situations where the
22 * origins differ, such as a presentation being opened from the
26 // Keep trying to connect until we get a 'connected' message back
27 var connectInterval = setInterval( function() {
28 notesPopup.postMessage( JSON.stringify( {
29 namespace: 'reveal-notes',
31 url: window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search,
32 state: Reveal.getState()
36 window.addEventListener( 'message', function( event ) {
37 var data = JSON.parse( event.data );
38 if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
39 clearInterval( connectInterval );
46 * Posts the current slide data to the notes window
50 var slideElement = Reveal.getCurrentSlide(),
51 notesElement = slideElement.querySelector( 'aside.notes' );
54 namespace: 'reveal-notes',
59 state: Reveal.getState()
62 // Look for notes defined in a slide attribute
63 if( slideElement.hasAttribute( 'data-notes' ) ) {
64 messageData.notes = slideElement.getAttribute( 'data-notes' );
65 messageData.whitespace = 'pre-wrap';
68 // Look for notes defined in an aside element
70 messageData.notes = notesElement.innerHTML;
71 messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
74 notesPopup.postMessage( JSON.stringify( messageData ), '*' );
79 * Called once we have established a connection to the notes
82 function onConnected() {
84 // Monitor events that trigger a change in state
85 Reveal.addEventListener( 'slidechanged', post );
86 Reveal.addEventListener( 'fragmentshown', post );
87 Reveal.addEventListener( 'fragmenthidden', post );
88 Reveal.addEventListener( 'overviewhidden', post );
89 Reveal.addEventListener( 'overviewshown', post );
90 Reveal.addEventListener( 'paused', post );
91 Reveal.addEventListener( 'resumed', post );
93 // Post the initial state
101 if( !/receiver/i.test( window.location.search ) ) {
103 // If the there's a 'notes' query set, open directly
104 if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
108 // Open the notes when the 's' key is hit
109 document.addEventListener( 'keydown', function( event ) {
110 // Disregard the event if the target is editable or a
111 // modifier is present
112 if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
114 if( event.keyCode === 83 ) {
115 event.preventDefault();
122 return { open: openNotes };