Рефакторинг
This commit is contained in:
@@ -201,7 +201,7 @@ class Reader extends Vue {
|
|||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
(async() => {
|
(async() => {
|
||||||
await bookManager.init(this.settings);
|
await bookManager.init();
|
||||||
await restoreOldSettings(this.settings, bookManager, this.commit);
|
await restoreOldSettings(this.settings, bookManager, this.commit);
|
||||||
|
|
||||||
if (this.$root.rootRoute == '/reader') {
|
if (this.$root.rootRoute == '/reader') {
|
||||||
|
|||||||
@@ -185,13 +185,13 @@
|
|||||||
<el-checkbox v-model="wordWrap">Перенос по слогам</el-checkbox>
|
<el-checkbox v-model="wordWrap">Перенос по слогам</el-checkbox>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="Обработка">
|
<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>
|
||||||
<el-form-item label="">
|
<el-form-item label="">
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
Добавлять пустые
|
Добавлять пустые
|
||||||
</el-col>
|
</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-item>
|
||||||
|
|
||||||
</el-form>
|
</el-form>
|
||||||
|
|||||||
@@ -210,6 +210,8 @@ class TextPage extends Vue {
|
|||||||
this.parsed.w = this.w;// px, ширина текста
|
this.parsed.w = this.w;// px, ширина текста
|
||||||
this.parsed.font = this.font;
|
this.parsed.font = this.font;
|
||||||
this.parsed.wordWrap = this.wordWrap;
|
this.parsed.wordWrap = this.wordWrap;
|
||||||
|
this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
|
||||||
|
this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
|
||||||
let t = '';
|
let t = '';
|
||||||
while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
|
while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
|
||||||
this.parsed.maxWordLength = t.length - 1;
|
this.parsed.maxWordLength = t.length - 1;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import sax from '../../../../server/core/BookConverter/sax';
|
|||||||
import {sleep} from '../../../share/utils';
|
import {sleep} from '../../../share/utils';
|
||||||
|
|
||||||
export default class BookParser {
|
export default class BookParser {
|
||||||
constructor(settings) {
|
constructor() {
|
||||||
// defaults
|
// defaults
|
||||||
this.p = 30;// px, отступ параграфа
|
this.p = 30;// px, отступ параграфа
|
||||||
this.w = 300;// px, ширина страницы
|
this.w = 300;// px, ширина страницы
|
||||||
@@ -13,12 +13,6 @@ export default class BookParser {
|
|||||||
this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
|
this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
|
||||||
return text.length*20;
|
return text.length*20;
|
||||||
};
|
};
|
||||||
|
|
||||||
//настройки
|
|
||||||
if (settings) {
|
|
||||||
this.cutEmptyParagraphs = settings.cutEmptyParagraphs;
|
|
||||||
this.addEmptyParagraphs = settings.addEmptyParagraphs;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async parse(data, callback) {
|
async parse(data, callback) {
|
||||||
@@ -51,23 +45,20 @@ export default class BookParser {
|
|||||||
index: Number,
|
index: Number,
|
||||||
offset: Number, //сумма всех length до этого параграфа
|
offset: Number, //сумма всех length до этого параграфа
|
||||||
length: Number, //длина text без тегов
|
length: Number, //длина text без тегов
|
||||||
text: String //текст параграфа (или title или epigraph и т.д) с вложенными тегами
|
text: String, //текст параграфа с вложенными тегами
|
||||||
|
cut: Boolean, //параграф - кандидат на сокрытие (cutEmptyParagraphs)
|
||||||
|
addIndex: Number, //индекс добавляемого пустого параграфа (addEmptyParagraphs)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
const newParagraph = (text, len, noCut) => {
|
const newParagraph = (text, len, addIndex) => {
|
||||||
//схлопывание пустых параграфов
|
|
||||||
if (!noCut && this.cutEmptyParagraphs && paraIndex >= 0 && len == 1 && text[0] == ' ') {
|
|
||||||
let p = para[paraIndex];
|
|
||||||
if (p.length == 1 && p.text[0] == ' ')
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
paraIndex++;
|
paraIndex++;
|
||||||
let p = {
|
let p = {
|
||||||
index: paraIndex,
|
index: paraIndex,
|
||||||
offset: paraOffset,
|
offset: paraOffset,
|
||||||
length: len,
|
length: len,
|
||||||
text: text,
|
text: text,
|
||||||
|
cut: (!addIndex && (len == 1 && text[0] == ' ')),
|
||||||
|
addIndex: (addIndex ? addIndex : 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
para[paraIndex] = p;
|
para[paraIndex] = p;
|
||||||
@@ -82,33 +73,32 @@ export default class BookParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let p = para[paraIndex];
|
let p = para[paraIndex];
|
||||||
if (p) {
|
//добавление пустых (addEmptyParagraphs) параграфов
|
||||||
//добавление пустых параграфов
|
if (p.length == 1 && p.text[0] == ' ' && len > 0) {
|
||||||
if (this.addEmptyParagraphs && p.length == 1 && p.text[0] == ' ' && len > 0) {
|
paraIndex--;
|
||||||
let i = this.addEmptyParagraphs;
|
paraOffset -= p.length;
|
||||||
while (i > 0) {
|
for (let i = 0; i < 2; i++) {
|
||||||
newParagraph(' ', 1, true);
|
newParagraph(' ', 1, i + 1);
|
||||||
i--;
|
|
||||||
}
|
|
||||||
p = para[paraIndex];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
paraOffset -= p.length;
|
paraIndex++;
|
||||||
if (p.length == 1 && p.text[0] == ' ' && len > 0) {
|
p.index = paraIndex;
|
||||||
p.length = 0;
|
p.offset = paraOffset;
|
||||||
p.text = p.text.substr(1);
|
para[paraIndex] = p;
|
||||||
}
|
paraOffset += p.length;
|
||||||
p.length += len;
|
|
||||||
p.text += text;
|
|
||||||
} else {
|
|
||||||
p = {
|
|
||||||
index: paraIndex,
|
|
||||||
offset: paraOffset,
|
|
||||||
length: len,
|
|
||||||
text: text
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
para[paraIndex] = p;
|
||||||
paraOffset += p.length;
|
paraOffset += p.length;
|
||||||
};
|
};
|
||||||
@@ -432,7 +422,9 @@ export default class BookParser {
|
|||||||
para.parsed.p === this.p &&
|
para.parsed.p === this.p &&
|
||||||
para.parsed.wordWrap === this.wordWrap &&
|
para.parsed.wordWrap === this.wordWrap &&
|
||||||
para.parsed.maxWordLength === this.maxWordLength &&
|
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;
|
return para.parsed;
|
||||||
|
|
||||||
@@ -442,6 +434,12 @@ export default class BookParser {
|
|||||||
wordWrap: this.wordWrap,
|
wordWrap: this.wordWrap,
|
||||||
maxWordLength: this.maxWordLength,
|
maxWordLength: this.maxWordLength,
|
||||||
font: this.font,
|
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);
|
let paraIndex = this.findParaIndex(bookPos);
|
||||||
|
|
||||||
if (paraIndex === undefined)
|
if (paraIndex === undefined)
|
||||||
return result;
|
return null;
|
||||||
|
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
let parsed = this.parsePara(paraIndex);
|
let parsed = this.parsePara(paraIndex);
|
||||||
let i = this.findLineIndex(bookPos, parsed.lines);
|
let i = this.findLineIndex(bookPos, parsed.lines);
|
||||||
if (i === undefined)
|
if (i === undefined)
|
||||||
return result;
|
return null;
|
||||||
|
|
||||||
while (n > 0) {
|
while (n > 0) {
|
||||||
result.push(parsed.lines[i]);
|
if (parsed.visible) {
|
||||||
|
result.push(parsed.lines[i]);
|
||||||
|
n--;
|
||||||
|
}
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
if (i >= parsed.lines.length) {
|
if (i >= parsed.lines.length) {
|
||||||
@@ -631,21 +632,22 @@ export default class BookParser {
|
|||||||
if (paraIndex < this.para.length)
|
if (paraIndex < this.para.length)
|
||||||
parsed = this.parsePara(paraIndex);
|
parsed = this.parsePara(paraIndex);
|
||||||
else
|
else
|
||||||
return result;
|
break;
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
n--;
|
|
||||||
}
|
}
|
||||||
} else if (n < 0) {
|
} else if (n < 0) {
|
||||||
n = -n;
|
n = -n;
|
||||||
let parsed = this.parsePara(paraIndex);
|
let parsed = this.parsePara(paraIndex);
|
||||||
let i = this.findLineIndex(bookPos, parsed.lines);
|
let i = this.findLineIndex(bookPos, parsed.lines);
|
||||||
if (i === undefined)
|
if (i === undefined)
|
||||||
return result;
|
return null;
|
||||||
|
|
||||||
while (n > 0) {
|
while (n > 0) {
|
||||||
result.push(parsed.lines[i]);
|
if (parsed.visible) {
|
||||||
|
result.push(parsed.lines[i]);
|
||||||
|
n--;
|
||||||
|
}
|
||||||
i--;
|
i--;
|
||||||
|
|
||||||
if (i < 0) {
|
if (i < 0) {
|
||||||
@@ -653,16 +655,15 @@ export default class BookParser {
|
|||||||
if (paraIndex >= 0)
|
if (paraIndex >= 0)
|
||||||
parsed = this.parsePara(paraIndex);
|
parsed = this.parsePara(paraIndex);
|
||||||
else
|
else
|
||||||
return result;
|
break;
|
||||||
i = parsed.lines.length - 1;
|
i = parsed.lines.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
n--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result.length)
|
if (!result.length)
|
||||||
result = null;
|
result = null;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,8 +18,7 @@ const bmRecentStore = localForage.createInstance({
|
|||||||
});
|
});
|
||||||
|
|
||||||
class BookManager {
|
class BookManager {
|
||||||
async init(settings) {
|
async init() {
|
||||||
this.settings = settings;
|
|
||||||
this.books = {};
|
this.books = {};
|
||||||
this.recent = {};
|
this.recent = {};
|
||||||
this.recentChanged1 = true;
|
this.recentChanged1 = true;
|
||||||
@@ -132,7 +131,7 @@ class BookManager {
|
|||||||
async parseBook(meta, data, callback) {
|
async parseBook(meta, data, callback) {
|
||||||
if (!this.books)
|
if (!this.books)
|
||||||
await this.init();
|
await this.init();
|
||||||
const parsed = new BookParser(this.settings);
|
const parsed = new BookParser();
|
||||||
|
|
||||||
const parsedMeta = await parsed.parse(data, callback);
|
const parsedMeta = await parsed.parse(data, callback);
|
||||||
const result = Object.assign({}, meta, parsedMeta, {
|
const result = Object.assign({}, meta, parsedMeta, {
|
||||||
|
|||||||
Reference in New Issue
Block a user