/* Add callbacks on text formatting buttons */

function edit(e, type) {
    function inline(type, str, repeat, insert) {
        // Characters used to format inline blocs
        // repeat: if true, add one more char to the longest suite found
        // insert: insert <insert> between char and str (before and after)
        var chars = {
            'bold': '*',
            'italic': '/',
            'underline': '_',
            'strikethrough': '~',
            'inline-code': '`',
            'h1': '===',
            'h2': '---',
            'h3': '...',
        }

        if (repeat) {
            // Detect longest suite of similar chars
            var n = 1; var tmp = 1;
            for(var i = 0; i < str.length; i++) {
                if(str[i] == chars[type]) tmp++;
                else tmp = 1;
                n = (tmp > n) ? tmp : n;
            }
            return chars[type].repeat(n) + insert + str + insert + chars[type].repeat(n);
        }

        return chars[type] + insert + str + insert + chars[type];
    }

    function list(type, str) {
        switch(type) {
            case 'list-bulleted':
                return '* ' + str.replaceAll('\n', '\n* ');
                break;
            case 'list-numbered':
                return '1. ' + str;
                break;
        }
    }

    var ta = e.parentNode.parentNode.querySelector('textarea');
    var start = ta.selectionStart;
    var end = ta.selectionEnd;

    switch(type) {
        case 'bold':
        case 'italic':
        case 'underline':
        case 'strikethrough':
        case 'inline-code':
            ta.value = ta.value.substring(0, start)
                + inline(type, ta.value.substring(start, end), true, '')
                + ta.value.substring(end);
            break;
        case 'h1':
        case 'h2':
        case 'h3':
            ta.value = ta.value.substring(0, start)
                + inline(type, ta.value.substring(start, end), false, ' ')
                + ta.value.substring(end);
            break;
        case 'list-bulleted':
        case 'list-numbered':
            ta.value = ta.value.substring(0, start)
                + list(type, ta.value.substring(start, end))
                + ta.value.substring(end);
            break;
    }
}

function pre(type, str, multiline) {

}


function bold(e) {
    var ta = e.parentNode.parentNode.querySelector('textarea');
    var indexStart = ta.selectionStart;
    var indexEnd = ta.selectionEnd;
    var txt = ta.value.substring(indexStart, indexEnd);
    ta.value += '\n' + inline('bold', txt);
}


// Tab insert some spaces
// Ctrl+Enter send the form
ta = document.querySelector(".editor textarea");
ta.addEventListener('keydown', function(e) {
    var keyCode = e.keyCode || e.which;
    if (keyCode == 9) {
        e.preventDefault();

        var start = e.target.selectionStart;
        var end = e.target.selectionEnd;
        // set textarea value to: text before caret + tab + text after caret
        e.target.value = e.target.value.substring(0, start) + "\t" + e.target.value.substring(end);
        e.target.selectionEnd = start + 1;
    }
    if (e.ctrlKey && keyCode == 13) {
        var e = e.target;
        while(! (e instanceof HTMLFormElement)) {
            e = e.parentNode;
        }
        try {
            e.submit();
        } catch(exception) {
            e.submit.click();
        }
    }
});
