Skip to content

Commit

Permalink
makes keylistener hook-based and mocks Byond a bit (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: jlsnow301 <[email protected]>
  • Loading branch information
ZeWaka and jlsnow301 authored Nov 22, 2024
1 parent 160dcb6 commit 481e964
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
13 changes: 12 additions & 1 deletion .storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<script type="text/javascript">
var Byond = (window.Byond = {});
var Byond = (window.Byond = {
command: function (command) {
console.log("Byond.command", command);
},
winget: function (command) {
console.log("Byond.winget", command);
return Promise.resolve("FAKE VALUE");
},
winset: function (command) {
console.log("Byond.winset", command);
},
});
</script>
38 changes: 16 additions & 22 deletions lib/components/KeyListener.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import { Component } from 'react';
import { useEffect } from 'react';
import type { KeyEvent } from '../common/events';
import { listenForKeyEvents } from '../common/hotkeys';

type KeyListenerProps = Partial<{
type Props = Partial<{
onKey: (key: KeyEvent) => void;
onKeyDown: (key: KeyEvent) => void;
onKeyUp: (key: KeyEvent) => void;
}>;

export class KeyListener extends Component<KeyListenerProps> {
dispose: () => void;

constructor(props) {
super(props);

this.dispose = listenForKeyEvents((key) => {
if (this.props.onKey) {
this.props.onKey(key);
export function KeyListener(props: Props) {
useEffect(() => {
const dispose = listenForKeyEvents((key) => {
if (props.onKey) {
props.onKey(key);
}

if (key.isDown() && this.props.onKeyDown) {
this.props.onKeyDown(key);
if (key.isDown() && props.onKeyDown) {
props.onKeyDown(key);
}

if (key.isUp() && this.props.onKeyUp) {
this.props.onKeyUp(key);
if (key.isUp() && props.onKeyUp) {
props.onKeyUp(key);
}
});
}

componentWillUnmount() {
this.dispose();
}
return () => {
dispose();
};
}, []);

render() {
return null;
}
return null;
}
20 changes: 16 additions & 4 deletions stories/KeyListener.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Meta, StoryObj } from '@storybook/react';
import type { ComponentProps } from 'react';
import type { KeyEvent } from 'lib/common/events';
import { type ComponentProps, useEffect } from 'react';
import { setupHotKeys } from '../lib/common/hotkeys';
import { KeyListener } from '../lib/components/KeyListener';

type StoryProps = ComponentProps<typeof KeyListener>;
Expand All @@ -11,8 +13,18 @@ export default {

type Story = StoryObj<StoryProps>;

export const Default: Story = {
args: {
children: 'KeyListener',
export const onKeyDown: Story = {
render: () => {
useEffect(() => {
setupHotKeys();
}, []);

return (
<KeyListener
onKeyDown={(e: KeyEvent) => {
console.log('onKeyDown', e.toString(), e);
}}
/>
);
},
};

0 comments on commit 481e964

Please sign in to comment.