-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathKeyboardShortcuts.vue
More file actions
283 lines (253 loc) · 8.13 KB
/
KeyboardShortcuts.vue
File metadata and controls
283 lines (253 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!--
- SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<!-- :style="{top:cardTop, left:cardLeft}" -->
<div v-if="card && selector"
ref="shortcutModal"
v-click-outside="close"
class="keyboard-shortcuts__modal"
tabindex="0"
@keydown.esc="close">
<CardItem :card="card" />
<DueDateSelector v-if="selector === 'due-date'" :card="card" :can-edit="true" />
<TagSelector v-if="selector === 'tag'" :card="card" :can-edit="true" />
<AssignmentSelector v-if="selector === 'assignment'" :card="card" :can-edit="true" />
</div>
</template>
<script>
import DueDateSelector from './card/DueDateSelector.vue'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { mapState } from 'vuex'
import TagSelector from './card/TagSelector.vue'
import AssignmentSelector from './card/AssignmentSelector.vue'
import CardItem from './cards/CardItem.vue'
export default {
name: 'KeyboardShortcuts',
components: {
DueDateSelector,
TagSelector,
AssignmentSelector,
CardItem,
},
data() {
return {
card: null,
cardTop: null,
cardLeft: null,
selector: null,
}
},
computed: {
...mapState({
board: state => state.currentBoard,
}),
},
created() {
document.addEventListener('keydown', this.onKeydown)
subscribe('deck:card:show-assignment-selector', this.handleShowAssignemnt)
subscribe('deck:card:show-due-date-selector', this.handleShowDueDate)
subscribe('deck:card:show-label-selector', this.handleShowLabel)
},
destroyed() {
document.removeEventListener('keydown', this.onKeydown)
unsubscribe('deck:card:show-assignment-selector', this.handleShowAssignemnt)
unsubscribe('deck:card:show-due-date-selector', this.handleShowDueDate)
unsubscribe('deck:card:show-label-selector', this.handleShowLabel)
},
methods: {
onKeydown(key) {
if (OCP.Accessibility.disableKeyboardShortcuts()) {
return
}
if (['INPUT', 'TEXTAREA', 'SELECT', 'BUTTON'].includes(key.target.tagName) || key.target.isContentEditable) {
return
}
// Global shortcuts (not board specific)
if ((key.metaKey || key.ctrlKey) && key.code === 'KeyF') {
const searchInput = document.getElementById('deck-search-input')
if (searchInput === document.activeElement) {
return false
}
document.getElementById('deck-search-input').focus()
key.preventDefault()
return true
}
if (key.code === 'Minus') {
emit('deck:global:toggle-help-dialog')
return
}
if (this.$store.state.shortcutLock || key.shiftKey || key.ctrlKey || key.altKey || key.metaKey) {
return
}
if (this.$route.name === 'card' && key.code === 'Escape') {
this.$router.push({ name: 'board' })
return
}
// Board specific shortcuts
if (!this.board) {
return
}
switch (key.code) {
case 'KeyN':
emit('deck:board:show-new-card', this.board.id)
break
case 'KeyF':
emit('deck:board:toggle-filter-popover', this.board.id)
break
case 'KeyX':
emit('deck:board:clear-filter', this.board.id)
break
case 'KeyQ':
emit('deck:board:toggle-filter-by-me', this.board.id)
break
case 'ArrowDown':
this.keyboardFocusDown()
break
case 'ArrowUp':
this.keyboardFocusUp()
break
case 'ArrowLeft':
this.keyboardFocusLeft()
break
case 'ArrowRight':
this.keyboardFocusRight()
break
default:
return
}
key.preventDefault()
},
keyboardFocusDown() {
const activeCard = document.activeElement.closest('.card')
const cards = document.querySelectorAll('.card')
const stacks = document.querySelectorAll('.stack')
const index = Array.from(cards).findIndex(card => card === activeCard)
if (index === -1) {
cards[0]?.focus()
return
}
const currentStack = Array.from(stacks).find(stack => stack.contains(document.activeElement))
const currentStackCards = currentStack.querySelectorAll('.card')
const currentStackIndex = Array.from(currentStackCards).findIndex(card => card === document.activeElement)
if (currentStackIndex === currentStackCards.length - 1) {
return
}
cards[index + 1]?.focus()
cards[index + 1]?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
},
keyboardFocusUp() {
const activeCard = document.activeElement.closest('.card')
const cards = document.querySelectorAll('.card')
const stacks = document.querySelectorAll('.stack')
const index = Array.from(cards).findIndex(card => card === activeCard)
if (index === -1) {
cards[0]?.focus()
return
}
const currentStack = Array.from(stacks).find(stack => stack.contains(document.activeElement))
const currentStackCards = currentStack.querySelectorAll('.card')
const currentStackIndex = Array.from(currentStackCards).findIndex(card => card === document.activeElement)
if (currentStackIndex === 0) {
return
}
cards[index - 1]?.focus()
cards[index - 1]?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
},
keyboardFocusLeft() {
const activeCard = document.activeElement.closest('.card')
const stacks = document.querySelectorAll('.stack')
const currentStackIndex = Array.from(stacks).findIndex(stack => stack.contains(activeCard))
if (!currentStackIndex === 0) {
return
}
const nextStack = stacks[currentStackIndex - 1] ?? stacks[0]
const currentCardTopOffset = document.activeElement.getBoundingClientRect().top
// iterate over all next stack cards and remember the one with the smallest offset
const nextStackCards = nextStack.querySelectorAll('.card')
let nextCard = null
for (const card of nextStackCards) {
const cardTopOffset = card.getBoundingClientRect().bottom
if (cardTopOffset < currentCardTopOffset) {
continue
}
nextCard = card
break
}
if (!nextCard) {
nextCard = nextStackCards[nextStackCards.length - 1]
}
nextCard?.focus()
nextCard?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
},
keyboardFocusRight() {
const activeCard = document.activeElement.closest('.card')
const stacks = document.querySelectorAll('.stack')
const currentStackIndex = Array.from(stacks).findIndex(stack => stack.contains(activeCard))
if (currentStackIndex === stacks.length - 1) {
return
}
const nextStack = stacks[currentStackIndex + 1]
const currentCardTopOffset = document.activeElement.getBoundingClientRect().top
// iterate over all next stack cards and remember the one with the smallest offset
const nextStackCards = nextStack.querySelectorAll('.card')
let nextCard = null
for (const card of nextStackCards) {
const cardTopOffset = card.getBoundingClientRect().bottom
if (cardTopOffset < currentCardTopOffset) {
continue
}
nextCard = card
break
}
if (!nextCard) {
nextCard = nextStackCards[nextStackCards.length - 1]
}
nextCard?.focus()
nextCard?.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
},
handleShowDueDate({ card, element }) {
// this.cardTop = element.getBoundingClientRect().top + 'px'
// this.cardLeft = element.getBoundingClientRect().left + 'px'
this.card = card
this.selector = 'due-date'
this.$refs.shortcutModal?.focus()
},
handleShowAssignemnt({ card, element }) {
// this.cardTop = element.getBoundingClientRect().top + 'px'
// this.cardLeft = element.getBoundingClientRect().left + 'px'
this.card = card
this.selector = 'assignment'
this.$refs.shortcutModal?.focus()
},
handleShowLabel({ card, element }) {
// this.cardTop = element.getBoundingClientRect().top + 'px'
// this.cardLeft = element.getBoundingClientRect().left + 'px'
this.card = card
this.selector = 'tag'
this.$refs.shortcutModal?.focus()
},
close() {
this.card = null
this.selector = null
},
},
}
</script>
<style lang="scss" scoped>
.keyboard-shortcuts__modal {
position: fixed;
z-index: 9999;
box-shadow: 0 0 100px 30px rgba(0, 0, 0, 0.5);
max-width: 500px;
bottom: 32px;
inset-inline-start: 50%;
transform: translateX(-50%);
background-color: var(--color-background-dark);
border-radius: var(--border-radius-rounded);
padding: 24px 32px;
width: 100%;
border: 2px solid var(--color-border-maxcontrast);
}
</style>