@@ -275,15 +275,12 @@ export function readHtmlRenderLinkRequest(data: unknown):
275275 : undefined ;
276276}
277277
278- /** Published local images arrive as `data:image/` URLs. */
279- export function readHtmlRenderImageRequest (
280- data : unknown ,
281- ) : { readonly src : string ; readonly name : string } | undefined {
282- if ( typeof data !== "object" || data === null ) return undefined ;
283- const { jsonrpc, method, params } = data as Record < string , unknown > ;
284- if ( jsonrpc !== "2.0" || method !== OPEN_IMAGE_METHOD ) return undefined ;
285- if ( typeof params !== "object" || params === null ) return undefined ;
286- const { src, name } = params as Record < string , unknown > ;
278+ interface HtmlRenderImage {
279+ readonly src : string ;
280+ readonly name : string ;
281+ }
282+
283+ function readHtmlRenderImage ( src : unknown , name : unknown ) : HtmlRenderImage | undefined {
287284 if ( typeof src !== "string" || ! / ^ (?: h t t p s ? : \/ \/ | d a t a : i m a g e \/ ) / i. test ( src ) ) return undefined ;
288285 const trimmed = typeof name === "string" ? name . trim ( ) : "" ;
289286 // The page sends any alt text; never end the label on half an emoji.
@@ -293,6 +290,39 @@ export function readHtmlRenderImageRequest(
293290 return { src, name : label || "Image" } ;
294291}
295292
293+ /**
294+ * Published local images arrive as `data:image/` URLs. `images` and `index` carry the
295+ * page's other images; a page bootstrapped by an older server sends only `src` and `name`.
296+ */
297+ export function readHtmlRenderImageRequest (
298+ data : unknown ,
299+ ) : { readonly images : ReadonlyArray < HtmlRenderImage > ; readonly index : number } | undefined {
300+ if ( typeof data !== "object" || data === null ) return undefined ;
301+ const { jsonrpc, method, params } = data as Record < string , unknown > ;
302+ if ( jsonrpc !== "2.0" || method !== OPEN_IMAGE_METHOD ) return undefined ;
303+ if ( typeof params !== "object" || params === null ) return undefined ;
304+ const { src, name, images, index } = params as Record < string , unknown > ;
305+ const clicked = readHtmlRenderImage ( src , name ) ;
306+ if ( clicked === undefined ) return undefined ;
307+ const gallery : HtmlRenderImage [ ] = [ ] ;
308+ let at = - 1 ;
309+ if ( Array . isArray ( images ) ) {
310+ images . forEach ( ( entry : unknown , position ) => {
311+ if ( typeof entry !== "object" || entry === null ) return ;
312+ const image = readHtmlRenderImage (
313+ ( entry as Record < string , unknown > ) . src ,
314+ ( entry as Record < string , unknown > ) . name ,
315+ ) ;
316+ if ( image === undefined ) return ;
317+ if ( position === index ) at = gallery . length ;
318+ gallery . push ( image ) ;
319+ } ) ;
320+ }
321+ return at >= 0 && gallery [ at ] ?. src === clicked . src
322+ ? { images : gallery , index : at }
323+ : { images : [ clicked ] , index : 0 } ;
324+ }
325+
296326export function readHtmlRenderKeyDown ( data : unknown ) :
297327 | {
298328 readonly key : string ;
@@ -373,7 +403,7 @@ function rootRule(theme: HtmlRenderTheme): string {
373403
374404// Apply overridable theme CSS before paint and strip the fragment before routing; framed navigation uses the client.
375405// Forward shortcuts after page handlers; text fields retain their keys.
376- const BOOTSTRAP_SCRIPT = `(function(){var s=document.getElementById("t3-theme"),n=0;if(!s)return;var b=${ JSON . stringify ( BASE_CSS ) } ;function a(t){if(!t||typeof t!=="object"||!t.variables||typeof t.variables!=="object")return;var c=":root{color-scheme:"+(t.appearance==="light"?"light":"dark")+";";for(var k in t.variables){if(/^--[a-z0-9-]+$/.test(k))c+=k+":"+String(t.variables[k]).replace(/[;{}<>]/g,"")+";";}s.textContent=c+"}"+b;}try{var m=/[#&]${ THEME_FRAGMENT_KEY } =([^&]*)/.exec(location.hash);if(m){a(JSON.parse(decodeURIComponent(m[1])));history.replaceState(history.state,"",location.pathname+location.search);}}catch(e){}window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(d&&d.jsonrpc==="2.0"&&d.method===${ JSON . stringify ( HOST_CONTEXT_CHANGED_METHOD ) } &&p&&p.styles)a({appearance:p.theme,variables:p.styles.variables});});document.addEventListener("click",function(e){var l=e.isTrusted?e.composedPath().find(function(t){return t&&t.matches&&t.matches("a[href]");}):null,u;if(!l)return;try{u=new URL(l.getAttribute("href"),document.baseURI);}catch(x){return;}if(!/^https?:$/.test(u.protocol)||u.href.split("#")[0]===location.href.split("#")[0])return;if(window.parent!==window){e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",id:"t3-link-"+(++n),method:${ JSON . stringify ( OPEN_LINK_METHOD ) } ,params:{url:u.href,metaKey:e.metaKey,ctrlKey:e.ctrlKey}},"*");}else{l.setAttribute("target","_blank");l.setAttribute("rel","noopener");}},true);if(window.parent!==window){var h,o,z=function(){var r=document.documentElement,v=Math.ceil(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height);if(v===h)return;h=v;window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( SIZE_CHANGED_METHOD ) } ,params:{height:v}},"*");};if(window.ResizeObserver){o=new ResizeObserver(z);o.observe(document.documentElement);}document.addEventListener("DOMContentLoaded",function(){if(o&&document.body)o.observe(document.body);z();});window.addEventListener("load",z);window.addEventListener("click",function(e){var t=e.composedPath()[0],u;if(!e.isTrusted||e.defaultPrevented||e.button!==0||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||!t||t.tagName!=="IMG"||t.closest("a[href],button"))return;u=t.currentSrc||t.src;if(u)setTimeout(function(){if(!e.defaultPrevented)window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( OPEN_IMAGE_METHOD ) } ,params:{src:u,name:t.alt||t.title||""}},"*");});});var K=[],B=[],C=${ JSON . stringify ( EVENT_CODE_SHORTCUT_KEYS ) } ;window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(e.source===window.parent&&d&&d.jsonrpc==="2.0"&&d.method===${ JSON . stringify ( SHORTCUTS_METHOD ) } &&p&&Array.isArray(p.shortcuts)&&Array.isArray(p.blocked)){K=p.shortcuts;B=p.blocked;}});window.addEventListener("keydown",function(e){var t=e.composedPath()[0],c=e.code||"",l=e.key.toLowerCase(),x=/^[a-z]$/.test(l)?[l]:[l,/^Key[A-Z]$/.test(c)?c.slice(3).toLowerCase():"",l==="+"&&!e.shiftKey&&!e.altKey&&!/^Digit/.test(c)?"":C.hasOwnProperty(c)?C[c]:""],f,m=function(s){return s&&s.metaKey===e.metaKey&&s.ctrlKey===e.ctrlKey&&s.shiftKey===e.shiftKey&&s.altKey===e.altKey&&x.indexOf(s.key)>-1;};if(!e.isTrusted||e.isComposing||e.getModifierState("AltGraph")&&!/^[a-z0-9]$/i.test(e.key)||t&&(t.isContentEditable||/^(?:INPUT|TEXTAREA|SELECT)$/.test(t.tagName))||!K.some(m)||B.some(m))return;f=function(v){window.removeEventListener("keydown",f);if(v!==e||e.defaultPrevented)return;e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( KEY_DOWN_METHOD ) } ,params:{key:e.key,code:e.code,metaKey:e.metaKey,ctrlKey:e.ctrlKey,shiftKey:e.shiftKey,altKey:e.altKey,repeat:e.repeat}},"*");};window.addEventListener("keydown",f);setTimeout(function(){window.removeEventListener("keydown",f);});},true);}})();` ;
406+ const BOOTSTRAP_SCRIPT = `(function(){var s=document.getElementById("t3-theme"),n=0;if(!s)return;var b=${ JSON . stringify ( BASE_CSS ) } ;function a(t){if(!t||typeof t!=="object"||!t.variables||typeof t.variables!=="object")return;var c=":root{color-scheme:"+(t.appearance==="light"?"light":"dark")+";";for(var k in t.variables){if(/^--[a-z0-9-]+$/.test(k))c+=k+":"+String(t.variables[k]).replace(/[;{}<>]/g,"")+";";}s.textContent=c+"}"+b;}try{var m=/[#&]${ THEME_FRAGMENT_KEY } =([^&]*)/.exec(location.hash);if(m){a(JSON.parse(decodeURIComponent(m[1])));history.replaceState(history.state,"",location.pathname+location.search);}}catch(e){}window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(d&&d.jsonrpc==="2.0"&&d.method===${ JSON . stringify ( HOST_CONTEXT_CHANGED_METHOD ) } &&p&&p.styles)a({appearance:p.theme,variables:p.styles.variables});});document.addEventListener("click",function(e){var l=e.isTrusted?e.composedPath().find(function(t){return t&&t.matches&&t.matches("a[href]");}):null,u;if(!l)return;try{u=new URL(l.getAttribute("href"),document.baseURI);}catch(x){return;}if(!/^https?:$/.test(u.protocol)||u.href.split("#")[0]===location.href.split("#")[0])return;if(window.parent!==window){e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",id:"t3-link-"+(++n),method:${ JSON . stringify ( OPEN_LINK_METHOD ) } ,params:{url:u.href,metaKey:e.metaKey,ctrlKey:e.ctrlKey}},"*");}else{l.setAttribute("target","_blank");l.setAttribute("rel","noopener");}},true);if(window.parent!==window){var h,o,z=function(){var r=document.documentElement,v=Math.ceil(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height);if(v===h)return;h=v;window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( SIZE_CHANGED_METHOD ) } ,params:{height:v}},"*");};if(window.ResizeObserver){o=new ResizeObserver(z);o.observe(document.documentElement);}document.addEventListener("DOMContentLoaded",function(){if(o&&document.body)o.observe(document.body);z();});window.addEventListener("load",z);window.addEventListener("click",function(e){var t=e.composedPath()[0],u;if(!e.isTrusted||e.defaultPrevented||e.button!==0||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||!t||t.tagName!=="IMG"||t.closest("a[href],button"))return;u=t.currentSrc||t.src;if(u)setTimeout(function(){var g=[],i=-1;if(e.defaultPrevented)return;Array.prototype.forEach.call(document.images,function(m){var v=m.currentSrc||m.src;if(!v||m.closest("a[href],button"))return;if(m===t)i=g.length;g.push({src:v,name:m.alt||m.title||""});});window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( OPEN_IMAGE_METHOD ) } ,params:{src:u,name:t.alt||t.title||"",images:g,index:i}},"*");});});var K=[],B=[],C=${ JSON . stringify ( EVENT_CODE_SHORTCUT_KEYS ) } ;window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(e.source===window.parent&&d&&d.jsonrpc==="2.0"&&d.method===${ JSON . stringify ( SHORTCUTS_METHOD ) } &&p&&Array.isArray(p.shortcuts)&&Array.isArray(p.blocked)){K=p.shortcuts;B=p.blocked;}});window.addEventListener("keydown",function(e){var t=e.composedPath()[0],c=e.code||"",l=e.key.toLowerCase(),x=/^[a-z]$/.test(l)?[l]:[l,/^Key[A-Z]$/.test(c)?c.slice(3).toLowerCase():"",l==="+"&&!e.shiftKey&&!e.altKey&&!/^Digit/.test(c)?"":C.hasOwnProperty(c)?C[c]:""],f,m=function(s){return s&&s.metaKey===e.metaKey&&s.ctrlKey===e.ctrlKey&&s.shiftKey===e.shiftKey&&s.altKey===e.altKey&&x.indexOf(s.key)>-1;};if(!e.isTrusted||e.isComposing||e.getModifierState("AltGraph")&&!/^[a-z0-9]$/i.test(e.key)||t&&(t.isContentEditable||/^(?:INPUT|TEXTAREA|SELECT)$/.test(t.tagName))||!K.some(m)||B.some(m))return;f=function(v){window.removeEventListener("keydown",f);if(v!==e||e.defaultPrevented)return;e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( KEY_DOWN_METHOD ) } ,params:{key:e.key,code:e.code,metaKey:e.metaKey,ctrlKey:e.ctrlKey,shiftKey:e.shiftKey,altKey:e.altKey,repeat:e.repeat}},"*");};window.addEventListener("keydown",f);setTimeout(function(){window.removeEventListener("keydown",f);});},true);}})();` ;
377407
378408function bootstrapMarkup ( markup : string ) : string {
379409 const dark = htmlRenderTheme ( T3_CODE_DARK_THEME_COLORS , "dark" ) ;
0 commit comments