Добавил keyHooks - перехват и подписку на события клавиатуры

This commit is contained in:
Book Pauk
2019-01-11 01:53:45 +07:00
parent 6fdb2e134f
commit 64bff259ff
2 changed files with 36 additions and 5 deletions

View File

@@ -50,7 +50,7 @@ import Component from 'vue-class-component';
export default @Component({ export default @Component({
watch: { watch: {
rootRoute: function(newValue, oldValue) { rootRoute: function(newValue) {
if ((this.mode == 'reader' || this.mode == 'omnireader') && (newValue != '/reader')) { if ((this.mode == 'reader' || this.mode == 'omnireader') && (newValue != '/reader')) {
this.$router.replace('/reader'); this.$router.replace('/reader');
} }
@@ -65,11 +65,36 @@ class App extends Vue {
this.state = this.$store.state; this.state = this.$store.state;
this.uistate = this.$store.state.uistate; this.uistate = this.$store.state.uistate;
this.config = this.$store.state.config; this.config = this.$store.state.config;
//global keyHooks
this.keyHooks = [];
this.keyHook = (event) => {
for (const hook of this.keyHooks)
hook(event);
}
this.$root.addKeyHook = (hook) => {
if (this.keyHooks.indexOf(hook) < 0)
this.keyHooks.push(hook);
}
this.$root.removeKeyHook = (hook) => {
const i = this.keyHooks.indexOf(hook);
if (i >= 0)
this.keyHooks.splice(i, 1);
}
document.addEventListener('keyup', (event) => {
this.keyHook(event);
});
document.addEventListener('keydown', (event) => {
this.keyHook(event);
});
} }
mounted() { mounted() {
this.dispatch('config/loadConfig'); this.dispatch('config/loadConfig');
this.$watch('apiError', function(newError, oldError) { this.$watch('apiError', function(newError) {
if (newError) { if (newError) {
this.$notify.error({ this.$notify.error({
title: 'Ошибка API', title: 'Ошибка API',
@@ -116,7 +141,7 @@ class App extends Vue {
} }
get rootRoute() { get rootRoute() {
const m = this.$route.path.match(/^(\/[^\/]*).*$/i); const m = this.$route.path.match(/^(\/[^/]*).*$/i);
return (m ? m[1] : this.$route.path); return (m ? m[1] : this.$route.path);
} }

View File

@@ -65,6 +65,8 @@ class Reader extends Vue {
this.commit = this.$store.commit; this.commit = this.$store.commit;
this.dispatch = this.$store.dispatch; this.dispatch = this.$store.dispatch;
this.reader = this.$store.state.reader; this.reader = this.$store.state.reader;
this.$root.addKeyHook(this.keyHook);
} }
get loaderActive() { get loaderActive() {
@@ -98,11 +100,15 @@ class Reader extends Vue {
result = 'LoaderPage'; result = 'LoaderPage';
if (!result) { if (!result) {
this.commit('reader/setLoaderActive', true); //this.commit('reader/setLoaderActive', true);
result = 'LoaderPage'; //result = 'LoaderPage';
} }
return result; return result;
} }
keyHook(event) {
//console.log(this.componentActive);
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
//, .tool-button:focus, .tool-button:active, .tool-button:hover //, .tool-button:focus, .tool-button:active, .tool-button:hover