2017-03-02 01:09:54 +01:00
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
var notesPopup = null;
|
|
|
|
|
2017-03-02 01:09:54 +01:00
|
|
|
function openNotes( notesFilePath ) {
|
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
if (notesPopup && !notesPopup.closed) {
|
|
|
|
notesPopup.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-02 01:09:54 +01:00
|
|
|
if( !notesFilePath ) {
|
|
|
|
var jsFileLocation = document.querySelector('script[src$="notes.js"]').src; // this js file path
|
|
|
|
jsFileLocation = jsFileLocation.replace(/notes\.js(\?.*)?$/, ''); // the js folder path
|
|
|
|
notesFilePath = jsFileLocation + 'notes.html';
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
notesPopup = window.open( notesFilePath, 'reveal.js - Notes', 'width=1100,height=700' );
|
2017-03-02 01:09:54 +01:00
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
if( !notesPopup ) {
|
|
|
|
alert( 'Speaker view popup failed to open. Please make sure popups are allowed and reopen the speaker view.' );
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 23:45:27 +01:00
|
|
|
|
2017-03-02 01:09:54 +01:00
|
|
|
/**
|
|
|
|
* 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 + window.location.search,
|
|
|
|
state: Reveal.getState()
|
|
|
|
} ), '*' );
|
|
|
|
}, 500 );
|
|
|
|
|
|
|
|
window.addEventListener( 'message', function( event ) {
|
|
|
|
var data = JSON.parse( event.data );
|
|
|
|
if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
|
|
|
|
clearInterval( connectInterval );
|
|
|
|
onConnected();
|
|
|
|
}
|
2019-06-13 12:31:37 +02:00
|
|
|
if( data && data.namespace === 'reveal-notes' && data.type === 'call' ) {
|
|
|
|
callRevealApi( data.methodName, data.arguments, data.callId );
|
|
|
|
}
|
2017-03-02 01:09:54 +01:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
/**
|
|
|
|
* Calls the specified Reveal.js method with the provided argument
|
|
|
|
* and then pushes the result to the notes frame.
|
|
|
|
*/
|
|
|
|
function callRevealApi( methodName, methodArguments, callId ) {
|
|
|
|
|
|
|
|
var result = Reveal[methodName].apply( Reveal, methodArguments );
|
|
|
|
notesPopup.postMessage( JSON.stringify( {
|
|
|
|
namespace: 'reveal-notes',
|
|
|
|
type: 'return',
|
|
|
|
result: result,
|
|
|
|
callId: callId
|
|
|
|
} ), '*' );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 01:09:54 +01:00
|
|
|
/**
|
|
|
|
* Posts the current slide data to the notes window
|
|
|
|
*/
|
2017-11-20 23:45:27 +01:00
|
|
|
function post( event ) {
|
2017-03-02 01:09:54 +01:00
|
|
|
|
|
|
|
var slideElement = Reveal.getCurrentSlide(),
|
2017-11-20 23:45:27 +01:00
|
|
|
notesElement = slideElement.querySelector( 'aside.notes' ),
|
|
|
|
fragmentElement = slideElement.querySelector( '.current-fragment' );
|
2017-03-02 01:09:54 +01:00
|
|
|
|
|
|
|
var messageData = {
|
|
|
|
namespace: 'reveal-notes',
|
|
|
|
type: 'state',
|
|
|
|
notes: '',
|
|
|
|
markdown: false,
|
|
|
|
whitespace: 'normal',
|
|
|
|
state: Reveal.getState()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Look for notes defined in a slide attribute
|
|
|
|
if( slideElement.hasAttribute( 'data-notes' ) ) {
|
|
|
|
messageData.notes = slideElement.getAttribute( 'data-notes' );
|
|
|
|
messageData.whitespace = 'pre-wrap';
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:45:27 +01:00
|
|
|
// Look for notes defined in a fragment
|
|
|
|
if( fragmentElement ) {
|
|
|
|
var fragmentNotes = fragmentElement.querySelector( 'aside.notes' );
|
|
|
|
if( fragmentNotes ) {
|
|
|
|
notesElement = fragmentNotes;
|
|
|
|
}
|
|
|
|
else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
|
|
|
|
messageData.notes = fragmentElement.getAttribute( 'data-notes' );
|
|
|
|
messageData.whitespace = 'pre-wrap';
|
|
|
|
|
|
|
|
// In case there are slide notes
|
|
|
|
notesElement = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 01:09:54 +01:00
|
|
|
// Look for notes defined in an aside element
|
|
|
|
if( notesElement ) {
|
|
|
|
messageData.notes = notesElement.innerHTML;
|
|
|
|
messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
notesPopup.postMessage( JSON.stringify( messageData ), '*' );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
connect();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
return {
|
|
|
|
init: function() {
|
2017-03-02 01:09:54 +01:00
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
if( !/receiver/i.test( window.location.search ) ) {
|
2017-03-02 01:09:54 +01:00
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
// If the there's a 'notes' query set, open directly
|
|
|
|
if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
|
|
|
|
openNotes();
|
|
|
|
}
|
2017-03-02 01:09:54 +01:00
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
// Open the notes when the 's' key is hit
|
|
|
|
Reveal.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
|
|
|
|
openNotes();
|
|
|
|
} );
|
2017-03-02 01:09:54 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
},
|
2017-03-02 01:09:54 +01:00
|
|
|
|
2019-06-13 12:31:37 +02:00
|
|
|
open: openNotes
|
|
|
|
};
|
2017-03-02 01:09:54 +01:00
|
|
|
|
|
|
|
})();
|
2019-06-13 12:31:37 +02:00
|
|
|
|
|
|
|
Reveal.registerPlugin( 'notes', RevealNotes );
|