From c40e9819a24922dd1f16ea7b0bb809127978a21b Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Mon, 4 Mar 2019 14:32:38 +0100 Subject: highlight, math, notes and zoom plugins now register via Reveal.registerPlugin --- plugin/zoom-js/zoom.js | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'plugin/zoom-js') diff --git a/plugin/zoom-js/zoom.js b/plugin/zoom-js/zoom.js index 8531790..1a731d3 100644 --- a/plugin/zoom-js/zoom.js +++ b/plugin/zoom-js/zoom.js @@ -1,29 +1,37 @@ // Custom reveal.js integration -(function(){ - var revealElement = document.querySelector( '.reveal' ); - if( revealElement ) { +var RevealZoom = (function(){ - revealElement.addEventListener( 'mousedown', function( event ) { - var defaultModifier = /Linux/.test( window.navigator.platform ) ? 'ctrl' : 'alt'; + return { + init: function() { - var modifier = ( Reveal.getConfig().zoomKey ? Reveal.getConfig().zoomKey : defaultModifier ) + 'Key'; - var zoomLevel = ( Reveal.getConfig().zoomLevel ? Reveal.getConfig().zoomLevel : 2 ); + Reveal.getRevealElement().addEventListener( 'mousedown', function( event ) { + var defaultModifier = /Linux/.test( window.navigator.platform ) ? 'ctrl' : 'alt'; - if( event[ modifier ] && !Reveal.isOverview() ) { - event.preventDefault(); + var modifier = ( Reveal.getConfig().zoomKey ? Reveal.getConfig().zoomKey : defaultModifier ) + 'Key'; + var zoomLevel = ( Reveal.getConfig().zoomLevel ? Reveal.getConfig().zoomLevel : 2 ); - zoom.to({ - x: event.clientX, - y: event.clientY, - scale: zoomLevel, - pan: false - }); - } - } ); + if( event[ modifier ] && !Reveal.isOverview() ) { + event.preventDefault(); + + zoom.to({ + x: event.clientX, + y: event.clientY, + scale: zoomLevel, + pan: false + }); + } + } ); + // lofi xbrowser Promise.resolve() + return { then: function( resolve ) { resolve(); }}; + + } } + })(); +Reveal.registerPlugin( 'zoom', RevealZoom ); + /*! * zoom.js 0.3 (modified for use with reveal.js) * http://lab.hakim.se/zoom-js -- cgit v1.2.3 From 5301a9ea03a2c5abe857105acbc224c532a8c0e8 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Thu, 14 Mar 2019 13:05:01 +0100 Subject: more forgiving code highlight line number format, add Promise polyfill --- lib/js/promise.js | 2 ++ plugin/highlight/highlight.js | 8 +++++--- plugin/math/math.js | 3 +-- plugin/notes/notes.js | 3 +-- plugin/zoom-js/zoom.js | 3 +-- 5 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 lib/js/promise.js (limited to 'plugin/zoom-js') diff --git a/lib/js/promise.js b/lib/js/promise.js new file mode 100644 index 0000000..88e9b19 --- /dev/null +++ b/lib/js/promise.js @@ -0,0 +1,2 @@ +/* MIT | https://github.com/taylorhakes/promise-polyfill */ +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n():"function"==typeof define&&define.amd?define(n):n()}(0,function(){"use strict";function e(e){var n=this.constructor;return this.then(function(t){return n.resolve(e()).then(function(){return t})},function(t){return n.resolve(e()).then(function(){return n.reject(t)})})}function n(){}function t(e){if(!(this instanceof t))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=undefined,this._deferreds=[],u(e,this)}function o(e,n){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,t._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null!==t){var o;try{o=t(e._value)}catch(f){return void i(n.promise,f)}r(n.promise,o)}else(1===e._state?r:i)(n.promise,e._value)})):e._deferreds.push(n)}function r(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var o=n.then;if(n instanceof t)return e._state=3,e._value=n,void f(e);if("function"==typeof o)return void u(function(e,n){return function(){e.apply(n,arguments)}}(o,n),e)}e._state=1,e._value=n,f(e)}catch(r){i(e,r)}}function i(e,n){e._state=2,e._value=n,f(e)}function f(e){2===e._state&&0===e._deferreds.length&&t._immediateFn(function(){e._handled||t._unhandledRejectionFn(e._value)});for(var n=0,r=e._deferreds.length;r>n;n++)o(e,e._deferreds[n]);e._deferreds=null}function u(e,n){var t=!1;try{e(function(e){t||(t=!0,r(n,e))},function(e){t||(t=!0,i(n,e))})}catch(o){if(t)return;t=!0,i(n,o)}}var c=setTimeout;t.prototype["catch"]=function(e){return this.then(null,e)},t.prototype.then=function(e,t){var r=new this.constructor(n);return o(this,new function(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}(e,t,r)),r},t.prototype["finally"]=e,t.all=function(e){return new t(function(n,t){function o(e,f){try{if(f&&("object"==typeof f||"function"==typeof f)){var u=f.then;if("function"==typeof u)return void u.call(f,function(n){o(e,n)},t)}r[e]=f,0==--i&&n(r)}catch(c){t(c)}}if(!e||"undefined"==typeof e.length)throw new TypeError("Promise.all accepts an array");var r=Array.prototype.slice.call(e);if(0===r.length)return n([]);for(var i=r.length,f=0;r.length>f;f++)o(f,r[f])})},t.resolve=function(e){return e&&"object"==typeof e&&e.constructor===t?e:new t(function(n){n(e)})},t.reject=function(e){return new t(function(n,t){t(e)})},t.race=function(e){return new t(function(n,t){for(var o=0,r=e.length;r>o;o++)e[o].then(n,t)})},t._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){c(e,0)},t._unhandledRejectionFn=function(e){void 0!==console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)};var l=function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw Error("unable to locate global object")}();"Promise"in l?l.Promise.prototype["finally"]||(l.Promise.prototype["finally"]=e):l.Promise=t}); \ No newline at end of file diff --git a/plugin/highlight/highlight.js b/plugin/highlight/highlight.js index 446cfe1..6d6910b 100644 --- a/plugin/highlight/highlight.js +++ b/plugin/highlight/highlight.js @@ -97,8 +97,7 @@ c:[{cN:"comment",b:/\(\*/,e:/\*\)/},e.ASM,e.QSM,e.CNM,{b:/\{/,e:/\}/,i:/:/}]}}); } } ); - // lofi xbrowser Promise.resolve() - return { then: function( resolve ) { resolve(); }}; + return Promise.resolve(); }, @@ -139,10 +138,13 @@ c:[{cN:"comment",b:/\(\*/,e:/\*\)/},e.ASM,e.QSM,e.CNM,{b:/\{/,e:/\}/,i:/:/}]}}); linesToHighlight.split( ',' ).forEach( function( lineNumbers ) { + // Avoid failures becase of whitespace + lineNumbers = lineNumbers.replace( /\s/g, '' ); + // Ensure that we looking at a valid slide number (1 or 1-2) if( /^[\d-]+$/.test( lineNumbers ) ) { - lineNumbers = lineNumbers.split( '-' ) + lineNumbers = lineNumbers.split( '-' ); var lineStart = lineNumbers[0]; var lineEnd = lineNumbers[1] || lineStart; diff --git a/plugin/math/math.js b/plugin/math/math.js index a640e1f..b78d120 100755 --- a/plugin/math/math.js +++ b/plugin/math/math.js @@ -84,8 +84,7 @@ var RevealMath = window.RevealMath || (function(){ } ); - // lofi xbrowser Promise.resolve() - return { then: function( resolve ) { resolve(); }}; + return Promise.resolve(); } } diff --git a/plugin/notes/notes.js b/plugin/notes/notes.js index 874e66d..54dcf31 100644 --- a/plugin/notes/notes.js +++ b/plugin/notes/notes.js @@ -168,8 +168,7 @@ var RevealNotes = (function() { } - // lofi xbrowser Promise.resolve() - return { then: function( resolve ) { resolve(); }}; + return Promise.resolve(); }, diff --git a/plugin/zoom-js/zoom.js b/plugin/zoom-js/zoom.js index 1a731d3..031514d 100644 --- a/plugin/zoom-js/zoom.js +++ b/plugin/zoom-js/zoom.js @@ -22,8 +22,7 @@ var RevealZoom = (function(){ } } ); - // lofi xbrowser Promise.resolve() - return { then: function( resolve ) { resolve(); }}; + return Promise.resolve(); } } -- cgit v1.2.3 From 6410ed15aa5ce2bdfc1f7f02c818a562ec8a1fc4 Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Thu, 14 Mar 2019 14:52:59 +0100 Subject: support for plugins where the init method doesn't return a Promise --- js/reveal.js | 29 +++++++++++++++++------------ plugin/highlight/highlight.js | 2 -- plugin/math/math.js | 2 -- plugin/notes/notes.js | 2 -- plugin/zoom-js/zoom.js | 2 -- 5 files changed, 17 insertions(+), 20 deletions(-) (limited to 'plugin/zoom-js') diff --git a/js/reveal.js b/js/reveal.js index 1ef10aa..51b1ed0 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -539,29 +539,34 @@ var pluginsToInitialize = Object.keys( plugins ).length; + var afterPlugInitialized = function() { + if( --pluginsToInitialize === 0 ) { + loadAsyncDependencies(); + } + }; + for( var i in plugins ) { var plugin = plugins[i]; - // If the plugin has an 'init' method, initialize and - // wait for the callback + // If the plugin has an 'init' method, invoke it if( typeof plugin.init === 'function' ) { - plugin.init().then( function() { - if( --pluginsToInitialize === 0 ) { - loadAsyncDependencies(); - } - } ); + var callback = plugin.init(); + + // If the plugin returned a Promise, wait for it + if( callback && typeof callback.then === 'function' ) { + callback.then( afterPlugInitialized ); + } + else { + afterPlugInitialized(); + } } else { - pluginsToInitialize -= 1; + afterPlugInitialized(); } } - if( pluginsToInitialize === 0 ) { - loadAsyncDependencies(); - } - } /** diff --git a/plugin/highlight/highlight.js b/plugin/highlight/highlight.js index 6d6910b..b3599e8 100644 --- a/plugin/highlight/highlight.js +++ b/plugin/highlight/highlight.js @@ -97,8 +97,6 @@ c:[{cN:"comment",b:/\(\*/,e:/\*\)/},e.ASM,e.QSM,e.CNM,{b:/\{/,e:/\}/,i:/:/}]}}); } } ); - return Promise.resolve(); - }, /** diff --git a/plugin/math/math.js b/plugin/math/math.js index b78d120..d76c9dd 100755 --- a/plugin/math/math.js +++ b/plugin/math/math.js @@ -84,8 +84,6 @@ var RevealMath = window.RevealMath || (function(){ } ); - return Promise.resolve(); - } } diff --git a/plugin/notes/notes.js b/plugin/notes/notes.js index 54dcf31..3d5eac4 100644 --- a/plugin/notes/notes.js +++ b/plugin/notes/notes.js @@ -168,8 +168,6 @@ var RevealNotes = (function() { } - return Promise.resolve(); - }, open: openNotes diff --git a/plugin/zoom-js/zoom.js b/plugin/zoom-js/zoom.js index 031514d..92c3ba5 100644 --- a/plugin/zoom-js/zoom.js +++ b/plugin/zoom-js/zoom.js @@ -22,8 +22,6 @@ var RevealZoom = (function(){ } } ); - return Promise.resolve(); - } } -- cgit v1.2.3