@protomaze/demo-engine 0.1.11 → 0.1.12
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 +1 -1
- package/page/Input.d.ts +9 -2
- package/page/Input.js +1 -1
package/package.json
CHANGED
package/page/Input.d.ts
CHANGED
|
@@ -22,7 +22,8 @@ type Point = {
|
|
|
22
22
|
export declare class Hand {
|
|
23
23
|
private cursor;
|
|
24
24
|
private speed;
|
|
25
|
-
|
|
25
|
+
/** What the hand went over, and its ancestors then: innermost first. */
|
|
26
|
+
private hoveredPath;
|
|
26
27
|
/** Script focus as the browser decides it again: see `pointing`. */
|
|
27
28
|
private unpatchFocus;
|
|
28
29
|
constructor(cursor: Cursor, speed?: () => number);
|
|
@@ -40,9 +41,15 @@ export declare class Hand {
|
|
|
40
41
|
moveTo(x: number, y: number): Promise<void>;
|
|
41
42
|
/** What a real pointer there would hit: the deepest element there, if it is `within`'s. */
|
|
42
43
|
hit(x: number, y: number, within?: Element): Element;
|
|
44
|
+
/**
|
|
45
|
+
* What the hand is over now. What it went over may have gone since - an icon swapped as its
|
|
46
|
+
* button was clicked (MUI's "show" for "hide") - and a browser then takes the pointer to be over
|
|
47
|
+
* the nearest ancestor still on the page: that one is left as the pointer moves on.
|
|
48
|
+
*/
|
|
49
|
+
private hovered;
|
|
43
50
|
/** Over it, and off what it was over: React's enter and leave follow over and out. */
|
|
44
51
|
over(element: Element, x: number, y: number): void;
|
|
45
|
-
/** Off what it is over. */
|
|
52
|
+
/** Off what it is over: out of it, and leaving each around it that `next` is not in. */
|
|
46
53
|
leave(next?: Element): void;
|
|
47
54
|
/** Pointed at: moved there and over it. */
|
|
48
55
|
point(element: Element, at: Point): Promise<void>;
|
package/page/Input.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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){
|
|
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,this.hoveredPath=[]}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}hovered(){return this.hoveredPath.find(t=>t.isConnected)}over(t,o,n){const s=this.hovered();if(s!==t){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.hoveredPath=[];for(let e=t;e;e=e.parentElement)this.hoveredPath.push(e)}e(t,"pointermove",o,n),e(t,"mousemove",o,n)}leave(t){const o=this.hovered();if(this.hoveredPath=[],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})}}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}
|