diff --git a/dingus.html b/dingus.html
@@ -12,37 +12,55 @@
var writer = new commonmark.HtmlRenderer();
var reader = new commonmark.DocParser();
-function getQueryVariable(variable)
-{
- var query = window.location.search.substring(1);
- var vars = query.split("&");
- for (var i=0;i<vars.length;i++) {
- var pair = vars[i].split("=");
- if(pair[0] == variable){return decodeURIComponent(pair[1]);}
- }
- return null;
+function getQueryVariable(variable) {
+ var query = window.location.search.substring(1);
+ var vars = query.split("&");
+ for (var i=0; i<vars.length; i++) {
+ var pair = vars[i].split("=");
+ if (pair[0] == variable){
+ return decodeURIComponent(pair[1]);
+ }
+ }
+ return null;
}
+// via http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
+function setSelectionRange(input, selectionStart, selectionEnd) {
+ if (input.setSelectionRange) {
+ input.focus();
+ input.setSelectionRange(selectionStart, selectionEnd);
+ }
+ else if (input.createTextRange) {
+ var range = input.createTextRange();
+ range.collapse(true);
+ range.moveEnd('character', selectionEnd);
+ range.moveStart('character', selectionStart);
+ range.select();
+ }
+}
+// via http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
+function setCaretToPos(input, pos) {
+ setSelectionRange(input, pos, pos);
+}
$(document).ready(function() {
var timer;
var x;
var parsed;
var render = function() {
- if (parsed === undefined) {
- return;
- }
- var startTime = new Date().getTime();
- var result = writer.renderBlock(parsed);
- var endTime = new Date().getTime();
- var renderTime = endTime - startTime;
- // $("#html").text(result);
- $("#preview").html(result);
- $("#html").text(result);
- $("#ast").text(commonmark.ASTRenderer(parsed));
- $("#rendertime").text(renderTime);
+ if (parsed === undefined) {
+ return;
+ }
+ var startTime = new Date().getTime();
+ var result = writer.renderBlock(parsed);
+ var endTime = new Date().getTime();
+ var renderTime = endTime - startTime;
+ $("#preview").html(result);
+ $("#html").text(result);
+ $("#ast").text(commonmark.ASTRenderer(parsed));
+ $("#rendertime").text(renderTime);
};
- var parseAndRender = function () {
+ var parseAndRender = function() {
if (x) { x.abort() } // If there is an existing XHR, abort it.
clearTimeout(timer); // Clear the timer so we don't end up with dupes.
timer = setTimeout(function() { // assign timer a new timeout
@@ -71,10 +89,16 @@ $(document).ready(function() {
$('#result-tabs a[href="#result"]').tab('show');
}
// make tab insert a tab in the text box:
- $("#text").keydown(function (e) {
+ $("#text").keydown(function(e) {
if (e.which == 9) {
- e.preventDefault();
+ e.preventDefault();
+ if (this.selectionStart !== undefined) {
+ var pos = this.selectionStart;
+ this.value = this.value.substring(0, pos) + "\t" + this.value.substring(pos);
+ setCaretToPos(this, pos + 1);
+ } else {
this.value += "\t";
+ }
}
});
parseAndRender();
@@ -143,7 +167,6 @@ $(document).ready(function() {
<div id="result-ast" class="tab-pane">
<pre id="astpre"><code id="ast"></code></pre>
</div>
- </div>
</div>
</div>
</div>