Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1837 views
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
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' );
23
24
if( !notesPopup ) {
25
alert( 'Speaker view popup failed to open. Please make sure popups are allowed and reopen the speaker view.' );
26
return;
27
}
28
29
// Allow popup window access to Reveal API
30
notesPopup.Reveal = window.Reveal;
31
32
/**
33
* Connect to the notes window through a postmessage handshake.
34
* Using postmessage enables us to work in situations where the
35
* origins differ, such as a presentation being opened from the
36
* file system.
37
*/
38
function connect() {
39
// Keep trying to connect until we get a 'connected' message back
40
var connectInterval = setInterval( function() {
41
notesPopup.postMessage( JSON.stringify( {
42
namespace: 'reveal-notes',
43
type: 'connect',
44
url: window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search,
45
state: Reveal.getState()
46
} ), '*' );
47
}, 500 );
48
49
window.addEventListener( 'message', function( event ) {
50
var data = JSON.parse( event.data );
51
if( data && data.namespace === 'reveal-notes' && data.type === 'connected' ) {
52
clearInterval( connectInterval );
53
onConnected();
54
}
55
} );
56
}
57
58
/**
59
* Posts the current slide data to the notes window
60
*/
61
function post( event ) {
62
63
var slideElement = Reveal.getCurrentSlide(),
64
notesElement = slideElement.querySelector( 'aside.notes' ),
65
fragmentElement = slideElement.querySelector( '.current-fragment' );
66
67
var messageData = {
68
namespace: 'reveal-notes',
69
type: 'state',
70
notes: '',
71
markdown: false,
72
whitespace: 'normal',
73
state: Reveal.getState()
74
};
75
76
// Look for notes defined in a slide attribute
77
if( slideElement.hasAttribute( 'data-notes' ) ) {
78
messageData.notes = slideElement.getAttribute( 'data-notes' );
79
messageData.whitespace = 'pre-wrap';
80
}
81
82
// Look for notes defined in a fragment
83
if( fragmentElement ) {
84
var fragmentNotes = fragmentElement.querySelector( 'aside.notes' );
85
if( fragmentNotes ) {
86
notesElement = fragmentNotes;
87
}
88
else if( fragmentElement.hasAttribute( 'data-notes' ) ) {
89
messageData.notes = fragmentElement.getAttribute( 'data-notes' );
90
messageData.whitespace = 'pre-wrap';
91
92
// In case there are slide notes
93
notesElement = null;
94
}
95
}
96
97
// Look for notes defined in an aside element
98
if( notesElement ) {
99
messageData.notes = notesElement.innerHTML;
100
messageData.markdown = typeof notesElement.getAttribute( 'data-markdown' ) === 'string';
101
}
102
103
notesPopup.postMessage( JSON.stringify( messageData ), '*' );
104
105
}
106
107
/**
108
* Called once we have established a connection to the notes
109
* window.
110
*/
111
function onConnected() {
112
113
// Monitor events that trigger a change in state
114
Reveal.addEventListener( 'slidechanged', post );
115
Reveal.addEventListener( 'fragmentshown', post );
116
Reveal.addEventListener( 'fragmenthidden', post );
117
Reveal.addEventListener( 'overviewhidden', post );
118
Reveal.addEventListener( 'overviewshown', post );
119
Reveal.addEventListener( 'paused', post );
120
Reveal.addEventListener( 'resumed', post );
121
122
// Post the initial state
123
post();
124
125
}
126
127
connect();
128
129
}
130
131
if( !/receiver/i.test( window.location.search ) ) {
132
133
// If the there's a 'notes' query set, open directly
134
if( window.location.search.match( /(\?|\&)notes/gi ) !== null ) {
135
openNotes();
136
}
137
138
// Open the notes when the 's' key is hit
139
Reveal.addKeyBinding({keyCode: 83, key: 'S', description: 'Speaker notes view'}, function() {
140
openNotes();
141
} );
142
143
}
144
145
return { open: openNotes };
146
147
})();
148
149