@protomaze/demo-engine 0.1.7 → 0.1.8

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.7",
3
+ "version": "0.1.8",
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/Input.d.ts CHANGED
@@ -23,7 +23,18 @@ export declare class Hand {
23
23
  private cursor;
24
24
  private speed;
25
25
  private hovered;
26
+ /** Script focus as the browser decides it again: see `pointing`. */
27
+ private unpatchFocus;
26
28
  constructor(cursor: Cursor, speed?: () => number);
29
+ /**
30
+ * From the hand's press on, the app's own script focus - a menu focusing its list, handing focus
31
+ * back when it closes - is not :focus-visible, as after a real click. The browser cannot tell: it
32
+ * has seen no trusted pointer input, so it takes script focus for keyboard focus, and MUI's
33
+ * tooltips open on it and stay. Until the hand presses a key, or is reset.
34
+ */
35
+ private pointing;
36
+ /** Keys pressed: script focus may show again, as a keyboard's does. */
37
+ private keyboard;
27
38
  private wait;
28
39
  /** The stand-in pointer, moved there along an eased path; longer ways take longer. */
29
40
  moveTo(x: number, y: number): Promise<void>;
package/page/Input.js CHANGED
@@ -1 +1 @@
1
- export function nextFrame(){return new Promise(t=>{const e=()=>{cancelAnimationFrame(n),clearTimeout(o),t()},n=requestAnimationFrame(e),o=setTimeout(e,40)})}export class Hand{constructor(t,e=()=>1){this.cursor=t,this.speed=e}wait(t){return(t=>new Promise(e=>setTimeout(e,t)))(t/this.speed())}async moveTo(t,e){const n=this.cursor.position()??{x:innerWidth/2,y:innerHeight/2},o=Math.hypot(t-n.x,e-n.y),s=Math.min(900,250+.6*o)/this.speed(),i=performance.now();for(;;){const o=Math.min(1,(performance.now()-i)/(s||1)),r=o<.5?4*o*o*o:1-Math.pow(-2*o+2,3)/2;if(this.cursor.move(n.x+(t-n.x)*r,n.y+(e-n.y)*r),o>=1)break;await nextFrame()}}hit(t,e,n){const o=document.elementFromPoint(t,e);return n?o&&n.contains(o)?o:n:o??document.body}over(t,n,o){if(this.hovered!==t){const s=this.hovered;this.leave(t),e(t,"pointerover",n,o,{relatedTarget:s??null}),e(t,"mouseover",n,o,{relatedTarget:s??null});const i=[];for(let e=t;e&&(!s||!e.contains(s));e=e.parentElement)i.unshift(e);i.forEach(t=>{e(t,"pointerenter",n,o,{bubbles:!1,relatedTarget:s??null}),e(t,"mouseenter",n,o,{bubbles:!1,relatedTarget:s??null})}),this.hovered=t}e(t,"pointermove",n,o),e(t,"mousemove",n,o)}leave(t){const n=this.hovered;if(n){e(n,"pointerout",0,0,{relatedTarget:t??null}),e(n,"mouseout",0,0,{relatedTarget:t??null});for(let o=n;o&&!o.contains(t??null);o=o.parentElement)e(o,"pointerleave",0,0,{bubbles:!1,relatedTarget:t??null}),e(o,"mouseleave",0,0,{bubbles:!1,relatedTarget:t??null});this.hovered=void 0}}async point(t,e){await this.moveTo(e.x,e.y),this.over(this.hit(e.x,e.y,t),e.x,e.y)}async click(o,s,{double:i=!1,right:r=!1,...a}={}){await this.point(o,s);const c=this.hit(s.x,s.y,o),l=t(a),u=r?2:0;for(let t=0;t<(i?2:1);t++)this.cursor.press(!0),e(c,"pointerdown",s.x,s.y,{button:u,buttons:r?2:1,...l}),e(c,"mousedown",s.x,s.y,{button:u,buttons:r?2:1,...l,detail:t+1}),n(c),await this.wait(60),e(c,"pointerup",s.x,s.y,{button:u,buttons:0,...l}),e(c,"mouseup",s.x,s.y,{button:u,buttons:0,...l,detail:t+1}),r?e(c,"contextmenu",s.x,s.y,{button:u,buttons:0,...l}):e(c,"click",s.x,s.y,{button:u,buttons:0,...l,detail:t+1}),setTimeout(()=>this.cursor.press(!1),160/this.speed()),i&&await this.wait(80);i&&e(c,"dblclick",s.x,s.y,{button:u,buttons:0,...l,detail:2})}async drag(o,s,i,r={}){if(o instanceof HTMLInputElement&&"range"===o.type)return this.slide(o,s,i);await this.point(o,s);const a=t(r),c=this.hit(s.x,s.y,o);this.cursor.press(!0),e(c,"pointerdown",s.x,s.y,{button:0,buttons:1,...a}),e(c,"mousedown",s.x,s.y,{button:0,buttons:1,...a}),n(c);const l=Math.max(8,Math.round(Math.hypot(i.x-s.x,i.y-s.y)/12));for(let t=1;t<=l;t++){const n=t/l,o=s.x+(i.x-s.x)*n,r=s.y+(i.y-s.y)*n;this.cursor.move(o,r);const u=document.elementFromPoint(o,r)??c;e(u,"pointermove",o,r,{buttons:1,...a}),e(u,"mousemove",o,r,{buttons:1,...a}),await this.wait(16)}const u=document.elementFromPoint(i.x,i.y)??c;e(u,"pointerup",i.x,i.y,{button:0,buttons:0,...a}),e(u,"mouseup",i.x,i.y,{button:0,buttons:0,...a}),(u===c||c.contains(u))&&e(u,"click",i.x,i.y,{button:0,buttons:0,...a}),this.cursor.press(!1),this.over(u,i.x,i.y)}async slide(t,e,n){await this.point(t,e),this.cursor.press(!0);const s=t.getBoundingClientRect(),[i,r]=[Number(t.min||0),Number(t.max||100)],a="any"===t.step?0:Number(t.step||1);for(let c=1;c<=12;c++){const l=e.x+(n.x-e.x)*(c/12);this.cursor.move(l,e.y+(n.y-e.y)*(c/12));let u=i+Math.min(1,Math.max(0,(l-s.left)/(s.width||1)))*(r-i);a&&(u=Math.round((u-i)/a)*a+i),o(t,String(u)),t.dispatchEvent(new Event("input",{bubbles:!0})),await this.wait(25)}t.dispatchEvent(new Event("change",{bubbles:!0})),this.cursor.press(!1)}async type(t,n){const s=t;s.focus?.({preventScroll:!0});const i=t.isContentEditable;for(let r=0===n.length?0:1;r<=n.length;r++){const a=n.slice(0,r),c=n[r-1];c&&e(t,"keydown",0,0,{key:c}),i?t.textContent=a:o(s,a),t.dispatchEvent(new InputEvent("input",{bubbles:!0,data:c??null,inputType:c?"insertText":"deleteContentBackward"})),c&&e(t,"keyup",0,0,{key:c}),await this.wait(55)}t.dispatchEvent(new Event("change",{bubbles:!0}))}key(t,e){const n=e??document.activeElement??document.body,{key:o,...s}=parseChord(t),r={key:o,code:i(o),bubbles:!0,cancelable:!0,composed:!0,...s},a=new KeyboardEvent("keydown",r),c=!n.dispatchEvent(a);n.dispatchEvent(new KeyboardEvent("keyup",r));const l=n.form;!c&&"Enter"===o&&l&&n instanceof HTMLInputElement&&l.requestSubmit()}async select(t,e,n){await this.point(t,n),this.cursor.press(!0);const s=e.trim().toLowerCase(),i=Array.from(t.options).find(t=>t.value===e||t.text.trim().toLowerCase()===s);if(!i)throw new Error(`No option "${e}"`);await this.wait(250),o(t,i.value),t.dispatchEvent(new Event("input",{bubbles:!0})),t.dispatchEvent(new Event("change",{bubbles:!0})),this.cursor.press(!1)}async scroll(t,e){e?(t??window).scrollBy({left:e[0],top:e[1],behavior:this.speed()>1?"auto":"smooth"}):t?.scrollIntoView({block:"center",inline:"nearest",behavior:this.speed()>1?"auto":"smooth"}),await this.wait(500)}reset(){this.leave()}}export function at(t,e=[.5,.5]){const n=t.getBoundingClientRect();return{x:n.left+e[0]*n.width,y:n.top+e[1]*n.height}}function t(t){return{shiftKey:Boolean(t.shift),ctrlKey:Boolean(t.ctrl),altKey:Boolean(t.alt),metaKey:Boolean(t.meta)}}function e(t,e,n,o,s={}){const i={bubbles:!0,cancelable:!0,composed:!0,clientX:n,clientY:o,screenX:n,screenY:o,view:window,pointerId:1,pointerType:"mouse",isPrimary:!0,...s};let r;r=e.startsWith("key")?new KeyboardEvent(e,{bubbles:!0,cancelable:!0,composed:!0,...s}):e.startsWith("pointer")?new PointerEvent(e,i):new MouseEvent(e,i),t.dispatchEvent(r)}function n(t){const e=t.closest('button, a[href], input, textarea, select, summary, [tabindex], [contenteditable=""], [contenteditable="true"]');e?.focus({preventScroll:!0})}function o(t,e){const n=Object.getPrototypeOf(t),o=Object.getOwnPropertyDescriptor(n,"value")?.set;o?o.call(t,e):t.value=e}const s={control:"ctrl",ctrl:"ctrl",shift:"shift",alt:"alt",option:"alt",meta:"meta",cmd:"meta",command:"meta"};export function parseChord(t){const e=t.length>1&&t.endsWith("++"),n=(e?t.slice(0,-2):t).split("+").map(t=>t.trim()).filter(Boolean),o=e?"+":n.pop()??"",i=new Set(n.map(t=>s[t.toLowerCase()]).filter(Boolean));return{key:1===o.length&&i.size?o.toLowerCase():o,ctrlKey:i.has("ctrl"),shiftKey:i.has("shift"),altKey:i.has("alt"),metaKey:i.has("meta")}}function i(t){return/^[a-z]$/i.test(t)?`Key${t.toUpperCase()}`:/^\d$/.test(t)?`Digit${t}`:" "===t?"Space":t}
1
+ export function nextFrame(){return new Promise(t=>{const e=()=>{cancelAnimationFrame(o),clearTimeout(n),t()},o=requestAnimationFrame(e),n=setTimeout(e,40)})}export class Hand{constructor(t,e=()=>1){this.cursor=t,this.speed=e}pointing(){if(this.unpatchFocus)return;const t=[];for(const e of[HTMLElement.prototype,SVGElement.prototype]){const o=e.focus,n=function(t){return o.call(this,t&&"focusVisible"in t?t:{...t,focusVisible:!1})};e.focus=n,t.push({prototype:e,original:o,focus:n})}this.unpatchFocus=()=>{for(const{prototype:e,original:o,focus:n}of t)e.focus===n&&(e.focus=o);this.unpatchFocus=void 0}}keyboard(){this.unpatchFocus?.()}wait(t){return(t=>new Promise(e=>setTimeout(e,t)))(t/this.speed())}async moveTo(t,e){const o=this.cursor.position()??{x:innerWidth/2,y:innerHeight/2},n=Math.hypot(t-o.x,e-o.y),s=Math.min(900,250+.6*n)/this.speed(),i=performance.now();for(;;){const n=Math.min(1,(performance.now()-i)/(s||1)),r=n<.5?4*n*n*n:1-Math.pow(-2*n+2,3)/2;if(this.cursor.move(o.x+(t-o.x)*r,o.y+(e-o.y)*r),n>=1)break;await nextFrame()}}hit(t,e,o){const n=document.elementFromPoint(t,e);return o?n&&o.contains(n)?n:o:n??document.body}over(t,o,n){if(this.hovered!==t){const s=this.hovered;this.leave(t),e(t,"pointerover",o,n,{relatedTarget:s??null}),e(t,"mouseover",o,n,{relatedTarget:s??null});const i=[];for(let e=t;e&&(!s||!e.contains(s));e=e.parentElement)i.unshift(e);i.forEach(t=>{e(t,"pointerenter",o,n,{bubbles:!1,relatedTarget:s??null}),e(t,"mouseenter",o,n,{bubbles:!1,relatedTarget:s??null})}),this.hovered=t}e(t,"pointermove",o,n),e(t,"mousemove",o,n)}leave(t){const o=this.hovered;if(o){e(o,"pointerout",0,0,{relatedTarget:t??null}),e(o,"mouseout",0,0,{relatedTarget:t??null});for(let n=o;n&&!n.contains(t??null);n=n.parentElement)e(n,"pointerleave",0,0,{bubbles:!1,relatedTarget:t??null}),e(n,"mouseleave",0,0,{bubbles:!1,relatedTarget:t??null});this.hovered=void 0}}async point(t,e){await this.moveTo(e.x,e.y),this.over(this.hit(e.x,e.y,t),e.x,e.y)}async click(n,s,{double:i=!1,right:r=!1,...a}={}){await this.point(n,s);const c=this.hit(s.x,s.y,n),u=t(a),l=r?2:0;this.pointing();for(let t=0;t<(i?2:1);t++)this.cursor.press(!0),e(c,"pointerdown",s.x,s.y,{button:l,buttons:r?2:1,...u}),e(c,"mousedown",s.x,s.y,{button:l,buttons:r?2:1,...u,detail:t+1}),o(c),await this.wait(60),e(c,"pointerup",s.x,s.y,{button:l,buttons:0,...u}),e(c,"mouseup",s.x,s.y,{button:l,buttons:0,...u,detail:t+1}),r?e(c,"contextmenu",s.x,s.y,{button:l,buttons:0,...u}):e(c,"click",s.x,s.y,{button:l,buttons:0,...u,detail:t+1}),setTimeout(()=>this.cursor.press(!1),160/this.speed()),i&&await this.wait(80);i&&e(c,"dblclick",s.x,s.y,{button:l,buttons:0,...u,detail:2})}async drag(n,s,i,r={}){if(n instanceof HTMLInputElement&&"range"===n.type)return this.slide(n,s,i);await this.point(n,s);const a=t(r),c=this.hit(s.x,s.y,n);this.pointing(),this.cursor.press(!0),e(c,"pointerdown",s.x,s.y,{button:0,buttons:1,...a}),e(c,"mousedown",s.x,s.y,{button:0,buttons:1,...a}),o(c);const u=Math.max(8,Math.round(Math.hypot(i.x-s.x,i.y-s.y)/12));for(let t=1;t<=u;t++){const o=t/u,n=s.x+(i.x-s.x)*o,r=s.y+(i.y-s.y)*o;this.cursor.move(n,r);const l=document.elementFromPoint(n,r)??c;e(l,"pointermove",n,r,{buttons:1,...a}),e(l,"mousemove",n,r,{buttons:1,...a}),await this.wait(16)}const l=document.elementFromPoint(i.x,i.y)??c;e(l,"pointerup",i.x,i.y,{button:0,buttons:0,...a}),e(l,"mouseup",i.x,i.y,{button:0,buttons:0,...a}),(l===c||c.contains(l))&&e(l,"click",i.x,i.y,{button:0,buttons:0,...a}),this.cursor.press(!1),this.over(l,i.x,i.y)}async slide(t,e,o){await this.point(t,e),this.cursor.press(!0);const s=t.getBoundingClientRect(),[i,r]=[Number(t.min||0),Number(t.max||100)],a="any"===t.step?0:Number(t.step||1);for(let c=1;c<=12;c++){const u=e.x+(o.x-e.x)*(c/12);this.cursor.move(u,e.y+(o.y-e.y)*(c/12));let l=i+Math.min(1,Math.max(0,(u-s.left)/(s.width||1)))*(r-i);a&&(l=Math.round((l-i)/a)*a+i),n(t,String(l)),t.dispatchEvent(new Event("input",{bubbles:!0})),await this.wait(25)}t.dispatchEvent(new Event("change",{bubbles:!0})),this.cursor.press(!1)}async type(t,o){this.keyboard();const s=t;s.focus?.({preventScroll:!0});const i=t.isContentEditable;for(let r=0===o.length?0:1;r<=o.length;r++){const a=o.slice(0,r),c=o[r-1];c&&e(t,"keydown",0,0,{key:c}),i?t.textContent=a:n(s,a),t.dispatchEvent(new InputEvent("input",{bubbles:!0,data:c??null,inputType:c?"insertText":"deleteContentBackward"})),c&&e(t,"keyup",0,0,{key:c}),await this.wait(55)}t.dispatchEvent(new Event("change",{bubbles:!0}))}key(t,e){this.keyboard();const o=e??document.activeElement??document.body,{key:n,...s}=parseChord(t),r={key:n,code:i(n),bubbles:!0,cancelable:!0,composed:!0,...s},a=new KeyboardEvent("keydown",r),c=!o.dispatchEvent(a);o.dispatchEvent(new KeyboardEvent("keyup",r));const u=o.form;!c&&"Enter"===n&&u&&o instanceof HTMLInputElement&&u.requestSubmit()}async select(t,e,o){await this.point(t,o),this.pointing(),this.cursor.press(!0);const s=e.trim().toLowerCase(),i=Array.from(t.options).find(t=>t.value===e||t.text.trim().toLowerCase()===s);if(!i)throw new Error(`No option "${e}"`);await this.wait(250),n(t,i.value),t.dispatchEvent(new Event("input",{bubbles:!0})),t.dispatchEvent(new Event("change",{bubbles:!0})),this.cursor.press(!1)}async scroll(t,e){e?(t??window).scrollBy({left:e[0],top:e[1],behavior:this.speed()>1?"auto":"smooth"}):t?.scrollIntoView({block:"center",inline:"nearest",behavior:this.speed()>1?"auto":"smooth"}),await this.wait(500)}reset(){this.leave(),this.unpatchFocus?.()}}export function at(t,e=[.5,.5]){const o=t.getBoundingClientRect();return{x:o.left+e[0]*o.width,y:o.top+e[1]*o.height}}function t(t){return{shiftKey:Boolean(t.shift),ctrlKey:Boolean(t.ctrl),altKey:Boolean(t.alt),metaKey:Boolean(t.meta)}}function e(t,e,o,n,s={}){const i={bubbles:!0,cancelable:!0,composed:!0,clientX:o,clientY:n,screenX:o,screenY:n,view:window,pointerId:1,pointerType:"mouse",isPrimary:!0,...s};let r;r=e.startsWith("key")?new KeyboardEvent(e,{bubbles:!0,cancelable:!0,composed:!0,...s}):e.startsWith("pointer")?new PointerEvent(e,i):new MouseEvent(e,i),t.dispatchEvent(r)}function o(t){const e=t.closest('button, a[href], input, textarea, select, summary, [tabindex], [contenteditable=""], [contenteditable="true"]');e?.focus({preventScroll:!0,focusVisible:!1})}function n(t,e){const o=Object.getPrototypeOf(t),n=Object.getOwnPropertyDescriptor(o,"value")?.set;n?n.call(t,e):t.value=e}const s={control:"ctrl",ctrl:"ctrl",shift:"shift",alt:"alt",option:"alt",meta:"meta",cmd:"meta",command:"meta"};export function parseChord(t){const e=t.length>1&&t.endsWith("++"),o=(e?t.slice(0,-2):t).split("+").map(t=>t.trim()).filter(Boolean),n=e?"+":o.pop()??"",i=new Set(o.map(t=>s[t.toLowerCase()]).filter(Boolean));return{key:1===n.length&&i.size?n.toLowerCase():n,ctrlKey:i.has("ctrl"),shiftKey:i.has("shift"),altKey:i.has("alt"),metaKey:i.has("meta")}}function i(t){return/^[a-z]$/i.test(t)?`Key${t.toUpperCase()}`:/^\d$/.test(t)?`Digit${t}`:" "===t?"Space":t}