UNPKG

emulate-key-in-browser

Version:
1 lines 7.38 kB
var emulateKey=function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var t,n,r=e((function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.emulateTab=void 0;var t,l=new Array("offsetHeight","scrollHeight","clientHeight");function n(n){return void 0===n&&(n=document.body),{toPreviousElement:function(){return r(n,i(),!0)},toNextElement:function(){return r(n,a(n))},to:function(e,t){return void 0===t&&(t=!1),r(n,e,t)}}}function r(e,t,n){if(void 0===n&&(n=!1),e.dispatchEvent(o("keydown",n))){if(e.blur(),t.focus(),document.activeElement!==t)try{document.activeElement=t}catch(e){console.warn("could not switch active element")}t instanceof HTMLInputElement&&(t.selectionStart=0);var r=o("keyup",n);return t.dispatchEvent(r),!0}return e.dispatchEvent(o("keypress",n)),e.dispatchEvent(o("keyup",n)),!1}function o(e,t){return void 0===t&&(t=!1),new KeyboardEvent(e,{code:"Tab",key:"Tab",cancelable:!0,bubbles:!0,shiftKey:t})}function u(){var e=document.activeElement;return e instanceof HTMLElement?e:void 0}function i(e){void 0===e&&(e=document.body);var t=c();if(t.length<1)throw new Error("no selectable elements found");var n=t.indexOf(e);return t[(0<n?n:t.length)-1]}function a(e){void 0===e&&(e=document.body);var t=c();if(t.length<1)throw new Error("no selectable elements found");var n=t.indexOf(e);return t[n+1<t.length?n+1:0]}function c(){var e=Array.from(document.querySelectorAll("*")).filter(s);d();var t=e.filter(function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return function(t){return!e.some(function(e){return!e(t)})}}(p,f,m,h,w)).reduce(function(e,t){var n=t.tabIndex,r=e[""+n]||{tabIndex:n,elements:[]};return r.elements.push(t),e[n]=r,e},{});return Object.values(t).sort(E).reduce(function(e,t){return e.concat(t.elements)},new Array)}e.emulateTab=function(){var e=u()||document.body;return r(e,a(e))},(t=e.emulateTab||(e.emulateTab={})).from=n,t.to=function(e,t){return void 0===t&&(t=!1),n(u()).to(e,t)},t.backwards=function(){return t.to(i(u()),!0)},t.forwards=function(){return t()},t.findSelectableElements=c;var f,s=function(e){return e instanceof HTMLElement},d=function(){var e=function e(t){void 0===t&&(t=document.body);var n=t;var r=l.find(function(e){var t=n[e];return t&&"number"==typeof t&&0<t});if(r)return r;var o=t.children;for(var u=0,i=Array.from(o);u<i.length;u++){var a=i[u],c=e(a);if(c)return c}return}();f=e?v(e):b,d=function(){}},v=function(n){return function(e){var t=e[n];return!!t&&"number"==typeof t&&0<t}};var b=function(e){if(!e.isConnected)return!1;if("BODY"===e.tagName)return!0;var t=getComputedStyle(e);if("none"===t.display)return!1;if("collapse"===t.visibility)return!1;var n=e.parentElement;return!!n&&b(n)};function p(e){return"number"==typeof e.tabIndex&&0<=e.tabIndex}var m=function(e){return!e.disabled};function h(e){return!(e instanceof HTMLAnchorElement&&!e.href&&null===e.getAttribute("tabIndex"))}var w=function(e){return"collapse"!==getComputedStyle(e).visibility};function E(e,t){if(0<e.tabIndex&&0<t.tabIndex)return e.tabIndex-t.tabIndex;if(0<e.tabIndex)return-e.tabIndex;if(0<t.tabIndex)return t.tabIndex;throw new Error("same tab index for two groups")}}((n={path:t,exports:{},require:function(e,t){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==t&&n.path)}},n.exports)),n.exports));function o(e){return e.value&&e.value.length||0}function i(e,t){return void 0===t&&(t=o(e)),"number"==typeof e.selectionStart?e.selectionStart:"number"==typeof e.selectionEnd?e.selectionEnd:t}function a(e,t){return void 0===t&&(t=o(e)),"number"==typeof e.selectionEnd?e.selectionEnd:"number"==typeof e.selectionStart?e.selectionStart:t}function c(e,t,n,r){void 0===n&&(n=document.activeElement||document.body);var o,u=n.dispatchEvent(new KeyboardEvent("keydown",{key:e,cancelable:!0,bubbles:!0}));(u=n.dispatchEvent(new KeyboardEvent("keypress",{key:e,cancelable:!0,bubbles:null==r?void 0:r.keypressBubbles}))&&u)&&t&&((o=n)instanceof HTMLInputElement||o instanceof HTMLTextAreaElement)&&(u=t(n)),u=n.dispatchEvent(new KeyboardEvent("keyup",{key:e,cancelable:!0}))&&u}var u,l=!0,f={up:function(){return c("ArrowUp",function(e){var t=d(e),n=i(e);return s(e,Math.max(n-t,0)),l})},right:function(){return c("ArrowRight",function(e){if(e.selectionStart!==e.selectionEnd)return s(e,a(e)),l;var t=v(e),n=a(e);return n<t&&s(e,n+1),l})},down:function(){return c("ArrowDown",function(e){var t=v(e),n=d(e,t),r=a(e);return s(e,Math.min(r+n,t)),l})},left:function(){return c("ArrowLeft",function(e){if(e.selectionStart!==e.selectionEnd)return s(e,i(e)),e.selectionEnd=e.selectionStart,l;var t=i(e);return 0<t&&s(e,t-1),l})},shift:{up:function(){return c("ArrowUp",function(e){var t=d(e);return(b(e,"forward")?m:p)(e,t),l})},right:function(){return c("ArrowRight",function(e){return(b(e,"backward")?w:h)(e,1),l})},down:function(){return c("ArrowDown",function(e){var t=d(e);return(b(e,"backward")?w:h)(e,t),l})},left:function(){return c("ArrowLeft",function(e){return(b(e,"forward")?m:p)(e,1),l})}}};function s(e,t){e.setSelectionRange(t,t,"none")}function d(e,t){return void 0===t&&(t=v(e)),e instanceof HTMLTextAreaElement?e.cols:t}function v(e){return e.value&&e.value.length||0}function b(e,t){return e.selectionDirection===t&&e.selectionStart!==e.selectionEnd}function p(e,t){var n=i(e),r=Math.max(n-t,0);e.selectionStart=r,"backward"!==e.selectionDirection&&(e.selectionDirection="backward")}function m(e,t){var n=i(e),r=a(e)-t;n<r?e.selectionEnd=r:(e.selectionEnd=n,e.selectionDirection="none")}function h(e,t){var n=v(e),r=a(e,n);e.selectionEnd=Math.min(r+t,n),"forward"!==e.selectionDirection&&(e.selectionDirection="forward")}function w(e,t){var n=i(e),r=a(e),o=n+t;o<r?e.selectionStart=o:(e.selectionStart=r,e.selectionDirection="none")}function E(e){var t,n,r,o,u,i,a=(n=function e(t){var n=t.offsetLeft;var r=t.offsetTop;var o=t.offsetParent;{var u;o instanceof HTMLElement&&(u=e(o),n+=u.left,r+=u.top)}return{left:n,top:r}}(t=e),r=n.left,o=n.top,u=t.offsetWidth,i=t.offsetHeight,{top:o,right:r+u,bottom:o+i,left:r,width:u,height:i}),c=a.left+Math.floor(a.width/2),l=a.top+Math.floor(a.height/2);return e.dispatchEvent(new MouseEvent("mouseover",{clientX:c,clientY:l,button:0,buttons:0}))}function y(u){return c(u,function(e){var t,n=e.value||"",r=n.length,o=i(e,r);return o!==r?(t=a(e,r),e.value=n.substr(0,o)+u+n.substr(t),e.selectionStart=e.selectionEnd=o+1,e.selectionDirection="none"):e.value+=u,e.dispatchEvent(new InputEvent("input",{bubbles:!0}))},void 0,{keypressBubbles:!0})}return(u=u||{}).hover=E,{tab:r.emulateTab,shiftTab:r.emulateTab.backwards,backspace:function(){return c("Backspace",function(e){var t,n,r,o=e.value||"",u=o.length;return 0<u&&((t=i(e,u))!==(n=a(e,u))?(e.value=o.substr(0,t)+o.substr(n),e.setSelectionRange(t,t,"none")):0<t&&(r=t-1,e.value=o.substr(0,r)+o.substr(n),e.setSelectionRange(r,r,"none"))),e.dispatchEvent(new InputEvent("input"))})},delete:function(){return c("Delete",function(e){var t,n,r=e.value||"",o=r.length;return 0<o&&((t=i(e,o))!==(n=a(e,o))?(e.value=r.substr(0,t)+r.substr(n),e.setSelectionRange(t,t,"none")):n<o&&(e.value=r.substr(0,n)+r.substr(n+1),e.setSelectionRange(n,n,"none"))),e.dispatchEvent(new InputEvent("input"))})},mouse:u,arrow:f,shiftArrow:f.shift,writeText:function(e){e.split("").forEach(y)}}}();