source: main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/components/TranscriptionItemEditor.vue@ 35819

Last change on this file since 35819 was 35819, checked in by cstephen, 2 years ago

Add autocomplete attribute to input fields

File size: 16.8 KB
Line 
1<template>
2<div>
3 <div class="text-container words-container" :hidden="enableEditing">
4 <ul class="list-view" v-if="!enableEditing">
5 <li v-for="word in words" :key="word.id" class="word-container" @click="playAudio(word.startTime)">
6 <span class="word-highlight word" :class="{ 'word-highlight-applied': word.shouldHighlight }">
7 {{ word.word }}
8 </span>
9 </li>
10 </ul>
11
12 <ul class="list-view" v-else>
13 <li v-for="(word, index) in words" :key="word.id" class="word-container">
14 <input :size="word.word.length === 0 ? 1 : word.word.length" :ref="word.id" @click="loadAudio(word.startTime)"
15 class="editor-word" v-model="word.word" type="text" :class="{ 'word-highlight-applied': word.shouldHighlight }"
16 @beforeinput="onEditorBeforeInput($event, index)" @focusout="onEditorFocusOut(index)" @focus="onEditorFocus(index)"
17 autocomplete="off" />
18 <span>&nbsp;</span>
19 </li>
20 </ul>
21 </div>
22
23 <word-timing-selector class="word-timing-selector" @wordUpdated="onWordUpdated" v-if="enableEditing"
24 :surroundingWords="surroundingWords" :wordIndex="surroundingWordPrincipleIndex" :transcriptionID="transcription.id" />
25</div>
26</template>
27
28<style scoped lang="scss">
29.word {
30 padding: 0.25em;
31}
32
33.words-container {
34 transition-duration: var(--transition-duration);
35
36 &[hidden] {
37 background-color: inherit;
38 }
39}
40
41.word-container {
42 font: var(--monospace-font);
43 display: inline-block;
44 line-height: 2em;
45}
46
47.word-highlight-applied {
48 background-color: var(--highlighted-word-bg);
49}
50
51.word-highlight {
52 &:hover {
53 @extend .word-highlight-applied;
54 }
55}
56
57.editor-word {
58 border: 1px solid black;
59 border-radius: 2px;
60 padding: 2px;
61 font-family: inherit;
62 font-size: 1rem;
63}
64
65.word-timing-selector {
66 margin: 0.5em auto;
67}
68</style>
69
70<script>
71import { mapState } from "vuex";
72import { TranscriptionViewModel } from "../main";
73import AudioPlayback from "../js/AudioPlaybackModule"
74import WordTimingSelector from "./WordTimingSelector.vue"
75import Util from "../js/Util";
76
77export class Word {
78 /**
79 * Initialises a new instance of the Word class.
80 * @param {String} word The word.
81 * @param {Number} startTime The time within the audio that this word starts being spoken.
82 * @param {Number} endTime The time within the audio that this word finishes being spoken.
83 */
84 constructor(word, startTime, endTime, id = null, shouldHighlight = false, deletionAttempts = 0) {
85 /** @type {String} The ID of this word. */
86 this.id = id ?? Util.generateUuid();
87
88 /** @type {String} The word. */
89 this.word = word;
90
91 /** @type {Number} The time within the audio that this word starts being spoken. */
92 this.startTime = startTime;
93
94 /** @type {Number} The time within the audio that this word finishes being spoken. */
95 this.endTime = endTime;
96
97 /** @type {Boolean} A value indicating whether this word should be highlighted. */
98 this.shouldHighlight = shouldHighlight;
99
100 /** @type {Number} The number of times the user has tried to delete this word. */
101 this.deletionAttempts = deletionAttempts;
102 }
103}
104
105export default {
106 name: "TranscriptionItemEditor",
107 components: {
108 WordTimingSelector
109 },
110 props: {
111 transcription: TranscriptionViewModel,
112 enableEditing: Boolean
113 },
114 data() {
115 return {
116 words: [],
117 lastHighlightedWordIndex: 0,
118 selectedIndex: 0,
119 surroundingWordPrincipleIndex: 0
120 }
121 },
122 computed: {
123 currentPlaybackTime: {
124 get() {
125 return this.$store.getters.transcriptionPlaybackTime(this.transcription.id);
126 },
127 set(value) {
128 this.$store.commit("playbackStateSetTime", { id: this.transcription.id, time: value });
129 }
130 },
131 surroundingWords() {
132 return this.getSurroundingWords(this.selectedIndex);
133 },
134 ...mapState({
135 playbackState: state => state.playbackState,
136 translations: state => state.translations
137 })
138 },
139 methods: {
140
141 /**
142 * Gets the words in a transcription.
143 * @param {TranscriptionViewModel} transcription The transcription.
144 * @returns {Word[]}
145 */
146 getWords() {
147 const metadata = this.transcription.metadata;
148
149 if (metadata.length === 0) {
150 return;
151 }
152
153 /** @type {Word[]} */
154 const words = [];
155
156 let currentWord = "";
157 let currStartTime = metadata[0].start_time;
158
159 for (let i = 0; i < metadata.length; i++) {
160 const mChar = metadata[i];
161
162 if (mChar.char === " ") {
163 words.push(new Word(currentWord, currStartTime, mChar.start_time));
164 currentWord = "";
165
166 if (i + 1 < metadata.length) {
167 currStartTime = metadata[i + 1].start_time;
168 }
169 else {
170 break;
171 }
172 }
173 else {
174 currentWord += mChar.char;
175 }
176 }
177
178 // Push the last word, as most transcriptions will not end in a space (hence breaking the above algorithm)
179 if (currentWord.length > 0) {
180 const newWord = new Word(currentWord, currStartTime, metadata[metadata.length - 1].start_time)
181 words.push(newWord);
182 }
183
184 return words;
185 },
186
187 async playAudio(startTime) {
188 await AudioPlayback.play(this.transcription.id, startTime);
189 },
190
191 async loadAudio(startTime) {
192 await AudioPlayback.load(this.transcription.id, startTime);
193 },
194
195 /**
196 * Gets the minimum viable index that surrounding words can start at.
197 * @param {Number} principleIndex The index of the primary surrounding word.
198 * @param {Number} buffer The maximum number of words to take on each side of the principle word.
199 */
200 getSurroundingWordMinIndex(principleIndex, buffer) {
201 let min = principleIndex;
202 for (let i = principleIndex; i > principleIndex - buffer && i > 0; i--) {
203 min--;
204 }
205
206 return min;
207 },
208
209 getSurroundingWords(index) {
210 const BUFFER = 2; // The number of words to take on each side TODO: Global constant
211
212 const min = this.getSurroundingWordMinIndex(index, BUFFER);
213
214 let max = index + 1;
215 for (let i = index; i < index + BUFFER && i < this.words.length; i++) {
216 max++;
217 }
218
219 this.surroundingWordPrincipleIndex = index - min;
220 return this.words.slice(min, max);
221 },
222
223 onWordUpdated(relativeIndex, word) {
224 const BUFFER = 2; // The number of words to take on each side
225
226 const min = this.getSurroundingWordMinIndex(this.selectedIndex, BUFFER);
227
228 this.words[min + relativeIndex] = word;
229 },
230
231 /**
232 * Invoked when the value of a word changes.
233 * @param {InputEvent} event The input event.
234 * @param {Number} index The index of the word that has changed.
235 */
236 onEditorBeforeInput(event, index) {
237 // https://rawgit.com/w3c/input-events/v1/index.html#interface-InputEvent-Attributes
238 const deletionEvents = [
239 "deleteWordForward", "deleteWordBackward",
240 "deleteSoftLineBackward", "deleteSoftLineForward", "deleteEntireSoftLine",
241 "deleteHardLineBackward", "deleteHardLineForward",
242 "deleteByDrag", "deleteByCut",
243 "deleteContent", "deleteContentBackward", "deleteContentForward"
244 ];
245
246 const backwardDeletionEvents = [
247 "deleteWordBackward", "deleteSoftLineBackward", "deleteHardLineBackward", "deleteContentBackward"
248 ]
249
250 const forwardDeletionEvents = [
251 "deleteWordForward", "deleteSoftLineForward", "deleteHardLineForward", "deleteContentForward"
252 ]
253
254 const insertionEvents = [
255 "insertText", "insertReplacementText",
256 "insertLineBreak", "insertParagraph",
257 "insertOrderedList", "insertUnorderedList", "insertHorizontalRule",
258 "insertFromYank", "insertFromDrop", "insertFromPaste", "insertFromPasteAsQuotation",
259 "insertTranspose", "insertCompositionText", "insertLink"
260 ];
261
262 if (event.inputType === "historyUndo" || event.inputType === "historyRedo") {
263 event.preventDefault();
264 return;
265 }
266
267 /** @type {Word} */
268 const word = this.words[index];
269 const inputIndex = this.$refs[word.id].selectionStart;
270 const inputEndIndex = this.$refs[word.id].selectionEnd;
271
272 if (deletionEvents.includes(event.inputType)) {
273 if (word.word.length === 0) { // Remove the word
274 if (index === 0) {
275 this.mergeWordForward(word, index);
276 }
277 else if (index === this.words.length - 1) {
278 this.mergeWordBackward(word, index);
279 }
280 else if (backwardDeletionEvents.includes(event.inputType)) {
281 this.mergeWordBackward(word, index);
282 }
283 else if (forwardDeletionEvents.includes(event.inputType)) {
284 this.mergeWordForward(word, index);
285 }
286
287 event.preventDefault();
288 }
289 else if (inputIndex === 0 && backwardDeletionEvents.includes(event.inputType) && !inputEndIndex) { // Join with last word
290 if (index === 0) {
291 return;
292 }
293 else {
294 this.mergeWordBackward(word, index);
295 }
296
297 event.preventDefault();
298 }
299 else if (inputIndex === word.word.length && forwardDeletionEvents.includes(event.inputType)) { // Join with next word
300 if (index === this.words.length - 1) {
301 return;
302 }
303 else {
304 this.mergeWordForward(word, index);
305 }
306
307 event.preventDefault();
308 }
309 }
310
311 if (insertionEvents.includes(event.inputType)) {
312 if (event.data === " ") { // ONLY whitespace entered. Split or add new word
313 let newWord;
314
315 if (inputIndex === 0) { // Insert on left side
316 const newEndTime = word.startTime + (word.endTime - word.startTime) / 2;
317 newWord = new Word("", word.startTime, newEndTime);
318
319 this.words.splice(index, 0, newWord);
320 word.startTime = newEndTime;
321 }
322 else if (inputIndex === word.word.length) { // Insert on right side
323 const newStartTime = word.startTime + ((word.endTime - word.startTime) / 2);
324 newWord = new Word("", newStartTime, word.endTime);
325
326 this.words.splice(index + 1, 0, newWord);
327 word.endTime = newStartTime;
328 }
329 else { // Split down the middle
330 const metadataIndex = this.transcription.metadata.findIndex(m => m.start_time === word.startTime) + inputIndex;
331 const newStartTime = this.transcription.metadata[metadataIndex].start_time;
332 newWord = new Word(word.word.slice(inputIndex), newStartTime, word.endTime);
333
334 this.words.splice(index + 1, 0, newWord);
335 word.endTime = newStartTime;
336 word.word = word.word.slice(0, inputIndex);
337 }
338
339 // Prevent the event from being propagated to the newly focused input
340 event.preventDefault();
341
342 // Give the element time to be mounted before focusing
343 setTimeout(
344 () => {
345 this.$refs[newWord.id].focus();
346 this.$refs[newWord.id].selectionStart = 0;
347 this.$refs[newWord.id].selectionEnd = null;
348 },
349 50
350 );
351 }
352 else if (event.data.includes(" ")) {
353 let wordsToAdd = event.data.split(" ");
354 wordsToAdd = wordsToAdd.filter(w => w.trim().length > 0).map(w => w.trim());
355 const sharedTime = (word.endTime - word.startTime) / wordsToAdd.length;
356
357 if (wordsToAdd.length === 0) {
358 return;
359 }
360
361 word.endTime = word.startTime + sharedTime;
362 let newStartTime = word.endTime;
363
364 for (let i = 0; i < wordsToAdd.length; i++) {
365 const newWord = new Word(wordsToAdd[i], newStartTime, newStartTime + sharedTime);
366 newStartTime += sharedTime;
367
368 this.words.splice(index + i + 1, 0, newWord);
369 }
370
371 event.preventDefault();
372 }
373 }
374 },
375
376 mergeWordForward(word, index) {
377 if (this.words.length < 2) {
378 return;
379 }
380
381 this.words[index + 1].startTime = word.startTime;
382 this.words[index + 1].word = word.word + this.words[index + 1].word;
383
384 word.word = "";
385 this.setFocus(index, false);
386 },
387
388 mergeWordBackward(word, index) {
389 if (this.words.length < 2) {
390 return;
391 }
392
393 this.words[index - 1].endTime = word.endTime;
394 this.words[index - 1].word += word.word;
395
396 word.word = "";
397 this.setFocus(index, true);
398 },
399
400 onEditorFocus(index) {
401 if (this.currentPlaybackTime < this.words[index].startTime || this.currentPlaybackTime > this.words[index].endTime) {
402 const wordStartTime = this.words[index].startTime;
403 this.$store.commit("playbackStateSetTime", { id: this.transcription.id, time: wordStartTime });
404 }
405
406 this.selectedIndex = index;
407 },
408
409 onEditorFocusOut(index) {
410 // Remove empty words when they lose focus
411 if (this.words[index].word === "") {
412 this.words.splice(index, 1);
413 }
414 },
415
416 /**
417 * Gets the index of an adjacent word to focus on.
418 * @param {Number} index The index of the currently focused word.
419 * @param {Boolean} focusLeft Whether to focus on the word to the left or right of the current index.
420 */
421 getFocusIndex(index, focusLeft) {
422 let focusIndex = 0;
423
424 if (focusLeft) {
425 focusIndex = index === 0 ? 0 : index - 1;
426 }
427 else {
428 focusIndex = index === this.words.length - 1 ? index : index + 1;
429 }
430
431 return focusIndex;
432 },
433
434 /**
435 * Sets the focus to an adjacent word.
436 * @param {Number} index The index of the currently focused word.
437 * @param {Boolean} focusLeft Whether to focus on the word to the left or right of the current index.
438 */
439 setFocus(index, focusLeft) {
440 const focusIndex = this.getFocusIndex(index, focusLeft);
441 const focusId = this.words[focusIndex].id;
442 this.$refs[focusId].focus();
443
444 if (!focusLeft) {
445 this.$refs[focusId].setSelectionRange(0, 0);
446 }
447 }
448
449 },
450 watch: {
451 currentPlaybackTime(newValue) {
452 this.words[this.lastHighlightedWordIndex].shouldHighlight = false;
453
454 if (this.playbackState.id !== this.transcription.id) {
455 return;
456 }
457
458 for (let i = 0; i < this.words.length; i++) {
459 const word = this.words[i];
460
461 if (word.startTime <= newValue && word.endTime > newValue) {
462 word.shouldHighlight = true;
463
464 if (this.$refs[word.id]) {
465 this.$refs[word.id].focus();
466 }
467
468 this.lastHighlightedWordIndex = i;
469 break;
470 }
471 }
472 }
473 },
474 beforeMount() {
475 this.words = this.getWords();
476 }
477}
478</script>
Note: See TracBrowser for help on using the repository browser.