Changeset 35396


Ignore:
Timestamp:
2021-09-14T17:01:31+12:00 (3 years ago)
Author:
cstephen
Message:

Improvements to word editing

File:
1 edited

Legend:

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

    r35395 r35396  
    2424                    <input :size="word.word.length <= 1 ? 1 : word.word.length - 1" :ref="word.id"
    2525                        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)" />
    2727                    <span>&nbsp;</span>
    2828                </li>
     
    8585    /**
    8686     * Initialises a new instance of the Word class.
    87      * @param {String} id The ID of the word.
    8887     * @param {String} word The word.
    8988     * @param {Number} startTime The time within the audio that this word starts being spoken.
     
    105104        /** @type {Boolean} A value indicating whether this word should be highlighted. */
    106105        this.shouldHighlight = false;
     106
     107        /** @type {Number} The number of times the user has tried to delete this word. */
     108        this.deletionAttempts = 0;
    107109    }
    108110}
     
    134136         */
    135137        onEditorInput(event, index) {
    136             console.log(event);
    137 
    138138            /** @type {Word} */
    139139            const word = this.words[index];
    140140
    141141            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 !== " ") {
    145144                    // TODO: Proper timing metadata
    146145                    const newWord = new Word("", word.startTime, word.endTime);
     
    150149                    setTimeout(() => this.$refs[newWord.id].focus(), 50);
    151150                }
     151
     152                word.word = word.word.replace(" ", "");
    152153            }
    153154        },
    154155        onEditorFocusOut(index) {
     156            // Remove empty words when they lose focus
    155157            if (this.words[index].word === "") {
    156158                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                }
    157228            }
    158229        }
Note: See TracChangeset for help on using the changeset viewer.