feat(slides): add timed playback and looping controls
This commit is contained in:
1 parent
fdf6dffc84
commit
1fd2de4c6b
7 files changed
+94
-8
No files matched your search
+18
-5
@@ -54,26 +54,39 @@
|
||||
const meta=doc.createElement('meta');meta.httpEquiv='Content-Security-Policy';
|
||||
meta.content=`default-src 'none'; script-src 'nonce-${nonce}'; script-src-attr 'none'; style-src ${origin} 'unsafe-inline'; img-src ${origin}; connect-src 'none'; object-src 'none'; frame-src 'none'; base-uri 'none'; form-action 'none'`;
|
||||
doc.head.prepend(meta);
|
||||
for(const file of ['/static/vendor/reveal/reveal.css','/static/css/slides-frame.css']){const link=doc.createElement('link');link.rel='stylesheet';link.href=origin+file;doc.head.append(link);}
|
||||
for(const file of ['/static/vendor/reveal/reveal.css','/static/css/slides-frame.css?v=2']){const link=doc.createElement('link');link.rel='stylesheet';link.href=origin+file;doc.head.append(link);}
|
||||
const colors=getComputedStyle(host),style=doc.createElement('style');
|
||||
// Computed colors only; no authored CSS enters this isolated document.
|
||||
style.textContent=`html,body,.reveal-viewport{background:${colors.backgroundColor==='rgba(0, 0, 0, 0)'?getComputedStyle(document.body).backgroundColor:colors.backgroundColor}!important;color:${colors.color}!important}`;doc.head.append(style);
|
||||
const root=doc.createElement('div');root.className='reveal';const container=doc.createElement('div');container.className='slides';
|
||||
for(const section of sections)container.append(doc.importNode(section,true));root.append(container);doc.body.append(root);
|
||||
doc.body.dataset.token=token;doc.body.dataset.index=host.dataset.slideIndex||'0';
|
||||
for(const file of ['/static/vendor/reveal/reveal.js','/static/js/slides-frame.js']){const script=doc.createElement('script');script.setAttribute('nonce',nonce);script.src=origin+file;doc.body.append(script);}
|
||||
for(const file of ['/static/vendor/reveal/reveal.js','/static/js/slides-frame.js?v=2']){const script=doc.createElement('script');script.setAttribute('nonce',nonce);script.src=origin+file;doc.body.append(script);}
|
||||
const frame=document.createElement('iframe');frame.className='slides-frame';frame.title=document.documentElement.lang.startsWith('ja')?'スライド':'Slides';
|
||||
frame.setAttribute('sandbox','allow-scripts allow-popups');frame.setAttribute('allow','fullscreen');frame.referrerPolicy='no-referrer';
|
||||
const fallback=document.createElement('div');fallback.className='slides-fallback';for(const section of sections)fallback.append(section);
|
||||
const ja=document.documentElement.lang.startsWith('ja'),text=(en,jp)=>ja?jp:en;
|
||||
const controls=document.createElement('div');controls.className='slides-playback';controls.setAttribute('role','group');controls.ariaLabel=text('Slideshow','スライドショー');
|
||||
const play=document.createElement('button');play.type='button';play.textContent='▶';play.setAttribute('aria-pressed','false');play.ariaLabel=play.title=text('Play / pause slideshow','スライドショーの再生/一時停止');play.disabled=true;
|
||||
const secondsLabel=document.createElement('label'),seconds=document.createElement('input');seconds.type='number';seconds.min='1';seconds.max='3600';seconds.step='1';seconds.value=host.dataset.slideSeconds||'30';seconds.ariaLabel=text('Seconds per slide','ページ送り間隔(秒)');secondsLabel.append(seconds,document.createTextNode(text(' sec',' 秒')));
|
||||
const loopLabel=document.createElement('label'),loop=document.createElement('input');loop.type='checkbox';loop.checked=host.dataset.slideLoop==='true';loopLabel.append(loop,document.createTextNode(text(' Loop',' ループ')));
|
||||
controls.append(play,secondsLabel,loopLabel);
|
||||
function configure(){
|
||||
if(!seconds.checkValidity())return false;
|
||||
host.dataset.slideSeconds=seconds.value;host.dataset.slideLoop=String(loop.checked);
|
||||
frame.contentWindow.postMessage({type:'slides-config',token,seconds:Number(seconds.value),loop:loop.checked,playing:host.dataset.slidePlaying==='true'},'*');return true;
|
||||
}
|
||||
play.onclick=()=>{if(!seconds.reportValidity())return;host.dataset.slidePlaying=String(host.dataset.slidePlaying!=='true');configure();};seconds.onchange=loop.onchange=()=>configure();
|
||||
// Keep a static projection available when the local runtime fails to load.
|
||||
host.classList.add('slides-preview');host.replaceChildren(frame,fallback);
|
||||
host.classList.add('slides-preview');host.replaceChildren(frame,fallback,controls);
|
||||
const listener=event=>{
|
||||
if(event.source!==frame.contentWindow||event.data?.token!==token)return;
|
||||
if(event.data.type==='slides-ready'){frame.dataset.ready='true';frame.dataset.count=String(sections.length);frame.hidden=false;fallback.hidden=true;clearTimeout(timer);}
|
||||
if(event.data.type==='slides-ready'){frame.dataset.ready='true';frame.dataset.count=String(sections.length);frame.hidden=false;fallback.hidden=true;clearTimeout(timer);play.disabled=false;configure();}
|
||||
if(event.data.type==='slides-playback'&&typeof event.data.playing==='boolean'){host.dataset.slidePlaying=String(event.data.playing);play.setAttribute('aria-pressed',String(event.data.playing));play.textContent=event.data.playing?'❚❚':'▶';}
|
||||
if(['slides-index','slides-ready'].includes(event.data.type)&&Number.isInteger(event.data.index)&&event.data.index>=0&&event.data.index<sections.length)host.dataset.slideIndex=String(event.data.index);
|
||||
};
|
||||
window.addEventListener('message',listener);
|
||||
const timer=setTimeout(()=>{if(frame.dataset.ready!=='true'){frame.hidden=true;fallback.hidden=false;}},8000);
|
||||
const timer=setTimeout(()=>{if(frame.dataset.ready!=='true'){frame.hidden=true;fallback.hidden=false;play.disabled=true;host.dataset.slidePlaying='false';}},8000);
|
||||
views.set(host,{listener,timer});frame.srcdoc='<!doctype html>'+doc.documentElement.outerHTML;
|
||||
}
|
||||
window.PagesSlides={render,clear};
|
||||
|
||||
Reference in new issue
Block a user