-
-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prepare for ember-modifier v4 #617
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,111 +1,225 @@ | ||
import Modifier from 'ember-modifier'; | ||
import { inject as service } from '@ember/service'; | ||
import { action } from '@ember/object'; | ||
import { registerDestructor } from '@ember/destroyable'; | ||
import { macroCondition, dependencySatisfies } from '@embroider/macros'; | ||
import listenerName from '../utils/listener-name'; | ||
import isKey from '../utils/is-key'; | ||
|
||
const ONLY_WHEN_FOCUSED_TAG_NAMES = ['input', 'select', 'textarea']; | ||
|
||
/* This is an element modifier to trigger some behavior when | ||
* specified key combo is pressed. When used with a form element | ||
* (input, textarea, or select), the action fires only when element | ||
* has focus. When used with another element type, it will trigger the | ||
* passed action, OR if no action is passed, it will trigger a `click` | ||
* on the element. This allows for easy declaration of keyboard shortcuts | ||
* for anything clickable: In the following example, we trigger a | ||
* click on the button when the B key is pressed: | ||
* | ||
* <button | ||
* type="button" | ||
* {{on-key 'b'}}> | ||
* Click me, or press "B" | ||
* </button> | ||
*/ | ||
export default class OnKeyModifier extends Modifier { | ||
@service keyboard; | ||
|
||
keyboardPriority = 0; | ||
activatedParamValue = true; | ||
eventName = 'keydown'; | ||
onlyWhenFocused = true; | ||
listenerName; | ||
|
||
didReceiveArguments() { | ||
let [keyCombo, callback] = this.args.positional; | ||
let { activated, event, priority } = this.args.named; | ||
this.keyCombo = keyCombo; | ||
this.callback = callback; | ||
this.eventName = event || 'keydown'; | ||
this.activatedParamValue = Object.keys(this.args.named).includes( | ||
'activated' | ||
) | ||
? !!activated | ||
: undefined; | ||
this.keyboardPriority = priority ? parseInt(priority, 10) : 0; | ||
this.listenerName = listenerName(this.eventName, this.keyCombo); | ||
if (this.args.named.onlyWhenFocused !== undefined) { | ||
this.onlyWhenFocused = this.args.named.onlyWhenFocused; | ||
} else { | ||
this.onlyWhenFocused = ONLY_WHEN_FOCUSED_TAG_NAMES.includes( | ||
this.element.tagName.toLowerCase() | ||
); | ||
} | ||
} | ||
|
||
didInstall() { | ||
this.keyboard.register(this); | ||
if (this.onlyWhenFocused) { | ||
this.element.addEventListener('click', this.onFocus, true); | ||
this.element.addEventListener('focus', this.onFocus, true); | ||
this.element.addEventListener('focusout', this.onFocusOut, true); | ||
let modifier; | ||
|
||
if (macroCondition(dependencySatisfies('ember-modifier', '>=3.2.0 || 4.x'))) { | ||
modifier = class OnKeyModifier extends Modifier { | ||
@service keyboard; | ||
|
||
keyboardPriority = 0; | ||
activatedParamValue = true; | ||
eventName = 'keydown'; | ||
onlyWhenFocused = true; | ||
listenerName; | ||
|
||
didReceiveArguments() { | ||
let [keyCombo, callback] = this.args.positional; | ||
let { activated, event, priority } = this.args.named; | ||
this.keyCombo = keyCombo; | ||
this.callback = callback; | ||
this.eventName = event || 'keydown'; | ||
this.activatedParamValue = Object.keys(this.args.named).includes( | ||
'activated' | ||
) | ||
? !!activated | ||
: undefined; | ||
this.keyboardPriority = priority ? parseInt(priority, 10) : 0; | ||
this.listenerName = listenerName(this.eventName, this.keyCombo); | ||
if (this.args.named.onlyWhenFocused !== undefined) { | ||
this.onlyWhenFocused = this.args.named.onlyWhenFocused; | ||
} else { | ||
this.onlyWhenFocused = ONLY_WHEN_FOCUSED_TAG_NAMES.includes( | ||
this.element.tagName.toLowerCase() | ||
); | ||
} | ||
} | ||
|
||
didInstall() { | ||
this.keyboard.register(this); | ||
if (this.onlyWhenFocused) { | ||
this.element.addEventListener('click', this.onFocus, true); | ||
this.element.addEventListener('focus', this.onFocus, true); | ||
this.element.addEventListener('focusout', this.onFocusOut, true); | ||
} | ||
} | ||
|
||
willRemove() { | ||
if (this.onlyWhenFocused) { | ||
this.element.removeEventListener('click', this.onFocus, true); | ||
this.element.removeEventListener('focus', this.onFocus, true); | ||
this.element.removeEventListener('focusout', this.onFocusOut, true); | ||
} | ||
this.keyboard.unregister(this); | ||
} | ||
} | ||
|
||
willRemove() { | ||
if (this.onlyWhenFocused) { | ||
this.element.removeEventListener('click', this.onFocus, true); | ||
this.element.removeEventListener('focus', this.onFocus, true); | ||
this.element.removeEventListener('focusout', this.onFocusOut, true); | ||
@action onFocus() { | ||
this.isFocused = true; | ||
} | ||
this.keyboard.unregister(this); | ||
} | ||
|
||
@action onFocus() { | ||
this.isFocused = true; | ||
} | ||
@action onFocusOut() { | ||
this.isFocused = false; | ||
} | ||
|
||
@action onFocusOut() { | ||
this.isFocused = false; | ||
} | ||
get keyboardActivated() { | ||
if (this.activatedParamValue === false) { | ||
return false; | ||
} | ||
if (this.onlyWhenFocused) { | ||
return this.isFocused; | ||
} | ||
return true; | ||
} | ||
|
||
get keyboardActivated() { | ||
if (this.activatedParamValue === false) { | ||
get keyboardFirstResponder() { | ||
if (this.onlyWhenFocused) { | ||
return this.isFocused; | ||
} | ||
return false; | ||
} | ||
if (this.onlyWhenFocused) { | ||
return this.isFocused; | ||
|
||
canHandleKeyboardEvent(event) { | ||
return isKey(this.listenerName, event); | ||
} | ||
|
||
handleKeyboardEvent(event, ekEvent) { | ||
if (isKey(this.listenerName, event)) { | ||
if (this.callback) { | ||
this.callback(event, ekEvent); | ||
} else { | ||
this.element.click(); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
}; | ||
} else { | ||
/** | ||
* This is an element modifier to trigger some behavior when | ||
* specified key combo is pressed. When used with a form element | ||
* (input, textarea, or select), the action fires only when element | ||
* has focus. When used with another element type, it will trigger the | ||
* passed action, OR if no action is passed, it will trigger a `click` | ||
* on the element. This allows for easy declaration of keyboard shortcuts | ||
* for anything clickable: In the following example, we trigger a | ||
* click on the button when the B key is pressed: | ||
* | ||
* <button | ||
* type="button" | ||
* {{on-key 'b'}}> | ||
* Click me, or press "B" | ||
* </button> | ||
*/ | ||
modifier = class OnKeyModifier extends Modifier { | ||
@service keyboard; | ||
|
||
element; | ||
keyboardPriority = 0; | ||
activatedParamValue = true; | ||
eventName = 'keydown'; | ||
onlyWhenFocused = true; | ||
listenerName; | ||
|
||
constructor(owner, args) { | ||
super(owner, args); | ||
this.keyboard.register(this); | ||
|
||
registerDestructor(this, () => { | ||
this.removeEventListeners(); | ||
this.keyboard.unregister(this); | ||
}); | ||
} | ||
|
||
modify(element, positional, named) { | ||
this.element = element; | ||
|
||
get keyboardFirstResponder() { | ||
if (this.onlyWhenFocused) { | ||
return this.isFocused; | ||
this.removeEventListeners(); | ||
|
||
this.setupProperties(positional, named); | ||
|
||
if (this.onlyWhenFocused) { | ||
this.addEventListeners(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
canHandleKeyboardEvent(event) { | ||
return isKey(this.listenerName, event); | ||
} | ||
setupProperties(positional, named) { | ||
let [keyCombo, callback] = positional; | ||
let { activated, event, priority, onlyWhenFocused } = named; | ||
|
||
handleKeyboardEvent(event, ekEvent) { | ||
if (isKey(this.listenerName, event)) { | ||
if (this.callback) { | ||
this.callback(event, ekEvent); | ||
this.keyCombo = keyCombo; | ||
this.callback = callback; | ||
this.eventName = event || 'keydown'; | ||
this.activatedParamValue = 'activated' in named ? !!activated : undefined; | ||
this.keyboardPriority = priority ? parseInt(priority, 10) : 0; | ||
this.listenerName = listenerName(this.eventName, this.keyCombo); | ||
if (onlyWhenFocused !== undefined) { | ||
this.onlyWhenFocused = onlyWhenFocused; | ||
} else { | ||
this.element.click(); | ||
this.onlyWhenFocused = ONLY_WHEN_FOCUSED_TAG_NAMES.includes( | ||
this.element.tagName.toLowerCase() | ||
); | ||
} | ||
} | ||
|
||
addEventListeners() { | ||
this.element.addEventListener('click', this.onFocus, true); | ||
this.element.addEventListener('focus', this.onFocus, true); | ||
this.element.addEventListener('focusout', this.onFocusOut, true); | ||
} | ||
|
||
removeEventListeners = () => { | ||
if (this.onlyWhenFocused) { | ||
this.element.removeEventListener('click', this.onFocus, true); | ||
this.element.removeEventListener('focus', this.onFocus, true); | ||
this.element.removeEventListener('focusout', this.onFocusOut, true); | ||
} | ||
}; | ||
|
||
@action onFocus() { | ||
this.isFocused = true; | ||
} | ||
|
||
@action onFocusOut() { | ||
this.isFocused = false; | ||
} | ||
|
||
get keyboardActivated() { | ||
if (this.activatedParamValue === false) { | ||
return false; | ||
} | ||
if (this.onlyWhenFocused) { | ||
return this.isFocused; | ||
} | ||
return true; | ||
} | ||
|
||
get keyboardFirstResponder() { | ||
if (this.onlyWhenFocused) { | ||
return this.isFocused; | ||
} | ||
return false; | ||
} | ||
|
||
canHandleKeyboardEvent(event) { | ||
return isKey(this.listenerName, event); | ||
} | ||
} | ||
|
||
handleKeyboardEvent(event, ekEvent) { | ||
if (isKey(this.listenerName, event)) { | ||
if (this.callback) { | ||
this.callback(event, ekEvent); | ||
} else { | ||
this.element.click(); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export default modifier; |
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 |
---|---|---|
|
@@ -56,7 +56,7 @@ | |
"npm": false | ||
}, | ||
"volta": { | ||
"node": "12.22.10", | ||
"yarn": "1.22.17" | ||
"node": "12.22.12", | ||
"yarn": "1.22.18" | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This increases code complexity.
However, my thought process is following:
modify
ordidReceiveArguments
/didInstall
/willRemove
in the same class.Hence having two separate classes is simpler to handle.