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