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

View File

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