Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@easepick/kbd-plugin

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@easepick/kbd-plugin - npm Package Compare versions

Comparing version 1.1.0 to 1.1.2

247

dist/index.esm.js

@@ -1,1 +0,246 @@

import{BasePlugin as e}from"@easepick/base-plugin";class t extends e{docElement=null;rangePlugin;binds={onView:this.onView.bind(this),onKeydown:this.onKeydown.bind(this)};options={unitIndex:1,dayIndex:2};getName(){return"KbdPlugin"}onAttach(){const e=this.picker.options.element,t=e.getBoundingClientRect();if(this.docElement=document.createElement("span"),this.docElement.style.position="absolute",this.docElement.style.top=`${e.offsetTop}px`,this.docElement.style.left=e.offsetLeft+t.width-25+"px",this.docElement.attachShadow({mode:"open"}),this.options.html)this.docElement.shadowRoot.innerHTML=this.options.html;else{const e=`\n <style>\n button {\n border: none;\n background: transparent;\n font-size: ${window.getComputedStyle(this.picker.options.element).fontSize};\n }\n </style>\n\n <button>&#128197;</button>\n `;this.docElement.shadowRoot.innerHTML=e}const n=this.docElement.shadowRoot.querySelector("button");n&&(n.addEventListener("click",(e=>{e.preventDefault(),this.picker.show({target:this.picker.options.element})}),{capture:!0}),n.addEventListener("keydown",(e=>{"Escape"===e.code&&this.picker.hide()}),{capture:!0})),this.picker.options.element.after(this.docElement),this.picker.on("view",this.binds.onView),this.picker.on("keydown",this.binds.onKeydown)}onDetach(){this.docElement&&this.docElement.isConnected&&this.docElement.remove(),this.picker.off("view",this.binds.onView),this.picker.off("keydown",this.binds.onKeydown)}onView(e){const{view:t,target:n}=e.detail;if(n&&"querySelector"in n)if("CalendarDay"!==t||["locked","not-available"].some((e=>n.classList.contains(e)))){[...n.querySelectorAll(".unit:not(.day)")].forEach((e=>e.tabIndex=this.options.unitIndex))}else n.tabIndex=this.options.dayIndex}onKeydown(e){switch(this.onMouseEnter(e),e.code){case"ArrowUp":case"ArrowDown":this.verticalMove(e);break;case"ArrowLeft":case"ArrowRight":this.horizontalMove(e);break;case"Enter":case"Space":this.handleEnter(e);break;case"Escape":this.picker.hide()}}findAllowableDaySibling(e,t,n){const i=Array.from(e.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`)),o=i.indexOf(t);return i.filter(((e,t)=>n(t,o)&&e.tabIndex===this.options.dayIndex))[0]}changeMonth(e){const t={ArrowLeft:"previous",ArrowRight:"next"},n=this.picker.ui.container.querySelector(`.${t[e.code]}-button[tabindex="${this.options.unitIndex}"]`);n&&!n.parentElement.classList.contains(`no-${t[e.code]}-month`)&&(n.dispatchEvent(new Event("click",{bubbles:!0})),setTimeout((()=>{let t=null;switch(e.code){case"ArrowLeft":const e=this.picker.ui.container.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`);t=e[e.length-1];break;case"ArrowRight":t=this.picker.ui.container.querySelector(`.day[tabindex="${this.options.dayIndex}"]`)}t&&t.focus()})))}verticalMove(e){const t=e.target;if(t.classList.contains("day")){e.preventDefault();const n=this.findAllowableDaySibling(this.picker.ui.container,t,((t,n)=>t===(n="ArrowUp"===e.code?n-7:n+7)));n&&n.focus()}}horizontalMove(e){const t=e.target;if(t.classList.contains("day")){e.preventDefault();const n=this.findAllowableDaySibling(this.picker.ui.container,t,((t,n)=>t===(n="ArrowLeft"===e.code?n-1:n+1)));n?n.focus():this.changeMonth(e)}}handleEnter(e){const t=e.target;t.classList.contains("day")&&(e.preventDefault(),t.dispatchEvent(new Event("click",{bubbles:!0})),setTimeout((()=>{if(this.rangePlugin=this.picker.PluginManager.getInstance("RangePlugin"),this.rangePlugin||!this.picker.options.autoApply){const e=this.picker.ui.container.querySelector(".day.selected");e&&setTimeout((()=>{e.focus()}))}})))}onMouseEnter(e){e.target.classList.contains("day")&&setTimeout((()=>{const e=this.picker.ui.shadowRoot.activeElement;e&&e.dispatchEvent(new Event("mouseenter",{bubbles:!0}))}))}}export{t as KbdPlugin};
import { BasePlugin } from '@easepick/base-plugin';
class KbdPlugin extends BasePlugin {
docElement = null;
rangePlugin;
binds = {
onView: this.onView.bind(this),
onKeydown: this.onKeydown.bind(this),
};
options = {
unitIndex: 1,
dayIndex: 2,
};
/**
* Returns plugin name
*
* @returns String
*/
getName() {
return 'KbdPlugin';
}
/**
* - Called automatically via BasePlugin.attach() -
* The function execute on initialize the picker
*/
onAttach() {
const element = this.picker.options.element;
const elementBounds = element.getBoundingClientRect();
this.docElement = document.createElement('span');
this.docElement.style.position = 'absolute';
this.docElement.style.top = `${element.offsetTop}px`;
this.docElement.style.left = `${(element.offsetLeft + elementBounds.width) - 25}px`; // 25px width of icon
this.docElement.attachShadow({ mode: 'open' });
if (this.options.html) {
this.docElement.shadowRoot.innerHTML = this.options.html;
}
else {
const style = window.getComputedStyle(this.picker.options.element);
const html = `
<style>
button {
border: none;
background: transparent;
font-size: ${style.fontSize};
}
</style>
<button>&#128197;</button>
`;
this.docElement.shadowRoot.innerHTML = html;
}
const button = this.docElement.shadowRoot.querySelector('button');
if (button) {
button.addEventListener('click', (evt) => {
evt.preventDefault();
this.picker.show({ target: this.picker.options.element });
}, { capture: true });
button.addEventListener('keydown', (evt) => {
if (evt.code === 'Escape') {
this.picker.hide();
}
}, { capture: true });
}
this.picker.options.element.after(this.docElement);
this.picker.on('view', this.binds.onView);
this.picker.on('keydown', this.binds.onKeydown);
}
/**
* - Called automatically via BasePlugin.detach() -
*/
onDetach() {
if (this.docElement && this.docElement.isConnected) {
this.docElement.remove();
}
this.picker.off('view', this.binds.onView);
this.picker.off('keydown', this.binds.onKeydown);
}
/**
* Function `view` event
* Adds `tabIndex` to the picker elements
*
* @param event
*/
onView(event) {
const { view, target } = event.detail;
if (target && 'querySelector' in target) {
if (view === 'CalendarDay' && !['locked', 'not-available'].some(x => target.classList.contains(x))) {
target.tabIndex = this.options.dayIndex;
}
else {
const elems = target.querySelectorAll('.unit:not(.day)');
[...elems].forEach((el) => el.tabIndex = this.options.unitIndex);
}
}
}
/**
* Function for `keydown` event
* Handle keys when the picker has focus
*
* @param event
*/
onKeydown(event) {
this.onMouseEnter(event);
switch (event.code) {
case 'ArrowUp':
case 'ArrowDown':
this.verticalMove(event);
break;
case 'ArrowLeft':
case 'ArrowRight':
this.horizontalMove(event);
break;
case 'Enter':
case 'Space':
this.handleEnter(event);
break;
case 'Escape':
this.picker.hide();
break;
}
}
/**
* Find closest day elements
*
* @param layout
* @param target
* @param isAllow
* @returns Boolean
*/
findAllowableDaySibling(layout, target, isAllow) {
const elms = Array.from(layout.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`));
const targetIdx = elms.indexOf(target);
return elms.filter((el, idx) => {
return isAllow(idx, targetIdx) && el.tabIndex === this.options.dayIndex;
})[0];
}
/**
* Switch month via buttons (previous month, next month)
*
* @param evt
*/
changeMonth(evt) {
const arrows = {
ArrowLeft: 'previous',
ArrowRight: 'next',
};
const button = this.picker.ui.container.querySelector(`.${arrows[evt.code]}-button[tabindex="${this.options.unitIndex}"]`);
if (button && !button.parentElement.classList.contains(`no-${arrows[evt.code]}-month`)) {
button.dispatchEvent(new Event('click', { bubbles: true }));
setTimeout(() => {
let focusEl = null;
switch (evt.code) {
case 'ArrowLeft':
// eslint-disable-next-line no-case-declarations
const elms = this.picker.ui.container.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`);
focusEl = elms[elms.length - 1];
break;
case 'ArrowRight':
focusEl = this.picker.ui.container.querySelector(`.day[tabindex="${this.options.dayIndex}"]`);
break;
}
if (focusEl) {
focusEl.focus();
}
});
}
}
/**
* Handle ArrowUp and ArrowDown keys
*
* @param evt
*/
verticalMove(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
evt.preventDefault();
const nextElement = this.findAllowableDaySibling(this.picker.ui.container, target, (idx, targetIdx) => {
targetIdx = evt.code === 'ArrowUp' ? targetIdx - 7 : targetIdx + 7;
return idx === targetIdx;
});
if (nextElement) {
nextElement.focus();
}
}
}
/**
* Handle ArrowLeft and ArrowRight keys
*
* @param evt
*/
horizontalMove(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
evt.preventDefault();
const nextElement = this.findAllowableDaySibling(this.picker.ui.container, target, (idx, targetIdx) => {
targetIdx = evt.code === 'ArrowLeft' ? targetIdx - 1 : targetIdx + 1;
return idx === targetIdx;
});
if (nextElement) {
nextElement.focus();
}
else {
this.changeMonth(evt);
}
}
}
/**
* Handle Enter and Space keys
*
* @param evt
*/
handleEnter(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
evt.preventDefault();
target.dispatchEvent(new Event('click', { bubbles: true }));
setTimeout(() => {
this.rangePlugin = this.picker.PluginManager.getInstance('RangePlugin');
if (this.rangePlugin || !this.picker.options.autoApply) {
const currentFocus = this.picker.ui.container.querySelector('.day.selected');
if (currentFocus) {
setTimeout(() => { currentFocus.focus(); });
}
}
});
}
}
/**
* Manually fire `mouseenter` event to display date range correctly
*
* @param evt
*/
onMouseEnter(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
setTimeout(() => {
const e = this.picker.ui.shadowRoot.activeElement;
if (e) {
e.dispatchEvent(new Event('mouseenter', { bubbles: true }));
}
});
}
}
}
export { KbdPlugin };

