Skip to content

Commit

Permalink
Merge pull request #755 from leoeuclids/feature-keycombo-plus
Browse files Browse the repository at this point in the history
Feature: Plus sign support
  • Loading branch information
NullVoxPopuli authored Mar 8, 2024
2 parents 7f73884 + 7629bd5 commit f173e2c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
52 changes: 32 additions & 20 deletions addon/src/utils/keyboard-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,42 @@ export default class KeyboardListener {
keyCombo = keyCombo.join(':'); // allow keyCombo contain semicolon
keyboardListener.type = eventType;

if (keyCombo === '+') {
keyboardListener.keyOrCode = keyCombo;
return keyboardListener;
}
let maybePlus = false;
keyCombo
.split('+')
.reduce((result, part) => {
if (part === '') {
if (maybePlus) {
result.push('+');
}

keyCombo.split('+').forEach((part) => {
if (ALT_REGEX.test(part)) {
keyboardListener.altKey = true;
} else if (CTRL_REGEX.test(part)) {
keyboardListener.ctrlKey = true;
} else if (META_REGEX.test(part)) {
keyboardListener.metaKey = true;
} else if (SHIFT_REGEX.test(part)) {
keyboardListener.shiftKey = true;
} else if (CMD_REGEX.test(part)) {
if (platform.indexOf('Mac') > -1) {
keyboardListener.metaKey = true;
maybePlus = !maybePlus;
} else {
result.push(part);
}

return result;
}, [])
.forEach((part) => {
if (ALT_REGEX.test(part)) {
keyboardListener.altKey = true;
} else if (CTRL_REGEX.test(part)) {
keyboardListener.ctrlKey = true;
} else if (META_REGEX.test(part)) {
keyboardListener.metaKey = true;
} else if (SHIFT_REGEX.test(part)) {
keyboardListener.shiftKey = true;
} else if (CMD_REGEX.test(part)) {
if (platform.indexOf('Mac') > -1) {
keyboardListener.metaKey = true;
} else {
keyboardListener.ctrlKey = true;
}
} else {
keyboardListener.keyOrCode = part;
}
} else {
keyboardListener.keyOrCode = part;
}
});
});

return keyboardListener;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export default class extends Controller {
wasSPressed = false;
wasSlashPressed = false;
wasQuestionMarkPressed = false;
wasCtrlPlusPressed = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@
</span>

{{on-key 'shift+Slash' (fn (mut this.wasQuestionMarkPressed) true)}}

<hr />


<span data-test-ctrl-plus>
{{if this.wasCtrlPlusPressed 'Ctrl++ pressed' 'Ctrl++ not pressed'}}
</span>

{{on-key 'ctrl++' (fn (mut this.wasCtrlPlusPressed) true)}}
21 changes: 21 additions & 0 deletions test-app/tests/acceptance/on-key-helper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,25 @@ module('Acceptance | on-key helper ', function (hooks) {
}
);
});

test('Ctrl++', async function (assert) {
assert.expect(3);

await textChanged(
assert,
() =>
triggerEvent(document.body, 'keydown', {
code: 'Equal',
key: '+',
keyCode: 43,
which: 43,
ctrlKey: true,
}),
{
selectorName: 'ctrl-plus',
beforeValue: 'Ctrl++ not pressed',
afterValue: 'Ctrl++ pressed',
}
);
});
});

0 comments on commit f173e2c

Please sign in to comment.