From bccee036b24011c10449c87ff173b7a158edff48 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 27 May 2012 14:52:33 +0200 Subject: [PATCH] Add quick code tags keybind. --- 4chan_x.user.js | 28 ++++++++++++++++++++-------- changelog | 3 +++ script.coffee | 35 +++++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 44d73e6b4..1fcc171d9 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -160,7 +160,8 @@ openEmptyQR: ['I', 'Open QR without post number inserted'], openOptions: ['ctrl+o', 'Open Options'], close: ['Esc', 'Close Options or QR'], - spoiler: ['ctrl+s', 'Quick spoiler'], + spoiler: ['ctrl+s', 'Quick spoiler tags'], + code: ['alt+c', 'Quick code tags'], submit: ['alt+s', 'Submit post'], watch: ['w', 'Watch thread'], update: ['u', 'Update now'], @@ -1023,7 +1024,7 @@ return $.on(d, 'keydown', Keybinds.keydown); }, keydown: function(e) { - var key, link, o, range, selEnd, selStart, ta, thread, value; + var key, link, o, ta, thread; if (!(key = Keybinds.keyCode(e)) || /TEXTAREA|INPUT/.test(e.target.nodeName) && !(e.altKey || e.ctrlKey || e.keyCode === 27)) { return; } @@ -1057,12 +1058,14 @@ if (ta.nodeName !== 'TEXTAREA') { return; } - value = ta.value; - selStart = ta.selectionStart; - selEnd = ta.selectionEnd; - ta.value = value.slice(0, selStart) + '[spoiler]' + value.slice(selStart, selEnd) + '[/spoiler]' + value.slice(selEnd); - range = 9 + selEnd; - ta.setSelectionRange(range, range); + Keybinds.tags('spoiler', ta); + break; + case Conf.code: + ta = e.target; + if (ta.nodeName !== 'TEXTAREA') { + return; + } + Keybinds.tags('code', ta); break; case Conf.watch: Watcher.toggle(thread); @@ -1203,6 +1206,15 @@ } return key; }, + tags: function(tag, ta) { + var range, selEnd, selStart, value; + value = ta.value; + selStart = ta.selectionStart; + selEnd = ta.selectionEnd; + ta.value = value.slice(0, selStart) + ("[" + tag + "]") + value.slice(selStart, selEnd) + ("[/" + tag + "]") + value.slice(selEnd); + range = ("[" + tag + "]").length + selEnd; + return ta.setSelectionRange(range, range); + }, img: function(thread, all) { var thumb; if (all) { diff --git a/changelog b/changelog index b46a7c3ab..11de6b000 100644 --- a/changelog +++ b/changelog @@ -1,5 +1,8 @@ master +- Mayhem + Add quick [code] tags keybind (alt+c). + 2.30.4 - Mayhem Add /co/ and /k/ archive redirection. diff --git a/script.coffee b/script.coffee index 4257bb01a..9c876b1df 100644 --- a/script.coffee +++ b/script.coffee @@ -123,7 +123,8 @@ Config = openEmptyQR: ['I', 'Open QR without post number inserted'] openOptions: ['ctrl+o', 'Open Options'] close: ['Esc', 'Close Options or QR'] - spoiler: ['ctrl+s', 'Quick spoiler'] + spoiler: ['ctrl+s', 'Quick spoiler tags'] + code: ['alt+c', 'Quick code tags'] submit: ['alt+s', 'Submit post'] # Thread related watch: ['w', 'Watch thread'] @@ -801,19 +802,11 @@ Keybinds = when Conf.spoiler ta = e.target return if ta.nodeName isnt 'TEXTAREA' - - value = ta.value - selStart = ta.selectionStart - selEnd = ta.selectionEnd - - ta.value = - value[...selStart] + - '[spoiler]' + value[selStart...selEnd] + '[/spoiler]' + - value[selEnd..] - - range = 9 + selEnd - # Move the caret to the end of the selection. - ta.setSelectionRange range, range + Keybinds.tags 'spoiler', ta + when Conf.code + ta = e.target + return if ta.nodeName isnt 'TEXTAREA' + Keybinds.tags 'code', ta # Thread related when Conf.watch Watcher.toggle thread @@ -884,6 +877,20 @@ Keybinds = if e.ctrlKey then key = 'ctrl+' + key key + tags: (tag, ta) -> + value = ta.value + selStart = ta.selectionStart + selEnd = ta.selectionEnd + + ta.value = + value[...selStart] + + "[#{tag}]" + value[selStart...selEnd] + "[/#{tag}]" + + value[selEnd..] + + range = "[#{tag}]".length + selEnd + # Move the caret to the end of the selection. + ta.setSelectionRange range, range + img: (thread, all) -> if all $.id('imageExpand').click()