<!DOCTYPE html>
<html lang="en">
<head>
<title>Jody's Notes</title>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<link rel="stylesheet" href="lib/codemirror.css">
<link rel="stylesheet" href="addon/dialog/dialog.css">
<script src="lib/codemirror.js"></script>
<script src="addon/selection/active-line.js"></script>
<script src="addon/dialog/dialog.js"></script>
<script src="addon/search/searchcursor.js"></script>
<script src="mode/markdown/markdown.js"></script>
<script src="addon/edit/matchbrackets.js"></script>
<script src="keymap/vim.js"></script>
<style type="text/css">
    .CodeMirror {
        border: 1px solid #eee;
        height: auto;
        border-top: 1px solid black;
        border-bottom: 1px solid black;
    }
</style>
</head>
<body>

<form><textarea id="editor" name="editor">
{{{notetxt}}}
</textarea></form>
<div style="font-size: 13px; width: 300px; height: 30px;">Key buffer: <span id="command-display"></span></div>

<script>
    var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
        mode:  "markdown",
        lineNumbers: "true",
        keyMap: "vim",
        matchBrackets: true,
        showCursorWhenSelecting: true,
        viewportMargin: Infinity,
        tabSize: 4,
        styleActiveLine: true
    });
    // Add my .vimrc stuff.
    CodeMirror.Vim.map('jj', '<Esc>', 'insert')
    // Hacked semi-colon/colon swap remap into /assets/keymap/vim.js...kind of a heavy hammer might bite me later.
    // ...and it's already confused me later the same day. I deleted ~/notes.kaplon.us/CodeMirror w/out realizing I'd symlinked it into /assets!
    // This no worky, there's no concept of <leader> in CodeMirror, but i was hoping this would work-around it.
    //CodeMirror.Vim.map(' c', '<Esc>o- [ ] ', 'normal')
    //CodeMirror.Vim.map(' x', '<ESC>;s/\[\s\]/[x]/g<CR>;noh<CR>', 'normal')
    //CodeMirror.commands.save = function(){ alert("Saving"); }
    var commandDisplay = document.getElementById('command-display');
    var keys = ''
    CodeMirror.on(editor, 'vim-keypress', function(key) {
        keys = keys + key
        commandDisplay.innerHTML = keys;
    });
    CodeMirror.on(editor, 'vim-command-done', function(e) {
        keys = '';
        commandDisplay.innerHTML = keys;
    });

    var typingTimer;
    editor.on("changes", function() {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(
        function() {
            //console.log(editor.getValue());
           var request = new XMLHttpRequest();
            request.onreadystatechange = function () {
                var DONE = this.DONE || 4;
                if (this.readyState === DONE){
                    console.log('ajax is done.');
                }
            };
            request.open('POST', '/', true);
            request.send(editor.getValue()); 
        },
        2000
      );
    });
</script>
</body>
</html>