2022-05-12 14:53:40 +02:00
|
|
|
import speakerViewHTML from './speaker-view.html'
|
2020-05-18 15:59:18 +02:00
|
|
|
|
2022-02-28 09:57:50 +01:00
|
|
|
import { marked } from 'marked';
|
2020-05-18 20:36:34 +02:00
|
|
|
|
2012-10-21 02:40:52 +02:00
|
|
|
/**
|
|
|
|
* Handles opening of and synchronization with the reveal.js
|
|
|
|
* notes window.
|
2014-04-19 10:54:14 +02:00
|
|
|
*
|
|
|
|
* 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
|
2012-10-21 02:40:52 +02:00
|
|
|
*/
|
2020-04-29 11:56:43 +02:00
|
|
|
const Plugin = () => {
|
2012-10-21 02:40:52 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
let connectInterval;
|
|
|
|
let speakerWindow = null;
|
|
|
|
let deck;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a new speaker view window.
|
|
|
|
*/
|
|
|
|
function openSpeakerWindow() {
|
|
|
|
|
|
|
|
// If a window is already open, focus it
|
|
|
|
if( speakerWindow && !speakerWindow.closed ) {
|
|
|
|
speakerWindow.focus();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
speakerWindow = window.open( 'about:blank', 'reveal.js - Notes', 'width=1100,height=700' );
|
|
|
|
speakerWindow.marked = marked;
|
|
|
|
speakerWindow.document.write( speakerViewHTML );
|
|
|
|
|
|
|
|
if( !speakerWindow ) {
|
|
|
|
alert( 'Speaker view popup failed to open. Please make sure popups are allowed and reopen the speaker view.' );
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 13:05:28 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
connect();
|
|
|
|
}
|
2016-01-08 14:33:34 +01:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
2018-10-05 15:18:08 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
/**
|
|
|
|
* Reconnect with an existing speaker view window.
|
|
|
|
*/
|
|
|
|
function reconnectSpeakerWindow( reconnectWindow ) {
|
2012-10-21 02:40:52 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
if( speakerWindow && !speakerWindow.closed ) {
|
|
|
|
speakerWindow.focus();
|
2018-04-24 14:23:28 +02:00
|
|
|
}
|
2022-02-10 13:28:37 +01:00
|
|
|
else {
|
|
|
|
speakerWindow = reconnectWindow;
|
|
|
|
window.addEventListener( 'message', onPostMessage );
|
|
|
|
onConnected();
|
2014-04-19 10:54:14 +02:00
|
|
|
}
|
2012-10-21 02:40:52 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
2018-10-04 13:26:16 +02:00
|
|
|
|
2022-02-10 13:28:37 +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() {
|
|
|
|
|
2022-02-25 09:42:44 +01:00
|
|
|
const presentationURL = deck.getConfig().url;
|
|
|
|
|
|
|
|
const url = typeof presentationURL === 'string' ? presentationURL :
|
|
|
|
window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search;
|
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
// Keep trying to connect until we get a 'connected' message back
|
|
|
|
connectInterval = setInterval( function() {
|
|
|
|
speakerWindow.postMessage( JSON.stringify( {
|
2018-02-18 21:30:17 +01:00
|
|
|
namespace: 'reveal-notes',
|
2022-02-10 13:28:37 +01:00
|
|
|
type: 'connect',
|
2022-02-25 09:42:44 +01:00
|
|
|
state: deck.getState(),
|
|
|
|
url
|
2018-02-18 21:30:17 +01:00
|
|
|
} ), '*' );
|
2022-02-10 13:28:37 +01:00
|
|
|
}, 500 );
|
2018-10-04 13:26:16 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
window.addEventListener( 'message', onPostMessage );
|
2018-02-18 21:30:17 +01:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
2014-04-19 10:54:14 +02:00
|
|
|
|
2022-02-10 13:28:37 +01: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 ) {
|
2012-10-21 02:40:52 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
let result = deck[methodName].apply( deck, methodArguments );
|
|
|
|
speakerWindow.postMessage( JSON.stringify( {
|
|
|
|
namespace: 'reveal-notes',
|
|
|
|
type: 'return',
|
|
|
|
result,
|
|
|
|
callId
|
|
|
|
} ), '*' );
|
2013-07-26 15:48:21 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
2014-02-17 20:07:41 +01:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
/**
|
|
|
|
* Posts the current slide data to the notes window.
|
|
|
|
*/
|
|
|
|
function post( event ) {
|
|
|
|
|
|
|
|
let slideElement = deck.getCurrentSlide(),
|
|
|
|
notesElement = slideElement.querySelector( 'aside.notes' ),
|
|
|
|
fragmentElement = slideElement.querySelector( '.current-fragment' );
|
|
|
|
|
|
|
|
let messageData = {
|
|
|
|
namespace: 'reveal-notes',
|
|
|
|
type: 'state',
|
|
|
|
notes: '',
|
|
|
|
markdown: false,
|
|
|
|
whitespace: 'normal',
|
|
|
|
state: deck.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-01-31 17:08:08 +01:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
// Look for notes defined in a fragment
|
|
|
|
if( fragmentElement ) {
|
|
|
|
let fragmentNotes = fragmentElement.querySelector( 'aside.notes' );
|
|
|
|
if( fragmentNotes ) {
|
|
|
|
notesElement = fragmentNotes;
|
2017-01-31 17:08:08 +01:00
|
|
|
}
|
2022-02-10 13:28:37 +01:00
|
|
|
else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
|
|
|
|
messageData.notes = fragmentElement.getAttribute( 'data-notes' );
|
|
|
|
messageData.whitespace = 'pre-wrap';
|
2017-01-31 17:08:08 +01:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
// In case there are slide notes
|
|
|
|
notesElement = null;
|
2014-02-17 20:07:41 +01:00
|
|
|
}
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
2014-02-17 20:07:41 +01:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
// Look for notes defined in an aside element
|
|
|
|
if( notesElement ) {
|
|
|
|
messageData.notes = notesElement.innerHTML;
|
|
|
|
messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
|
2012-10-21 02:40:52 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
speakerWindow.postMessage( JSON.stringify( messageData ), '*' );
|
2014-04-19 10:54:14 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
2014-04-19 10:54:14 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
function onPostMessage( event ) {
|
2014-04-19 10:54:14 +02:00
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
let data = JSON.parse( event.data );
|
|
|
|
if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
|
|
|
|
clearInterval( connectInterval );
|
|
|
|
onConnected();
|
|
|
|
}
|
|
|
|
else if( data && data.namespace === 'reveal-notes' && data.type === 'call' ) {
|
|
|
|
callRevealApi( data.methodName, data.arguments, data.callId );
|
2014-04-19 10:54:14 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called once we have established a connection to the notes
|
|
|
|
* window.
|
|
|
|
*/
|
|
|
|
function onConnected() {
|
|
|
|
|
|
|
|
// Monitor events that trigger a change in state
|
|
|
|
deck.on( 'slidechanged', post );
|
|
|
|
deck.on( 'fragmentshown', post );
|
|
|
|
deck.on( 'fragmenthidden', post );
|
|
|
|
deck.on( 'overviewhidden', post );
|
|
|
|
deck.on( 'overviewshown', post );
|
|
|
|
deck.on( 'paused', post );
|
|
|
|
deck.on( 'resumed', post );
|
|
|
|
|
|
|
|
// Post the initial state
|
|
|
|
post();
|
2016-01-08 14:33:34 +01:00
|
|
|
|
2012-10-21 02:40:52 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:11:21 +01:00
|
|
|
return {
|
2020-04-08 13:05:28 +02:00
|
|
|
id: 'notes',
|
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
init: function( reveal ) {
|
2020-04-08 13:05:28 +02:00
|
|
|
|
2020-04-15 10:23:51 +02:00
|
|
|
deck = reveal;
|
2012-10-21 02:40:52 +02:00
|
|
|
|
2019-03-04 14:11:21 +01:00
|
|
|
if( !/receiver/i.test( window.location.search ) ) {
|
|
|
|
|
|
|
|
// If the there's a 'notes' query set, open directly
|
|
|
|
if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
|
2022-02-10 13:28:37 +01:00
|
|
|
openSpeakerWindow();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Keep listening for speaker view hearbeats. If we receive a
|
|
|
|
// heartbeat from an orphaned window, reconnect it. This ensures
|
|
|
|
// that we remain connected to the notes even if the presentation
|
|
|
|
// is reloaded.
|
|
|
|
window.addEventListener( 'message', event => {
|
2022-03-21 10:36:22 +01:00
|
|
|
|
2022-03-21 09:59:49 +01:00
|
|
|
if( !speakerWindow && typeof event.data === 'string' ) {
|
|
|
|
let data;
|
|
|
|
|
|
|
|
try {
|
|
|
|
data = JSON.parse( event.data );
|
|
|
|
}
|
|
|
|
catch( error ) {}
|
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
if( data && data.namespace === 'reveal-notes' && data.type === 'heartbeat' ) {
|
|
|
|
reconnectSpeakerWindow( event.source );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-03-04 14:11:21 +01:00
|
|
|
}
|
2014-09-27 16:19:39 +02:00
|
|
|
|
2019-03-04 14:11:21 +01:00
|
|
|
// Open the notes when the 's' key is hit
|
2020-04-08 13:05:28 +02:00
|
|
|
deck.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
|
2022-02-10 13:28:37 +01:00
|
|
|
openSpeakerWindow();
|
2019-03-04 14:11:21 +01:00
|
|
|
} );
|
2016-01-08 13:49:06 +01:00
|
|
|
|
2019-03-04 14:11:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2022-02-10 13:28:37 +01:00
|
|
|
open: openSpeakerWindow
|
2019-03-04 14:11:21 +01:00
|
|
|
};
|
2014-09-27 16:19:39 +02:00
|
|
|
|
2020-04-29 11:56:43 +02:00
|
|
|
};
|
2019-03-04 14:11:21 +01:00
|
|
|
|
2020-04-29 11:56:43 +02:00
|
|
|
export default Plugin;
|