Skip to content

Commit

Permalink
Remove the URL-based removal method and perform a recompute on any add
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Jul 13, 2022
1 parent ca9100e commit 48f02e0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
1 change: 0 additions & 1 deletion ui/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export default class ApplicationController extends Controller {
// eslint-disable-next-line ember/classic-decorator-hooks
constructor() {
super(...arguments);
console.log('do i listen for keypress');
this.keyboard.listenForKeypress();
}

Expand Down
2 changes: 0 additions & 2 deletions ui/app/helpers/keyboard-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ export default class keyboardCommands extends Helper {
@service keyboard;

constructor() {
console.log('kc const', ...arguments);
super(...arguments);
}

compute([commands]) {
console.log('computing', commands);
if (commands) {
this.commands = commands;
this.keyboard.addCommands(commands);
Expand Down
2 changes: 1 addition & 1 deletion ui/app/modifiers/keyboard-shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default class KeyboardShortcutModifier extends Modifier {
element,
menuLevel,
enumerated,
url: this.router.currentURL,
},
];

this.keyboard.addCommands(commands);
registerDestructor(this, () => {
this.keyboard.removeCommands(commands);
Expand Down
35 changes: 14 additions & 21 deletions ui/app/services/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,35 +169,28 @@ export default class KeyboardService extends Service {
return [`Shift+${('0' + iter).slice(-2)}`]; // Shift+01, not Shift+1
}

recomputeEnumeratedCommands() {
this.keyCommands.filterBy('enumerated').forEach((command, iter) => {
command.pattern = this.cleanPattern(iter);
});
}

addCommands(commands) {
commands.forEach((command) => {
if (command.enumerated) {
// Ember's registerDestructor on destroyables fires AFTER a page load, meaning our enumerated array will be full of both old and new commands.
// Filter not only by enumerated, but also make sure we're only counting by those commands with our new command's URL.
// Without this second filterBy, moving from tabled-page to tabled-page will start your new commands at a number greater than 01.
command.pattern = this.cleanPattern(
this.keyCommands
.filterBy('enumerated')
.filter((c) => c.url === command.url).length
);
}
schedule('afterRender', () => {
commands.forEach((command) => {
this.keyCommands.pushObject(command);
if (command.enumerated) {
// Recompute enumerated numbers to handle things like sort
this.recomputeEnumeratedCommands();
}
});
});
this.keyCommands.pushObjects(commands);
}

removeCommands(commands = A([])) {
this.keyCommands.removeObjects(commands);
}

@action
generateIteratorShortcut(element, [action, iter]) {
this.keyCommands.pushObject({
label: `Hit up item ${iter}`,
pattern: [`Shift+${iter}`],
action,
});
}

//#region Nav Traversal

subnavLinks = [];
Expand Down

0 comments on commit 48f02e0

Please sign in to comment.