aboutsummaryrefslogtreecommitdiff
path: root/plugin/notes/notes.js
diff options
context:
space:
mode:
authorHakim El Hattab <hakim.elhattab@gmail.com>2014-04-19 10:54:14 +0200
committerHakim El Hattab <hakim.elhattab@gmail.com>2014-04-19 10:54:26 +0200
commit5b18c1f308523527566cefc85414170e922bc4a2 (patch)
tree480b2b7b4d296ff41fec6e26a3a83f8063c70a57 /plugin/notes/notes.js
parentce31184bf30fcfbc45dab480d0072e51f626b15a (diff)
downloadfosdem-2018-presentation-5b18c1f308523527566cefc85414170e922bc4a2.tar
fosdem-2018-presentation-5b18c1f308523527566cefc85414170e922bc4a2.tar.gz
notes plugin now operates entirely through window.postMessage, adding support for file protocol
Diffstat (limited to 'plugin/notes/notes.js')
-rw-r--r--plugin/notes/notes.js88
1 files changed, 59 insertions, 29 deletions
diff --git a/plugin/notes/notes.js b/plugin/notes/notes.js
index 3f68b5d..31efd81 100644
--- a/plugin/notes/notes.js
+++ b/plugin/notes/notes.js
@@ -1,6 +1,13 @@
/**
* Handles opening of and synchronization with the reveal.js
* notes window.
+ *
+ * Handshake process:
+ * 1. This window posts 'connect' to notes window
+ * - Includes URL of presentation to show
+ * 2. Notes window responds with 'connected' when it is available
+ * 3. This window proceeds to send the current presentation state
+ * to the notes window
*/
var RevealNotes = (function() {
@@ -9,41 +16,46 @@ var RevealNotes = (function() {
jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
var notesPopup = window.open( jsFileLocation + 'notes.html', 'reveal.js - Notes', 'width=1120,height=850' );
- // Fires when slide is changed
- Reveal.addEventListener( 'slidechanged', post );
-
- // Fires when a fragment is shown
- Reveal.addEventListener( 'fragmentshown', post );
+ /**
+ * Connect to the notes window through a postmessage handshake.
+ * Using postmessage enables us to work in situations where the
+ * origins differ, such as a presentation being opened from the
+ * file system.
+ */
+ function connect() {
+ // Keep trying to connect until we get a 'connected' message back
+ var connectInterval = setInterval( function() {
+ notesPopup.postMessage( JSON.stringify( {
+ namespace: 'reveal-notes',
+ type: 'connect',
+ url: window.location.protocol + '//' + window.location.host + window.location.pathname,
+ state: Reveal.getState()
+ } ), '*' );
+ }, 500 );
- // Fires when a fragment is hidden
- Reveal.addEventListener( 'fragmenthidden', post );
+ window.addEventListener( 'message', function( event ) {
+ var data = JSON.parse( event.data );
+ if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
+ clearInterval( connectInterval );
+ onConnected();
+ }
+ } );
+ }
/**
* Posts the current slide data to the notes window
*/
function post() {
+
var slideElement = Reveal.getCurrentSlide(),
- slideIndices = Reveal.getIndices(),
- notesElement = slideElement.querySelector( 'aside.notes' ),
- nextindexh,
- nextindexv;
-
- if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) {
- nextindexh = slideIndices.h;
- nextindexv = slideIndices.v + 1;
- } else {
- nextindexh = slideIndices.h + 1;
- nextindexv = 0;
- }
+ notesElement = slideElement.querySelector( 'aside.notes' );
var messageData = {
- notes : '',
- indexh : slideIndices.h,
- indexv : slideIndices.v,
- indexf : slideIndices.f,
- nextindexh : nextindexh,
- nextindexv : nextindexv,
- markdown : false
+ namespace: 'reveal-notes',
+ type: 'state',
+ notes: '',
+ markdown: false,
+ state: Reveal.getState()
};
// Look for notes defined in a slide attribute
@@ -58,12 +70,30 @@ var RevealNotes = (function() {
}
notesPopup.postMessage( JSON.stringify( messageData ), '*' );
+
}
- // Navigate to the current slide when the notes are loaded
- notesPopup.addEventListener( 'load', function( event ) {
+ /**
+ * Called once we have established a connection to the notes
+ * window.
+ */
+ function onConnected() {
+
+ // Monitor events that trigger a change in state
+ Reveal.addEventListener( 'slidechanged', post );
+ Reveal.addEventListener( 'fragmentshown', post );
+ Reveal.addEventListener( 'fragmenthidden', post );
+ Reveal.addEventListener( 'overviewhidden', post );
+ Reveal.addEventListener( 'overviewshown', post );
+ Reveal.addEventListener( 'paused', post );
+ Reveal.addEventListener( 'resumed', post );
+
+ // Post the initial state
post();
- }, false );
+
+ }
+
+ connect();
}
// If the there's a 'notes' query set, open directly