// retoor <retoor@molodetz.nl>

export class TextInput {
    static applyValue(element, value, caretPos) {
        element.value = value;
        element.setSelectionRange(caretPos, caretPos);
        element.focus();
        element.dispatchEvent(new Event("input", { bubbles: true }));
    }

    static insertAtCursor(element, text) {
        const start = element.selectionStart;
        const end = element.selectionEnd;
        const value = element.value.substring(0, start) + text + element.value.substring(end);
        TextInput.applyValue(element, value, start + text.length);
    }
}

window.TextInput = TextInput;
