@protomaze/demo-engine 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protomaze/demo-engine",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Protomaze Demo Engine: record, edit, narrate, play and film demos of a web app, in the browser",
5
5
  "repository": {
6
6
  "type": "git",
package/page/Targets.d.ts CHANGED
@@ -75,6 +75,11 @@ export declare class Targets {
75
75
  limit?: number;
76
76
  }): Listed[];
77
77
  }
78
+ /**
79
+ * Whether it shows: laid out, not hidden, not see-through - except a field drawn see-through over its
80
+ * own look (MUI's checkboxes, switches and radios; styled file inputs), which shows as its parent does:
81
+ * it is what a click lands on.
82
+ */
78
83
  export declare function shown(element: Element): boolean;
79
84
  /** Where in it, as fractions of its box. */
80
85
  export declare function fractionIn(element: Element, x: number, y: number): Fraction;
package/page/Targets.js CHANGED
@@ -1 +1 @@
1
- export const IGNORED="[data-demo-ignore]";const t=["button","a[href]","input","textarea","select","summary","canvas","video",'[contenteditable=""]','[contenteditable="true"]','[role="button"]','[role="link"]','[role="tab"]','[role="option"]','[role="menuitem"]','[role="menuitemcheckbox"]','[role="menuitemradio"]','[role="switch"]','[role="checkbox"]','[role="radio"]','[role="slider"]','[role="combobox"]','[role="treeitem"]','[role="gridcell"]','[role="row"]',"li[role]","[data-demo]"];export function normal(t){return t.replace(/\s+/g," ").replace(/\d[\d.,]*/g,"#").trim().toLowerCase().slice(0,80)}export function dictionaryLabels(t,e){let n;const r=()=>{const e=t();if(n?.from===e)return n;const r=new Map,o=new Map,i=(t,e)=>Object.entries(t).forEach(([t,n])=>{const s=e+t;if("object"==typeof n&&n)i(n,s+".");else if("string"==typeof n&&!n.includes("{{")){const t=normal(n);r.set(t,r.has(t)?null:s),o.set(s,n)}});return i(e,""),n={from:e,keys:r,texts:o}};return{keyOf:t=>r().keys.get(normal(t))??void 0,textOf:t=>e?.(t)??r().texts.get(t)}}export class Targets{constructor(e={}){this.options=e,this.interactive=[...t,...e.interactive??[]].join(","),this.attributes=["aria-label","title",...e.labelAttributes??[]]}labelOf(t){const e=t,n=t.getAttribute("aria-labelledby")?.split(/\s+/).map(e=>t.ownerDocument.getElementById(e)?.textContent??"").join(" "),r=t.matches("input, select, textarea"),o=r?t.closest("label"):null,i=t.getAttribute("data-demo")||(t instanceof HTMLInputElement&&["button","submit","reset"].includes(t.type)?t.value:"")||(r?"":e.innerText?.trim())||n?.trim()||this.attributes.map(e=>t.getAttribute(e)).find(Boolean)||t.placeholder||t.alt||t.id&&t.ownerDocument.querySelector(`label[for="${CSS.escape(t.id)}"]`)?.textContent||o&&Array.from(o.childNodes).filter(t=>t.nodeType===Node.TEXT_NODE).map(t=>t.textContent).join(" ").trim()||t.name||"";if(i.trim())return normal(i);const s=t.querySelector("svg[data-testid], [data-icon]"),a=s?.getAttribute("data-testid")??s?.getAttribute("data-icon");if(a)return normal(a);let l=t.parentElement;for(let t=0;l&&t<4;t++,l=l.parentElement){const t=l.innerText??"";if(t.trim())return normal(t)}return""}cssOf(t){const e=t.tagName.toLowerCase(),n=t.getAttribute("role"),r="input"===e?t.getAttribute("type"):null,o=this.options.stableClass&&Array.from(t.classList).find(t=>this.options.stableClass.test(t));return e+(o?`.${CSS.escape(o)}`:"")+(n?`[role="${n}"]`:"")+(r?`[type="${r}"]`:"")}labelIn(t){if(t.key){const e=this.options.labels?.textOf(t.key);if(void 0!==e)return normal(e)}return void 0===t.text?void 0:normal(t.text)}targetElement(t){return!t||t.closest(IGNORED)?null:t.closest(this.interactive)??t}matches(t){const e=this.labelIn(t);let n;try{n=Array.from(document.querySelectorAll(t.css))}catch{return[]}return n.filter(t=>!t.closest(IGNORED)&&(void 0===e||this.labelOf(t)===e))}describe(t){const r=this.cssOf(t),o=this.labelOf(t),i=o&&!t.getAttribute("data-demo")?this.options.labels?.keyOf(o):void 0,s=t=>({css:t,...i?{key:i}:o?{text:o}:{}}),a=s(r);if(this.matches(a).length<=1)return a;for(let o=t.parentElement;o&&o!==document.body;o=o.parentElement){const i=n(o);if(!i)continue;const a=s(`${i} ${r}`),l=this.matches(a);if(1===l.length)return a;if(l.length>1)return{...a,...e(l,t)}}return{...a,...e(this.matches(a),t)}}async find(t,e=1e4,n=()=>!1){const r=performance.now()+e;do{const e=this.matches(t)[t.index??0];if(e&&shown(e))return e;await new Promise(t=>setTimeout(t,100))}while(performance.now()<r&&!n())}list({within:t,query:e,limit:n=400}={}){const o=t?Array.from(document.querySelectorAll(t)):[document.body],i=new Set,s=[],a=e?normal(e):void 0;for(const t of o)for(const e of Array.from(t.querySelectorAll(this.interactive))){if(s.length>=n)break;if(i.has(e)||e.closest(IGNORED)||!shown(e))continue;const o=e.parentElement?.closest(this.interactive);if(o&&!o.closest(IGNORED)&&t.contains(o)&&this.labelOf(o)===this.labelOf(e)&&!e.matches("input, select, textarea, canvas"))continue;i.add(e);const l=this.labelOf(e);if(a&&!l.includes(a))continue;const c=e.getBoundingClientRect();s.push({target:this.describe(e),label:l,tag:e.tagName.toLowerCase(),...e.getAttribute("role")?{role:e.getAttribute("role")}:{},...r(e),box:{x:Math.round(c.x),y:Math.round(c.y),width:Math.round(c.width),height:Math.round(c.height)}})}return s}}function e(t,e){const n=t.indexOf(e);return n>0?{index:n}:{}}function n(t){const e=t.getAttribute("data-demo");if(e)return`[data-demo="${e.replace(/"/g,'\\"')}"]`;const n=t.id;return n&&/^[a-z][\w-]*$/i.test(n)&&!/\d{3,}|[:]|^(mui|radix|react|headlessui|rc)-?/i.test(n)?`#${CSS.escape(n)}`:void 0}export function shown(t){const e=t.checkVisibility;return!(e&&!e.call(t,{checkOpacity:!0,checkVisibilityCSS:!0}))&&t.getClientRects().length>0}function r(t){const e=[],n=t;t.matches("input[type=checkbox], input[type=radio]")?e.push(n.checked?"checked":"unchecked"):t.matches("input, textarea")?e.push(`value "${n.value.slice(0,60)}"`):t instanceof HTMLSelectElement&&e.push(`value "${t.selectedOptions[0]?.text??""}"`);for(const n of["aria-checked","aria-selected","aria-expanded","aria-pressed","aria-current"]){const r=t.getAttribute(n);r&&"false"!==r&&e.push(`${n.slice(5)}${"true"===r?"":`=${r}`}`)}return(t.disabled||"true"===t.getAttribute("aria-disabled"))&&e.push("disabled"),e.length?{state:e.join(", ")}:{}}export function fractionIn(t,e,n){const r=t.getBoundingClientRect(),o=t=>Math.round(1e3*t)/1e3;return[o((e-r.left)/(r.width||1)),o((n-r.top)/(r.height||1))]}export function pointIn(t,e=[.5,.5]){const n=t.getBoundingClientRect();return{x:n.left+e[0]*n.width,y:n.top+e[1]*n.height}}
1
+ export const IGNORED="[data-demo-ignore]";const t=["button","a[href]","input","textarea","select","summary","canvas","video",'[contenteditable=""]','[contenteditable="true"]','[role="button"]','[role="link"]','[role="tab"]','[role="option"]','[role="menuitem"]','[role="menuitemcheckbox"]','[role="menuitemradio"]','[role="switch"]','[role="checkbox"]','[role="radio"]','[role="slider"]','[role="combobox"]','[role="treeitem"]','[role="gridcell"]','[role="row"]',"li[role]","[data-demo]"];export function normal(t){return t.replace(/\s+/g," ").replace(/\d[\d.,]*/g,"#").trim().toLowerCase().slice(0,80)}export function dictionaryLabels(t,e){let n;const r=()=>{const e=t();if(n?.from===e)return n;const r=new Map,i=new Map,o=(t,e)=>Object.entries(t).forEach(([t,n])=>{const s=e+t;if("object"==typeof n&&n)o(n,s+".");else if("string"==typeof n&&!n.includes("{{")){const t=normal(n);r.set(t,r.has(t)?null:s),i.set(s,n)}});return o(e,""),n={from:e,keys:r,texts:i}};return{keyOf:t=>r().keys.get(normal(t))??void 0,textOf:t=>e?.(t)??r().texts.get(t)}}export class Targets{constructor(e={}){this.options=e,this.interactive=[...t,...e.interactive??[]].join(","),this.attributes=["aria-label","title",...e.labelAttributes??[]]}labelOf(t){const e=t,n=t.getAttribute("aria-labelledby")?.split(/\s+/).map(e=>t.ownerDocument.getElementById(e)?.textContent??"").join(" "),r=t.matches("input, select, textarea"),i=r?t.closest("label"):null,o=r?"":e.innerText?.trim()??"",s=t.getAttribute("data-demo")||(t instanceof HTMLInputElement&&["button","submit","reset"].includes(t.type)?t.value:"")||(/[\p{L}\p{N}]/u.test(o)?o:"")||n?.trim()||this.attributes.map(e=>t.getAttribute(e)).find(Boolean)||t.placeholder||t.alt||t.id&&t.ownerDocument.querySelector(`label[for="${CSS.escape(t.id)}"]`)?.textContent||i&&Array.from(i.childNodes).filter(t=>t.nodeType===Node.TEXT_NODE).map(t=>t.textContent).join(" ").trim()||t.name||o;if(s.trim())return normal(s);const a=t.querySelector("svg[data-testid], [data-icon]"),l=a?.getAttribute("data-testid")??a?.getAttribute("data-icon");if(l)return normal(l);let c=t.parentElement;for(let t=0;c&&t<4;t++,c=c.parentElement){const t=c.innerText??"";if(t.trim())return normal(t)}return""}cssOf(t){const e=t.tagName.toLowerCase(),n=t.getAttribute("role"),r="input"===e?t.getAttribute("type"):null,i=this.options.stableClass&&Array.from(t.classList).find(t=>this.options.stableClass.test(t));return e+(i?`.${CSS.escape(i)}`:"")+(n?`[role="${n}"]`:"")+(r?`[type="${r}"]`:"")}labelIn(t){if(t.key){const e=this.options.labels?.textOf(t.key);if(void 0!==e)return normal(e)}return void 0===t.text?void 0:normal(t.text)}targetElement(t){return!t||t.closest(IGNORED)?null:t.closest(this.interactive)??t}matches(t){const e=this.labelIn(t);let n;try{n=Array.from(document.querySelectorAll(t.css))}catch{return[]}return n.filter(t=>!t.closest(IGNORED)&&(void 0===e||this.labelOf(t)===e))}describe(t){const r=this.cssOf(t),i=this.labelOf(t),o=i&&!t.getAttribute("data-demo")?this.options.labels?.keyOf(i):void 0,s=t=>({css:t,...o?{key:o}:i?{text:i}:{}}),a=s(r);if(this.matches(a).length<=1)return a;for(let i=t.parentElement;i&&i!==document.body;i=i.parentElement){const o=n(i);if(!o)continue;const a=s(`${o} ${r}`),l=this.matches(a);if(1===l.length)return a;if(l.length>1)return{...a,...e(l,t)}}return{...a,...e(this.matches(a),t)}}async find(t,e=1e4,n=()=>!1){const r=performance.now()+e;do{const e=this.matches(t)[t.index??0];if(e&&shown(e))return e;await new Promise(t=>setTimeout(t,100))}while(performance.now()<r&&!n())}list({within:t,query:e,limit:n=400}={}){const i=t?Array.from(document.querySelectorAll(t)):[document.body],o=new Set,s=[],a=e?normal(e):void 0;for(const t of i)for(const e of Array.from(t.querySelectorAll(this.interactive))){if(s.length>=n)break;if(o.has(e)||e.closest(IGNORED)||!shown(e))continue;const i=e.parentElement?.closest(this.interactive);if(i&&!i.closest(IGNORED)&&t.contains(i)&&this.labelOf(i)===this.labelOf(e)&&!e.matches("input, select, textarea, canvas"))continue;o.add(e);const l=this.labelOf(e);if(a&&!l.includes(a))continue;const c=e.getBoundingClientRect();s.push({target:this.describe(e),label:l,tag:e.tagName.toLowerCase(),...e.getAttribute("role")?{role:e.getAttribute("role")}:{},...r(e),box:{x:Math.round(c.x),y:Math.round(c.y),width:Math.round(c.width),height:Math.round(c.height)}})}return s}}function e(t,e){const n=t.indexOf(e);return n>0?{index:n}:{}}function n(t){const e=t.getAttribute("data-demo");if(e)return`[data-demo="${e.replace(/"/g,'\\"')}"]`;const n=t.id;return n&&/^[a-z][\w-]*$/i.test(n)&&!/\d{3,}|[:]|^(mui|radix|react|headlessui|rc)-?/i.test(n)?`#${CSS.escape(n)}`:void 0}export function shown(t){const e=t.checkVisibility;if(e){if(!e.call(t,{checkVisibilityCSS:!0}))return!1;if(!e.call(t,{checkOpacity:!0,checkVisibilityCSS:!0})){const e=t.parentElement;if(!t.matches("input, select, textarea")||!e||!shown(e))return!1}}return t.getClientRects().length>0}function r(t){const e=[],n=t;t.matches("input[type=checkbox], input[type=radio]")?e.push(n.checked?"checked":"unchecked"):t.matches("input, textarea")?e.push(`value "${n.value.slice(0,60)}"`):t instanceof HTMLSelectElement&&e.push(`value "${t.selectedOptions[0]?.text??""}"`);for(const n of["aria-checked","aria-selected","aria-expanded","aria-pressed","aria-current"]){const r=t.getAttribute(n);r&&"false"!==r&&e.push(`${n.slice(5)}${"true"===r?"":`=${r}`}`)}return(t.disabled||"true"===t.getAttribute("aria-disabled"))&&e.push("disabled"),e.length?{state:e.join(", ")}:{}}export function fractionIn(t,e,n){const r=t.getBoundingClientRect(),i=t=>Math.round(1e3*t)/1e3;return[i((e-r.left)/(r.width||1)),i((n-r.top)/(r.height||1))]}export function pointIn(t,e=[.5,.5]){const n=t.getBoundingClientRect();return{x:n.left+e[0]*n.width,y:n.top+e[1]*n.height}}
@@ -3,7 +3,8 @@
3
3
  * this page and the phone and sees nothing - they are encrypted end to end with a key that travels
