Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid triggerring easter eggs when typing on inputs #9

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
Loading