Actually prevent default, fix Esc.

This commit is contained in:
Nicolas Stepien 2011-06-24 03:06:26 +02:00
parent a64ad843a7
commit e30db0dc6f
2 changed files with 139 additions and 167 deletions

View File

@ -693,7 +693,7 @@
keybinds = { keybinds = {
init: function() { init: function() {
var key; var key;
keybinds.close = (key = $.getValue('key/close', 0)).length ? key : '<Esc>'; keybinds.close = (key = $.getValue('key/close', 0)).length ? key : 'Esc';
keybinds.spoiler = (key = $.getValue('key/spoiler', 0)).length ? key : 'ctrl+s'; keybinds.spoiler = (key = $.getValue('key/spoiler', 0)).length ? key : 'ctrl+s';
keybinds.zero = (key = $.getValue('key/zero', 0)).length ? key : '0'; keybinds.zero = (key = $.getValue('key/zero', 0)).length ? key : '0';
keybinds.openQR = (key = $.getValue('key/openQR', 0)).length ? key : 'i'; keybinds.openQR = (key = $.getValue('key/openQR', 0)).length ? key : 'i';
@ -715,12 +715,7 @@
}, },
cb: { cb: {
keydown: function(e) { keydown: function(e) {
var kc, key, _ref; var kc, key, o, qr, range, selEnd, selStart, ta, thread, valEnd, valMid, valStart, value;
if ((_ref = d.activeElement.nodeName) === 'TEXTAREA' || _ref === 'INPUT') {
keybinds.mode = keybinds.insert;
} else {
keybinds.mode = keybinds.normal;
}
kc = e.keyCode; kc = e.keyCode;
if ((65 <= kc && kc <= 90)) { if ((65 <= kc && kc <= 90)) {
key = String.fromCharCode(kc); key = String.fromCharCode(kc);
@ -735,22 +730,20 @@
} }
} else { } else {
if (kc === 27) { if (kc === 27) {
key = '<Esc>'; key = 'Esc';
} else if ((48 <= kc && kc <= 57)) { } else if ((48 <= kc && kc <= 57)) {
key = String.fromCharCode(kc); key = String.fromCharCode(kc);
} }
} }
return keybinds.key = key; keybinds.key = key;
}, thread = nav.getThread();
keypress: function(e) {
return keybinds.mode(e);
}
},
insert: function(e) {
var range, selEnd, selStart, ta, valEnd, valMid, valStart, value;
switch (keybinds.key) { switch (keybinds.key) {
case keybinds.close: case keybinds.close:
$.rm($('#qr')); if (o = $('#overlay')) {
$.rm(o);
} else if (qr = $('#qr')) {
$.rm(qr);
}
break; break;
case keybinds.spoiler: case keybinds.spoiler:
ta = d.activeElement; ta = d.activeElement;
@ -767,20 +760,6 @@
range = valStart.length + valMid.length; range = valStart.length + valMid.length;
ta.setSelectionRange(range, range); ta.setSelectionRange(range, range);
break; 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: case keybinds.zero:
window.location = "/" + g.BOARD + "/0#0"; window.location = "/" + g.BOARD + "/0#0";
break; break;
@ -831,6 +810,10 @@
} }
return e.preventDefault(); return e.preventDefault();
}, },
keypress: function(e) {
return keybinds.mode(e);
}
},
img: function(thread, all) { img: function(thread, all) {
var root, thumb; var root, thumb;
if (all) { 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; 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 + "/", {}); hiddenThreads = $.getValue("hiddenThreads/" + g.BOARD + "/", {});
hiddenNum = Object.keys(g.hiddenReplies).length + Object.keys(hiddenThreads).length; hiddenNum = Object.keys(g.hiddenReplies).length + Object.keys(hiddenThreads).length;
html = " <div class='reply dialog'> <div id=optionsbar> <div id=floaty> <a name=main>main</a> | <a name=flavors>sauce</a> | <a name=time>time</a> | <a name=keybinds>keybinds</a> </div> <div id=credits> <a href=http://chat.now.im/x/aeos>support throd</a> | <a href=https://github.com/aeosynth/4chan-x/issues>github</a> | <a href=http://userscripts.org/scripts/show/51412>uso</a> | <a href=https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=2DBVZBUAM4DHC&lc=US&item_name=Aeosynth&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted>donate</a> </div> </div> <hr> <div id=content> <div id=main> </div> <textarea style='display: none;' name=flavors id=flavors>" + ($.config('flavors')) + "</textarea> <div style='display: none;' id=time> <div><input type=text name=time value='" + ($.config('time')) + "'> <span id=timePreview></span></div> <table> <caption>Format specifiers <a href=http://en.wikipedia.org/wiki/Date_%28Unix%29#Formatting>(source)</a></caption> <tbody> <tr><th>Specifier</th><th>Description</th><th>Values/Example</th></tr> <tr><td>%a</td><td>weekday, abbreviated</td><td>Sat</td></tr> <tr><td>%A</td><td>weekday, full</td><td>Saturday</td></tr> <tr><td>%b</td><td>month, abbreviated</td><td>Jun</td></tr> <tr><td>%B</td><td>month, full length</td><td>June</td></tr> <tr><td>%d</td><td>day of the month, zero padded</td><td>03</td></tr> <tr><td>%H</td><td>hour (24 hour clock) zero padded</td><td>13</td></tr> <tr><td>%I (uppercase i)</td><td>hour (12 hour clock) zero padded</td><td>02</td></tr> <tr><td>%m</td><td>month, zero padded</td><td>06</td></tr> <tr><td>%M</td><td>minutes, zero padded</td><td>54</td></tr> <tr><td>%p</td><td>upper case AM or PM</td><td>PM</td></tr> <tr><td>%P</td><td>lower case am or pm</td><td>pm</td></tr> <tr><td>%y</td><td>two digit year</td><td>00-99</td></tr> </tbody> </table> </div> <div style='display: none;' id=keybinds> <table> <tbody> <tr><th>Actions</th><th>Keybinds</th></tr> <tr><td>Close Options or QR</td><td><input type=text name=close value='<Esc>'></td></tr> <tr><td>Quick spoiler</td><td><input type=text name=spoiler value='ctrl+s'></td></tr> <tr><td>Jump to page 0</td><td><input type=text name=zero value='0'></td></tr> <tr><td>Open QR with post number inserted</td><td><input type=text name=openQR value='i'></td></tr> <tr><td>Open QR without post number inserted</td><td><input type=text name=openEmptyQR value='I'></td></tr> <tr><td>Select next reply</td><td><input type=text name=nextReply value='J'></td></tr> <tr><td>Select previous reply</td><td><input type=text name=previousReply value='K'></td></tr> <tr><td>See next thread</td><td><input type=text name=nextThread value='n'></td></tr> <tr><td>See previous thread</td><td><input type=text name=previousThread value='p'></td></tr> <tr><td>Open thread in current tab</td><td><input type=text name=openThread value='O'></td></tr> <tr><td>Open thread in new tab</td><td><input type=text name=openThreadTab value='o'></td></tr> <tr><td>Expand thread</td><td><input type=text name=expandThread value='e'></td></tr> <tr><td>Watch thread</td><td><input type=text name=watch value='w'></td></tr> <tr><td>Hide thread</td><td><input type=text name=hide value='x'></td></tr> <tr><td>Expand selected image</td><td><input type=text name=expandImages value='m'></td></tr> <tr><td>Expand all images</td><td><input type=text name=expandAllImages value='M'></td></tr> <tr><td>Update now</td><td><input type=text name=update value='u'></td></tr> </tbody> </table> </div> </div> </div> "; html = " <div class='reply dialog'> <div id=optionsbar> <div id=floaty> <a name=main>main</a> | <a name=flavors>sauce</a> | <a name=time>time</a> | <a name=keybinds>keybinds</a> </div> <div id=credits> <a href=http://chat.now.im/x/aeos>support throd</a> | <a href=https://github.com/aeosynth/4chan-x/issues>github</a> | <a href=http://userscripts.org/scripts/show/51412>uso</a> | <a href=https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=2DBVZBUAM4DHC&lc=US&item_name=Aeosynth&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted>donate</a> </div> </div> <hr> <div id=content> <div id=main> </div> <textarea style='display: none;' name=flavors id=flavors>" + ($.config('flavors')) + "</textarea> <div style='display: none;' id=time> <div><input type=text name=time value='" + ($.config('time')) + "'> <span id=timePreview></span></div> <table> <caption>Format specifiers <a href=http://en.wikipedia.org/wiki/Date_%28Unix%29#Formatting>(source)</a></caption> <tbody> <tr><th>Specifier</th><th>Description</th><th>Values/Example</th></tr> <tr><td>%a</td><td>weekday, abbreviated</td><td>Sat</td></tr> <tr><td>%A</td><td>weekday, full</td><td>Saturday</td></tr> <tr><td>%b</td><td>month, abbreviated</td><td>Jun</td></tr> <tr><td>%B</td><td>month, full length</td><td>June</td></tr> <tr><td>%d</td><td>day of the month, zero padded</td><td>03</td></tr> <tr><td>%H</td><td>hour (24 hour clock) zero padded</td><td>13</td></tr> <tr><td>%I (uppercase i)</td><td>hour (12 hour clock) zero padded</td><td>02</td></tr> <tr><td>%m</td><td>month, zero padded</td><td>06</td></tr> <tr><td>%M</td><td>minutes, zero padded</td><td>54</td></tr> <tr><td>%p</td><td>upper case AM or PM</td><td>PM</td></tr> <tr><td>%P</td><td>lower case am or pm</td><td>pm</td></tr> <tr><td>%y</td><td>two digit year</td><td>00-99</td></tr> </tbody> </table> </div> <div style='display: none;' id=keybinds> <table> <tbody> <tr><th>Actions</th><th>Keybinds</th></tr> <tr><td>Close Options or QR</td><td><input type=text name=close value='Esc'></td></tr> <tr><td>Quick spoiler</td><td><input type=text name=spoiler value='ctrl+s'></td></tr> <tr><td>Jump to page 0</td><td><input type=text name=zero value='0'></td></tr> <tr><td>Open QR with post number inserted</td><td><input type=text name=openQR value='i'></td></tr> <tr><td>Open QR without post number inserted</td><td><input type=text name=openEmptyQR value='I'></td></tr> <tr><td>Select next reply</td><td><input type=text name=nextReply value='J'></td></tr> <tr><td>Select previous reply</td><td><input type=text name=previousReply value='K'></td></tr> <tr><td>See next thread</td><td><input type=text name=nextThread value='n'></td></tr> <tr><td>See previous thread</td><td><input type=text name=previousThread value='p'></td></tr> <tr><td>Open thread in current tab</td><td><input type=text name=openThread value='O'></td></tr> <tr><td>Open thread in new tab</td><td><input type=text name=openThreadTab value='o'></td></tr> <tr><td>Expand thread</td><td><input type=text name=expandThread value='e'></td></tr> <tr><td>Watch thread</td><td><input type=text name=watch value='w'></td></tr> <tr><td>Hide thread</td><td><input type=text name=hide value='x'></td></tr> <tr><td>Expand selected image</td><td><input type=text name=expandImages value='m'></td></tr> <tr><td>Expand all images</td><td><input type=text name=expandAllImages value='M'></td></tr> <tr><td>Update now</td><td><input type=text name=update value='u'></td></tr> </tbody> </table> </div> </div> </div> ";
dialog = $.el('div', { dialog = $.el('div', {
id: 'options', id: 'options',
innerHTML: html innerHTML: html
@ -1120,7 +1103,7 @@
} }
} else { } else {
if (kc === 27) { if (kc === 27) {
key = '<Esc>'; key = 'Esc';
} else if ((48 <= kc && kc <= 57)) { } else if ((48 <= kc && kc <= 57)) {
key = String.fromCharCode(kc); key = String.fromCharCode(kc);
} else if (kc === 8) { } else if (kc === 8) {

View File

@ -489,7 +489,7 @@ replyHiding =
keybinds = keybinds =
init: -> init: ->
keybinds.close = if (key = $.getValue 'key/close', 0).length then key else '<Esc>' 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.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.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' keybinds.openQR = if (key = $.getValue 'key/openQR', 0).length then key else 'i'
@ -512,11 +512,6 @@ keybinds =
cb: cb:
keydown: (e) -> keydown: (e) ->
if d.activeElement.nodeName in ['TEXTAREA', 'INPUT']
keybinds.mode = keybinds.insert
else
keybinds.mode = keybinds.normal
kc = e.keyCode kc = e.keyCode
if 65 <= kc <= 90 #A-Z if 65 <= kc <= 90 #A-Z
key = String.fromCharCode kc key = String.fromCharCode kc
@ -526,18 +521,18 @@ keybinds =
if e.altKey then key = 'alt+' + key if e.altKey then key = 'alt+' + key
else else
if kc is 27 if kc is 27
key = '<Esc>' key = 'Esc'
else if 48 <= kc <= 57 #0-9 else if 48 <= kc <= 57 #0-9
key = String.fromCharCode kc key = String.fromCharCode kc
keybinds.key = key keybinds.key = key
keypress: (e) -> thread = nav.getThread()
keybinds.mode e
insert: (e) ->
switch keybinds.key switch keybinds.key
when keybinds.close when keybinds.close
$.rm $ '#qr' if o = $ '#overlay'
$.rm o
else if qr = $ '#qr'
$.rm qr
when keybinds.spoiler when keybinds.spoiler
ta = d.activeElement ta = d.activeElement
return unless ta.nodeName is 'TEXTAREA' return unless ta.nodeName is 'TEXTAREA'
@ -553,15 +548,6 @@ keybinds =
ta.value = valStart + valMid + valEnd ta.value = valStart + valMid + valEnd
range = valStart.length + valMid.length range = valStart.length + valMid.length
ta.setSelectionRange range, range 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 when keybinds.zero
window.location = "/#{g.BOARD}/0#0" window.location = "/#{g.BOARD}/0#0"
when keybinds.openEmptyQR when keybinds.openEmptyQR
@ -596,6 +582,9 @@ keybinds =
return return
e.preventDefault() e.preventDefault()
keypress: (e) ->
keybinds.mode e
img: (thread, all) -> img: (thread, all) ->
if all if all
$("#imageExpand").click() $("#imageExpand").click()
@ -791,7 +780,7 @@ options =
<table> <table>
<tbody> <tbody>
<tr><th>Actions</th><th>Keybinds</th></tr> <tr><th>Actions</th><th>Keybinds</th></tr>
<tr><td>Close Options or QR</td><td><input type=text name=close value='<Esc>'></td></tr> <tr><td>Close Options or QR</td><td><input type=text name=close value='Esc'></td></tr>
<tr><td>Quick spoiler</td><td><input type=text name=spoiler value='ctrl+s'></td></tr> <tr><td>Quick spoiler</td><td><input type=text name=spoiler value='ctrl+s'></td></tr>
<tr><td>Jump to page 0</td><td><input type=text name=zero value='0'></td></tr> <tr><td>Jump to page 0</td><td><input type=text name=zero value='0'></td></tr>
<tr><td>Open QR with post number inserted</td><td><input type=text name=openQR value='i'></td></tr> <tr><td>Open QR with post number inserted</td><td><input type=text name=openQR value='i'></td></tr>
@ -887,7 +876,7 @@ options =
if e.altKey then key = 'alt+' + key if e.altKey then key = 'alt+' + key
else else
if kc is 27 if kc is 27
key = '<Esc>' key = 'Esc'
else if 48 <= kc <= 57 #0-9 else if 48 <= kc <= 57 #0-9
key = String.fromCharCode kc key = String.fromCharCode kc
else if kc is 8 else if kc is 8