Skip to content

Commit

Permalink
Merge pull request #9 from EGAMAGZ/development
Browse files Browse the repository at this point in the history
Avoid triggerring easter eggs when typing on inputs
  • Loading branch information
EGAMAGZ authored May 27, 2024
2 parents 2a02475 + 42b027a commit 0dc6a42
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egamagz/paska-ovo",
"version": "1.0.3",
"version": "1.0.4",
"exports": "./mod.ts",
"lock": false,
"compilerOptions": {
Expand Down
15 changes: 15 additions & 0 deletions example/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true,
"**/node_modules": false
}
}
12 changes: 6 additions & 6 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/check": "^0.5.10",
"@astrojs/preact": "^3.2.0",
"@astrojs/check": "^0.7.0",
"@astrojs/preact": "^3.3.0",
"@astrojs/tailwind": "^5.1.0",
"@preact/signals": "^1.2.3",
"@preact/signals-core": "^1.6.0",
"@tabler/icons-preact": "^3.3.0",
"astro": "^4.6.3",
"preact": "^10.20.2",
"@tabler/icons-preact": "^3.5.0",
"astro": "^4.9.2",
"preact": "^10.22.0",
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5"
},
"devDependencies": {
"shiki": "^1.4.0"
"shiki": "^1.6.0"
}
}
8 changes: 7 additions & 1 deletion src/paska-ovo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import type { Callback, EasterEgg, EasterEggState } from "./types.ts";
import { codeToChars } from "./util/code.ts";
import { isInputElement } from "./util/dom.ts";

/**
* Class that is used to manage easter eggs.
Expand Down Expand Up @@ -106,13 +107,18 @@ export class PaskaOvo {
}

/**
* Handles the key event for the current instance of PaskaOvo.
* Handles the key event for the current instance of PaskaOvo. In case the
* active element is an input element (select, input or textarea), it will
* not handle the key event to avoid triggering an easter egg.
*
* @param {KeyboardEvent} event - The key event to handle.
* @param {EasterEgg[]} easterEggs - List of easter eggs to trigger.
*/
private handleKeyEvent(event: KeyboardEvent, easterEggs: EasterEgg[]) {
const { key } = event;

if (isInputElement(document.activeElement)) return;

for (const easterEgg of easterEggs) {
const actualCodePosition = this.easterEggState[easterEgg.tag] || 0;
const actualCode = easterEgg.code[actualCodePosition];
Expand Down
12 changes: 11 additions & 1 deletion src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* A map of special key names to their corresponding standardized key codes.
* This is a constant record type to ensure immutability.
*/
*/
export const SPECIAL_KEYS: Record<string, string> = {
"slash": "/",
"up": "ArrowUp",
Expand All @@ -21,3 +21,13 @@ export const SPECIAL_KEYS: Record<string, string> = {
"tab": "Tab",
"esc": "Escape",
} as const;

/**
* List of tag input elements.
*/

export const INPUT_ELEMENTS = [
"textarea",
"input",
"select"
];
11 changes: 11 additions & 0 deletions src/util/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { INPUT_ELEMENTS } from "./constants.ts"

/**
* Checks if the given element is an input element.
*
* @param {Element | null} actualElement - The element to check.
* @return {boolean} Returns true if the element is an input element, false otherwise.
*/
export const isInputElement =
(actualElement: Element | null): boolean =>
Boolean(actualElement && INPUT_ELEMENTS.includes(actualElement.tagName.toLowerCase()));

0 comments on commit 0dc6a42

Please sign in to comment.