From e30db0dc6fd93b5580a0b3328ceffb7cddae27a0 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 24 Jun 2011 03:06:26 +0200 Subject: [PATCH] Actually prevent default, fix Esc. --- 4chan_x.user.js | 175 ++++++++++++++++++++++-------------------------- script.coffee | 131 +++++++++++++++++------------------- 2 files changed, 139 insertions(+), 167 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 2ca3cb689..4c4b2bf70 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -693,7 +693,7 @@ keybinds = { init: function() { var key; - keybinds.close = (key = $.getValue('key/close', 0)).length ? key : ''; + keybinds.close = (key = $.getValue('key/close', 0)).length ? key : 'Esc'; keybinds.spoiler = (key = $.getValue('key/spoiler', 0)).length ? key : 'ctrl+s'; keybinds.zero = (key = $.getValue('key/zero', 0)).length ? key : '0'; keybinds.openQR = (key = $.getValue('key/openQR', 0)).length ? key : 'i'; @@ -715,12 +715,7 @@ }, cb: { keydown: function(e) { - var kc, key, _ref; - if ((_ref = d.activeElement.nodeName) === 'TEXTAREA' || _ref === 'INPUT') { - keybinds.mode = keybinds.insert; - } else { - keybinds.mode = keybinds.normal; - } + var kc, key, o, qr, range, selEnd, selStart, ta, thread, valEnd, valMid, valStart, value; kc = e.keyCode; if ((65 <= kc && kc <= 90)) { key = String.fromCharCode(kc); @@ -735,102 +730,90 @@ } } else { if (kc === 27) { - key = ''; + key = 'Esc'; } else if ((48 <= kc && kc <= 57)) { key = String.fromCharCode(kc); } } - return keybinds.key = key; + keybinds.key = key; + thread = nav.getThread(); + switch (keybinds.key) { + case keybinds.close: + if (o = $('#overlay')) { + $.rm(o); + } else if (qr = $('#qr')) { + $.rm(qr); + } + break; + case keybinds.spoiler: + ta = d.activeElement; + if (ta.nodeName !== 'TEXTAREA') { + return; + } + 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.setSelectionRange(range, range); + break; + case keybinds.zero: + window.location = "/" + g.BOARD + "/0#0"; + break; + case keybinds.openEmptyQR: + keybinds.qr(thread); + break; + case keybinds.nextReply: + keybinds.hl.next(thread); + break; + case keybinds.previousReply: + keybinds.hl.prev(thread); + break; + case keybinds.expandAllImages: + keybinds.img(thread, true); + break; + case keybinds.openThread: + keybinds.open(thread); + break; + case keybinds.expandThread: + expandThread.toggle(thread); + break; + case keybinds.openQR: + keybinds.qr(thread, true); + break; + case keybinds.expandImages: + keybinds.img(thread); + break; + case keybinds.nextThread: + nav.next(); + break; + case keybinds.openThreadTab: + keybinds.open(thread, true); + break; + case keybinds.previousThread: + nav.prev(); + break; + case keybinds.update: + updater.update(); + break; + case keybinds.watch: + watcher.toggle(thread); + break; + case keybinds.hide: + threadHiding.toggle(thread); + break; + default: + return; + } + return e.preventDefault(); }, keypress: function(e) { return keybinds.mode(e); } }, - insert: function(e) { - var range, selEnd, selStart, ta, valEnd, valMid, valStart, value; - switch (keybinds.key) { - case keybinds.close: - $.rm($('#qr')); - break; - case keybinds.spoiler: - ta = d.activeElement; - if (ta.nodeName !== 'TEXTAREA') { - return; - } - 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.setSelectionRange(range, range); - break; - default: - return; - } - return e.preventDefault(); - }, - normal: function(e) { - var o, thread; - thread = nav.getThread(); - switch (keybinds.key) { - case keybinds.close: - if (o = $('#overlay')) { - $.rm(o); - } - break; - case keybinds.zero: - window.location = "/" + g.BOARD + "/0#0"; - break; - case keybinds.openEmptyQR: - keybinds.qr(thread); - break; - case keybinds.nextReply: - keybinds.hl.next(thread); - break; - case keybinds.previousReply: - keybinds.hl.prev(thread); - break; - case keybinds.expandAllImages: - keybinds.img(thread, true); - break; - case keybinds.openThread: - keybinds.open(thread); - break; - case keybinds.expandThread: - expandThread.toggle(thread); - break; - case keybinds.openQR: - keybinds.qr(thread, true); - break; - case keybinds.expandImages: - keybinds.img(thread); - break; - case keybinds.nextThread: - nav.next(); - break; - case keybinds.openThreadTab: - keybinds.open(thread, true); - break; - case keybinds.previousThread: - nav.prev(); - break; - case keybinds.update: - updater.update(); - break; - case keybinds.watch: - watcher.toggle(thread); - break; - case keybinds.hide: - threadHiding.toggle(thread); - break; - default: - return; - } - return e.preventDefault(); - }, img: function(thread, all) { var root, thumb; if (all) { @@ -1014,7 +997,7 @@ var arr, checked, description, dialog, hiddenNum, hiddenThreads, html, input, key, li, link, main, obj, overlay, ul, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3, _ref4; hiddenThreads = $.getValue("hiddenThreads/" + g.BOARD + "/", {}); hiddenNum = Object.keys(g.hiddenReplies).length + Object.keys(hiddenThreads).length; - html = "

"; + html = "

"; dialog = $.el('div', { id: 'options', innerHTML: html @@ -1120,7 +1103,7 @@ } } else { if (kc === 27) { - key = ''; + key = 'Esc'; } else if ((48 <= kc && kc <= 57)) { key = String.fromCharCode(kc); } else if (kc === 8) { diff --git a/script.coffee b/script.coffee index 4356217a0..15753b7de 100644 --- a/script.coffee +++ b/script.coffee @@ -489,7 +489,7 @@ replyHiding = keybinds = init: -> - keybinds.close = if (key = $.getValue 'key/close', 0).length then key else '' + keybinds.close = if (key = $.getValue 'key/close', 0).length then key else 'Esc' keybinds.spoiler = if (key = $.getValue 'key/spoiler', 0).length then key else 'ctrl+s' keybinds.zero = if (key = $.getValue 'key/zero', 0).length then key else '0' keybinds.openQR = if (key = $.getValue 'key/openQR', 0).length then key else 'i' @@ -512,11 +512,6 @@ keybinds = cb: keydown: (e) -> - if d.activeElement.nodeName in ['TEXTAREA', 'INPUT'] - keybinds.mode = keybinds.insert - else - keybinds.mode = keybinds.normal - kc = e.keyCode if 65 <= kc <= 90 #A-Z key = String.fromCharCode kc @@ -526,76 +521,70 @@ keybinds = if e.altKey then key = 'alt+' + key else if kc is 27 - key = '' + key = 'Esc' else if 48 <= kc <= 57 #0-9 key = String.fromCharCode kc keybinds.key = key + thread = nav.getThread() + switch keybinds.key + when keybinds.close + if o = $ '#overlay' + $.rm o + else if qr = $ '#qr' + $.rm qr + when keybinds.spoiler + ta = d.activeElement + return unless ta.nodeName is '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 = valStart + valMid + valEnd + range = valStart.length + valMid.length + ta.setSelectionRange range, range + when keybinds.zero + window.location = "/#{g.BOARD}/0#0" + when keybinds.openEmptyQR + keybinds.qr thread + when keybinds.nextReply + keybinds.hl.next thread + when keybinds.previousReply + keybinds.hl.prev thread + when keybinds.expandAllImages + keybinds.img thread, true + when keybinds.openThread + keybinds.open thread + when keybinds.expandThread + expandThread.toggle thread + when keybinds.openQR + keybinds.qr thread, true + when keybinds.expandImages + keybinds.img thread + when keybinds.nextThread + nav.next() + when keybinds.openThreadTab + keybinds.open thread, true + when keybinds.previousThread + nav.prev() + when keybinds.update + updater.update() + when keybinds.watch + watcher.toggle thread + when keybinds.hide + threadHiding.toggle thread + else + return + e.preventDefault() + keypress: (e) -> keybinds.mode e - insert: (e) -> - switch keybinds.key - when keybinds.close - $.rm $ '#qr' - when keybinds.spoiler - ta = d.activeElement - return unless ta.nodeName is '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 = valStart + valMid + valEnd - range = valStart.length + valMid.length - ta.setSelectionRange range, range - else - return - e.preventDefault() - - normal: (e) -> - thread = nav.getThread() - switch keybinds.key - when keybinds.close - $.rm o if o = $ '#overlay' - when keybinds.zero - window.location = "/#{g.BOARD}/0#0" - when keybinds.openEmptyQR - keybinds.qr thread - when keybinds.nextReply - keybinds.hl.next thread - when keybinds.previousReply - keybinds.hl.prev thread - when keybinds.expandAllImages - keybinds.img thread, true - when keybinds.openThread - keybinds.open thread - when keybinds.expandThread - expandThread.toggle thread - when keybinds.openQR - keybinds.qr thread, true - when keybinds.expandImages - keybinds.img thread - when keybinds.nextThread - nav.next() - when keybinds.openThreadTab - keybinds.open thread, true - when keybinds.previousThread - nav.prev() - when keybinds.update - updater.update() - when keybinds.watch - watcher.toggle thread - when keybinds.hide - threadHiding.toggle thread - else - return - e.preventDefault() - img: (thread, all) -> if all $("#imageExpand").click() @@ -791,7 +780,7 @@ options = - + @@ -887,7 +876,7 @@ options = if e.altKey then key = 'alt+' + key else if kc is 27 - key = '' + key = 'Esc' else if 48 <= kc <= 57 #0-9 key = String.fromCharCode kc else if kc is 8
ActionsKeybinds
Close Options or QR
Close Options or QR
Quick spoiler
Jump to page 0
Open QR with post number inserted