Skip to content

Commit

Permalink
fix: Listen on printing shortcut to open print view for printing
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Feb 10, 2023
1 parent 085446d commit 2ab443a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/components/Collective/CollectiveActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<NcActionSeparator v-if="collectiveCanShare(collective) && !isPublic" />
<NcActionLink :close-after-click="true"
:href="printLink"
target="_blank">
target="ncCollectivesPrint">
{{ t('collectives', 'Export or print') }}
<template #icon>
<DownloadIcon :size="16" />
Expand Down Expand Up @@ -130,6 +130,7 @@ export default {
'isCollectiveAdmin',
'isPublic',
'loading',
'printLink',
]),
isContactsInstalled() {
Expand Down Expand Up @@ -164,12 +165,6 @@ export default {
unshareIcon() {
return this.loading('unshare') ? 'icon-loading-small' : 'icon-public'
},
printLink() {
return this.isPublic
? generateUrl(`/apps/collectives/p/${this.shareTokenParam}/print/${this.collective.name}`)
: generateUrl(`/apps/collectives/_/print/${this.collective.name}`)
},
},
watch: {
Expand Down
5 changes: 5 additions & 0 deletions src/store/store.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generateUrl } from '@nextcloud/router'
import Vue, { set } from 'vue'
import Vuex, { Store } from 'vuex'

Expand Down Expand Up @@ -48,6 +49,10 @@ export default new Store({

isPublic: (_state, get) =>
!!get.shareTokenParam,

printLink: (_state, get) => get.isPublic
? generateUrl(`/apps/collectives/p/${get.shareTokenParam}/print/${get.currentCollective.name}`)
: generateUrl(`/apps/collectives/_/print/${get.currentCollective.name}`),
},

mutations: {
Expand Down
28 changes: 28 additions & 0 deletions src/views/CollectiveView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
<script>
import { mapGetters, mapMutations } from 'vuex'
import { showWarning } from '@nextcloud/dialogs'
import { NcAppContent, NcEmptyContent, NcLoadingIcon } from '@nextcloud/vue'
import Collective from '../components/Collective.vue'
import CollectiveNotFound from '../components/CollectiveNotFound.vue'
import PageList from '../components/PageList.vue'
Expand All @@ -39,12 +41,38 @@ export default {
...mapGetters([
'currentCollective',
'loading',
'printLink',
'showing',
]),
},
mounted() {
window.addEventListener('keydown', this.printKeyHandler)
},
beforeDestroy() {
window.removeEventListener('keydown', this.printKeyHandler)
},
methods: {
...mapMutations(['hide']),
/**
* @param {KeyboardEvent} event the keydown event
*/
printKeyHandler(event) {
// Handle `CTRL+P` or `CMD+P` but ensure ALT or SHIFT are NOT pressed (e.g. CTRL+SHIFT+P is new private tab on firefox)
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'p' && !(event.altKey || event.shiftKey)) {
const handle = window.open(this.printLink, 'ncCollectivesPrint').focus()
if (handle === null) {
// This might happen because of popup blockers etc
showWarning(t('collectives', 'Could not open print view, document might be formatted incorrectly.'))
} else {
event.preventDefault()
event.stopImmediatePropagation()
}
}
},
},
}
</script>
Expand Down

0 comments on commit 2ab443a

Please sign in to comment.