From 6bb37aed449f2b8674d27d2324201f54e0ca6b1b Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 12 Apr 2016 14:05:56 +0200 Subject: [PATCH] main.js : support tab insertion in /hack/ pad --- www/hack/main.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/www/hack/main.js b/www/hack/main.js index ab38f8402..8877260eb 100644 --- a/www/hack/main.js +++ b/www/hack/main.js @@ -3,9 +3,10 @@ define([ '/common/realtime-input.js', '/common/messages.js', '/common/crypto.js', + '/common/cursor.js', '/bower_components/jquery/dist/jquery.min.js', '/customize/pad.js' -], function (Config, Realtime, Messages, Crypto) { +], function (Config, Realtime, Messages, Crypto, Cursor) { var $ = window.jQuery; $(window).on('hashchange', function() { window.location.reload(); @@ -59,6 +60,48 @@ define([ var rt = Realtime.start(config); + var cursor = Cursor($textarea[0]); + + var splice = function (str, index, chars) { + var count = chars.length; + return str.slice(0, index) + chars + str.slice((index -1) + count); + }; + + var setSelectionRange = function (input, start, end) { + if (input.setSelectionRange) { + input.focus(); + input.setSelectionRange(start, end); + } else if (input.createTextRange) { + var range = input.createTextRange(); + range.collapse(true); + range.moveEnd('character', end); + range.moveStart('character', start); + range.select(); + } + }; + + var setCursor = function (el, pos) { + setSelectionRange(el, pos, pos); + }; + + $textarea.on('keypress', function (e) { + switch (e.key) { + case 'Tab': + // insert a tab wherever the cursor is... + var position = $textarea.prop("selectionStart"); + if (typeof position !== 'undefined') { + $textarea.val(function (i, val) { + return splice(val, position, "\t"); + }); + setCursor($textarea[0], position +1); + } + // prevent default behaviour for tab + e.preventDefault(); + default: + break; + } + }); + $run.click(function (e) { e.preventDefault(); var content = $textarea.val();