Работа над BookManager
This commit is contained in:
@@ -123,7 +123,7 @@ class Reader extends Vue {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
loadBook(url) {
|
loadBook(opts) {
|
||||||
this.progressActive = true;
|
this.progressActive = true;
|
||||||
this.$nextTick(async() => {
|
this.$nextTick(async() => {
|
||||||
const progress = this.$refs.page;
|
const progress = this.$refs.page;
|
||||||
@@ -131,16 +131,16 @@ class Reader extends Vue {
|
|||||||
progress.setState({totalSteps: 5});
|
progress.setState({totalSteps: 5});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const book = await readerApi.loadBook(url, (state) => {
|
const book = await readerApi.loadBook(opts.url, (state) => {
|
||||||
progress.setState(state);
|
progress.setState(state);
|
||||||
});
|
});
|
||||||
|
|
||||||
progress.setState({state: 'parse', step: 5, progress: 0});
|
progress.setState({state: 'parse', step: 5, progress: 0});
|
||||||
const meta = await bookManager.addBook(book, (prog) => {
|
let addedBook = await bookManager.addBook(book, (prog) => {
|
||||||
progress.setState({progress: prog});
|
progress.setState({progress: prog});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.commit('reader/addOpenedBook', meta);
|
this.commit('reader/addOpenedBook', bookManager.metaOnly(addedBook));
|
||||||
this.commit('reader/setLoaderActive', false);
|
this.commit('reader/setLoaderActive', false);
|
||||||
|
|
||||||
this.progressActive = await progress.hide();
|
this.progressActive = await progress.hide();
|
||||||
|
|||||||
@@ -1,2 +1,12 @@
|
|||||||
export default class BookParser {
|
export default class BookParser {
|
||||||
|
constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
async parse(data, callback) {
|
||||||
|
this.data = data;
|
||||||
|
|
||||||
|
if (callback)
|
||||||
|
callback(100);
|
||||||
|
return {author: 'Захарова Елена', title: 'Возвращение'};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,95 @@
|
|||||||
import 'localforage';
|
import localForage from 'localforage';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import BookParser from './BookParser';
|
import BookParser from './BookParser';
|
||||||
|
|
||||||
class BookManager {
|
class BookManager {
|
||||||
async addBook(book, callback) {
|
async init() {
|
||||||
let meta = {url: book.url, path: book.path};
|
this.books = {};
|
||||||
|
|
||||||
meta.key = path.basename(book.path);
|
const len = await localForage.length();
|
||||||
|
for (let i = 0; i < len; i++){
|
||||||
|
const key = await localForage.key(i);
|
||||||
|
const keySplit = key.split('-');
|
||||||
|
if (keySplit.length == 2 && keySplit[1] == 'meta') {
|
||||||
|
let meta = await localForage.getItem(key);
|
||||||
|
meta.data = await localForage.getItem(keySplit[0]);
|
||||||
|
|
||||||
if (callback)
|
this.books[meta.key] = meta;
|
||||||
callback(100);
|
this.books[meta.url] = meta;
|
||||||
return meta;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasBook(meta) {
|
console.log(this.books);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBook(meta) {
|
async addBook(newBook, callback) {
|
||||||
|
if (!this.books)
|
||||||
|
await this.init();
|
||||||
|
let meta = {url: newBook.url, path: newBook.path};
|
||||||
|
meta.key = path.basename(newBook.path);
|
||||||
|
|
||||||
|
const result = await this.parseBook(meta, newBook.data, callback);
|
||||||
|
|
||||||
|
await localForage.setItem(meta.key, result.data);
|
||||||
|
await localForage.setItem(`${meta.key}-meta`, meta);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBook(meta, callback) {
|
||||||
|
if (!this.books)
|
||||||
|
await this.init();
|
||||||
|
let result = undefined;
|
||||||
|
if (meta.key)
|
||||||
|
result = this.books[meta.key];
|
||||||
|
else
|
||||||
|
result = this.books[meta.url];
|
||||||
|
|
||||||
|
if (result && !result.parsed) {
|
||||||
|
result = await this.parseBook(result, result.data, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async delBook(meta) {
|
||||||
|
if (!this.books)
|
||||||
|
await this.init();
|
||||||
|
let book = undefined;
|
||||||
|
if (meta.key)
|
||||||
|
book = this.books[meta.key];
|
||||||
|
else
|
||||||
|
book = this.books[meta.url];
|
||||||
|
|
||||||
|
if (book) {
|
||||||
|
await localForage.removeItem(book.key);
|
||||||
|
await localForage.removeItem(`${book.key}-meta`);
|
||||||
|
|
||||||
|
delete this.books[book.key];
|
||||||
|
delete this.books[book.url];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async parseBook(meta, data, callback) {
|
||||||
|
if (!this.books)
|
||||||
|
await this.init();
|
||||||
|
const parsed = new BookParser();
|
||||||
|
|
||||||
|
const parsedMeta = await parsed.parse(data, callback);
|
||||||
|
const result = Object.assign({}, meta, parsedMeta, {data, parsed});
|
||||||
|
|
||||||
|
this.books[meta.key] = result;
|
||||||
|
this.books[meta.url] = result;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
metaOnly(book) {
|
||||||
|
let result = Object.assign({}, book);
|
||||||
|
delete result.data;
|
||||||
|
delete result.parsed;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user