
export class TextareaUtils {

    public static autoExpand(field: HTMLTextAreaElement) {
        // console.log('autoExpand');
        if (field) {
            // Reset field height
            field.style.height = '0px';
            const computed = window.getComputedStyle(field);
            // Calculate the height
            var height = 0
                + parseInt(computed.getPropertyValue('border-top-width'), 10)
                + field.scrollHeight
                + parseInt(computed.getPropertyValue('border-bottom-width'), 10);

            field.style.height = height + 'px';
        }
    }

    public static isEditingKeyPress(e: KeyboardEvent) {

        if ([46, 8, 9, 27, 13, 110, 190].indexOf(e.keyCode) !== -1 ||
            // Allow: Ctrl+A
            (e.keyCode === 65 && (e.ctrlKey || e.metaKey)) ||
            // Allow: Ctrl+C
            (e.keyCode === 67 && (e.ctrlKey || e.metaKey)) ||
            // Allow: Ctrl+V
            (e.keyCode === 86 && (e.ctrlKey || e.metaKey)) ||
            // Allow: Ctrl+X
            (e.keyCode === 88 && (e.ctrlKey || e.metaKey)) ||
            // Allow: page up, page down, home, end, left, right
            (e.keyCode >= 33 && e.keyCode <= 39)) {
            // let it happen, don't do anything
            return false;
        }

        if (typeof e.which == "undefined") {
            // This is IE, which only fires keypress events for printable keys
            return true;
        } else if (e.ctrlKey && e.key.toUpperCase() == 'V') {
            return true;
        }
        else if (!e.ctrlKey && !e.metaKey && !e.altKey && e.code != 'Tab') {
            return true;
        }

        return false;
    }
}
