Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #144 from kwebio/issue-143-Extract_keySpecificKeyu…
Browse files Browse the repository at this point in the history
…p_code_into_internal_plugin

Move specificKeyUp to be a plugin
  • Loading branch information
sanity authored May 22, 2020
2 parents 061937a + c89d696 commit 6a7f197
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ...tlin/kweb/html/events/keySpecificKeyup.kt → ...eb/plugins/specificKeyUp/SpecificKeyUp.kt
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package kweb.html.events
package kweb.plugins.specificKeyUp

import kweb.Element
import kweb.InputElement
import kweb.html.events.KeyboardEvent
import kweb.html.events.OnImmediateReceiver
import kweb.html.events.OnReceiver
import kweb.plugins.KwebPlugin

/**
* @author sanity
*
* This plugin allows registering a listener on a specific key to avoid sending all `keyup` events
* to the backend.
*/
object SpecificKeyUpPlugin : KwebPlugin()

private const val ENTER_PRESSED_EVENT_ATTACHED_FLAG = "enterPressedEventAttached"

Expand All @@ -11,7 +23,7 @@ fun InputElement.attachKeySpecificKeyupEvent(vararg keys: String) {
flags += ENTER_PRESSED_EVENT_ATTACHED_FLAG
this.execute("""
$jsExpression.addEventListener("keyup", function(origEvent) {
var keys = [${keys.map { "\"$it\"" }.joinToString(separator = ",")}]
var keys = [${keys.joinToString(separator = ",") { "\"$it\"" }}]
if (keys.includes(origEvent.key)) {
if (window.CustomEvent) {
var keySpecificKeyUpEvent = new CustomEvent('keySpecificKeyUpEvent');
Expand Down

0 comments on commit 6a7f197

Please sign in to comment.