Skip to content
Snippets Groups Projects
Commit a306a637 authored by Eva Bardou's avatar Eva Bardou Committed by Valentin Rigal
Browse files

Refactor keyboards storage + Add an input to edit a keyboard name

parent 65630309
No related branches found
No related tags found
1 merge request!15Refactor keyboards storage + Add an input to edit a keyboard name
Pipeline #5640 passed with warnings
...@@ -8,9 +8,13 @@ ...@@ -8,9 +8,13 @@
</a> </a>
</div> </div>
<div class="content"> <div class="content" v-if="keyboard">
<KeyboardDisplay :keyboard="keyboard" v-on:input="addChar" /> <KeyboardDisplay
:keyboard-index="selectedKeyboard"
v-on:input="addChar"
/>
</div> </div>
<div v-else>No keyboard to display, please add one.</div>
<Modal v-if="optionsModal" v-model="optionsModal"> <Modal v-if="optionsModal" v-model="optionsModal">
<KeyboardManager /> <KeyboardManager />
...@@ -24,6 +28,7 @@ import { mapState } from "vuex"; ...@@ -24,6 +28,7 @@ import { mapState } from "vuex";
import KeyboardDisplay from "./KeyboardDisplay.vue"; import KeyboardDisplay from "./KeyboardDisplay.vue";
import KeyboardManager from "./KeyboardManager.vue"; import KeyboardManager from "./KeyboardManager.vue";
import Modal from "./Modal.vue"; import Modal from "./Modal.vue";
export default { export default {
components: { components: {
KeyboardDisplay, KeyboardDisplay,
...@@ -46,7 +51,8 @@ export default { ...@@ -46,7 +51,8 @@ export default {
A: "Ǡ", A: "Ǡ",
}, },
optionsModal: false, optionsModal: false,
selectedKeyboard: null, // Select the keyboard at index 0 in the keyboard list by default
selectedKeyboard: 0,
}), }),
created() { created() {
this.inputField.addEventListener("blur", this.looseFocus); this.inputField.addEventListener("blur", this.looseFocus);
...@@ -59,18 +65,10 @@ export default { ...@@ -59,18 +65,10 @@ export default {
computed: { computed: {
...mapState(["keyboards"]), ...mapState(["keyboards"]),
keyboard() { keyboard() {
if (!this.selectedKeyboard) return;
return this.keyboards[this.selectedKeyboard]; return this.keyboards[this.selectedKeyboard];
}, },
}, },
methods: { methods: {
selectDefaultKeyboard() {
const keyboard = Object.values(this.keyboards).find(
(k) => (k.default = true)
);
if (keyboard) this.selectedKeyboard = keyboard.name;
else if (this.keyboards.length) this.keyboards[0].name;
},
addChar(key) { addChar(key) {
if (!key.character) return; if (!key.character) return;
// Add a character to the input depending on the cursor selection // Add a character to the input depending on the cursor selection
...@@ -109,14 +107,6 @@ export default { ...@@ -109,14 +107,6 @@ export default {
// Focus the input when closing the modal // Focus the input when closing the modal
if (!value) this.inputField.focus(); if (!value) this.inputField.focus();
}, },
keyboards: {
immediate: true,
handler() {
if (!this.selectedKeyboard || !this.keyboards[this.selectedKeyboard]) {
this.selectDefaultKeyboard();
}
},
},
}, },
}; };
</script> </script>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<UTF8Picker v-if="picker" :selectedChar.sync="selectedChar" /> <UTF8Picker v-if="picker" :selectedChar.sync="selectedChar" />
<KeyboardDisplay <KeyboardDisplay
v-else v-else
:keyboard="keyboard" :keyboard-index="keyboardIndex"
:selectedKey="selectedKey" :selectedKey="selectedKey"
v-on:input="(i) => (selectedKey = i)" v-on:input="(i) => (selectedKey = i)"
/> />
...@@ -44,14 +44,15 @@ import { mapMutations } from "vuex"; ...@@ -44,14 +44,15 @@ import { mapMutations } from "vuex";
import KeyboardDisplay from "./KeyboardDisplay.vue"; import KeyboardDisplay from "./KeyboardDisplay.vue";
import UTF8Picker from "./UTF8Picker.vue"; import UTF8Picker from "./UTF8Picker.vue";
import { displayUTF8 } from "../helpers.js"; import { displayUTF8 } from "../helpers.js";
export default { export default {
components: { components: {
KeyboardDisplay, KeyboardDisplay,
UTF8Picker, UTF8Picker,
}, },
props: { props: {
keyboard: { keyboardIndex: {
type: Object, type: Number,
required: true, required: true,
}, },
}, },
...@@ -70,7 +71,7 @@ export default { ...@@ -70,7 +71,7 @@ export default {
// Update the keyboard // Update the keyboard
this.selectedKey = this.updateKey({ this.selectedKey = this.updateKey({
...this.selectedKey, ...this.selectedKey,
keyboardName: this.keyboard.name, index: this.keyboardIndex,
code: this.selectedChar.code, code: this.selectedChar.code,
}); });
this.selectedChar = null; this.selectedChar = null;
......
...@@ -23,10 +23,12 @@ ...@@ -23,10 +23,12 @@
</template> </template>
<script> <script>
import { mapState } from "vuex";
export default { export default {
props: { props: {
keyboard: { keyboardIndex: {
type: Object, type: Number,
required: true, required: true,
}, },
// Row and column value of the selected keyboard key // Row and column value of the selected keyboard key
...@@ -36,8 +38,9 @@ export default { ...@@ -36,8 +38,9 @@ export default {
}, },
}, },
computed: { computed: {
...mapState(["keyboards"]),
characters() { characters() {
return this.keyboard.characters; return this.keyboards[this.keyboardIndex].characters;
}, },
rowsCount() { rowsCount() {
return ( return (
......
<template> <template>
<div> <div>
<div v-if="editedKeyboard"> <div v-if="editedKeyboard !== null">
<a title="Back to keyboards" href="#" v-on:click="editedKeyboard = null"> <a title="Back to keyboards" href="#" v-on:click="editedKeyboard = null">
</a> </a>
<span>{{ editedKeyboard.name }}</span> <input type="text" v-model="keyboardName" />
<KeyboardConfiguration :keyboard="editedKeyboard" /> <button type="button" v-on:click="updateKeyboardName">Save</button>
<KeyboardConfiguration :keyboard-index="editedKeyboard" />
</div> </div>
<div v-else-if="selectedKeyboard"> <div v-else-if="selectedKeyboard !== null">
<a <a
title="Back to keyboards" title="Back to keyboards"
href="#" href="#"
...@@ -15,8 +16,8 @@ ...@@ -15,8 +16,8 @@
> >
</a> </a>
<span>{{ selectedKeyboard.name }}</span> <span>{{ keyboards[selectedKeyboard].name }}</span>
<KeyboardDisplay :keyboard="selectedKeyboard" /> <KeyboardDisplay :keyboard-index="selectedKeyboard" />
</div> </div>
<div v-else> <div v-else>
<strong>Keyboards manager</strong> <strong>Keyboards manager</strong>
...@@ -30,13 +31,13 @@ ...@@ -30,13 +31,13 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr v-for="k in keyboards" :key="k.name"> <tr v-for="(k, index) in keyboards" :key="index">
<td> <td>
<a href="#" v-on:click="selectedKeyboard = k">{{ k.name }}</a> <a href="#" v-on:click="selectedKeyboard = index">{{ k.name }}</a>
</td> </td>
<td>{{ k.characters.length }}</td> <td>{{ k.characters.length }}</td>
<td> <td>
<button type="button" v-on:click="editedKeyboard = k"> <button type="button" v-on:click="editedKeyboard = index">
Edit Edit
</button> </button>
</td> </td>
...@@ -48,6 +49,7 @@ ...@@ -48,6 +49,7 @@
</template> </template>
<script> <script>
import { mapMutations } from "vuex";
import { mapState } from "vuex"; import { mapState } from "vuex";
import KeyboardConfiguration from "./KeyboardConfiguration.vue"; import KeyboardConfiguration from "./KeyboardConfiguration.vue";
import KeyboardDisplay from "./KeyboardDisplay.vue"; import KeyboardDisplay from "./KeyboardDisplay.vue";
...@@ -60,10 +62,26 @@ export default { ...@@ -60,10 +62,26 @@ export default {
data: () => ({ data: () => ({
selectedKeyboard: null, selectedKeyboard: null,
editedKeyboard: null, editedKeyboard: null,
keyboardName: null,
}), }),
computed: { computed: {
...mapState(["keyboards"]), ...mapState(["keyboards"]),
}, },
methods: {
...mapMutations(["updateName"]),
updateKeyboardName() {
this.updateName({
index: this.editedKeyboard,
name: this.keyboardName,
});
},
},
watch: {
editedKeyboard() {
if (this.editedKeyboard !== null)
this.keyboardName = this.keyboards[this.editedKeyboard].name;
},
},
}; };
</script> </script>
......
...@@ -9,12 +9,11 @@ export const KEYBOARDS_LOCALSTORAGE_KEY = "virtual_keyboards"; ...@@ -9,12 +9,11 @@ export const KEYBOARDS_LOCALSTORAGE_KEY = "virtual_keyboards";
export const UTF8_ASSETS_URL = export const UTF8_ASSETS_URL =
"https://assets.teklia.com/virtual-keyboard/scripts"; "https://assets.teklia.com/virtual-keyboard/scripts";
export const DEFAULT_KEYBOARDS = { export const DEFAULT_KEYBOARDS = [
demo: { {
version: "0.1", version: "0.1",
name: "demo", name: "demo",
author: "Teklia <team@teklia.com>", author: "Teklia <team@teklia.com>",
default: true,
characters: [ characters: [
{ {
row: 0, row: 0,
...@@ -42,4 +41,4 @@ export const DEFAULT_KEYBOARDS = { ...@@ -42,4 +41,4 @@ export const DEFAULT_KEYBOARDS = {
}, },
], ],
}, },
}; ];
...@@ -44,9 +44,13 @@ export default new Vuex.Store({ ...@@ -44,9 +44,13 @@ export default new Vuex.Store({
setSelectedScript(state, { name, page }) { setSelectedScript(state, { name, page }) {
state.selectedScript = { name, page }; state.selectedScript = { name, page };
}, },
updateKey(state, { keyboardName, row, column, code }) { updateName(state, { index, name }) {
// Updates a key on a specific keyboard and return this key if (!state.keyboards[index]) return;
const keyboard = state.keyboards[keyboardName]; state.keyboards[index].name = name;
},
updateKey(state, { index, row, column, code }) {
// Copy the keyboard if the index is correct to avoid modifying it directly in the state
const keyboard = state.keyboards[index] && { ...state.keyboards[index] };
if (!keyboard) return; if (!keyboard) return;
// Find the key and replace it // Find the key and replace it
const newKey = { const newKey = {
...@@ -60,10 +64,9 @@ export default new Vuex.Store({ ...@@ -60,10 +64,9 @@ export default new Vuex.Store({
); );
if (keyPos > 0) keyboard.characters[keyPos] = newKey; if (keyPos > 0) keyboard.characters[keyPos] = newKey;
else keyboard.characters.push(newKey); else keyboard.characters.push(newKey);
state.keyboards = { state.keyboards[index] = keyboard;
...state.keyboards, // Update list reference to propagate changes across components
[keyboardName]: { ...keyboard }, state.keyboards = [...state.keyboards];
};
}, },
}, },
actions: { actions: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment