From fcfafa6ae36cbe5e1137d49fefbe11fcd86ecf4a Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Mon, 5 Mar 2012 23:56:18 +0100 Subject: [PATCH] Simpler textarea selection manipulations. --- 4chan_x.user.js | 17 ++++++++--------- script.coffee | 17 ++++++++++------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 8e70c65ff..20b60896d 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -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; diff --git a/script.coffee b/script.coffee index 315463077..d741e4a21 100644 --- a/script.coffee +++ b/script.coffee @@ -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.