Bind esc key handler once

Fixes #3690
This commit is contained in:
Pavel Djundik 2020-01-24 11:32:28 +02:00
parent a9bad593b0
commit 2451f222e8
3 changed files with 9 additions and 9 deletions

View File

@ -44,6 +44,7 @@ export default {
this.prepareOpenStates(); this.prepareOpenStates();
}, },
mounted() { mounted() {
Mousetrap.bind("esc", this.escapeKey);
Mousetrap.bind("alt+u", this.toggleUserList); Mousetrap.bind("alt+u", this.toggleUserList);
Mousetrap.bind("alt+s", this.toggleSidebar); Mousetrap.bind("alt+s", this.toggleSidebar);
@ -64,6 +65,7 @@ export default {
this.dayChangeTimeout = setTimeout(emitDayChange, this.msUntilNextDay()); this.dayChangeTimeout = setTimeout(emitDayChange, this.msUntilNextDay());
}, },
beforeDestroy() { beforeDestroy() {
Mousetrap.unbind("esc", this.escapeKey);
Mousetrap.unbind("alt+u", this.toggleUserList); Mousetrap.unbind("alt+u", this.toggleUserList);
Mousetrap.unbind("alt+s", this.toggleSidebar); Mousetrap.unbind("alt+s", this.toggleSidebar);
@ -71,6 +73,9 @@ export default {
clearTimeout(this.dayChangeTimeout); clearTimeout(this.dayChangeTimeout);
}, },
methods: { methods: {
escapeKey() {
this.$root.$emit("escapekey");
},
toggleSidebar(e) { toggleSidebar(e) {
// Do not handle this keybind in the chat input because // Do not handle this keybind in the chat input because
// it can be used to type letters with umlauts // it can be used to type letters with umlauts

View File

@ -38,7 +38,6 @@
</template> </template>
<script> <script>
import Mousetrap from "mousetrap";
import { import {
generateUserContextMenu, generateUserContextMenu,
generateChannelContextMenu, generateChannelContextMenu,
@ -63,15 +62,13 @@ export default {
}; };
}, },
mounted() { mounted() {
Mousetrap.bind("esc", this.close); this.$root.$on("escapekey", this.close);
this.$root.$on("contextmenu:user", this.openUserContextMenu); this.$root.$on("contextmenu:user", this.openUserContextMenu);
this.$root.$on("contextmenu:channel", this.openChannelContextMenu); this.$root.$on("contextmenu:channel", this.openChannelContextMenu);
this.$root.$on("contextmenu:removenetwork", this.openRemoveNetworkContextMenu); this.$root.$on("contextmenu:removenetwork", this.openRemoveNetworkContextMenu);
}, },
destroyed() { destroyed() {
Mousetrap.unbind("esc", this.close); this.$root.$off("escapekey", this.close);
this.$root.$off("contextmenu:user", this.openUserContextMenu); this.$root.$off("contextmenu:user", this.openUserContextMenu);
this.$root.$off("contextmenu:channel", this.openChannelContextMenu); this.$root.$off("contextmenu:channel", this.openChannelContextMenu);
this.$root.$off("contextmenu:removenetwork", this.openRemoveNetworkContextMenu); this.$root.$off("contextmenu:removenetwork", this.openRemoveNetworkContextMenu);

View File

@ -25,8 +25,6 @@
</template> </template>
<script> <script>
import Mousetrap from "mousetrap";
export default { export default {
name: "ImageViewer", name: "ImageViewer",
data() { data() {
@ -68,10 +66,10 @@ export default {
}, },
}, },
mounted() { mounted() {
Mousetrap.bind("esc", this.closeViewer); this.$root.$on("escapekey", this.closeViewer);
}, },
destroyed() { destroyed() {
Mousetrap.unbind("esc", this.closeViewer); this.$root.$off("escapekey", this.closeViewer);
}, },
methods: { methods: {
closeViewer() { closeViewer() {