瀏覽代碼

Рефакторинг

Book Pauk 6 年之前
父節點
當前提交
c7db0ec643

+ 1 - 1
client/components/Reader/Reader.vue

@@ -201,7 +201,7 @@ class Reader extends Vue {
 
     mounted() {
         (async() => {
-            await bookManager.init(this.settings);
+            await bookManager.init();
             await restoreOldSettings(this.settings, bookManager, this.commit);
 
             if (this.$root.rootRoute == '/reader') {

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

@@ -185,13 +185,13 @@
                                 <el-checkbox v-model="wordWrap">Перенос по слогам</el-checkbox>
                             </el-form-item>
                             <el-form-item label="Обработка">
-                                <el-checkbox v-model="cutEmptyParagraphs" @change="needReload">Убирать пустые параграфы</el-checkbox>
+                                <el-checkbox v-model="cutEmptyParagraphs">Убирать пустые параграфы</el-checkbox>
                             </el-form-item>
                             <el-form-item label="">
                                 <el-col :span="12">
                                     Добавлять пустые
                                 </el-col>
-                                <el-input-number v-model="addEmptyParagraphs" :min="0" :max="2" @change="needReload"></el-input-number>
+                                <el-input-number v-model="addEmptyParagraphs" :min="0" :max="2"></el-input-number>
                             </el-form-item>
                             
                         </el-form>

+ 2 - 0
client/components/Reader/TextPage/TextPage.vue

@@ -210,6 +210,8 @@ class TextPage extends Vue {
             this.parsed.w = this.w;// px, ширина текста
             this.parsed.font = this.font;
             this.parsed.wordWrap = this.wordWrap;
+            this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
+            this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
             let t = '';
             while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
             this.parsed.maxWordLength = t.length - 1;

+ 52 - 51
client/components/Reader/share/BookParser.js

@@ -3,7 +3,7 @@ import sax from '../../../../server/core/BookConverter/sax';
 import {sleep} from '../../../share/utils';
 
 export default class BookParser {
-    constructor(settings) {
+    constructor() {
         // defaults
         this.p = 30;// px, отступ параграфа
         this.w = 300;// px, ширина страницы
@@ -13,12 +13,6 @@ export default class BookParser {
         this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
             return text.length*20;
         };
-
-        //настройки
-        if (settings) {
-            this.cutEmptyParagraphs = settings.cutEmptyParagraphs;
-            this.addEmptyParagraphs = settings.addEmptyParagraphs;
-        }
     }
 
     async parse(data, callback) {
@@ -51,23 +45,20 @@ export default class BookParser {
                 index: Number,
                 offset: Number, //сумма всех length до этого параграфа
                 length: Number, //длина text без тегов
-                text: String //текст параграфа (или title или epigraph и т.д) с вложенными тегами
+                text: String, //текст параграфа с вложенными тегами
+                cut: Boolean, //параграф - кандидат на сокрытие (cutEmptyParagraphs)
+                addIndex: Number, //индекс добавляемого пустого параграфа (addEmptyParagraphs)
             }
         */
-        const newParagraph = (text, len, noCut) => {
-            //схлопывание пустых параграфов
-            if (!noCut && this.cutEmptyParagraphs && paraIndex >= 0 && len == 1 && text[0] == ' ') {
-                let p = para[paraIndex];
-                if (p.length == 1 && p.text[0] == ' ')
-                    return;
-            }
-
+        const newParagraph = (text, len, addIndex) => {
             paraIndex++;
             let p = {
                 index: paraIndex,
                 offset: paraOffset,
                 length: len,
                 text: text,
+                cut: (!addIndex && (len == 1 && text[0] == ' ')),
+                addIndex: (addIndex ? addIndex : 0),
             };
 
             para[paraIndex] = p;
@@ -82,33 +73,32 @@ export default class BookParser {
             }
 
             let p = para[paraIndex];
-            if (p) {
-                //добавление пустых параграфов
-                if (this.addEmptyParagraphs && p.length == 1 && p.text[0] == ' ' && len > 0) {
-                    let i = this.addEmptyParagraphs;
-                    while (i > 0) {
-                        newParagraph(' ', 1, true);
-                        i--;
-                    }
-                    p = para[paraIndex];
-                }
-
+            //добавление пустых (addEmptyParagraphs) параграфов
+            if (p.length == 1 && p.text[0] == ' ' && len > 0) {
+                paraIndex--;
                 paraOffset -= p.length;
-                if (p.length == 1 && p.text[0] == ' ' && len > 0) {
-                    p.length = 0;
-                    p.text = p.text.substr(1);
+                for (let i = 0; i < 2; i++) {
+                    newParagraph(' ', 1, i + 1);
                 }
-                p.length += len;
-                p.text += text;
-            } else {
-                p = {
-                    index: paraIndex,
-                    offset: paraOffset,
-                    length: len,
-                    text: text
-                };
+
+                paraIndex++;
+                p.index = paraIndex;
+                p.offset = paraOffset;
+                para[paraIndex] = p;
+                paraOffset += p.length;
+            }
+
+            paraOffset -= p.length;
+            //параграф оказался непустой
+            if (p.length == 1 && p.text[0] == ' ' && len > 0) {
+                p.length = 0;
+                p.text = p.text.substr(1);
+                p.cut = (len == 1 && text[0] == ' ');
             }
 
+            p.length += len;
+            p.text += text;
+
             para[paraIndex] = p;
             paraOffset += p.length;
         };
@@ -432,7 +422,9 @@ export default class BookParser {
             para.parsed.p === this.p &&
             para.parsed.wordWrap === this.wordWrap &&
             para.parsed.maxWordLength === this.maxWordLength &&
-            para.parsed.font === this.font
+            para.parsed.font === this.font &&
+            para.parsed.cutEmptyParagraphs === this.cutEmptyParagraphs &&
+            para.parsed.addEmptyParagraphs === this.addEmptyParagraphs
             )
             return para.parsed;
 
@@ -442,6 +434,12 @@ export default class BookParser {
             wordWrap: this.wordWrap,
             maxWordLength: this.maxWordLength,
             font: this.font,
+            cutEmptyParagraphs: this.cutEmptyParagraphs,
+            addEmptyParagraphs: this.addEmptyParagraphs,
+            visible: !(
+                (this.cutEmptyParagraphs && para.cut) ||
+                (para.addIndex > this.addEmptyParagraphs)
+            )
         };
 
 
@@ -614,16 +612,19 @@ export default class BookParser {
         let paraIndex = this.findParaIndex(bookPos);
 
         if (paraIndex === undefined)
-            return result;
+            return null;
         
         if (n > 0) {
             let parsed = this.parsePara(paraIndex);
             let i = this.findLineIndex(bookPos, parsed.lines);
             if (i === undefined)
-                return result;
+                return null;
 
             while (n > 0) {
-                result.push(parsed.lines[i]);
+                if (parsed.visible) {
+                    result.push(parsed.lines[i]);
+                    n--;
+                }
                 i++;
 
                 if (i >= parsed.lines.length) {
@@ -631,21 +632,22 @@ export default class BookParser {
                     if (paraIndex < this.para.length)
                         parsed = this.parsePara(paraIndex);
                     else
-                        return result;
+                        break;
                     i = 0;
                 }
-
-                n--;
             }
         } else if (n < 0) {
             n = -n;
             let parsed = this.parsePara(paraIndex);
             let i = this.findLineIndex(bookPos, parsed.lines);
             if (i === undefined)
-                return result;
+                return null;
 
             while (n > 0) {
-                result.push(parsed.lines[i]);
+                if (parsed.visible) {
+                    result.push(parsed.lines[i]);
+                    n--;
+                }
                 i--;
 
                 if (i < 0) {
@@ -653,16 +655,15 @@ export default class BookParser {
                     if (paraIndex >= 0)
                         parsed = this.parsePara(paraIndex);
                     else
-                        return result;
+                        break;
                     i = parsed.lines.length - 1;
                 }
-                
-                n--;
             }
         }
 
         if (!result.length)
             result = null;
+
         return result;
     }
 }

+ 2 - 3
client/components/Reader/share/bookManager.js

@@ -18,8 +18,7 @@ const bmRecentStore = localForage.createInstance({
 });
 
 class BookManager {
-    async init(settings) {
-        this.settings = settings;
+    async init() {
         this.books = {};
         this.recent = {};
         this.recentChanged1 = true;
@@ -132,7 +131,7 @@ class BookManager {
     async parseBook(meta, data, callback) {
         if (!this.books) 
             await this.init();
-        const parsed = new BookParser(this.settings);
+        const parsed = new BookParser();
 
         const parsedMeta = await parsed.parse(data, callback);
         const result = Object.assign({}, meta, parsedMeta, {