Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
nodenoggin vue3 duplicate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Adam Procter
nodenoggin vue3 duplicate
Commits
918133ca
Commit
918133ca
authored
4 years ago
by
Adam Procter
Browse files
Options
Downloads
Patches
Plain Diff
added simple shortcut mixin
parent
9395e449
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/components/ToolBar.vue
+12
-0
12 additions, 0 deletions
src/components/ToolBar.vue
src/mixins/shortcuts.js
+89
-0
89 additions, 0 deletions
src/mixins/shortcuts.js
with
101 additions
and
0 deletions
src/components/ToolBar.vue
+
12
−
0
View file @
918133ca
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
<
script
>
<
script
>
// @ is an alias to /src
// @ is an alias to /src
import
UploadMedia
from
'
@/components/UploadMedia.vue
'
import
UploadMedia
from
'
@/components/UploadMedia.vue
'
import
shortcuts
from
'
@/mixins/shortcuts
'
export
default
{
export
default
{
name
:
'
ToolBar
'
,
name
:
'
ToolBar
'
,
...
@@ -20,6 +21,17 @@ export default {
...
@@ -20,6 +21,17 @@ export default {
return
{}
return
{}
},
},
mixins
:
[
shortcuts
(
'
n
'
,
function
()
{
// n key pressed
this
.
addNode
()
}),
shortcuts
(
'
ArrowRight
'
,
function
()
{
// Arrow right key was pressed
console
.
log
(
'
->
'
)
}),
],
methods
:
{
methods
:
{
addNode
()
{
addNode
()
{
this
.
$store
.
dispatch
(
'
addNode
'
)
this
.
$store
.
dispatch
(
'
addNode
'
)
...
...
This diff is collapsed.
Click to expand it.
src/mixins/shortcuts.js
0 → 100644
+
89
−
0
View file @
918133ca
// Original code from here
// https://gist.github.com/loilo/2a1834dc20d842f63bd048ffbcf3dc19#file-shortcut-mjs
// Modifier keys
export
const
CTRL
=
0b000001
export
const
ALT
=
0b000010
export
const
SHIFT
=
0b000100
export
const
META
=
0b001000
// The Windows key
export
const
WIN
=
0b010000
// The CMD key
export
const
CMD
=
0b100000
// Check for macOS
const
isMac
=
navigator
.
appVersion
.
includes
(
'
Macintosh
'
)
// Determine the primary modifier key
export
const
PRIMARY
=
isMac
?
CMD
:
CTRL
/**
* Create a mixin for simple keyboard shortcuts
*
* @param {string|string[]} matcher The key name(s) to react to
* @param {number} modifierKeys A bitmask of modifier keys
* @returns {object}
*/
export
default
function
shortcut
(
matcher
,
...
args
)
{
// If only one remaining argument, treat it as callback
if
(
args
.
length
===
1
)
{
return
shortcut
(
matcher
,
0b0000
,
args
[
0
])
}
// The key the listener function will be stored at
const
LISTENER
=
Symbol
(
'
keydown listener
'
)
let
[
modifierKeys
,
callback
]
=
args
// Check modifier keys for WIN or CMD
let
excludedByOS
=
false
if
(
modifierKeys
&
(
WIN
|
CMD
))
{
// Add META to modifier keys if OS matches
if
(
modifierKeys
&
(
isMac
?
CMD
:
WIN
))
{
modifierKeys
=
modifierKeys
|
META
}
else
{
excludedByOS
=
true
}
}
// Normalize matcher towards a function
if
(
typeof
matcher
===
'
string
'
)
{
matcher
=
[
matcher
]
}
if
(
Array
.
isArray
(
matcher
))
{
const
lowerKeys
=
matcher
.
map
((
key
)
=>
key
.
toLowerCase
())
matcher
=
(
event
)
=>
lowerKeys
.
includes
(
event
.
key
.
toLowerCase
())
}
return
{
mounted
()
{
this
[
LISTENER
]
=
(
event
)
=>
{
if
(
// Check for exclusion by OS
!
excludedByOS
&&
// No explicitely focused element
document
.
activeElement
===
document
.
body
&&
// Control key matches requirement
event
.
ctrlKey
===
Boolean
(
modifierKeys
&
CTRL
)
&&
// Alt key matches requirement
event
.
altKey
===
Boolean
(
modifierKeys
&
ALT
)
&&
// Shift key matches requirement
event
.
shiftKey
===
Boolean
(
modifierKeys
&
SHIFT
)
&&
// Meta key matches requirement
event
.
metaKey
===
Boolean
(
modifierKeys
&
META
)
&&
// Key name is the requested one
matcher
(
event
)
)
{
callback
.
call
(
this
,
event
)
}
}
document
.
addEventListener
(
'
keydown
'
,
this
[
LISTENER
])
},
unmounted
()
{
document
.
removeEventListener
(
'
keydown
'
,
this
[
LISTENER
])
},
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment