-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create @fibbojs/event package and FKeyboard helper class
- Loading branch information
1 parent
eb0440e
commit 425d0c4
Showing
11 changed files
with
175 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
entries: [ | ||
'src/index', | ||
], | ||
declaration: true, | ||
clean: true, | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "@fibbojs/event", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"description": "Event package for Fibbo", | ||
"author": "Augustin Mercier <mercier.augustin@outlook.fr>", | ||
"license": "Apache-2.0", | ||
"homepage": "https://fibbojs.github.io/fibbo", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/fibbojs/fibbo.git" | ||
}, | ||
"bugs": "https://github.com/fibbojs/fibbo/issues", | ||
"keywords": [], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"main": "./dist/index.mjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"./dist/*", | ||
"./dist/index.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "unbuild", | ||
"lint": "eslint .", | ||
"test": "vitest run", | ||
"typecheck": "tsc --noEmit" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @description A helper class to manage keyboard events. | ||
*/ | ||
export class FKeyboard { | ||
constructor() {} | ||
|
||
/** | ||
* @description Add a listener a given key event | ||
* @param key The key to listen to | ||
* @param callback The callback to call when the key is pressed | ||
* @returns The callback function | ||
* @example Basic usage | ||
* ```ts | ||
* const keyboard = new FKeyboard() | ||
* keyboard.on('ArrowUp', () => { | ||
* console.log('ArrowUp key pressed!') | ||
* }) | ||
* ``` | ||
* @example Removing a listener | ||
* ```ts | ||
* const keyboard = new FKeyboard() | ||
* // Get the remove listener function from the on method | ||
* const removeListener = keyboard.on('ArrowUp', () => { | ||
* console.log('ArrowUp key pressed!') | ||
* }) | ||
* // Remove the listener | ||
* removeListener() | ||
* ``` | ||
*/ | ||
static on(key: string, callback: () => void): () => void { | ||
// Create a listener for the keydown event | ||
const listener = (event: KeyboardEvent) => { | ||
if (event.key === key) { | ||
callback() | ||
} | ||
} | ||
// Attach the listener to the document | ||
document.addEventListener('keydown', listener) | ||
|
||
// Return a function to remove the listener if needed | ||
return () => { | ||
document.removeEventListener('keydown', listener) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Export Event classes | ||
*/ | ||
export { FKeyboard } from './FKeyboard' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
describe('should', () => { | ||
it('exported', () => { | ||
expect(1).toEqual(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.json" | ||
} |