Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bundler-friendly dependency injection #2627

Merged
merged 2 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,13 @@ You can add your own extensions using the same syntax. The following properties
- **callback**: [optional] Function to execute when the script has loaded
- **condition**: [optional] Function which must return true for the script to be loaded

You can additionally use the following syntax, in case you are using a bundler:
- **id**: the id of the plugin to load
- **plugin**: the plugin object to load. It is the plugin implementation that can contain an `init` function
- **async**: [optional] Flags if the script should load after reveal.js has started, defaults to false
- **callback**: [optional] Function to execute when the script has loaded
- **condition**: [optional] Function which must return true for the script to be loaded

### Ready Event

A `ready` event is fired when reveal.js has loaded all non-async dependencies and is ready to start navigating. To check if reveal.js is already 'ready' you can call `Reveal.isReady()`.
Expand Down
33 changes: 22 additions & 11 deletions js/controllers/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,22 @@ export default class Plugins {
if( scripts.length ) {
scriptsToLoad = scripts.length;

// Load synchronous scripts
scripts.forEach( s => {
loadScript( s.src, () => {

if( typeof s.callback === 'function' ) s.callback();
const scriptLoadedCallback = (s) => {
if( typeof s.callback === 'function' ) s.callback();

if( --scriptsToLoad === 0 ) {
this.initPlugins().then( resolve );
}
if( --scriptsToLoad === 0 ) {
this.initPlugins().then( resolve );
}
};

} );
// Load synchronous scripts
scripts.forEach( s => {
if (s.id) {
this.registerPlugin(s.id, s.plugin);
scriptLoadedCallback(s);
} else {
loadScript( s.src, () => scriptLoadedCallback(s));
}
} );
}
else {
Expand Down Expand Up @@ -129,7 +134,13 @@ export default class Plugins {

if( this.asyncDependencies.length ) {
this.asyncDependencies.forEach( s => {
loadScript( s.src, s.callback );
if (s.id) {
this.registerPlugin(s.id, s.plugin);
if (typeof s.plugin.init === 'function') { s.plugin.init(); }
if (typeof s.callback === 'function') { s.callback(); }
} else {
loadScript( s.src, s.callback );
}
} );
}

Expand Down Expand Up @@ -190,4 +201,4 @@ export default class Plugins {

}

}
}
2 changes: 1 addition & 1 deletion js/reveal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4238,4 +4238,4 @@ export default function( revealElement, options ) {

} );

};
};