Use mousetrap for escape binds

This commit is contained in:
Pavel Djundik 2019-11-18 21:18:35 +02:00
parent 0cb8dc73bb
commit 9147772cb2
2 changed files with 14 additions and 10 deletions

View File

@ -38,11 +38,7 @@ export default {
}; };
}, },
mounted() { mounted() {
document.addEventListener("keydown", (e) => { Mousetrap.bind("esc", this.close);
if (e.code === "Escape") {
this.close();
}
});
const trap = Mousetrap(this.$refs.contextMenu); const trap = Mousetrap(this.$refs.contextMenu);
@ -78,6 +74,9 @@ export default {
return false; return false;
}); });
}, },
destroyed() {
Mousetrap.unbind("esc", this.close);
},
methods: { methods: {
open(event, items) { open(event, items) {
this.items = items; this.items = items;

View File

@ -25,6 +25,8 @@
</template> </template>
<script> <script>
import Mousetrap from "mousetrap";
export default { export default {
name: "ImageViewer", name: "ImageViewer",
data() { data() {
@ -61,14 +63,17 @@ export default {
}, },
}, },
mounted() { mounted() {
document.addEventListener("keydown", (e) => { Mousetrap.bind("esc", this.closeViewer);
if (e.code === "Escape") { },
this.closeViewer(); destroyed() {
} Mousetrap.unbind("esc", this.closeViewer);
});
}, },
methods: { methods: {
closeViewer() { closeViewer() {
if (this.link === null) {
return;
}
this.$root.$off("resize", this.correctPosition); this.$root.$off("resize", this.correctPosition);
this.link = null; this.link = null;
}, },