Skip to content

Commit 0bbcc6b

Browse files
committed
add Reveal.syncSlide
1 parent 042fbde commit 0bbcc6b

File tree

1 file changed

+75
-20
lines changed

1 file changed

+75
-20
lines changed

js/reveal.js

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,57 @@
924924
*/
925925
function createBackground( slide, container ) {
926926

927+
928+
// Main slide background element
929+
var element = document.createElement( 'div' );
930+
element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
931+
932+
// Inner background element that wraps images/videos/iframes
933+
var contentElement = document.createElement( 'div' );
934+
contentElement.className = 'slide-background-content';
935+
936+
element.appendChild( contentElement );
937+
container.appendChild( element );
938+
939+
slide.slideBackgroundElement = element;
940+
slide.slideBackgroundContentElement = contentElement;
941+
942+
// Syncs the background to reflect all current background settings
943+
syncBackground( slide );
944+
945+
return element;
946+
947+
}
948+
949+
/**
950+
* Renders all of the visual properties of a slide background
951+
* based on the various background attributes.
952+
*
953+
* @param {HTMLElement} slide
954+
*/
955+
function syncBackground( slide ) {
956+
957+
var element = slide.slideBackgroundElement,
958+
contentElement = slide.slideBackgroundContentElement;
959+
960+
// Reset the prior background state in case this is not the
961+
// initial sync
962+
slide.classList.remove( 'has-dark-background' );
963+
slide.classList.remove( 'has-light-background' );
964+
965+
element.removeAttribute( 'data-loaded' );
966+
element.removeAttribute( 'data-background-hash' );
967+
element.removeAttribute( 'data-background-size' );
968+
element.removeAttribute( 'data-background-transition' );
969+
element.style.backgroundColor = '';
970+
971+
contentElement.style.backgroundSize = '';
972+
contentElement.style.backgroundRepeat = '';
973+
contentElement.style.backgroundPosition = '';
974+
contentElement.style.backgroundImage = '';
975+
contentElement.style.opacity = '';
976+
contentElement.innerHTML = '';
977+
927978
var data = {
928979
background: slide.getAttribute( 'data-background' ),
929980
backgroundSize: slide.getAttribute( 'data-background-size' ),
@@ -937,14 +988,6 @@
937988
backgroundOpacity: slide.getAttribute( 'data-background-opacity' )
938989
};
939990

940-
// Main slide background element
941-
var element = document.createElement( 'div' );
942-
element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
943-
944-
// Inner background element that wraps images/videos/iframes
945-
var contentElement = document.createElement( 'div' );
946-
contentElement.className = 'slide-background-content';
947-
948991
if( data.background ) {
949992
// Auto-wrap image urls in url(...)
950993
if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)([?#\s]|$)/gi.test( data.background ) ) {
@@ -982,16 +1025,6 @@
9821025
if( data.backgroundPosition ) contentElement.style.backgroundPosition = data.backgroundPosition;
9831026
if( data.backgroundOpacity ) contentElement.style.opacity = data.backgroundOpacity;
9841027

985-
element.appendChild( contentElement );
986-
container.appendChild( element );
987-
988-
// If backgrounds are being recreated, clear old classes
989-
slide.classList.remove( 'has-dark-background' );
990-
slide.classList.remove( 'has-light-background' );
991-
992-
slide.slideBackgroundElement = element;
993-
slide.slideBackgroundContentElement = contentElement;
994-
9951028
// If this slide has a background color, add a class that
9961029
// signals if it is light or dark. If the slide has no background
9971030
// color, no class will be set
@@ -1012,8 +1045,6 @@
10121045
}
10131046
}
10141047

1015-
return element;
1016-
10171048
}
10181049

10191050
/**
@@ -2638,6 +2669,29 @@
26382669

26392670
}
26402671

2672+
/**
2673+
* Updates reveal.js to keep in sync with new slide attributes. For
2674+
* example, if you add a new `data-background-image` you can call
2675+
* this to have reveal.js render the new background image.
2676+
*
2677+
* Similar to #sync() but more efficient when you only need to
2678+
* refresh a specific slide.
2679+
*
2680+
* @param {HTMLElement} slide
2681+
*/
2682+
function syncSlide( slide ) {
2683+
2684+
syncBackground( slide );
2685+
2686+
sortFragments( slide.querySelectorAll( '.fragment' ) );
2687+
2688+
updateBackground();
2689+
updateNotes();
2690+
2691+
loadSlide( slide );
2692+
2693+
}
2694+
26412695
/**
26422696
* Resets all vertical slides so that only the first
26432697
* is visible.
@@ -5233,6 +5287,7 @@
52335287
initialize: initialize,
52345288
configure: configure,
52355289
sync: sync,
5290+
syncSlide: syncSlide,
52365291

52375292
// Navigation methods
52385293
slide: slide,

0 commit comments

Comments
 (0)