From 8468d82433cd04952e1fdc0ce68363a2cf618b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Jur=C4=8Da?= Date: Sun, 18 Feb 2018 21:30:17 +0100 Subject: [PATCH 1/3] fixed showing speaker's view with timings/pacing while serving the presentation from the file system --- js/reveal.js | 16 ++++ plugin/notes/notes.html | 187 ++++++++++++++++++++++++---------------- plugin/notes/notes.js | 15 +++- 3 files changed, 141 insertions(+), 77 deletions(-) diff --git a/js/reveal.js b/js/reveal.js index 230d001..091372e 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -3901,6 +3901,20 @@ } + function getSlidesMetaInfo() { + + var slides = getSlides(); + return slides.map( function (slide) { + var meta = {}; + for( var i = 0; i < slide.attributes.length; i++ ) { + var attribute = slide.attributes[ i ]; + meta[ attribute.name ] = attribute.value; + } + return meta; + } ); + + } + /** * Retrieves the total number of slides in this presentation. * @@ -5252,6 +5266,8 @@ // Returns an Array of all slides getSlides: getSlides, + getSlidesMetaInfo: getSlidesMetaInfo, + // Returns the total number of slides getTotalSlides: getTotalSlides, diff --git a/plugin/notes/notes.html b/plugin/notes/notes.html index 5b75d73..eff1275 100644 --- a/plugin/notes/notes.html +++ b/plugin/notes/notes.html @@ -329,6 +329,8 @@ upcomingSlide, layoutLabel, layoutDropdown, + pendingCalls = {}, + lastRevealApiCallId = 0, connected = false; var SPEAKER_LAYOUTS = { @@ -356,6 +358,10 @@ else if( data.type === 'state' ) { handleStateMessage( data ); } + else if( data.type === 'return' ) { + pendingCalls[data.callId](data.result); + delete pendingCalls[data.callId]; + } } // Messages sent by the reveal.js inside of the current slide preview else if( data && data.namespace === 'reveal' ) { @@ -372,6 +378,18 @@ } ); + function callRevealApi( methodName, methodArguments, callback ) { + var callId = ++lastRevealApiCallId; + pendingCalls[callId] = callback; + window.opener.postMessage( JSON.stringify( { + namespace: 'reveal-notes', + type: 'call', + callId: callId, + methodName: methodName, + arguments: methodArguments + } ), '*' ); + } + /** * Called when the main window is trying to establish a * connection. @@ -486,28 +504,34 @@ } - function getTimings() { + function getTimings( callback ) { - var slides = Reveal.getSlides(); - var defaultTiming = Reveal.getConfig().defaultTiming; - if (defaultTiming == null) { - return null; - } - var timings = []; - for ( var i in slides ) { - var slide = slides[i]; - var timing = defaultTiming; - if( slide.hasAttribute( 'data-timing' )) { - var t = slide.getAttribute( 'data-timing' ); - timing = parseInt(t); - if( isNaN(timing) ) { - console.warn("Could not parse timing '" + t + "' of slide " + i + "; using default of " + defaultTiming); - timing = defaultTiming; + callRevealApi( 'getSlidesMetaInfo', [], function ( slides ) { + callRevealApi( 'getConfig', [], function ( config ) { + var defaultTiming = config.defaultTiming; + if (defaultTiming == null) { + callback(null); + return; } - } - timings.push(timing); - } - return timings; + + var timings = []; + for ( var i in slides ) { + var slide = slides[ i ]; + var timing = defaultTiming; + if( slide.hasOwnProperty( 'data-timing' )) { + var t = slide[ 'data-timing' ]; + timing = parseInt(t); + if( isNaN(timing) ) { + console.warn("Could not parse timing '" + t + "' of slide " + i + "; using default of " + defaultTiming); + timing = defaultTiming; + } + } + timings.push(timing); + } + + callback( timings ); + } ); + } ); } @@ -515,15 +539,15 @@ * Return the number of seconds allocated for presenting * all slides up to and including this one. */ - function getTimeAllocated(timings) { + function getTimeAllocated( timings, callback ) { - var slides = Reveal.getSlides(); - var allocated = 0; - var currentSlide = Reveal.getSlidePastCount(); - for (var i in slides.slice(0, currentSlide + 1)) { - allocated += timings[i]; - } - return allocated; + callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) { + var allocated = 0; + for (var i in timings.slice(0, currentSlide + 1)) { + allocated += timings[i]; + } + callback( allocated ); + } ); } @@ -545,12 +569,51 @@ pacingMinutesEl = pacingEl.querySelector( '.minutes-value' ), pacingSecondsEl = pacingEl.querySelector( '.seconds-value' ); - var timings = getTimings(); - if (timings !== null) { - pacingTitleEl.style.removeProperty('display'); - pacingEl.style.removeProperty('display'); + var timings = null; + getTimings( function ( _timings ) { + + timings = _timings; + if (_timings !== null) { + pacingTitleEl.style.removeProperty('display'); + pacingEl.style.removeProperty('display'); + } + + // Update once directly + _updateTimer(); + + // Then update every second + setInterval( _updateTimer, 1000 ); + + } ); + + + function _resetTimer() { + + if (timings == null) { + start = new Date(); + _updateTimer(); + } + else { + // Reset timer to beginning of current slide + getTimeAllocated( timings, function ( slideEndTimingSeconds ) { + var slideEndTiming = slideEndTimingSeconds * 1000; + callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) { + var currentSlideTiming = timings[currentSlide] * 1000; + var previousSlidesTiming = slideEndTiming - currentSlideTiming; + var now = new Date(); + start = new Date(now.getTime() - previousSlidesTiming); + _updateTimer(); + } ); + } ); + } + } + timeEl.addEventListener( 'click', function() { + _resetTimer(); + return false; + } ); + function _displayTime( hrEl, minEl, secEl, time) { var sign = Math.sign(time) == -1 ? "-" : ""; @@ -592,52 +655,26 @@ function _updatePacing(diff) { - var slideEndTiming = getTimeAllocated(timings) * 1000; - var currentSlide = Reveal.getSlidePastCount(); - var currentSlideTiming = timings[currentSlide] * 1000; - var timeLeftCurrentSlide = slideEndTiming - diff; - if (timeLeftCurrentSlide < 0) { - pacingEl.className = 'pacing behind'; - } - else if (timeLeftCurrentSlide < currentSlideTiming) { - pacingEl.className = 'pacing on-track'; - } - else { - pacingEl.className = 'pacing ahead'; - } - _displayTime( pacingHoursEl, pacingMinutesEl, pacingSecondsEl, timeLeftCurrentSlide ); + getTimeAllocated( timings, function ( slideEndTimingSeconds ) { + var slideEndTiming = slideEndTimingSeconds * 1000; + callRevealApi( 'getSlidePastCount', [], function ( currentSlide ) { + var currentSlideTiming = timings[currentSlide] * 1000; + var timeLeftCurrentSlide = slideEndTiming - diff; + if (timeLeftCurrentSlide < 0) { + pacingEl.className = 'pacing behind'; + } + else if (timeLeftCurrentSlide < currentSlideTiming) { + pacingEl.className = 'pacing on-track'; + } + else { + pacingEl.className = 'pacing ahead'; + } + _displayTime( pacingHoursEl, pacingMinutesEl, pacingSecondsEl, timeLeftCurrentSlide ); + } ); + } ); } - // Update once directly - _updateTimer(); - - // Then update every second - setInterval( _updateTimer, 1000 ); - - function _resetTimer() { - - if (timings == null) { - start = new Date(); - } - else { - // Reset timer to beginning of current slide - var slideEndTiming = getTimeAllocated(timings) * 1000; - var currentSlide = Reveal.getSlidePastCount(); - var currentSlideTiming = timings[currentSlide] * 1000; - var previousSlidesTiming = slideEndTiming - currentSlideTiming; - var now = new Date(); - start = new Date(now.getTime() - previousSlidesTiming); - } - _updateTimer(); - - } - - timeEl.addEventListener( 'click', function() { - _resetTimer(); - return false; - } ); - } /** diff --git a/plugin/notes/notes.js b/plugin/notes/notes.js index 3f00eb6..dd7df8e 100644 --- a/plugin/notes/notes.js +++ b/plugin/notes/notes.js @@ -21,8 +21,6 @@ var RevealNotes = (function() { var notesPopup = window.open( notesFilePath, 'reveal.js - Notes', 'width=1100,height=700' ); - // Allow popup window access to Reveal API - notesPopup.Reveal = this.Reveal; /** * Connect to the notes window through a postmessage handshake. @@ -47,9 +45,22 @@ var RevealNotes = (function() { clearInterval( connectInterval ); onConnected(); } + if( data && data.namespace === 'reveal-notes' && data.type === 'call' ) { + callRevealApi( data.methodName, data.arguments, data.callId ); + } } ); } + function callRevealApi( methodName, methodArguments, callId ) { + var result = Reveal[methodName].call(Reveal, methodArguments); + notesPopup.postMessage( JSON.stringify( { + namespace: 'reveal-notes', + type: 'return', + result: result, + callId: callId + } ), '*' ); + } + /** * Posts the current slide data to the notes window */ From 6a2c5b4de833c73f656945253e7b72e31870c88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Jur=C4=8Da?= Date: Tue, 20 Feb 2018 10:30:56 +0100 Subject: [PATCH 2/3] documentation --- js/reveal.js | 3 +++ plugin/notes/notes.html | 3 +++ plugin/notes/notes.js | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/js/reveal.js b/js/reveal.js index 091372e..9a1422f 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -3901,6 +3901,9 @@ } + /** + * Returns an array of objects where each object represents the attributes on its respective slide. + */ function getSlidesMetaInfo() { var slides = getSlides(); diff --git a/plugin/notes/notes.html b/plugin/notes/notes.html index eff1275..a6bd5e2 100644 --- a/plugin/notes/notes.html +++ b/plugin/notes/notes.html @@ -378,6 +378,9 @@ } ); + /** + * Asynchronously calls the Reveal.js API of the main frame. + */ function callRevealApi( methodName, methodArguments, callback ) { var callId = ++lastRevealApiCallId; pendingCalls[callId] = callback; diff --git a/plugin/notes/notes.js b/plugin/notes/notes.js index dd7df8e..dce9b4e 100644 --- a/plugin/notes/notes.js +++ b/plugin/notes/notes.js @@ -51,6 +51,10 @@ var RevealNotes = (function() { } ); } + /** + * 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].call(Reveal, methodArguments); notesPopup.postMessage( JSON.stringify( { From 250580fc4c97f83185e46f9d0e22286d0a01d3af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Jur=C4=8Da?= Date: Tue, 20 Feb 2018 16:08:29 +0100 Subject: [PATCH 3/3] added missing documentation --- index.html | 23 ++++++++++++++++++++--- js/reveal.js | 2 ++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 98accc3..e3e10c1 100644 --- a/index.html +++ b/index.html @@ -24,8 +24,23 @@
-
Slide 1
-
Slide 2
+
+ Slide 1 + + + +
+
+ Slide 2 + + +
@@ -42,7 +57,9 @@ { src: 'plugin/markdown/markdown.js' }, { src: 'plugin/notes/notes.js', async: true }, { src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } } - ] + ], + controlsTutorial: false, + defaultTiming: 3 }); diff --git a/js/reveal.js b/js/reveal.js index 9a1422f..12d67bf 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -5269,6 +5269,8 @@ // Returns an Array of all slides getSlides: getSlides, + // Returns an Array of objects representing the attributes on + // the slides getSlidesMetaInfo: getSlidesMetaInfo, // Returns the total number of slides