main.js : support tab insertion in /hack/ pad

pull/1/head
ansuz 9 years ago
parent 6b9d982d40
commit 6bb37aed44

@ -3,9 +3,10 @@ define([
'/common/realtime-input.js', '/common/realtime-input.js',
'/common/messages.js', '/common/messages.js',
'/common/crypto.js', '/common/crypto.js',
'/common/cursor.js',
'/bower_components/jquery/dist/jquery.min.js', '/bower_components/jquery/dist/jquery.min.js',
'/customize/pad.js' '/customize/pad.js'
], function (Config, Realtime, Messages, Crypto) { ], function (Config, Realtime, Messages, Crypto, Cursor) {
var $ = window.jQuery; var $ = window.jQuery;
$(window).on('hashchange', function() { $(window).on('hashchange', function() {
window.location.reload(); window.location.reload();
@ -59,6 +60,48 @@ define([
var rt = Realtime.start(config); 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) { $run.click(function (e) {
e.preventDefault(); e.preventDefault();
var content = $textarea.val(); var content = $textarea.val();

Loading…
Cancel
Save