Skip to content
Snippets Groups Projects
KeyboardDisplay.vue 1.98 KiB
Newer Older
  <div v-if="characters && characters.length">
Valentin Rigal's avatar
Valentin Rigal committed
    <div v-if="!charTable">An error occured loading the keyboard</div>
    <div v-else v-for="(row, i) in charTable" :key="i">
      <span v-for="(char, j) in row" :key="j">
        <!-- Emit a key of the keyboard or its position when it is empty -->
Valentin Rigal's avatar
Valentin Rigal committed
        <button
          :class="{
            'is-selected':
              selectedKey && selectedKey.row == i && selectedKey.column == j,
          }"
Valentin Rigal's avatar
Valentin Rigal committed
          type="button"
          v-on:mouseup="
            char ? $emit('input', char) : $emit('input', { row: i, column: j })
          "
Valentin Rigal's avatar
Valentin Rigal committed
        >
          <template v-if="char">{{ char.character }}</template>
          <template v-else>&nbsp;</template>
Valentin Rigal's avatar
Valentin Rigal committed
        </button>
      </span>
    </div>
import { mapState } from "vuex";

    // Row and column value of the selected keyboard key
    selectedKey: {
      type: Object,
      default: null,
    },
Valentin Rigal's avatar
Valentin Rigal committed
  computed: {
Valentin Rigal's avatar
Valentin Rigal committed
    characters() {
      return this.keyboards[this.keyboardIndex].characters;
Valentin Rigal's avatar
Valentin Rigal committed
    rowsCount() {
      return (
        this.characters && Math.max(...this.characters.map((c) => c.row)) + 1
      );
Valentin Rigal's avatar
Valentin Rigal committed
    columnsCount() {
      return (
        this.characters && Math.max(...this.characters.map((c) => c.column)) + 1
      );
Valentin Rigal's avatar
Valentin Rigal committed
    charTable() {
      if (!this.columnsCount || !this.rowsCount) return;
      // Return a list of every column with padding
      const table = Array.from(
        Array(this.rowsCount),
        () => new Array(this.columnsCount)
      );
      if (!this.characters) return table;
      this.characters.forEach((c) => {
        table[c.row][c.column] = c;
      });
      return table;
  width: 3rem;
  height: 3rem;
  margin: 0.1rem;
button.is-selected {
  border: solid black 0.25rem;
  border-radius: 0.25rem;
}