4
4
  * only in the QR code's link (after `#`, never sent to a server). See `remote/README.md`.
5
5
  */
6
- export declare const DEFAULT_RELAY = "https://remote.protomaze.com";
6
+ /** The Protomaze relay, deployed from `remote/` (protomaze.com's DNS is not on Cloudflare: its workers.dev address). */
7
+ export declare const DEFAULT_RELAY = "https://protomaze-remote.tom-1d5.workers.dev";
7
8
  /** What the phone shows. */
8
9
  export type RemoteState = {
9
10
  /** False: nothing plays; the rest describes the last demo, or is empty. */
package/remote/Remote.js CHANGED
@@ -1 +1 @@
1
- import t from"qrcode-generator";export const DEFAULT_RELAY="https://remote.protomaze.com";const e=t=>btoa(String.fromCharCode(...t)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,""),s=t=>Uint8Array.from(atob(t.replace(/-/g,"+").replace(/_/g,"/")),t=>t.charCodeAt(0));export function qrSvg(e,s=240){const r=t(0,"M");r.addData(e),r.make();const o=r.getModuleCount(),i=o+8;let a="";for(let t=0;t<o;t++)for(let e=0;e<o;e++)r.isDark(t,e)&&(a+=`M${e+4},${t+4}h1v1h-1z`);return`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${i} ${i}" width="${s}" height="${s}" shape-rendering="crispEdges"><rect width="${i}" height="${i}" fill="#fff"/><path d="${a}" fill="#000"/></svg>`}export class RemoteLink{constructor(t,e,s,r,o){this.relay=t,this.room=e,this.key=s,this.handlers=o,this.peers=0,this.closed=!1,this.retry=0,this.url=`${t.replace(/\/$/,"")}/#r=${e}&k=${r}`,this.connect()}static async open(t,s){if(!window.isSecureContext||!crypto.subtle)throw new Error("The phone remote needs the app on https (or localhost): browsers encrypt only there");const r=e(crypto.getRandomValues(new Uint8Array(16))),o=crypto.getRandomValues(new Uint8Array(32)),i=await crypto.subtle.importKey("raw",o,"AES-GCM",!1,["encrypt","decrypt"]);return new RemoteLink(t,r,i,e(o),s)}connect(){if(this.closed)return;const t=new WebSocket(`${this.relay.replace(/\/$/,"").replace(/^http/,"ws")}/room/${this.room}`);this.socket=t,t.onopen=()=>this.retry=0,t.onmessage=t=>{this.receive(String(t.data))},t.onclose=t=>{if(this.setPeers(0),4e3!==t.code&&4001!==t.code||(this.closed=!0),this.closed)return;const e=Math.min(15e3,500*2**this.retry++);setTimeout(()=>this.connect(),e)}}setPeers(t){t>this.peers&&this.last&&this.post({type:"state",state:{...this.last,at:Date.now()}}),this.peers=t,this.handlers.peers(t)}async receive(t){let e,r;try{e=JSON.parse(t)}catch{return}if("peers"===e.relay)return this.setPeers(Number(e.count)||0);if(e.iv&&e.data){try{const t=await crypto.subtle.decrypt({name:"AES-GCM",iv:s(e.iv)},this.key,s(e.data));r=JSON.parse((new TextDecoder).decode(t))}catch{return}"hello"===r.type?(this.handlers.hello(),this.last&&this.post({type:"state",state:{...this.last,at:Date.now()}})):"command"===r.type&&this.handlers.command({do:r.do,...void 0!==r.step?{step:r.step}:{}})}}async post(t){if(this.socket?.readyState!==WebSocket.OPEN)return;const s=crypto.getRandomValues(new Uint8Array(12)),r=await crypto.subtle.encrypt({name:"AES-GCM",iv:s},this.key,(new TextEncoder).encode(JSON.stringify(t)));this.socket.send(JSON.stringify({iv:e(s),data:e(new Uint8Array(r))}))}send(t){this.last=t,this.post({type:"state",state:t})}close(){this.closed=!0,this.post({type:"bye"}).finally(()=>this.socket?.close())}}
1
+ import t from"qrcode-generator";export const DEFAULT_RELAY="https://protomaze-remote.tom-1d5.workers.dev";const e=t=>btoa(String.fromCharCode(...t)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,""),s=t=>Uint8Array.from(atob(t.replace(/-/g,"+").replace(/_/g,"/")),t=>t.charCodeAt(0));export function qrSvg(e,s=240){const r=t(0,"M");r.addData(e),r.make();const o=r.getModuleCount(),i=o+8;let a="";for(let t=0;t<o;t++)for(let e=0;e<o;e++)r.isDark(t,e)&&(a+=`M${e+4},${t+4}h1v1h-1z`);return`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${i} ${i}" width="${s}" height="${s}" shape-rendering="crispEdges"><rect width="${i}" height="${i}" fill="#fff"/><path d="${a}" fill="#000"/></svg>`}export class RemoteLink{constructor(t,e,s,r,o){this.relay=t,this.room=e,this.key=s,this.handlers=o,this.peers=0,this.closed=!1,this.retry=0,this.url=`${t.replace(/\/$/,"")}/#r=${e}&k=${r}`,this.connect()}static async open(t,s){if(!window.isSecureContext||!crypto.subtle)throw new Error("The phone remote needs the app on https (or localhost): browsers encrypt only there");const r=e(crypto.getRandomValues(new Uint8Array(16))),o=crypto.getRandomValues(new Uint8Array(32)),i=await crypto.subtle.importKey("raw",o,"AES-GCM",!1,["encrypt","decrypt"]);return new RemoteLink(t,r,i,e(o),s)}connect(){if(this.closed)return;const t=new WebSocket(`${this.relay.replace(/\/$/,"").replace(/^http/,"ws")}/room/${this.room}`);this.socket=t,t.onopen=()=>this.retry=0,t.onmessage=t=>{this.receive(String(t.data))},t.onclose=t=>{if(this.setPeers(0),4e3!==t.code&&4001!==t.code||(this.closed=!0),this.closed)return;const e=Math.min(15e3,500*2**this.retry++);setTimeout(()=>this.connect(),e)}}setPeers(t){t>this.peers&&this.last&&this.post({type:"state",state:{...this.last,at:Date.now()}}),this.peers=t,this.handlers.peers(t)}async receive(t){let e,r;try{e=JSON.parse(t)}catch{return}if("peers"===e.relay)return this.setPeers(Number(e.count)||0);if(e.iv&&e.data){try{const t=await crypto.subtle.decrypt({name:"AES-GCM",iv:s(e.iv)},this.key,s(e.data));r=JSON.parse((new TextDecoder).decode(t))}catch{return}"hello"===r.type?(this.handlers.hello(),this.last&&this.post({type:"state",state:{...this.last,at:Date.now()}})):"command"===r.type&&this.handlers.command({do:r.do,...void 0!==r.step?{step:r.step}:{}})}}async post(t){if(this.socket?.readyState!==WebSocket.OPEN)return;const s=crypto.getRandomValues(new Uint8Array(12)),r=await crypto.subtle.encrypt({name:"AES-GCM",iv:s},this.key,(new TextEncoder).encode(JSON.stringify(t)));this.socket.send(JSON.stringify({iv:e(s),data:e(new Uint8Array(r))}))}send(t){this.last=t,this.post({type:"state",state:t})}close(){this.closed=!0,this.post({type:"bye"}).finally(()=>this.socket?.close())}}