键入一个空格自动转换全角空格

JavaScript   2025-01-16 15:09   54   0  
handleInput(event, row, projectSn, textareaId) {        
        const inputElement = event.target;        
        let inputValue = inputElement.value;        
        const cursorPosition = inputElement.selectionStart;        
        // Replace single spaces with full-width spaces to occupy one Chinese character space
        const newValue = inputValue.replace(/ /g, '\u3000');        
        // Update the model value
        this.$set(row, projectSn, newValue);        
        // Calculate new cursor position
        const newCursorPosition = cursorPosition + (newValue.length - inputValue.length);        
        // Update the textarea content directly to reflect the changes immediately
        this.$nextTick(() => {
            inputElement.value = newValue;
            inputElement.setSelectionRange(newCursorPosition, newCursorPosition);
        });
},
<textarea
    :id="'textarea_' + scope.$index"
    v-model.lazy="scope.row[project.projectSn]"
    :ref="'value_item_' + scope.$index + '_' + (pindex + 2)"
    @input="handleInput($event, scope.row, project.projectSn, 'textarea_' + scope.$index)"
</textarea>