hotkeys-js icon indicating copy to clipboard operation
hotkeys-js copied to clipboard

setScope query

Open ray73864 opened this issue 5 years ago • 5 comments

On my main page, i have 2 hotkeys defined: pageup, pagedown

If I press 'pageup' and 'pagedown' it works properly.

Then I open a modal window (using bootstrap), and on that modal, I have pageup and pagedown also defined.

For the main page, I am doing:

hotkeys('pagedown', function(e, handler) { e.preventDefault(); e.stopPropagation(); console.log("main page - pagedown"); });
hotkeys('pageup', function(e, handler) { e.preventDefault(); e.stopPropagation(); console.log("main page - pageup"); });

For the modal window, I am doing:

hotkeys('pagedown', { scope: 'modal' }, function(e, handler) { e.preventDefault(); e.stopPropagation(); console.log("modal - pagedown"); });
hotkeys('pageup', { scope: 'modal' }, function(e, handler) { e.preventDefault(); e.stopPropagation(); console.log("modal - pageup"); });
hotkeys.setScope('modal');

However, when I press the 'pageup' or 'pagedown' keys on the modal, it still triggers the main page versions.

Looking at the documentation for 'setScope', it has this line "// set the scope (only 'all' and 'issues' shortcuts will be honored)".

Does this mean that I will have to ensure there is a scope for the main page too? Or is there a way to stop 'all' from ever being triggered when a different scope has been set?

ray73864 avatar Jul 16 '19 07:07 ray73864

If you do this, you must set 'setScope', I don't think there is a better way. @ray73864

jaywcjlove avatar Jul 17 '19 01:07 jaywcjlove

Is there anyway to have it so that if I setScope, it'll ignore anything in the 'all' scope, and then if I 'deleteScope' afterwards, it'll reset it all back to the 'all' scope?

It appears my issue is because even if I setScope (which i'm currently doing), it still searches the 'all' scope along with the scope I just set, would rather it didn't do that.

ray73864 avatar Jul 17 '19 01:07 ray73864

What I can probably do in the short term is on all my 'all' hotkeys that are going to share a key with a different scope, run a 'getScope', and if the scope is anything other than 'all', return.

It's not perfect, I would rather 'all' didn't happen if I change the scope, but it's a decent fix for now.

I'm not sure how feasible it might be, but maybe a scope-level option when defining the hotkey that says to ignore all other scopes.

eg:

hotkeys('pagedown', { scope: 'test', ignoreAllScope: true }, function(e, handler) { e.preventDefault(); console.log("Won't trigger a hotkey set on the 'all' scope."); });

No idea if that will work, but it would make things easy for people using the plugin if they wish to ignore the 'all' scope.

ray73864 avatar Jul 17 '19 01:07 ray73864

@ray73864 You can use hotkeys.deleteScope('all'); to delete all

https://codepen.io/jaywcjlove/pen/BgXgry

jaywcjlove avatar Jul 17 '19 02:07 jaywcjlove

Can I bring the scope back again when the modal closes using setScope?

Looks like that would be a no, once the scope is deleted, all the hotkeys have to be recreated :(

ray73864 avatar Jul 17 '19 02:07 ray73864