Pārlūkot izejas kodu

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

Book Pauk 6 gadi atpakaļ
vecāks
revīzija
64bff259ff
2 mainītis faili ar 36 papildinājumiem un 5 dzēšanām
  1. 28 3
      client/components/App.vue
  2. 8 2
      client/components/Reader/Reader.vue

+ 28 - 3
client/components/App.vue

@@ -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);
     }
     }
 
 

+ 8 - 2
client/components/Reader/Reader.vue

@@ -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);
-            result = 'LoaderPage';
+            //this.commit('reader/setLoaderActive', true);
+            //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