Simpler textarea selection manipulations.

This commit is contained in:
Nicolas Stepien 2012-03-05 23:56:18 +01:00
parent 457b908504
commit fcfafa6ae3
2 changed files with 18 additions and 16 deletions

View File

@ -972,7 +972,7 @@
return $.on(d, 'keydown', Keybinds.keydown);
},
keydown: function(e) {
var o, range, selEnd, selStart, ta, thread, valEnd, valMid, valStart, value, _ref, _ref2;
var o, range, selEnd, selStart, ta, thread, value, _ref, _ref2;
if (!(key = Keybinds.keyCode(e)) || /TEXTAREA|INPUT/.test(e.target.nodeName) && !(e.altKey || e.ctrlKey || e.keyCode === 27)) {
return;
}
@ -994,11 +994,8 @@
value = ta.value;
selStart = ta.selectionStart;
selEnd = ta.selectionEnd;
valStart = value.slice(0, selStart) + '[spoiler]';
valMid = value.slice(selStart, selEnd);
valEnd = '[/spoiler]' + value.slice(selEnd);
ta.value = valStart + valMid + valEnd;
range = valStart.length + valMid.length;
ta.value = value.slice(0, selStart) + '[spoiler]' + value.slice(selStart, selEnd) + '[/spoiler]' + value.slice(selEnd);
range = 9 + selEnd;
ta.setSelectionRange(range, range);
break;
case conf.zero:
@ -1449,7 +1446,7 @@
}
},
quote: function(e) {
var caretPos, id, s, sel, ta, text, _ref;
var caretPos, id, range, s, sel, ta, text, _ref;
if (e != null) e.preventDefault();
qr.open();
if (!g.REPLY) {
@ -1464,9 +1461,11 @@
}
ta = $('textarea', qr.el);
caretPos = ta.selectionStart;
qr.selected.el.lastChild.textContent = qr.selected.com = ta.value = ta.value.slice(0, caretPos) + text + ta.value.slice(ta.selectionEnd, ta.value.length);
qr.selected.el.lastChild.textContent = qr.selected.com = ta.value = ta.value.slice(0, caretPos) + text + ta.value.slice(ta.selectionEnd);
ta.focus();
return ta.selectionEnd = ta.selectionStart = caretPos + text.length;
ta.selectionEnd = ta.selectionStart = caretPos + text.length;
range = caretPos + text.length;
return ta.setSelectionRange(range, range);
},
drag: function(e) {
var i;

View File

@ -826,18 +826,19 @@ Keybinds =
qr.close()
when conf.spoiler
ta = e.target
return unless ta.nodeName is 'TEXTAREA'
return if ta.nodeName isnt 'TEXTAREA'
value = ta.value
selStart = ta.selectionStart
selEnd = ta.selectionEnd
valStart = value[0...selStart] + '[spoiler]'
valMid = value[selStart...selEnd]
valEnd = '[/spoiler]' + value[selEnd..]
ta.value =
value[...selStart] +
'[spoiler]' + value[selStart...selEnd] + '[/spoiler]' +
value[selEnd..]
range = 9 + selEnd
ta.value = valStart + valMid + valEnd
range = valStart.length + valMid.length
# Move the caret to the end of the selection.
ta.setSelectionRange range, range
when conf.zero
window.location = "/#{g.BOARD}/0#0"
@ -1182,10 +1183,12 @@ qr =
qr.selected.el.lastChild.textContent =
qr.selected.com =
ta.value =
ta.value[0...caretPos] + text + ta.value[ta.selectionEnd...ta.value.length]
ta.value[...caretPos] + text + ta.value[ta.selectionEnd..]
ta.focus()
# Move the caret to the end of the new quote.
ta.selectionEnd = ta.selectionStart = caretPos + text.length
range = caretPos + text.length
ta.setSelectionRange range, range
drag: (e) ->
# Let it drag anything from the page.