@@ -246,6 +246,7 @@ export const HTML_RENDER_LAYOUT_GUIDE = [
246246 "If a box needs its own background (a mock of a specific screen, a panel that must stand apart), give it at least 16px of padding on every side and var(--radius) corners, so content never touches its edge." ,
247247 "Give charts fixed pixel heights rather than heights that scale with width." ,
248248 "Let content set the page's height. The frame grows to fit the page, so a height that follows the viewport makes it grow again and again: html_preview and html_render reject vh, svh, lvh, and dvh units, and height:100% on html or body has the same problem." ,
249+ "The frame's height changes whenever the page's does, and each change fires resize in the page, so a drag or other gesture should cancel on resize only when the width changed." ,
249250] . join ( " " ) ;
250251
251252const HOST_CONTEXT_CHANGED_METHOD = "ui/notifications/host-context-changed" ;
@@ -258,6 +259,8 @@ const SHORTCUTS_METHOD = "t3/notifications/shortcuts";
258259const KEY_DOWN_METHOD = "t3/notifications/keydown" ;
259260// Not part of MCP Apps: a click on a page image opens it in the app's image viewer.
260261const OPEN_IMAGE_METHOD = "t3/notifications/open-image" ;
262+ // Not part of MCP Apps: a press inside the page asks the client to hold the frame still.
263+ const POINTER_HELD_METHOD = "t3/notifications/pointer-held" ;
261264
262265export function readHtmlRenderContentHeight ( data : unknown ) : number | undefined {
263266 if ( typeof data !== "object" || data === null ) return undefined ;
@@ -270,6 +273,15 @@ export function readHtmlRenderContentHeight(data: unknown): number | undefined {
270273 return typeof height === "number" && Number . isFinite ( height ) && height > 0 ? height : undefined ;
271274}
272275
276+ export function readHtmlRenderPointerHeld ( data : unknown ) : boolean | undefined {
277+ if ( typeof data !== "object" || data === null ) return undefined ;
278+ const { jsonrpc, method, params } = data as Record < string , unknown > ;
279+ if ( jsonrpc !== "2.0" || method !== POINTER_HELD_METHOD ) return undefined ;
280+ const held =
281+ typeof params === "object" && params !== null ? ( params as { held ?: unknown } ) . held : undefined ;
282+ return typeof held === "boolean" ? held : undefined ;
283+ }
284+
273285export function readHtmlRenderLinkRequest ( data : unknown ) :
274286 | {
275287 readonly id : string | number ;
@@ -397,12 +409,16 @@ function rootRule(theme: HtmlRenderTheme): string {
397409// left click on a framed page's image, outside a link or button and not
398410// prevented by any of the page's listeners, asks the client to open the image
399411// in its viewer. A framed page also reports its content height, measured as the server measures it,
400- // so its client can fit the frame to the page. A chord the client named as an
412+ // so its client can fit the frame to the page. Reports wait while a pointer is
413+ // pressed: resizing the frame fires resize in the page, and pages commonly
414+ // cancel a drag on resize. The page also tells its client a press is held, so
415+ // the thread does not scroll the frame out from under the pointer. Blur ends a
416+ // hold whose release never arrived, so the frame cannot stop fitting. A chord the client named as an
401417// app shortcut is passed on rather than left to the browser, unless a text
402418// field has focus or the page handled it: the deciding listener is added while
403419// the key dispatches, so it runs after every listener the page has. Keys
404420// match as resolveEventKeys reads them, so the client accepts what it claims.
405- 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():"",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);}})();` ;
421+ 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,P={},D=0,Y=function(p){window.parent.postMessage({jsonrpc:"2.0",method:${ JSON . stringify ( POINTER_HELD_METHOD ) } ,params:{held:p}},"*");},z=function(){if(D)return;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);var U=function(e){if(!P[e.pointerId])return;delete P[e.pointerId];if(--D)return;Y(false);requestAnimationFrame(z);};window.addEventListener("pointerdown",function(e){if(!e.isTrusted||P[e.pointerId])return;P[e.pointerId]=1;if(!D++)Y(true);},true);window.addEventListener("pointerup",U,true);window.addEventListener("pointercancel",U,true);window.addEventListener("blur",function(){if(!D)return;P={};D=0;Y(false);requestAnimationFrame(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():"",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);}})();` ;
406422
407423function bootstrapMarkup ( markup : string ) : string {
408424 const dark = htmlRenderTheme ( T3_CODE_DARK_THEME_COLORS , "dark" ) ;
0 commit comments