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

feat: add keyboard support #12

Closed
Closed
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
33 changes: 32 additions & 1 deletion src/components/Hive.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<script setup lang="ts">
import { ref } from "vue";
import { onMounted, onUnmounted, ref } from "vue";
import { useMainStore } from "../store";
import { shuffle } from "../utils";
import { useI18n } from "vue-i18n";
import en from "../locales/en.json";

const { t } = useI18n({
inheritLocale: true,
messages: {
en,
},
});

// `defineProps` is a compiler macro and no longer needs to be imported.
defineProps({
Expand All @@ -16,10 +25,32 @@ const otherLetters = ref(
);
let userGuess = ref("");

const onKeyPress = (e: KeyboardEvent) => {
if (e.key == "Enter") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I probably won't have time to review this properly for a couple days, but a few changes I'd want to see in the meantime are:

  1. Triple equals for comparison
  2. Remove console.log
  3. Guard clauses instead of nested if statements.

If any of those aren't clear, I can give examples once I'm back at my computer (I'm writing this on a phone).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is something more like this:

const onKeyPress = (e: KeyboardEvent) => {
  const key = e.key.toLowerCase();
  if (key === "enter") return submitGuess({ $t: t, guess: userGuess.value });
  if (key === "backspace") {
    userGuess.value = userGuess.value.slice(0, -1);
    return false;
  }
  if (key.length === 1 && store.availableLetters.includes(key)) {
    userGuess.value += key;
    return true;
  }
};

If you just copy the block above into your pr I'd be happy to merge tbh.
Happy to listen to arguments for different approaches either.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these comments only submitted now since I added a new comment through the request changes dialog 🤦‍♂️
For the record I'm pretty sure I added the first comment the day you opened the pr, and the second one a few days later. What a pain

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a fairly long time now so I've gone ahead and done this myself. Thanks for your contribution though!

submitGuess({ $t: t, guess: userGuess.value });
} else if (e.key == "Backspace") {
userGuess.value = userGuess.value.slice(0, -1);
} else {
const letter = e.key.toLowerCase();
if (store.availableLetters.split("").includes(letter)) {
userGuess.value += letter;
} else {
console.log(letter + " is not in " + store.availableLetters.split(""));
}
}
};

const submitGuess = ({ $t, guess }: { $t: Function; guess: string }) => {
userGuess.value = "";
store.submitGuess({ $t, guess });
};

onMounted(() => {
window.addEventListener("keyup", onKeyPress);
});
onUnmounted(() => {
window.removeEventListener("keyup", onKeyPress);
});
</script>

<template>
Expand Down