Skip to content
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 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ jobs:
- ember-classic
- embroider-safe
- embroider-optimized
- ember-modifier-2.x
- ember-modifier-3.1

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 2 additions & 1 deletion addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"dependencies": {
"@embroider/addon-shim": "^1.5.0",
"ember-modifier": "^2.1.2 || ^3.1.0",
"ember-modifier-manager-polyfill": "^1.2.0"
"ember-modifier-manager-polyfill": "^1.2.0",
"ember-destroyable-polyfill": "^2.0.3"
},
"devDependencies": {
"@babel/core": "^7.17.7",
Expand Down
286 changes: 200 additions & 86 deletions addon/src/modifiers/on-key.js
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'))) {
Copy link
Contributor Author

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:

  • with such condition, only one version of the modifier class gets into bundle.
  • we'll drop older version of ember-modifier support at some point, so one branch will just be deleted
  • and most important IMO, it would be messy to use either modify or didReceiveArguments/didInstall/willRemove in the same class.
    Hence having two separate classes is simpler to handle.

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;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"npm": false
},
"volta": {
"node": "12.22.10",
"yarn": "1.22.17"
"node": "12.22.12",
"yarn": "1.22.18"
}
}
16 changes: 16 additions & 0 deletions test-app/config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ module.exports = async function () {
},
embroiderSafe(),
embroiderOptimized(),
{
name: 'ember-modifier-2.x',
npm: {
devDependencies: {
'ember-modifier': '^2.0.0',
},
},
},
{
name: 'ember-modifier-3.1',
npm: {
devDependencies: {
'ember-modifier': '~3.1.0',
},
},
},
],
};
};
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5147,10 +5147,10 @@ ember-cli-typescript@^2.0.2:
stagehand "^1.0.0"
walk-sync "^1.0.0"

ember-cli-typescript@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/ember-cli-typescript/-/ember-cli-typescript-4.2.1.tgz#54d08fc90318cc986f3ea562f93ce58a6cc4c24d"
integrity sha512-0iKTZ+/wH6UB/VTWKvGuXlmwiE8HSIGcxHamwNhEC5x1mN3z8RfvsFZdQWYUzIWFN2Tek0gmepGRPTwWdBYl/A==
ember-cli-typescript@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/ember-cli-typescript/-/ember-cli-typescript-5.0.0.tgz#52f53843082d0a0128f318809dadabf83a76bbff"
integrity sha512-UDKZlG7bInRo7eDsF3jvPz1Dxh1YvRhMw9wyj5rj2K3brw0xEh1IMTqPgWRbPESqjcWuwa8s0FCNuYWDc4PTfg==
dependencies:
ansi-to-html "^0.6.15"
broccoli-stew "^3.0.0"
Expand Down Expand Up @@ -5370,14 +5370,14 @@ ember-modifier-manager-polyfill@^1.2.0:
ember-compatibility-helpers "^1.2.0"

"ember-modifier@^2.1.2 || ^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/ember-modifier/-/ember-modifier-3.1.0.tgz#ba5b0941302accd787ed3dcfc8d20400b77ffc41"
integrity sha512-G5Lj9jVFsD2sVJcRNQfaGKG1p81wT4LGfClBhCuB4TgwP1NGJKdqI+Q8BW2MptONxQt/71UjjUH0YK7Gm9eahg==
version "3.2.5"
resolved "https://registry.yarnpkg.com/ember-modifier/-/ember-modifier-3.2.5.tgz#b82052afe941f3b27c0840019992d59466dfbd77"
integrity sha512-66YA4pQijoDIAfoI0pYAhjYWu/VrTaD3BfxpA311hLIoBlaI2QQY/LR+32aah1EvKY/yInnCJA5wcl7C+f3mkg==
dependencies:
ember-cli-babel "^7.26.6"
ember-cli-normalize-entity-name "^1.0.0"
ember-cli-string-utils "^1.1.0"
ember-cli-typescript "^4.2.1"
ember-cli-typescript "^5.0.0"
ember-compatibility-helpers "^1.2.5"

ember-on-helper@^0.1.0:
Expand Down