Skip to content

Commit

Permalink
Clean up OnKey function
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Dec 8, 2024
1 parent ff8220b commit 9fc64a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
47 changes: 18 additions & 29 deletions app/javascript/src/legacy/common.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,46 @@ Object.extend Element.Methods,
element = element.parentNode
false
Element.addMethods()
KeysDown = new Hash

### Many browsers eat keyup events if focus is lost while the button
keysDown = new Map
# Many browsers eat keyup events if focus is lost while the button
# is pressed.
###

document.observe 'blur', (e) ->
KeysDown = new Hash
return
document.addEventListener 'blur', ->
keysDown = new Map

window.OnKey = (key, options, press, release) ->
if !options
options = {}
element = options['Element']
if !element
element = document
if element == document and window.opera and !options.AlwaysAllowOpera
return
element = $(element)
element.observe 'keyup', (e) ->
options ?= {}
element = options.Element ? document

element.addEventListener 'keyup', (e) ->
if e.keyCode != key
return
KeysDown[e.keyCode] = false
keysDown.set(e.keyCode, false)
if release
release e
return
element.observe 'keydown', (e) ->
element.addEventListener 'keydown', (e) ->
if e.keyCode != key
return
if e.metaKey
return
if e.shiftKey != ! !options.shiftKey
if e.shiftKey != !!options.shiftKey
return
if e.altKey != ! !options.altKey
if e.altKey != !!options.altKey
return
if e.ctrlKey != ! !options.ctrlKey
if e.ctrlKey != !!options.ctrlKey
return
if !options.allowRepeat and KeysDown[e.keyCode]
if !options.allowRepeat && keysDown.get(e.keyCode) == true
return
KeysDown[e.keyCode] = true
keysDown.set(e.keyCode, true)
target = e.target
if !options.AllowTextAreaFields and target.tagName == 'TEXTAREA'
if !options.AllowTextAreaFields && target.tagName == 'TEXTAREA'
return
if !options.AllowInputFields and target.tagName == 'INPUT'
if !options.AllowInputFields && target.tagName == 'INPUT'
return
if press and !press(e)
if press? && !press(e)
return
e.stop()
e.preventDefault()
return
return

window.InitTextAreas = ->
$$('TEXTAREA').each (elem) ->
Expand Down
2 changes: 1 addition & 1 deletion app/views/post/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

<% browser_url = "/post/browse##{@post.id}" %>
<% browser_url += "/pool:#{@following_pool_post.pool_id}" if not @following_pool_post.nil? %>
OnKey(66, { AlwaysAllowOpera: true }, function(e) { window.location.href = <%= json_escape browser_url.to_json.html_safe %>; });
OnKey(66, {}, function(e) { window.location.href = <%= json_escape browser_url.to_json.html_safe %>; });
</script>
<% end %>
<% end %>
Expand Down

0 comments on commit 9fc64a2

Please sign in to comment.