-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP start implementing the on-key helper
- Loading branch information
Showing
9 changed files
with
451 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Helper from '@ember/component/helper'; | ||
import { inject as service } from '@ember/service'; | ||
import listenerName from 'ember-keyboard/utils/listener-name'; | ||
|
||
export default class extends Helper { | ||
@service keyboard; | ||
keyCombo; | ||
callback; | ||
keyboardActivated = true; | ||
keyboardPriority = 0; | ||
eventName = 'keydown'; | ||
listenerName; | ||
|
||
compute([keyCombo, callback], { event = 'keydown', activated = true, priority = 0 }) { | ||
this.keyCombo = keyCombo; | ||
this.callback = callback; | ||
this.eventName = event; | ||
this.keyboardActivated = activated; | ||
this.keyboardPriority = priority; | ||
this.listenerName = listenerName(this.eventName, this.keyCombo.split('+')); | ||
this.keyboard.register(this); | ||
} | ||
|
||
destroy() { | ||
this.keyboard.unregister(this); | ||
super.destroy(...arguments); | ||
} | ||
|
||
has(triggerName) { | ||
return triggerName === this.listenerName; | ||
} | ||
|
||
trigger(triggerName) { | ||
if (triggerName === this.listenerName) { | ||
if (this.callback) { | ||
this.callback(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-keyboard/helpers/on-key'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { visit, currentURL, triggerEvent } from '@ember/test-helpers'; | ||
import { setupApplicationTest } from 'ember-qunit'; | ||
import { module, test } from 'qunit'; | ||
|
||
import { keyDown } from 'ember-keyboard/test-support/test-helpers'; | ||
|
||
import { textChanged } from '../helpers/text-changed'; | ||
|
||
module('Acceptance | on-key helper ', function(hooks) { | ||
setupApplicationTest(hooks); | ||
|
||
hooks.beforeEach(async function(assert) { | ||
await visit('/test-scenario/on-key-examples'); | ||
assert.equal(currentURL(), '/test-scenario/on-key-examples'); | ||
}); | ||
|
||
test('KeyS, no modifiers', async function(assert) { | ||
assert.expect(3); | ||
|
||
await textChanged( | ||
assert, | ||
() => keyDown('KeyS'), { | ||
selectorName: 's', | ||
beforeValue: 'S not pressed', | ||
afterValue: 'S pressed' | ||
}); | ||
}); | ||
|
||
test('Ctrl+KeyK', async function(assert) { | ||
assert.expect(3); | ||
|
||
await textChanged( | ||
assert, | ||
() => keyDown('ctrl+KeyK'), { | ||
selectorName: 'ctrl-k', | ||
beforeValue: 'Ctrl+K not pressed', | ||
afterValue: 'Ctrl+K pressed' | ||
}); | ||
}); | ||
|
||
test('keydown: Ctrl+k on a Dvorak Keyboard', async function(assert) { | ||
assert.expect(3); | ||
|
||
const ctrlKDownProperties = { | ||
code: 'KeyV', | ||
key: 'k', | ||
keyCode: 75, | ||
which: 75, | ||
ctrlKey: true | ||
}; | ||
|
||
await textChanged( | ||
assert, | ||
() => triggerEvent(document.body, 'keydown', ctrlKDownProperties), { | ||
selectorName: 'ctrl-k', | ||
beforeValue: 'Ctrl+K not pressed', | ||
afterValue: 'Ctrl+K pressed' | ||
}); | ||
}); | ||
|
||
test('shift+Slash', async function(assert) { | ||
assert.expect(3); | ||
|
||
await textChanged( | ||
assert, | ||
() => keyDown('shift+Slash'), { | ||
selectorName: 'question-mark', | ||
beforeValue: 'question mark not pressed', | ||
afterValue: 'question mark pressed' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Controller from '@ember/controller'; | ||
import { tracked } from '@glimmer/tracking'; | ||
|
||
export default class extends Controller { | ||
@tracked wasCtrlKPressed = false; | ||
@tracked wasSPressed = false; | ||
@tracked wasSlashPressed = false; | ||
@tracked wasQuestionMarkPressed = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
tests/dummy/app/templates/test-scenario/on-key-examples.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<span data-test-ctrl-k> | ||
{{if this.wasCtrlKPressed 'Ctrl+K pressed' 'Ctrl+K not pressed'}} | ||
</span> | ||
|
||
{{on-key 'ctrl+KeyK' (fn (mut this.wasCtrlKPressed) true)}} | ||
|
||
<hr /> | ||
|
||
|
||
<span data-test-s> | ||
{{if this.wasSPressed 'S pressed' 'S not pressed'}} | ||
</span> | ||
|
||
{{on-key 'KeyS' (fn (mut this.wasSPressed) true)}} | ||
|
||
<hr /> | ||
|
||
|
||
<span data-test-slash> | ||
{{if this.wasSlashPressed 'slash pressed' 'slash not pressed'}} | ||
</span> | ||
|
||
{{on-key 'Slash' (fn (mut this.wasSlashPressed) true)}} | ||
|
||
<hr /> | ||
|
||
|
||
<span data-test-question-mark> | ||
{{if this.wasQuestionMarkPressed 'question mark pressed' 'question mark not pressed'}} | ||
</span> | ||
|
||
{{on-key 'shift+Slash' (fn (mut this.wasQuestionMarkPressed) true)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.