瀏覽代碼

Добавил реакцию на PageUp-PageDown, поправки багов

Book Pauk 6 年之前
父節點
當前提交
2faa584a01
共有 2 個文件被更改,包括 45 次插入8 次删除
  1. 43 4
      client/components/Reader/TextPage/TextPage.vue
  2. 2 4
      client/components/Reader/share/BookParser.js

+ 43 - 4
client/components/Reader/TextPage/TextPage.vue

@@ -24,10 +24,11 @@ export default @Component({
     },
 })
 class TextPage extends Vue {
-    meta = null;
     lastBook = null;
     bookPos = 0;
 
+    //убрать
+    meta = null;
     items = null;
 
     created() {
@@ -44,6 +45,10 @@ class TextPage extends Vue {
         this.fb2 = null;
         this.parsed = null;
 
+        this.linesUp = null;
+        this.linesDown = null;
+        this.pageLineCount = 0;
+
         this.drawPage();//пока не загрузили, очистим канвас
 
         if (this.lastBook) {
@@ -64,6 +69,8 @@ class TextPage extends Vue {
                     this.fb2.bookTitle
                 ]).join(' '));
 
+                this.pageLineCount = 30;
+
                 const parsed = this.book.parsed;
                 parsed.p = 30;//px, отступ параграфа
                 parsed.w = 300;//px, ширина страницы
@@ -89,11 +96,13 @@ class TextPage extends Vue {
 
         if (!this.book)
             return;
-        const lines = this.parsed.getLines(this.bookPos, 30);
+        const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
 
         let newItems = [];
-        for (const line of lines) {
-//console.log(line);
+        let len = lines.length;
+        len = (len > this.pageLineCount ? len = this.pageLineCount : len);
+        for (let i = 0; i < len; i++) {
+            const line = lines[i];
             /* line:
             {
                 begin: Number,
@@ -111,9 +120,39 @@ class TextPage extends Vue {
             newItems.push(item);
         }
         this.items = newItems;
+
+        this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
+        this.linesDown = lines;
+    }
+    
+    pageDown() {
+        let i = this.pageLineCount;
+        i--;
+        if (this.linesDown && this.linesDown.length > i) {
+            this.bookPos = this.linesDown[i].begin;
+        }
+    }
+
+    pageUp() {
+        let i = this.pageLineCount;
+        i--;
+        i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
+        if (this.linesUp && this.linesUp.length > i) {
+            this.bookPos = this.linesUp[i].begin;
+        }
     }
 
     keyHook(event) {
+        if (event.type == 'keydown') {
+            switch (event.key) {
+                case 'PageDown': 
+                    this.pageDown();
+                    break;
+                case 'PageUp':
+                    this.pageUp();
+                    break;
+            }
+        }
     }
 }
 //-----------------------------------------------------------------------------

+ 2 - 4
client/components/Reader/share/BookParser.js

@@ -13,8 +13,6 @@ export default class BookParser {
         this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
             return text.length*10;
         };
-
-        // stuff
     }
 
     async parse(data, callback) {
@@ -349,9 +347,9 @@ export default class BookParser {
                 result.push(parsed.lines[i]);
                 i--;
 
-                if (i > 0) {
+                if (i < 0) {
                     paraIndex--;
-                    if (paraIndex >= this.para.length)
+                    if (paraIndex >= 0)
                         parsed = this.parsePara(paraIndex);
                     else
                         return result;