@@ -10,2 +10,255 @@ /**

*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@easepick/base-plugin")):"function"==typeof define&&define.amd?define(["exports","@easepick/base-plugin"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).easepick=e.easepick||{},e.easepick)}(this,(function(e,t){"use strict";class n extends t.BasePlugin{docElement=null;rangePlugin;binds={onView:this.onView.bind(this),onKeydown:this.onKeydown.bind(this)};options={unitIndex:1,dayIndex:2};getName(){return"KbdPlugin"}onAttach(){const e=this.picker.options.element,t=e.getBoundingClientRect();if(this.docElement=document.createElement("span"),this.docElement.style.position="absolute",this.docElement.style.top=`${e.offsetTop}px`,this.docElement.style.left=e.offsetLeft+t.width-25+"px",this.docElement.attachShadow({mode:"open"}),this.options.html)this.docElement.shadowRoot.innerHTML=this.options.html;else{const e=`\n <style>\n button {\n border: none;\n background: transparent;\n font-size: ${window.getComputedStyle(this.picker.options.element).fontSize};\n }\n </style>\n\n <button>&#128197;</button>\n `;this.docElement.shadowRoot.innerHTML=e}const n=this.docElement.shadowRoot.querySelector("button");n&&(n.addEventListener("click",(e=>{e.preventDefault(),this.picker.show({target:this.picker.options.element})}),{capture:!0}),n.addEventListener("keydown",(e=>{"Escape"===e.code&&this.picker.hide()}),{capture:!0})),this.picker.options.element.after(this.docElement),this.picker.on("view",this.binds.onView),this.picker.on("keydown",this.binds.onKeydown)}onDetach(){this.docElement&&this.docElement.isConnected&&this.docElement.remove(),this.picker.off("view",this.binds.onView),this.picker.off("keydown",this.binds.onKeydown)}onView(e){const{view:t,target:n}=e.detail;if(n&&"querySelector"in n)if("CalendarDay"!==t||["locked","not-available"].some((e=>n.classList.contains(e)))){[...n.querySelectorAll(".unit:not(.day)")].forEach((e=>e.tabIndex=this.options.unitIndex))}else n.tabIndex=this.options.dayIndex}onKeydown(e){switch(this.onMouseEnter(e),e.code){case"ArrowUp":case"ArrowDown":this.verticalMove(e);break;case"ArrowLeft":case"ArrowRight":this.horizontalMove(e);break;case"Enter":case"Space":this.handleEnter(e);break;case"Escape":this.picker.hide()}}findAllowableDaySibling(e,t,n){const i=Array.from(e.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`)),o=i.indexOf(t);return i.filter(((e,t)=>n(t,o)&&e.tabIndex===this.options.dayIndex))[0]}changeMonth(e){const t={ArrowLeft:"previous",ArrowRight:"next"},n=this.picker.ui.container.querySelector(`.${t[e.code]}-button[tabindex="${this.options.unitIndex}"]`);n&&!n.parentElement.classList.contains(`no-${t[e.code]}-month`)&&(n.dispatchEvent(new Event("click",{bubbles:!0})),setTimeout((()=>{let t=null;switch(e.code){case"ArrowLeft":const e=this.picker.ui.container.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`);t=e[e.length-1];break;case"ArrowRight":t=this.picker.ui.container.querySelector(`.day[tabindex="${this.options.dayIndex}"]`)}t&&t.focus()})))}verticalMove(e){const t=e.target;if(t.classList.contains("day")){e.preventDefault();const n=this.findAllowableDaySibling(this.picker.ui.container,t,((t,n)=>t===(n="ArrowUp"===e.code?n-7:n+7)));n&&n.focus()}}horizontalMove(e){const t=e.target;if(t.classList.contains("day")){e.preventDefault();const n=this.findAllowableDaySibling(this.picker.ui.container,t,((t,n)=>t===(n="ArrowLeft"===e.code?n-1:n+1)));n?n.focus():this.changeMonth(e)}}handleEnter(e){const t=e.target;t.classList.contains("day")&&(e.preventDefault(),t.dispatchEvent(new Event("click",{bubbles:!0})),setTimeout((()=>{if(this.rangePlugin=this.picker.PluginManager.getInstance("RangePlugin"),this.rangePlugin||!this.picker.options.autoApply){const e=this.picker.ui.container.querySelector(".day.selected");e&&setTimeout((()=>{e.focus()}))}})))}onMouseEnter(e){e.target.classList.contains("day")&&setTimeout((()=>{const e=this.picker.ui.shadowRoot.activeElement;e&&e.dispatchEvent(new Event("mouseenter",{bubbles:!0}))}))}}e.KbdPlugin=n,Object.defineProperty(e,"__esModule",{value:!0})}));
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@easepick/base-plugin')) :
typeof define === 'function' && define.amd ? define(['exports', '@easepick/base-plugin'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.easepick = global.easepick || {}, global.easepick));
})(this, (function (exports, basePlugin) { 'use strict';
class KbdPlugin extends basePlugin.BasePlugin {
docElement = null;
rangePlugin;
binds = {
onView: this.onView.bind(this),
onKeydown: this.onKeydown.bind(this),
};
options = {
unitIndex: 1,
dayIndex: 2,
};
/**
* Returns plugin name
*
* @returns String
*/
getName() {
return 'KbdPlugin';
}
/**
* - Called automatically via BasePlugin.attach() -
* The function execute on initialize the picker
*/
onAttach() {
const element = this.picker.options.element;
const elementBounds = element.getBoundingClientRect();
this.docElement = document.createElement('span');
this.docElement.style.position = 'absolute';
this.docElement.style.top = `${element.offsetTop}px`;
this.docElement.style.left = `${(element.offsetLeft + elementBounds.width) - 25}px`; // 25px width of icon
this.docElement.attachShadow({ mode: 'open' });
if (this.options.html) {
this.docElement.shadowRoot.innerHTML = this.options.html;
}
else {
const style = window.getComputedStyle(this.picker.options.element);
const html = `
<style>
button {
border: none;
background: transparent;
font-size: ${style.fontSize};
}
</style>
<button>&#128197;</button>
`;
this.docElement.shadowRoot.innerHTML = html;
}
const button = this.docElement.shadowRoot.querySelector('button');
if (button) {
button.addEventListener('click', (evt) => {
evt.preventDefault();
this.picker.show({ target: this.picker.options.element });
}, { capture: true });
button.addEventListener('keydown', (evt) => {
if (evt.code === 'Escape') {
this.picker.hide();
}
}, { capture: true });
}
this.picker.options.element.after(this.docElement);
this.picker.on('view', this.binds.onView);
this.picker.on('keydown', this.binds.onKeydown);
}
/**
* - Called automatically via BasePlugin.detach() -
*/
onDetach() {
if (this.docElement && this.docElement.isConnected) {
this.docElement.remove();
}
this.picker.off('view', this.binds.onView);
this.picker.off('keydown', this.binds.onKeydown);
}
/**
* Function `view` event
* Adds `tabIndex` to the picker elements
*
* @param event
*/
onView(event) {
const { view, target } = event.detail;
if (target && 'querySelector' in target) {
if (view === 'CalendarDay' && !['locked', 'not-available'].some(x => target.classList.contains(x))) {
target.tabIndex = this.options.dayIndex;
}
else {
const elems = target.querySelectorAll('.unit:not(.day)');
[...elems].forEach((el) => el.tabIndex = this.options.unitIndex);
}
}
}
/**
* Function for `keydown` event
* Handle keys when the picker has focus
*
* @param event
*/
onKeydown(event) {
this.onMouseEnter(event);
switch (event.code) {
case 'ArrowUp':
case 'ArrowDown':
this.verticalMove(event);
break;
case 'ArrowLeft':
case 'ArrowRight':
this.horizontalMove(event);
break;
case 'Enter':
case 'Space':
this.handleEnter(event);
break;
case 'Escape':
this.picker.hide();
break;
}
}
/**
* Find closest day elements
*
* @param layout
* @param target
* @param isAllow
* @returns Boolean
*/
findAllowableDaySibling(layout, target, isAllow) {
const elms = Array.from(layout.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`));
const targetIdx = elms.indexOf(target);
return elms.filter((el, idx) => {
return isAllow(idx, targetIdx) && el.tabIndex === this.options.dayIndex;
})[0];
}
/**
* Switch month via buttons (previous month, next month)
*
* @param evt
*/
changeMonth(evt) {
const arrows = {
ArrowLeft: 'previous',
ArrowRight: 'next',
};
const button = this.picker.ui.container.querySelector(`.${arrows[evt.code]}-button[tabindex="${this.options.unitIndex}"]`);
if (button && !button.parentElement.classList.contains(`no-${arrows[evt.code]}-month`)) {
button.dispatchEvent(new Event('click', { bubbles: true }));
setTimeout(() => {
let focusEl = null;
switch (evt.code) {
case 'ArrowLeft':
// eslint-disable-next-line no-case-declarations
const elms = this.picker.ui.container.querySelectorAll(`.day[tabindex="${this.options.dayIndex}"]`);
focusEl = elms[elms.length - 1];
break;
case 'ArrowRight':
focusEl = this.picker.ui.container.querySelector(`.day[tabindex="${this.options.dayIndex}"]`);
break;
}
if (focusEl) {
focusEl.focus();
}
});
}
}
/**
* Handle ArrowUp and ArrowDown keys
*
* @param evt
*/
verticalMove(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
evt.preventDefault();
const nextElement = this.findAllowableDaySibling(this.picker.ui.container, target, (idx, targetIdx) => {
targetIdx = evt.code === 'ArrowUp' ? targetIdx - 7 : targetIdx + 7;
return idx === targetIdx;
});
if (nextElement) {
nextElement.focus();
}
}
}
/**
* Handle ArrowLeft and ArrowRight keys
*
* @param evt
*/
horizontalMove(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
evt.preventDefault();
const nextElement = this.findAllowableDaySibling(this.picker.ui.container, target, (idx, targetIdx) => {
targetIdx = evt.code === 'ArrowLeft' ? targetIdx - 1 : targetIdx + 1;
return idx === targetIdx;
});
if (nextElement) {
nextElement.focus();
}
else {
this.changeMonth(evt);
}
}
}
/**
* Handle Enter and Space keys
*
* @param evt
*/
handleEnter(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
evt.preventDefault();
target.dispatchEvent(new Event('click', { bubbles: true }));
setTimeout(() => {
this.rangePlugin = this.picker.PluginManager.getInstance('RangePlugin');
if (this.rangePlugin || !this.picker.options.autoApply) {
const currentFocus = this.picker.ui.container.querySelector('.day.selected');
if (currentFocus) {
setTimeout(() => { currentFocus.focus(); });
}
}
});
}
}
/**
* Manually fire `mouseenter` event to display date range correctly
*
* @param evt
*/
onMouseEnter(evt) {
const target = evt.target;
if (target.classList.contains('day')) {
setTimeout(() => {
const e = this.picker.ui.shadowRoot.activeElement;
if (e) {
e.dispatchEvent(new Event('mouseenter', { bubbles: true }));
}
});
}
}
}
exports.KbdPlugin = KbdPlugin;
Object.defineProperty(exports, '__esModule', { value: true });
}));

2

package.json
{
"name": "@easepick/kbd-plugin",
"description": "Plugin for easepick.",
"version": "1.1.0",
"version": "1.1.2",
"main": "dist/index.umd.js",

@@ -6,0 +6,0 @@ "module": "dist/index.esm.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc