Files
pages/static/js/slides-frame.js
T

28 lines
1.6 KiB
JavaScript

(() => {
'use strict';
const token=document.body.dataset.token;
let interval=30,loop=false,playing=false,timer;
const state=()=>parent.postMessage({type:'slides-playback',token,playing,seconds:interval,loop},'*');
function schedule(){clearTimeout(timer);if(!playing||document.hidden)return;timer=setTimeout(()=>{if(deck.isLastSlide()){if(loop)deck.slide(0);else{playing=false;state();return;}}else deck.next();schedule();},interval*1000);}
const deck=new Reveal(document.querySelector('.reveal'),{
embedded:true,hash:false,history:false,respondToHashChanges:false,
transition:'none',backgroundTransition:'none',controls:true,progress:true,
slideNumber:'c/t',autoSlide:0,autoPlayMedia:false,postMessage:false,
postMessageEvents:false,view:'slide',scrollActivationWidth:null,width:960,height:700,
});
deck.initialize().then(()=>{
deck.slide(Math.min(Number(document.body.dataset.index)||0,deck.getTotalSlides()-1));
parent.postMessage({type:'slides-ready',token,index:deck.getIndices().h},'*');
deck.on('slidechanged',()=>{parent.postMessage({type:'slides-index',token,index:deck.getIndices().h},'*');schedule();});
window.addEventListener('message',event=>{
const data=event.data;
if(event.source!==parent||!data||data.token!==token||data.type!=='slides-config')return;
if(!Number.isInteger(data.seconds)||data.seconds<1||data.seconds>3600||typeof data.loop!=='boolean'||typeof data.playing!=='boolean')return;
interval=data.seconds;loop=data.loop;playing=data.playing;
if(playing&&deck.isLastSlide()&&!loop)deck.slide(0);
schedule();state();
});
document.addEventListener('visibilitychange',schedule);
});
})();