Browse Source

Рефакторинг

Book Pauk 4 years ago
parent
commit
f44378ec84

+ 6 - 25
client/components/Reader/RecentBooksPage/RecentBooksPage.vue

@@ -87,7 +87,7 @@
 import Vue from 'vue';
 import Vue from 'vue';
 import Component from 'vue-class-component';
 import Component from 'vue-class-component';
 import path from 'path';
 import path from 'path';
-import _ from 'lodash';
+//import _ from 'lodash';
 
 
 import * as utils from '../../../share/utils';
 import * as utils from '../../../share/utils';
 import Window from '../../share/Window.vue';
 import Window from '../../share/Window.vue';
@@ -222,30 +222,11 @@ class RecentBooksPage extends Vue {
                 textLen = ` ${Math.round(book.textLength/1000)}k`;
                 textLen = ` ${Math.round(book.textLength/1000)}k`;
             }
             }
 
 
-            const fb2 = (book.fb2 ? book.fb2 : {});
-
-            let title = fb2.bookTitle;
-            if (title)
-                title = `"${title}"`;
-            else
-                title = '';
-
-            let author = '';
-            if (fb2.author) {
-                const authorNames = fb2.author.map(a => _.compact([
-                    a.lastName,
-                    a.firstName,
-                    a.middleName
-                ]).join(' '));
-                author = authorNames.join(', ');
-            } else {//TODO: убрать в будущем
-                author = _.compact([
-                    fb2.lastName,
-                    fb2.firstName,
-                    fb2.middleName
-                ]).join(' ');
-            }
-            author = (author ? author : (fb2.bookTitle ? fb2.bookTitle : book.url));
+            const bt = utils.getBookTitle(book.fb2);
+
+            let title = bt.bookTitle;
+            title = (title ? `"${title}"`: '');
+            const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : book.url));
 
 
             result.push({
             result.push({
                 num,
                 num,

+ 18 - 31
client/components/Reader/TextPage/TextPage.vue

@@ -40,7 +40,7 @@ import Component from 'vue-class-component';
 import {loadCSS} from 'fg-loadcss';
 import {loadCSS} from 'fg-loadcss';
 import _ from 'lodash';
 import _ from 'lodash';
 
 
-import {sleep} from '../../../share/utils';
+import * as utils from '../../../share/utils';
 import bookManager from '../share/bookManager';
 import bookManager from '../share/bookManager';
 import DrawHelper from './DrawHelper';
 import DrawHelper from './DrawHelper';
 import rstore from '../../../store/modules/reader';
 import rstore from '../../../store/modules/reader';
@@ -134,7 +134,7 @@ class TextPage extends Vue {
 
 
         this.$root.$on('resize', async() => {
         this.$root.$on('resize', async() => {
             this.$nextTick(this.onResize);
             this.$nextTick(this.onResize);
-            await sleep(500);
+            await utils.sleep(500);
             this.$nextTick(this.onResize);
             this.$nextTick(this.onResize);
         });
         });
     }
     }
@@ -285,7 +285,7 @@ class TextPage extends Vue {
 
 
         let close = null;
         let close = null;
         (async() => {
         (async() => {
-            await sleep(500);
+            await utils.sleep(500);
             if (this.fontsLoading)
             if (this.fontsLoading)
                 close = this.$root.notify.info('Загрузка шрифта &nbsp;<i class="la la-snowflake icon-rotate" style="font-size: 150%"></i>');
                 close = this.$root.notify.info('Загрузка шрифта &nbsp;<i class="la la-snowflake icon-rotate" style="font-size: 150%"></i>');
         })();
         })();
@@ -342,7 +342,7 @@ class TextPage extends Vue {
             let i = 0;
             let i = 0;
             const t = this.parsed.testText;
             const t = this.parsed.testText;
             while (i++ < 50 && this.parsed === parsed && this.drawHelper.measureText(t, {}) === this.parsed.testWidth)
             while (i++ < 50 && this.parsed === parsed && this.drawHelper.measureText(t, {}) === this.parsed.testWidth)
-                await sleep(100);
+                await utils.sleep(100);
 
 
             if (this.parsed === parsed) {
             if (this.parsed === parsed) {
                 this.parsed.testWidth = this.drawHelper.measureText(t, {});
                 this.parsed.testWidth = this.drawHelper.measureText(t, {});
@@ -366,7 +366,6 @@ class TextPage extends Vue {
         this.updateLayout();
         this.updateLayout();
         this.book = null;
         this.book = null;
         this.meta = null;
         this.meta = null;
-        this.fb2 = null;
         this.parsed = null;
         this.parsed = null;
 
 
         this.linesUp = null;
         this.linesUp = null;
@@ -383,7 +382,7 @@ class TextPage extends Vue {
                 try {
                 try {
                     //подождем ленивый парсинг
                     //подождем ленивый парсинг
                     this.stopLazyParse = true;
                     this.stopLazyParse = true;
-                    while (this.doingLazyParse) await sleep(10);
+                    while (this.doingLazyParse) await utils.sleep(10);
 
 
                     const isParsed = await bookManager.hasBookParsed(this.lastBook);
                     const isParsed = await bookManager.hasBookParsed(this.lastBook);
                     if (!isParsed) {
                     if (!isParsed) {
@@ -392,21 +391,9 @@ class TextPage extends Vue {
 
 
                     this.book = await bookManager.getBook(this.lastBook);
                     this.book = await bookManager.getBook(this.lastBook);
                     this.meta = bookManager.metaOnly(this.book);
                     this.meta = bookManager.metaOnly(this.book);
-                    this.fb2 = this.meta.fb2;
-
-                    let authorNames = [];
-                    if (this.fb2.author) {
-                        authorNames = this.fb2.author.map(a => _.compact([
-                            a.lastName,
-                            a.firstName,
-                            a.middleName
-                        ]).join(' '));
-                    }
+                    const bt = utils.getBookTitle(this.meta.fb2);
 
 
-                    this.title = _.compact([
-                        authorNames.join(', '),
-                        this.fb2.bookTitle
-                    ]).join(' - ');
+                    this.title = bt.title;
 
 
                     this.$root.$emit('set-app-title', this.title);
                     this.$root.$emit('set-app-title', this.title);
 
 
@@ -493,7 +480,7 @@ class TextPage extends Vue {
                 let wait = (timeout + 201)/100;
                 let wait = (timeout + 201)/100;
                 while (wait > 0 && !this[stopPropertyName]) {
                 while (wait > 0 && !this[stopPropertyName]) {
                     wait--;
                     wait--;
-                    await sleep(100);
+                    await utils.sleep(100);
                 }
                 }
                 resolve();
                 resolve();
             })().catch(reject); });
             })().catch(reject); });
@@ -509,7 +496,7 @@ class TextPage extends Vue {
         }
         }
 
 
         //ждем анимацию
         //ждем анимацию
-        while (this.inAnimation) await sleep(10);
+        while (this.inAnimation) await utils.sleep(10);
 
 
         this.stopScrolling = false;
         this.stopScrolling = false;
         this.doingScrolling = true;
         this.doingScrolling = true;
@@ -520,7 +507,7 @@ class TextPage extends Vue {
             this.page1 = this.page2;
             this.page1 = this.page2;
         this.toggleLayout = true;
         this.toggleLayout = true;
         await this.$nextTick();
         await this.$nextTick();
-        await sleep(50);
+        await utils.sleep(50);
 
 
         this.cachedPos = -1;
         this.cachedPos = -1;
         this.draw();
         this.draw();
@@ -557,7 +544,7 @@ class TextPage extends Vue {
         page.style.transform = 'none';
         page.style.transform = 'none';
         page.offsetHeight;
         page.offsetHeight;
 
 
-        while (this.doingScrolling) await sleep(10);
+        while (this.doingScrolling) await utils.sleep(10);
     }
     }
 
 
     draw() {
     draw() {
@@ -766,7 +753,7 @@ class TextPage extends Vue {
         for (let i = 0; i < this.parsed.para.length; i++) {
         for (let i = 0; i < this.parsed.para.length; i++) {
             j++;
             j++;
             if (j > 1) {
             if (j > 1) {
-                await sleep(1);
+                await utils.sleep(1);
                 j = 0;
                 j = 0;
             }
             }
             if (this.stopLazyParse)
             if (this.stopLazyParse)
@@ -788,7 +775,7 @@ class TextPage extends Vue {
     async refreshTime() {
     async refreshTime() {
         if (!this.timeRefreshing) {
         if (!this.timeRefreshing) {
             this.timeRefreshing = true;
             this.timeRefreshing = true;
-            await sleep(60*1000);
+            await utils.sleep(60*1000);
 
 
             if (this.book && this.parsed.textLength) {
             if (this.book && this.parsed.textLength) {
                 this.debouncedDrawStatusBar();
                 this.debouncedDrawStatusBar();
@@ -905,7 +892,7 @@ class TextPage extends Vue {
             this.settingsChanging = true;
             this.settingsChanging = true;
             const newSize = (this.settings.fontSize + 1 < 200 ? this.settings.fontSize + 1 : 100);
             const newSize = (this.settings.fontSize + 1 < 200 ? this.settings.fontSize + 1 : 100);
             this.commit('reader/setSettings', {fontSize: newSize});
             this.commit('reader/setSettings', {fontSize: newSize});
-            await sleep(50);
+            await utils.sleep(50);
             this.settingsChanging = false;
             this.settingsChanging = false;
         }
         }
     }
     }
@@ -915,7 +902,7 @@ class TextPage extends Vue {
             this.settingsChanging = true;
             this.settingsChanging = true;
             const newSize = (this.settings.fontSize - 1 > 5 ? this.settings.fontSize - 1 : 5);
             const newSize = (this.settings.fontSize - 1 > 5 ? this.settings.fontSize - 1 : 5);
             this.commit('reader/setSettings', {fontSize: newSize});
             this.commit('reader/setSettings', {fontSize: newSize});
-            await sleep(50);
+            await utils.sleep(50);
             this.settingsChanging = false;
             this.settingsChanging = false;
         }
         }
     }
     }
@@ -925,7 +912,7 @@ class TextPage extends Vue {
             this.settingsChanging = true;
             this.settingsChanging = true;
             const newDelay = (this.settings.scrollingDelay - 50 > 1 ? this.settings.scrollingDelay - 50 : 1);
             const newDelay = (this.settings.scrollingDelay - 50 > 1 ? this.settings.scrollingDelay - 50 : 1);
             this.commit('reader/setSettings', {scrollingDelay: newDelay});
             this.commit('reader/setSettings', {scrollingDelay: newDelay});
-            await sleep(50);
+            await utils.sleep(50);
             this.settingsChanging = false;
             this.settingsChanging = false;
         }
         }
     }
     }
@@ -935,7 +922,7 @@ class TextPage extends Vue {
             this.settingsChanging = true;
             this.settingsChanging = true;
             const newDelay = (this.settings.scrollingDelay + 50 < 10000 ? this.settings.scrollingDelay + 50 : 10000);
             const newDelay = (this.settings.scrollingDelay + 50 < 10000 ? this.settings.scrollingDelay + 50 : 10000);
             this.commit('reader/setSettings', {scrollingDelay: newDelay});
             this.commit('reader/setSettings', {scrollingDelay: newDelay});
-            await sleep(50);
+            await utils.sleep(50);
             this.settingsChanging = false;
             this.settingsChanging = false;
         }
         }
     }
     }
@@ -949,7 +936,7 @@ class TextPage extends Vue {
             let delay = 400;
             let delay = 400;
             while (this.repDoing) {
             while (this.repDoing) {
                 this.handleClick(pointX, pointY);
                 this.handleClick(pointX, pointY);
-                await sleep(delay);
+                await utils.sleep(delay);
                 if (delay > 15)
                 if (delay > 15)
                     delay *= 0.8;
                     delay *= 0.8;
             }
             }

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

@@ -455,6 +455,8 @@ export default class BookParser {
         return {fb2};
         return {fb2};
     }
     }
 
 
+    getAuthor
+
     findParaIndex(bookPos) {
     findParaIndex(bookPos) {
         let result = undefined;
         let result = undefined;
         //дихотомия
         //дихотомия

+ 38 - 0
client/share/utils.js

@@ -308,3 +308,41 @@ export function userHotKeysObjectSwap(userHotKeys) {
 export function removeHtmlTags(s) {
 export function removeHtmlTags(s) {
     return s.replace(/(<([^>]+)>)/ig, '');
     return s.replace(/(<([^>]+)>)/ig, '');
 }
 }
+
+export function makeValidFilename(filename, repl = '_') {
+    let f = filename.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
+    f = f.trim();
+    while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
+        f = f.substring(0, f.length - 1);
+    }
+
+    if (f)
+        return f;
+    else
+        throw new Error('Invalid filename');
+}
+
+export function getBookTitle(fb2) {
+    fb2 = (fb2 ? fb2 : {});
+    const result = {};
+
+   if (fb2.author) {
+        const authorNames = fb2.author.map(a => _.compact([
+            a.lastName,
+            a.firstName,
+            a.middleName
+        ]).join(' '));
+
+        result.author = authorNames.join(', ');
+    }
+
+    if (fb2.bookTitle)
+        result.bookTitle = fb2.bookTitle;
+
+    result.title = _.compact([
+        result.author,
+        result.bookTitle
+    ]).join(' - ');
+
+    return result;
+}