Changeset 35516


Ignore:
Timestamp:
2021-09-29T13:37:53+13:00 (3 years ago)
Author:
cstephen
Message:

Improve word insertion and deletion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/model-interfaces-dev/atea/korero-maori-asr/src/components/TranscriptionItemEditor.vue

    r35510 r35516  
    1212        <ul class="list-view" v-else>
    1313            <li v-for="(word, index) in words" :key="word.id" class="word-container">
    14                 <input :size="word.word.length" :ref="word.id"
     14                <input :size="word.word.length === 0 ? 1 : word.word.length" :ref="word.id"
    1515                    class="editor-word" v-model="word.word" type="text" onpaste="return false;" :class="{ 'word-highlight-applied': word.shouldHighlight }"
    16                     @input="onEditorInput($event, index)" @focusout="onEditorFocusOut(index)" @keyup="onEditorKeyUp($event, index)" @focus="onEditorFocus(index)" />
     16                    @beforeinput="onEditorBeforeInput($event, index)" @focusout="onEditorFocusOut(index)" @keyup="onEditorKeyUp($event, index)" @focus="onEditorFocus(index)" />
    1717                <span>&nbsp;</span>
    1818            </li>
     
    8888import AudioTimeBar from "./AudioTimeBar.vue"
    8989import WordTimingSelector from "./WordTimingSelector.vue"
    90 import Util from "../js/Util";
     90import Util, { log } from "../js/Util";
    9191
    9292export class Word {
     
    179179        /**
    180180         * Invoked when the value of a word changes.
    181          * @param event The input event.
     181         * @param {InputEvent} event The input event.
    182182         * @param {Number} index The index of the word that has changed.
    183183         */
    184         onEditorInput(event, index) {
     184        onEditorBeforeInput(event, index) {
     185            log(event);
     186
     187            const deletionEvents = [
     188                "deleteWordForward", "deleteWordBackward",
     189                "deleteSoftLineBackward", "deleteSoftLineForward", "deleteEntireSoftLine",
     190                "deleteHardLineBackward", "deleteHardLineForward",
     191                "deleteByDrag", "deleteByCut",
     192                "deleteContent", "deleteContentBackward", "deleteContentForward"
     193            ];
     194
     195            const insertionEvents = [
     196                "insertText", "insertReplacementText",
     197                "insertLineBreak", "insertParagraph",
     198                "insertOrderedList", "insertUnorderedList", "insertHorizontalRule",
     199                "insertFromYank", "insertFromDrop", "insertFromPaste", "insertFromPasteAsQuotation",
     200                "insertTranspose", "insertCompositionText", "insertLink"
     201            ];
     202
     203            if (event.inputType === "historyUndo" || event.inputType === "historyRedo") {
     204                event.preventDefault();
     205                return;
     206            }
     207
    185208            /** @type {Word} */
    186209            const word = this.words[index];
    187210
    188             if (event.inputType === "insertText") {
    189                 // Insert a new word if whitespace is entered on an existing word
    190                 if (event.data === " " && word.word !== " ") {
    191                     const newStartTime = word.startTime + ((word.endTime - word.startTime) / 2);
    192                     const newWord = new Word("", newStartTime, word.endTime);
    193                     this.words.splice(index + 1, 0, newWord);
    194 
    195                     // We have to give the element some time to render, even though the ref is registered immediately
    196                     setTimeout(() => this.$refs[newWord.id].focus(), 50);
     211            if (deletionEvents.includes(event.inputType) && word.word.length === 0) {
     212                event.preventDefault(); // Prevents the event from being propagated to the newly focused input
     213                this.setFocus(index, event.inputType === "deleteContentBackward"); // Shifting focus off an empty input, hence the word will be removed
     214            }
     215
     216            if (insertionEvents.includes(event.inputType)) {
     217                if (event.data === " ") {
     218                    // Only insert if the current word isn't blank, to prevent 'jumping jacks' as the duplicate empty boxes are removed
     219                    if (word.word !== "") {
     220                        const newStartTime = word.startTime + ((word.endTime - word.startTime) / 2);
     221                        const newWord = new Word("", newStartTime, word.endTime);
     222                        this.words.splice(index + 1, 0, newWord);
     223
     224                        // We have to give the element some time to be mounted before we can focus it
     225                        setTimeout(() => this.$refs[newWord.id].focus(), 50);
     226                    }
     227
     228                    event.preventDefault();
    197229                }
    198 
    199                 word.word = word.word.replace(" ", "");
    200230            }
    201231        },
     
    218248            const word = this.words[index];
    219249
    220             // Remove the word if the user is repetitively hitting backspace/delete on an empty cell
    221             if (event.code === "Backspace" || event.code === "Delete") {
    222                 if (word.word.length < 1) {
    223                     if (word.deletionAttempts === 1) {
    224                         this.words.splice(index, 1);
    225 
    226                         this.setFocus(index, event.code === "Backspace");
    227                     }
    228 
    229                     word.deletionAttempts++;
    230                 }
    231             }
    232             else {
    233                 word.deletionAttempts = 0;
    234             }
    235 
    236             // Focus on the next word if the user is moving right with their caret at the word's end
     250            if (event.code === "Spacebar" && word.length > 0) {
     251                // - if at either edge, create new word before/after
     252                // - else split word
     253            }
     254
     255            // // Focus on the next word if the user is moving right with their caret at the word's end
    237256            // if (event.code === "ArrowRight") {
    238257            //     const inputElement = this.$refs[word.id];
    239258
    240259            //     if (inputElement.selectionStart === word.word.length) {
    241             //         setFocus(false, this);
     260            //         this.setFocus(index, false);
    242261            //     }
    243262            // }
    244263
    245             // Focus on the previous word if the user is moving left with their caret at the word's start
     264            // // Focus on the previous word if the user is moving left with their caret at the word's start
    246265            // if (event.code === "ArrowLeft") {
    247266            //     const inputElement = this.$refs[word.id];
    248267
    249268            //     if (inputElement.selectionStart === 0) {
    250             //         setFocus(true, this);
     269            //         this.setFocus(index, true);
    251270            //     }
    252271            // }
Note: See TracChangeset for help on using the changeset viewer.