Fonts: the presenter window's first pictures are ready before it shows
The window's first paint put Now and Next up once their fonts were ready, whatever their pictures were doing, so a slow picture came in after the preview. The font gate now also waits, on the presenter window, for every picture on its slides to decode, until the layout deadline it already gives the editor (3.5 s from navigation start): a broken or missing picture never holds the window past it. Review finding, Important 7.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+91
−3
fixtures/fonts-gate.mjs+17 −1internal/slides/credits/assets.json+1 −1internal/slides/static/fontsgate.09db91091a8e7726.js+1 −0internal/slides/static/fontsgate.971e476eab8b8588.js+0 −1internal/slidestest/presenter_wire_browser_test.go+72 −0
diff --git a/fixtures/fonts-gate.mjs b/fixtures/fonts-gate.mjs| index 12a5a82..68bfd93 100644 |
| --- a/fixtures/fonts-gate.mjs |
| +++ b/fixtures/fonts-gate.mjs |
| @@ -15,7 +15,10 @@ |
| // rest are put on (their faces join document.fonts), still hidden. |
| // 2. On the editor, wait for the editor's first mount — its views bound |
| // to the slide, the stored viewport applied — which it reports with |
| -// settled(), until the layout deadline. |
| +// settled(), until the layout deadline. On the presenter window, wait |
| +// for the pictures of the slides it opens on (Now and Next) to be |
| +// decoded, until the same deadline: a slow picture never puts a |
| +// preview up without it, and a broken one never holds the window. |
| // 3. Show the slides, once. On the editor, then bring every other |
| // family into the tab at idle, so that an action rarely waits. |
| // |
| @@ -36,6 +39,10 @@ import { mountViewerNav } from './viewer-nav.mjs' |
| // (editor-entry.js, shownBeforeMount). |
| const LAYOUT_DEADLINE_MS = 3500 |
| +// The presenter window's first pictures get as long, from navigation |
| +// start, before its slides are shown anyway (step 2). |
| +const PICTURES_DEADLINE_MS = LAYOUT_DEADLINE_MS |
| + |
| // How long the substitutes note stays, unless dismissed. |
| const SUBSTITUTE_NOTE_MS = 10000 |
| @@ -97,6 +104,8 @@ async function run (session) { |
| fontsOn() |
| const editor = document.querySelector('[slides-editor]') |
| if (editor) await Promise.race([editorSettled, after(LAYOUT_DEADLINE_MS)]) |
| + const presenter = document.querySelector('[presenter]') |
| + if (presenter) await Promise.race([picturesOf(presenter), after(PICTURES_DEADLINE_MS)]) |
| show(session) |
| if (editor) { |
| await prefetch(session) |
| @@ -104,6 +113,13 @@ async function run (session) { |
| } |
| } |
| +// picturesOf resolves once every picture on the presenter window's |
| +// slides is decoded or has failed. |
| +function picturesOf (presenter) { |
| + const imgs = [...presenter.querySelectorAll('[presenter-panel] [slides-slide] img')] |
| + return Promise.all(imgs.map(img => (typeof img.decode === 'function' ? img.decode() : Promise.resolve()).catch(() => {}))) |
| +} |
| + |
| function withHelpers (session) { |
| return Object.assign(session, { |
| fontsReady, |
diff --git a/internal/slides/credits/assets.json b/internal/slides/credits/assets.json| index 607a1b1..e931e2c 100644 |
| --- a/internal/slides/credits/assets.json |
| +++ b/internal/slides/credits/assets.json |
| @@ -66,7 +66,7 @@ |
| "internal/slides/static/fonts/LiberationSerif-Italic.woff2": "4a770ae5fceacdacfa2a2aba18d18b73d8fabd946cce1f16cb0758a7f4ce50e0", |
| "internal/slides/static/fonts/LiberationSerif-Regular.ttf": "9caef765d2e891c10dd73658894f01e660fe0c1c83e0bda7d6edf561d8f623d4", |
| "internal/slides/static/fonts/LiberationSerif-Regular.woff2": "922f544d0eabdf45ecd389b3bb35367e670b736e40851d01e53c150352c012f2", |
| - "internal/slides/static/fontsgate.971e476eab8b8588.js": "971e476eab8b8588be520ac38eb823ab19beb2567db734555a7edc40d58567b2", |
| + "internal/slides/static/fontsgate.09db91091a8e7726.js": "09db91091a8e772622e4254bc397a968791181c243b47731713fa29599e298a4", |
| "internal/slides/static/fontshold.876f3d8d396da42f.js": "876f3d8d396da42f81954539f30b7200edfcde719d2920504707822f176a36ab", |
| "internal/slides/static/present.js": "c685310f5725a79b7637bc3ae524684e3ffd4d99c4995a084b4164af8238460d", |
| "internal/slides/static/presenter.dcb18149a10135cf.js": "dcb18149a10135cf4704f194d8beef7cc46570d211dc393701d005715c2de0d6", |
diff --git a/internal/slides/static/fontsgate.09db91091a8e7726.js b/internal/slides/static/fontsgate.09db91091a8e7726.js| new file mode 100644 |
| index 0000000..07e2533 |
| --- /dev/null |
| +++ b/internal/slides/static/fontsgate.09db91091a8e7726.js |
| @@ -0,0 +1 @@ |
| +var ee={sans:"Liberation Sans",serif:"Liberation Serif",mono:"Liberation Mono"},w=["sans","serif","mono"],te=/^[A-Za-z0-9 ,'._-]{1,200}$/;function U(e){if(typeof e!="string")return"";let o=e.trim();return te.test(o)?o:""}function V(e){if(typeof e!="string"||!e)return[];let o=new Set;for(let s of e.split(",")){let f=s.trim().replace(/^["']+|["']+$/g,"").toLowerCase();for(let d of w)f===ee[d].toLowerCase()&&o.add(d)}return w.filter(s=>o.has(s))}function se(e,o){let s=o.themes||{},f=e&&typeof e.theme=="string"&&s[e.theme]?e.theme:o.defaultTheme,[d,l]=s[f]||["",""],p=e&&e.fonts;if(p&&typeof p=="object"){let h=U(p.heading),u=U(p.body);h&&(d=h),u&&(l=u)}return[d,l]}function H(e){return Array.isArray(e)?e.some(o=>Array.isArray(o)&&o.some(s=>s&&s.c)):!1}function re(e){for(let o of e&&e.slides||[])for(let s of o.blocks||[])if(s.type==="code"||H(s.text)||s.type==="table"&&(s.props&&Array.isArray(s.props.cells)?s.props.cells:[]).some(H))return!0;return!1}function Y(e,o){if(!e||!Array.isArray(e.slides)||e.slides.length===0)return[];let s=se(e.meta,o);re(e)&&s.push(o.code);let f=new Set(s.flatMap(V));return w.filter(d=>f.has(d))}function F(e,o,{styleOf:s,hasCodeIn:f}=oe()){if(typeof e!="string"||!e)return[];let d=s(e)||"",l=[];for(let h of d.split(";")){let u=h.indexOf(":");if(u<0)continue;let g=h.slice(0,u).trim();(g==="--slides-font-heading"||g==="--slides-font-body")&&l.push(h.slice(u+1))}f(e)&&l.push(o.code);let p=new Set(l.flatMap(V));return w.filter(h=>p.has(h))}function oe(){let e=o=>{let s=document.createElement("template");return s.innerHTML=o,s.content};return{styleOf:o=>{let s=e(o).querySelector("[slides-slide]");return s?s.getAttribute("style"):""},hasCodeIn:o=>!!e(o).querySelector("pre, code")}}var $=2500,ne=3e3,ie=4e3;function O(e,o){return new Promise(s=>o(()=>s("late"),e))}function K({manifest:e,fontSet:o,FontFaceCtor:s,root:f=null,setTimer:d=setTimeout}={}){let l=new Map,p=new Set,h=!1,u=i=>!!(e&&e.families&&e.families[i]),g=i=>{let a=l.get(i);return a||(a={state:"idle",faces:[],promise:null,added:!1},l.set(i,a)),a};function q(){if(!f)return;let i=w.filter(a=>l.has(a)&&l.get(a).state==="committed").join(" ");(f.getAttribute("data-slides-fonts-fallback")||"")!==i&&f.setAttribute("data-slides-fonts-fallback",i)}function S(i){if(!u(i))return Promise.resolve(!1);let a=g(i);if(a.state==="committed")return Promise.resolve(!1);if(a.promise)return a.promise;let t=e.families[i];return a.state="loading",a.promise=Promise.resolve().then(()=>(a.faces=t.faces.map(r=>new s(t.css,`url("${r.url}") format("woff2")`,{weight:String(r.weight),style:r.style,display:"block"})),Promise.all(a.faces.map(r=>r.load().then(()=>!0,()=>!1))))).then(r=>r.every(Boolean),()=>!1).then(r=>(a.state==="loading"&&(a.state=r?"loaded":"failed"),a.state==="loaded")),a.promise}let b=i=>{if(!u(i))return!0;let a=l.has(i)?l.get(i).state:"idle";return a==="loaded"||a==="committed"};function E(i){for(let a of i){if(!u(a))continue;let t=g(a);t.added||t.state==="committed"||(t.state="committed")}q()}function A(i){for(let a of i){if(!u(a))continue;let t=g(a);if(t.state==="loaded"&&!t.added){for(let r of t.faces)o.add(r);t.added=!0}(t.state==="loaded"||t.state==="committed")&&p.add(a)}}let T={web:!0,get manifest(){return e},ready:b,loaded:i=>u(i)&&l.has(i)&&l.get(i).state==="loaded",committed:i=>u(i)&&l.has(i)&&l.get(i).state==="committed",failed:i=>u(i)&&l.has(i)&&l.get(i).state==="failed",state:i=>l.has(i)?l.get(i).state:"idle",get inUse(){return[...p]},get fallback(){return w.filter(i=>l.has(i)&&l.get(i).state==="committed")},use:A,load:S,markShown(){h=!0},get shown(){return h},async gate(i,a){let t=i.filter(u),r=Promise.all(t.map(S));await Promise.race([r,O(a,d)]);let n=t.filter(c=>!T.loaded(c));return E(n),A(t),n},async waitFor(i,a=ie){let t=i.filter(u);if(t.every(b))return!0;let r=Promise.all(t.map(n=>b(n)?!0:S(n)));return await Promise.race([r,O(a,d)]),t.every(b)},async prepare(i,{timeout:a=ne}={}){let t=[...new Set(i)].filter(u),r=t.filter(n=>!b(n));r.length&&(await Promise.race([Promise.all(r.map(S)),O(a,d)]),E(r.filter(n=>!T.loaded(n)))),A(t)},commitFallback(i){E(i.filter(a=>!b(a))),A(i)},async prefetch({idle:i=()=>Promise.resolve()}={}){for(let a of w)!u(a)||l.has(a)||(await i(),!l.has(a)&&await S(a))}};return T}function B(){return{web:!1,manifest:null,ready:()=>!0,loaded:()=>!1,committed:()=>!1,failed:()=>!1,state:()=>"system",inUse:[],fallback:[],use(){},load:async()=>!1,markShown(){},shown:!0,gate:async()=>[],waitFor:async()=>!0,prepare:async()=>{},commitFallback(){},prefetch:async()=>{}}}function W(e,{setTimer:o=setTimeout,clearTimer:s=clearTimeout}={}){if(!e)return{say(){},clear(){},state:""};let f=e.querySelector("[slides-fonts-status-text]"),d=e.querySelector("[slides-fonts-status-dismiss]"),l=null,p=()=>e.getAttribute("data-state")||"";function h(u){u&&p()!==u||(s(l),e.removeAttribute("data-state"),f&&(f.textContent=""))}return d&&d.addEventListener("click",()=>h()),{say(u,g=0){s(l),e.setAttribute("data-state",u),f&&(f.textContent=e.getAttribute("data-"+u)||""),g&&(l=o(()=>h(u),g))},clear:h,get state(){return p()}}}var j="[slides-viewer], [slides-present]";function C({fonts:e,doc:o=document,win:s=window}={}){let f=()=>o.querySelector(j);if(!f()||!s.history||!s.history.pushState||typeof s.fetch!="function"||typeof s.DOMParser!="function")return null;let d=new Map,l=0,p=new AbortController;s.addEventListener("pagehide",()=>p.abort()),s.addEventListener("pageshow",t=>{t.persisted&&(p=new AbortController,a())});function h(t){let r;try{r=new URL(t,s.location.href)}catch{return null}return r.origin!==s.location.origin||r.pathname!==s.location.pathname||r.hash?null:r.href}async function u(t,{cached:r=!1}={}){let n=d.get(t);if(r&&n&&Date.now()-n.at<3e4)return n.html;let c=await s.fetch(t,{credentials:"same-origin",headers:{Accept:"text/html"},signal:p.signal});if(!c.ok)throw new Error(`viewer: ${t}: ${c.status}`);let m=await c.text();return d.set(t,{at:Date.now(),html:m}),d.size>6&&d.delete(d.keys().next().value),m}let g=t=>new s.DOMParser().parseFromString(t,"text/html");function q(t){let r=[...t.querySelectorAll("img[src]")].map(n=>{let c=new s.Image;return c.src=n.getAttribute("src"),c.decode?c.decode().catch(()=>{}):Promise.resolve()});return Promise.race([Promise.all(r),new Promise(n=>setTimeout(n,1500))])}async function S(t,{push:r}){let n=++l,c,m;try{let _=g(await u(t));if(c=_.querySelector(j),m=_.title,!c)throw new Error("viewer: not a viewer page");let k=c.querySelector("[slides-slide]");k&&e&&e.prepareSection&&await e.prepareSection(k.outerHTML),await q(c)}catch{n===l&&!p.signal.aborted&&s.location.assign(t);return}if(n!==l)return;let M=f(),L=o.activeElement,N=L&&M.contains(L)?L:null,P=o.importNode(c,!0),X=b(M);M.replaceWith(P),E(P,X),m&&(o.title=m),r&&s.history.pushState({slidesViewer:!0},"",t),N&&T(P,N),i(P),a()}function b(t){let r=t.querySelector("[slides-film-nav]"),n=t.querySelector("[slides-film]");return{nav:r?r.scrollTop:0,list:n?n.scrollLeft:0}}function E(t,r){let n=t.querySelector("[slides-film-nav]"),c=t.querySelector("[slides-film]");n&&(n.scrollTop=r.nav),c&&(c.scrollLeft=r.list),A(t)}function A(t){let r=t.querySelector('[slides-film] a[aria-current="true"]');if(r)for(let n of[t.querySelector("[slides-film-nav]"),t.querySelector("[slides-film]")]){if(!n||typeof n.getBoundingClientRect!="function")continue;let c=n.getBoundingClientRect(),m=r.getBoundingClientRect();n.scrollHeight>n.clientHeight&&(m.top<c.top?n.scrollTop-=c.top-m.top+8:m.bottom>c.bottom&&(n.scrollTop+=m.bottom-c.bottom+8)),n.scrollWidth>n.clientWidth&&(m.left<c.left?n.scrollLeft-=c.left-m.left+8:m.right>c.right&&(n.scrollLeft+=m.right-c.right+8))}}function T(t,r){let n=null,c=r.getAttribute&&r.getAttribute("rel");c&&(n=t.querySelector(`[slides-pager] a[rel="${c}"]`)),!n&&r.closest&&r.closest("[slides-film]")&&(n=[...t.querySelectorAll("[slides-film] a[href]")].find(m=>m.getAttribute("href")===r.getAttribute("href"))||null),n||(n=t.querySelector("[slides-slide]"),n&&!n.hasAttribute("tabindex")&&n.setAttribute("tabindex","-1")),n&&n.focus({preventScroll:!0})}function i(t){let r=o.querySelector("[slides-announce]"),n=t.getAttribute("data-announce");r&&n&&(r.textContent=n)}function a(){(r=>typeof s.requestIdleCallback=="function"?s.requestIdleCallback(r,{timeout:2e3}):setTimeout(r,300))(async()=>{if(!p.signal.aborted)for(let r of f()?f().querySelectorAll('[slides-pager] a[rel="prev"], [slides-pager] a[rel="next"]'):[]){let n=h(r.href);if(n)try{let c=g(await u(n,{cached:!0})).querySelector("[slides-slide]");c&&e&&e.familiesOfSection&&e.load&&await Promise.all(e.familiesOfSection(c.outerHTML).map(m=>e.load(m)))}catch{}}})}return o.addEventListener("click",t=>{if(t.defaultPrevented||t.button!==0||t.metaKey||t.ctrlKey||t.shiftKey||t.altKey)return;let r=t.target.closest&&t.target.closest("a[href]");if(!r||!f()||!f().contains(r)||r.target||r.hasAttribute("download")||!r.closest("[slides-pager], [slides-film]"))return;let n=h(r.href);n&&(t.preventDefault(),S(n,{push:!0}))}),s.addEventListener("popstate",()=>{S(s.location.href,{push:!1})}),s.history.replaceState({slidesViewer:!0},"",s.location.href),f()&&A(f()),a(),{go:S}}var J=3500,ae=J,le=1e4,v=document.documentElement,I=v.getAttribute("data-slides-fonts")==="pending";I&&clearTimeout(globalThis.__slidesFontsHold);var y=null;try{let e=document.querySelector('meta[name="slides-fonts"]');y=e?JSON.parse(e.getAttribute("content")||"null"):null}catch{y=null}var Z,ce=new Promise(e=>{Z=e}),x,fe=new Promise(e=>{x=e}),z=e=>new Promise(o=>setTimeout(o,Math.max(0,e-performance.now())));function Q(e){e.markShown(),v.getAttribute("data-slides-fonts")!=="shown"&&v.setAttribute("data-slides-fonts","shown"),e.fallback.length&&W(document.querySelector("[slides-fonts-status]")).say("substitute",le)}var D=!1,R=[];addEventListener("pagehide",()=>{D=!0});addEventListener("pageshow",e=>{if(!e.persisted)return;D=!1;let o=R;R=[];for(let s of o)s()});function ue(e){let o=navigator.connection;if(o&&(o.saveData||/(^|-)2g$/.test(o.effectiveType||"")))return Promise.resolve();let s=()=>new Promise(f=>typeof requestIdleCallback=="function"?requestIdleCallback(()=>f(),{timeout:5e3}):setTimeout(f,200)).then(()=>D?new Promise(f=>R.push(f)):void 0);return e.prefetch({idle:s})}async function de(e){await e.gate(y.need||[],Math.max(0,$-performance.now())),x();let o=document.querySelector("[slides-editor]");o&&await Promise.race([ce,z(J)]);let s=document.querySelector("[presenter]");s&&await Promise.race([me(s),z(ae)]),Q(e),o&&(await ue(e),v.setAttribute("data-slides-fonts-prefetch","done"))}function me(e){let o=[...e.querySelectorAll("[presenter-panel] [slides-slide] img")];return Promise.all(o.map(s=>(typeof s.decode=="function"?s.decode():Promise.resolve()).catch(()=>{})))}function G(e){return Object.assign(e,{fontsReady:fe,settled:()=>Z(),familiesOfView:o=>y?Y(o,y):[],familiesOfSection:o=>y?F(o,y):[],prepareSection:o=>e.web&&y?e.prepare(F(o,y)):Promise.resolve()})}if(!I||!y||typeof FontFace!="function"||!document.fonts){I&&v.setAttribute("data-slides-fonts","shown");let e=G(B());x(),globalThis.slidesFonts=e,C({fonts:e})}else{let e=G(K({manifest:y,fontSet:document.fonts,FontFaceCtor:FontFace,root:v}));globalThis.slidesFonts=e,C({fonts:e}),de(e).catch(o=>{console.error("fonts:",o),x(),Q(e)})} |
diff --git a/internal/slides/static/fontsgate.971e476eab8b8588.js b/internal/slides/static/fontsgate.971e476eab8b8588.js| deleted file mode 100644 |
| index 86af045..0000000 |
| --- a/internal/slides/static/fontsgate.971e476eab8b8588.js |
| +++ /dev/null |
| @@ -1 +0,0 @@ |
| -var Q={sans:"Liberation Sans",serif:"Liberation Serif",mono:"Liberation Mono"},w=["sans","serif","mono"],X=/^[A-Za-z0-9 ,'._-]{1,200}$/;function U(e){if(typeof e!="string")return"";let n=e.trim();return X.test(n)?n:""}function V(e){if(typeof e!="string"||!e)return[];let n=new Set;for(let o of e.split(",")){let f=o.trim().replace(/^["']+|["']+$/g,"").toLowerCase();for(let d of w)f===Q[d].toLowerCase()&&n.add(d)}return w.filter(o=>n.has(o))}function ee(e,n){let o=n.themes||{},f=e&&typeof e.theme=="string"&&o[e.theme]?e.theme:n.defaultTheme,[d,l]=o[f]||["",""],p=e&&e.fonts;if(p&&typeof p=="object"){let h=U(p.heading),u=U(p.body);h&&(d=h),u&&(l=u)}return[d,l]}function H(e){return Array.isArray(e)?e.some(n=>Array.isArray(n)&&n.some(o=>o&&o.c)):!1}function te(e){for(let n of e&&e.slides||[])for(let o of n.blocks||[])if(o.type==="code"||H(o.text)||o.type==="table"&&(o.props&&Array.isArray(o.props.cells)?o.props.cells:[]).some(H))return!0;return!1}function Y(e,n){if(!e||!Array.isArray(e.slides)||e.slides.length===0)return[];let o=ee(e.meta,n);te(e)&&o.push(n.code);let f=new Set(o.flatMap(V));return w.filter(d=>f.has(d))}function _(e,n,{styleOf:o,hasCodeIn:f}=se()){if(typeof e!="string"||!e)return[];let d=o(e)||"",l=[];for(let h of d.split(";")){let u=h.indexOf(":");if(u<0)continue;let g=h.slice(0,u).trim();(g==="--slides-font-heading"||g==="--slides-font-body")&&l.push(h.slice(u+1))}f(e)&&l.push(n.code);let p=new Set(l.flatMap(V));return w.filter(h=>p.has(h))}function se(){let e=n=>{let o=document.createElement("template");return o.innerHTML=n,o.content};return{styleOf:n=>{let o=e(n).querySelector("[slides-slide]");return o?o.getAttribute("style"):""},hasCodeIn:n=>!!e(n).querySelector("pre, code")}}var $=2500,oe=3e3,re=4e3;function O(e,n){return new Promise(o=>n(()=>o("late"),e))}function K({manifest:e,fontSet:n,FontFaceCtor:o,root:f=null,setTimer:d=setTimeout}={}){let l=new Map,p=new Set,h=!1,u=i=>!!(e&&e.families&&e.families[i]),g=i=>{let a=l.get(i);return a||(a={state:"idle",faces:[],promise:null,added:!1},l.set(i,a)),a};function q(){if(!f)return;let i=w.filter(a=>l.has(a)&&l.get(a).state==="committed").join(" ");(f.getAttribute("data-slides-fonts-fallback")||"")!==i&&f.setAttribute("data-slides-fonts-fallback",i)}function S(i){if(!u(i))return Promise.resolve(!1);let a=g(i);if(a.state==="committed")return Promise.resolve(!1);if(a.promise)return a.promise;let t=e.families[i];return a.state="loading",a.promise=Promise.resolve().then(()=>(a.faces=t.faces.map(s=>new o(t.css,`url("${s.url}") format("woff2")`,{weight:String(s.weight),style:s.style,display:"block"})),Promise.all(a.faces.map(s=>s.load().then(()=>!0,()=>!1))))).then(s=>s.every(Boolean),()=>!1).then(s=>(a.state==="loading"&&(a.state=s?"loaded":"failed"),a.state==="loaded")),a.promise}let b=i=>{if(!u(i))return!0;let a=l.has(i)?l.get(i).state:"idle";return a==="loaded"||a==="committed"};function T(i){for(let a of i){if(!u(a))continue;let t=g(a);t.added||t.state==="committed"||(t.state="committed")}q()}function A(i){for(let a of i){if(!u(a))continue;let t=g(a);if(t.state==="loaded"&&!t.added){for(let s of t.faces)n.add(s);t.added=!0}(t.state==="loaded"||t.state==="committed")&&p.add(a)}}let x={web:!0,get manifest(){return e},ready:b,loaded:i=>u(i)&&l.has(i)&&l.get(i).state==="loaded",committed:i=>u(i)&&l.has(i)&&l.get(i).state==="committed",failed:i=>u(i)&&l.has(i)&&l.get(i).state==="failed",state:i=>l.has(i)?l.get(i).state:"idle",get inUse(){return[...p]},get fallback(){return w.filter(i=>l.has(i)&&l.get(i).state==="committed")},use:A,load:S,markShown(){h=!0},get shown(){return h},async gate(i,a){let t=i.filter(u),s=Promise.all(t.map(S));await Promise.race([s,O(a,d)]);let r=t.filter(c=>!x.loaded(c));return T(r),A(t),r},async waitFor(i,a=re){let t=i.filter(u);if(t.every(b))return!0;let s=Promise.all(t.map(r=>b(r)?!0:S(r)));return await Promise.race([s,O(a,d)]),t.every(b)},async prepare(i,{timeout:a=oe}={}){let t=[...new Set(i)].filter(u),s=t.filter(r=>!b(r));s.length&&(await Promise.race([Promise.all(s.map(S)),O(a,d)]),T(s.filter(r=>!x.loaded(r)))),A(t)},commitFallback(i){T(i.filter(a=>!b(a))),A(i)},async prefetch({idle:i=()=>Promise.resolve()}={}){for(let a of w)!u(a)||l.has(a)||(await i(),!l.has(a)&&await S(a))}};return x}function B(){return{web:!1,manifest:null,ready:()=>!0,loaded:()=>!1,committed:()=>!1,failed:()=>!1,state:()=>"system",inUse:[],fallback:[],use(){},load:async()=>!1,markShown(){},shown:!0,gate:async()=>[],waitFor:async()=>!0,prepare:async()=>{},commitFallback(){},prefetch:async()=>{}}}function W(e,{setTimer:n=setTimeout,clearTimer:o=clearTimeout}={}){if(!e)return{say(){},clear(){},state:""};let f=e.querySelector("[slides-fonts-status-text]"),d=e.querySelector("[slides-fonts-status-dismiss]"),l=null,p=()=>e.getAttribute("data-state")||"";function h(u){u&&p()!==u||(o(l),e.removeAttribute("data-state"),f&&(f.textContent=""))}return d&&d.addEventListener("click",()=>h()),{say(u,g=0){o(l),e.setAttribute("data-state",u),f&&(f.textContent=e.getAttribute("data-"+u)||""),g&&(l=n(()=>h(u),g))},clear:h,get state(){return p()}}}var j="[slides-viewer], [slides-present]";function C({fonts:e,doc:n=document,win:o=window}={}){let f=()=>n.querySelector(j);if(!f()||!o.history||!o.history.pushState||typeof o.fetch!="function"||typeof o.DOMParser!="function")return null;let d=new Map,l=0,p=new AbortController;o.addEventListener("pagehide",()=>p.abort()),o.addEventListener("pageshow",t=>{t.persisted&&(p=new AbortController,a())});function h(t){let s;try{s=new URL(t,o.location.href)}catch{return null}return s.origin!==o.location.origin||s.pathname!==o.location.pathname||s.hash?null:s.href}async function u(t,{cached:s=!1}={}){let r=d.get(t);if(s&&r&&Date.now()-r.at<3e4)return r.html;let c=await o.fetch(t,{credentials:"same-origin",headers:{Accept:"text/html"},signal:p.signal});if(!c.ok)throw new Error(`viewer: ${t}: ${c.status}`);let m=await c.text();return d.set(t,{at:Date.now(),html:m}),d.size>6&&d.delete(d.keys().next().value),m}let g=t=>new o.DOMParser().parseFromString(t,"text/html");function q(t){let s=[...t.querySelectorAll("img[src]")].map(r=>{let c=new o.Image;return c.src=r.getAttribute("src"),c.decode?c.decode().catch(()=>{}):Promise.resolve()});return Promise.race([Promise.all(s),new Promise(r=>setTimeout(r,1500))])}async function S(t,{push:s}){let r=++l,c,m;try{let F=g(await u(t));if(c=F.querySelector(j),m=F.title,!c)throw new Error("viewer: not a viewer page");let D=c.querySelector("[slides-slide]");D&&e&&e.prepareSection&&await e.prepareSection(D.outerHTML),await q(c)}catch{r===l&&!p.signal.aborted&&o.location.assign(t);return}if(r!==l)return;let M=f(),L=n.activeElement,N=L&&M.contains(L)?L:null,E=n.importNode(c,!0),Z=b(M);M.replaceWith(E),T(E,Z),m&&(n.title=m),s&&o.history.pushState({slidesViewer:!0},"",t),N&&x(E,N),i(E),a()}function b(t){let s=t.querySelector("[slides-film-nav]"),r=t.querySelector("[slides-film]");return{nav:s?s.scrollTop:0,list:r?r.scrollLeft:0}}function T(t,s){let r=t.querySelector("[slides-film-nav]"),c=t.querySelector("[slides-film]");r&&(r.scrollTop=s.nav),c&&(c.scrollLeft=s.list),A(t)}function A(t){let s=t.querySelector('[slides-film] a[aria-current="true"]');if(s)for(let r of[t.querySelector("[slides-film-nav]"),t.querySelector("[slides-film]")]){if(!r||typeof r.getBoundingClientRect!="function")continue;let c=r.getBoundingClientRect(),m=s.getBoundingClientRect();r.scrollHeight>r.clientHeight&&(m.top<c.top?r.scrollTop-=c.top-m.top+8:m.bottom>c.bottom&&(r.scrollTop+=m.bottom-c.bottom+8)),r.scrollWidth>r.clientWidth&&(m.left<c.left?r.scrollLeft-=c.left-m.left+8:m.right>c.right&&(r.scrollLeft+=m.right-c.right+8))}}function x(t,s){let r=null,c=s.getAttribute&&s.getAttribute("rel");c&&(r=t.querySelector(`[slides-pager] a[rel="${c}"]`)),!r&&s.closest&&s.closest("[slides-film]")&&(r=[...t.querySelectorAll("[slides-film] a[href]")].find(m=>m.getAttribute("href")===s.getAttribute("href"))||null),r||(r=t.querySelector("[slides-slide]"),r&&!r.hasAttribute("tabindex")&&r.setAttribute("tabindex","-1")),r&&r.focus({preventScroll:!0})}function i(t){let s=n.querySelector("[slides-announce]"),r=t.getAttribute("data-announce");s&&r&&(s.textContent=r)}function a(){(s=>typeof o.requestIdleCallback=="function"?o.requestIdleCallback(s,{timeout:2e3}):setTimeout(s,300))(async()=>{if(!p.signal.aborted)for(let s of f()?f().querySelectorAll('[slides-pager] a[rel="prev"], [slides-pager] a[rel="next"]'):[]){let r=h(s.href);if(r)try{let c=g(await u(r,{cached:!0})).querySelector("[slides-slide]");c&&e&&e.familiesOfSection&&e.load&&await Promise.all(e.familiesOfSection(c.outerHTML).map(m=>e.load(m)))}catch{}}})}return n.addEventListener("click",t=>{if(t.defaultPrevented||t.button!==0||t.metaKey||t.ctrlKey||t.shiftKey||t.altKey)return;let s=t.target.closest&&t.target.closest("a[href]");if(!s||!f()||!f().contains(s)||s.target||s.hasAttribute("download")||!s.closest("[slides-pager], [slides-film]"))return;let r=h(s.href);r&&(t.preventDefault(),S(r,{push:!0}))}),o.addEventListener("popstate",()=>{S(o.location.href,{push:!1})}),o.history.replaceState({slidesViewer:!0},"",o.location.href),f()&&A(f()),a(),{go:S}}var ne=3500,ie=1e4,v=document.documentElement,I=v.getAttribute("data-slides-fonts")==="pending";I&&clearTimeout(globalThis.__slidesFontsHold);var y=null;try{let e=document.querySelector('meta[name="slides-fonts"]');y=e?JSON.parse(e.getAttribute("content")||"null"):null}catch{y=null}var G,ae=new Promise(e=>{G=e}),P,le=new Promise(e=>{P=e}),ce=e=>new Promise(n=>setTimeout(n,Math.max(0,e-performance.now())));function J(e){e.markShown(),v.getAttribute("data-slides-fonts")!=="shown"&&v.setAttribute("data-slides-fonts","shown"),e.fallback.length&&W(document.querySelector("[slides-fonts-status]")).say("substitute",ie)}var k=!1,R=[];addEventListener("pagehide",()=>{k=!0});addEventListener("pageshow",e=>{if(!e.persisted)return;k=!1;let n=R;R=[];for(let o of n)o()});function fe(e){let n=navigator.connection;if(n&&(n.saveData||/(^|-)2g$/.test(n.effectiveType||"")))return Promise.resolve();let o=()=>new Promise(f=>typeof requestIdleCallback=="function"?requestIdleCallback(()=>f(),{timeout:5e3}):setTimeout(f,200)).then(()=>k?new Promise(f=>R.push(f)):void 0);return e.prefetch({idle:o})}async function ue(e){await e.gate(y.need||[],Math.max(0,$-performance.now())),P();let n=document.querySelector("[slides-editor]");n&&await Promise.race([ae,ce(ne)]),J(e),n&&(await fe(e),v.setAttribute("data-slides-fonts-prefetch","done"))}function z(e){return Object.assign(e,{fontsReady:le,settled:()=>G(),familiesOfView:n=>y?Y(n,y):[],familiesOfSection:n=>y?_(n,y):[],prepareSection:n=>e.web&&y?e.prepare(_(n,y)):Promise.resolve()})}if(!I||!y||typeof FontFace!="function"||!document.fonts){I&&v.setAttribute("data-slides-fonts","shown");let e=z(B());P(),globalThis.slidesFonts=e,C({fonts:e})}else{let e=z(K({manifest:y,fontSet:document.fonts,FontFaceCtor:FontFace,root:v}));globalThis.slidesFonts=e,C({fonts:e}),ue(e).catch(n=>{console.error("fonts:",n),P(),J(e)})} |
diff --git a/internal/slidestest/presenter_wire_browser_test.go b/internal/slidestest/presenter_wire_browser_test.go| index 28fd3ce..ec82070 100644 |
| --- a/internal/slidestest/presenter_wire_browser_test.go |
| +++ b/internal/slidestest/presenter_wire_browser_test.go |
| @@ -13,6 +13,7 @@ import ( |
| "testing" |
| "time" |
| + "github.com/chromedp/cdproto/page" |
| "github.com/chromedp/chromedp" |
| "github.com/chromedp/chromedp/kb" |
| ) |
| @@ -170,3 +171,74 @@ func TestPresenterKeysBeforeTheScriptArrive(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// firstPaintLogJS records, from the document's start, every frame's |
| +// Next panel: whether its slide is shown (the font gate's hold off and |
| +// the slide visible) and whether its picture is decoded. |
| +const firstPaintLogJS = `window.__paint = []; (function log () { |
| + const next = document.querySelector('[presenter-panel="next"] [slides-slide]'); |
| + const img = next && next.querySelector('img'); |
| + if (next) window.__paint.push({ t: performance.now(), shown: getComputedStyle(next).visibility === 'visible', |
| + img: !!img, ok: !!img && img.complete && img.naturalWidth > 0 }); |
| + requestAnimationFrame(log) })()` |
| + |
| +type paintFrame struct { |
| + T float64 |
| + Shown, OK bool |
| + Img bool |
| +} |
| + |
| +// TestPresenterFirstPaintWaitsForItsPictures: the window opening with |
| +// a picture slide coming Next shows its slides once that picture is |
| +// decoded, never the preview without it; and a picture that does not |
| +// come does not hold the window past its deadline. |
| +func TestPresenterFirstPaintWaitsForItsPictures(t *testing.T) { |
| + for _, c := range []struct { |
| + name string |
| + delay time.Duration |
| + }{ |
| + {"a slow picture", 1500 * time.Millisecond}, |
| + {"a picture that does not come", 12 * time.Second}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + mt := newMovesTalk(t) |
| + mt.gotoSlide(t, 2) // plain: Next is the picture slide |
| + ctx := mt.fr.rig.Context() |
| + hold := func(r *http.Request) { |
| + if strings.Contains(r.URL.Path, "/assets/") { |
| + time.Sleep(c.delay) |
| + } |
| + } |
| + mt.hold.Store(&hold) |
| + run(t, ctx, chromedp.ActionFunc(func(ctx context.Context) error { |
| + _, err := page.AddScriptToEvaluateOnNewDocument(firstPaintLogJS).Do(ctx) |
| + return err |
| + }), mt.fr.navigate(mt.presenter())) |
| + mt.fr.shown(t) |
| + run(t, ctx, chromedp.Sleep(200*time.Millisecond)) |
| + var frames []paintFrame |
| + run(t, ctx, chromedp.Evaluate(`window.__paint`, &frames)) |
| + first := -1 |
| + for i, f := range frames { |
| + if f.Shown { |
| + first = i |
| + break |
| + } |
| + } |
| + if first < 0 || !frames[first].Img { |
| + t.Fatalf("Next never showed the picture slide in %d frames", len(frames)) |
| + } |
| + if c.delay < 5*time.Second { |
| + for i, f := range frames[first:] { |
| + if !f.OK { |
| + t.Fatalf("frame %d: Next was shown with its picture still on its way", first+i) |
| + } |
| + } |
| + return |
| + } |
| + if frames[first].T > 6000 { |
| + t.Errorf("a picture that does not come held the window until %.0fms", frames[first].T) |
| + } |
| + }) |
| + } |
| +} |