Changeset 35396
- Timestamp:
- 2021-09-14T17:01:31+12:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/components/TranscriptionItemEditor.vue
r35395 r35396 24 24 <input :size="word.word.length <= 1 ? 1 : word.word.length - 1" :ref="word.id" 25 25 class="editor-word" v-model="word.word" type="text" 26 @input="onEditorInput($event, index)" @focusout="onEditorFocusOut(index)" />26 @input="onEditorInput($event, index)" @focusout="onEditorFocusOut(index)" @keydown="onEditorKeyDown($event, index)" /> 27 27 <span> </span> 28 28 </li> … … 85 85 /** 86 86 * Initialises a new instance of the Word class. 87 * @param {String} id The ID of the word.88 87 * @param {String} word The word. 89 88 * @param {Number} startTime The time within the audio that this word starts being spoken. … … 105 104 /** @type {Boolean} A value indicating whether this word should be highlighted. */ 106 105 this.shouldHighlight = false; 106 107 /** @type {Number} The number of times the user has tried to delete this word. */ 108 this.deletionAttempts = 0; 107 109 } 108 110 } … … 134 136 */ 135 137 onEditorInput(event, index) { 136 console.log(event);137 138 138 /** @type {Word} */ 139 139 const word = this.words[index]; 140 140 141 141 if (event.inputType === "insertText") { 142 if (event.data === " ") { 143 word.word = word.word.replace(" ", ""); 144 142 // Insert a new word if whitespace is entered on an existing word 143 if (event.data === " " && word.word !== " ") { 145 144 // TODO: Proper timing metadata 146 145 const newWord = new Word("", word.startTime, word.endTime); … … 150 149 setTimeout(() => this.$refs[newWord.id].focus(), 50); 151 150 } 151 152 word.word = word.word.replace(" ", ""); 152 153 } 153 154 }, 154 155 onEditorFocusOut(index) { 156 // Remove empty words when they lose focus 155 157 if (this.words[index].word === "") { 156 158 this.words.splice(index, 1); 159 } 160 }, 161 onEditorKeyDown(event, index) { 162 /** @type {Word} */ 163 const word = this.words[index]; 164 165 // Remove the word if the user is repetitively hitting backspace/backspace on an empty cell 166 if (event.code === "Backspace" || event.Code === "Delete") { 167 if (word.word.length <= 1) { 168 if (word.deletionAttempts === 1) { 169 this.words.splice(index, 1); 170 171 setFocus(event.code === "Backspace", this); 172 } 173 174 console.log(word.deletionAttempts); 175 word.deletionAttempts++; 176 } 177 } 178 179 // Focus on the next word if the user is moving right with their caret at the word's end 180 if (event.code === "ArrowRight") { 181 const inputElement = this.$refs[word.id]; 182 183 if (inputElement.selectionStart === word.word.length) { 184 setFocus(false, this); 185 } 186 } 187 188 // Focus on the previous word if the user is moving left with their caret at the word's start 189 if (event.code === "ArrowLeft") { 190 const inputElement = this.$refs[word.id]; 191 192 if (inputElement.selectionStart === 0) { 193 setFocus(true, this); 194 } 195 } 196 197 /** 198 * Gets the index of an adjacent word to focus on. 199 * @param {Boolean} focusLeft Whether to focus on the word to the left or right of the current index. 200 * @param that The view model. 201 */ 202 function getFocusIndex(focusLeft, that) { 203 let focusIndex = 0; 204 205 if (focusLeft) { 206 focusIndex = index === 0 ? 0 : index - 1; 207 } 208 else { 209 focusIndex = index === that.words.length - 1 ? that.words.length - 1 : index + 1; 210 } 211 212 return focusIndex; 213 } 214 215 /** 216 * Sets the focus to an adjacent word. 217 * @param {Boolean} focusLeft Whether to focus on the word to the left or right of the current index. 218 * @param that The view model. 219 */ 220 function setFocus(focusLeft, that) { 221 const focusIndex = getFocusIndex(focusLeft, that); 222 const focusId = that.words[focusIndex].id; 223 that.$refs[focusId].focus(); 224 225 if (!focusLeft) { 226 that.$refs[focusId].setSelectionRange(-1, 0); 227 } 157 228 } 158 229 }
Note:
See TracChangeset
for help on using the changeset viewer.