|
| 1 | +import { _buildID } from 'utils'; |
| 2 | +import _ from 'lodash'; |
1 | 3 | import { buildHTMLFromChapter } from '../html-converters'; |
2 | | -import EPubChapterLikeData from './EPubChapterLikeData'; |
3 | | -import EPubSubChapterData from './EPubSubChapterData'; |
4 | | - |
5 | | -let untitledChapterNum = 0; |
6 | | -function _createChapterTitle() { |
7 | | - untitledChapterNum += 1; |
8 | | - let chapterNum = untitledChapterNum > 0 ? ` (${untitledChapterNum})` : ''; |
9 | | - return `Untitled Chapter${chapterNum}`; |
10 | | -} |
| 4 | +import EPubImageData from './EPubImageData'; |
| 5 | + |
| 6 | +class EPubChapterData { |
| 7 | + __data__ = { |
| 8 | + id: '', |
| 9 | + title: '', |
| 10 | + start: '00:00:00', |
| 11 | + end: '00:00:00', |
| 12 | + timemerge: '00:00:00', |
| 13 | + condition: ['default'], |
| 14 | + subChapters: [], |
| 15 | + contents: [] |
| 16 | + }; |
| 17 | + |
| 18 | + constructor(data, resetText = true, sourceId) { |
| 19 | + if (data instanceof EPubChapterData) { |
| 20 | + this.__data__ = data.__data__; |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + let { |
| 25 | + id, |
| 26 | + title, |
| 27 | + items, |
| 28 | + contents = [], |
| 29 | + start |
| 30 | + } = data; |
| 31 | + |
| 32 | + // const { start, end } = findChapterTimeSpan(data); // TODO |
| 33 | + |
| 34 | + this.__data__ = { |
| 35 | + ...this.__data__, |
| 36 | + id: id || _buildID(), |
| 37 | + start, |
| 38 | + title: title || EPubChapterData.createChapterTitle(), |
| 39 | + condition: ['default'], |
| 40 | + contents: resetText |
| 41 | + ? EPubChapterData.buildContentsFromItems(items, sourceId) |
| 42 | + : contents.map(con => typeof con === 'string' ? con : { ...con }) |
| 43 | + }; |
| 44 | + } |
11 | 45 |
|
12 | | -class EPubChapterData extends EPubChapterLikeData { |
13 | | - constructor(chapterLike, resetText) { |
14 | | - super(chapterLike, resetText, _createChapterTitle); |
| 46 | + contentToObject(content) { |
| 47 | + return content instanceof EPubImageData ? content.toObject() : content; |
| 48 | + } |
15 | 49 |
|
16 | | - const { subChapters = [] } = chapterLike; |
17 | | - this.subChapters = subChapters.map(sch => { |
18 | | - const subchapter = new EPubSubChapterData(sch, resetText); |
19 | | - return subchapter.__data__ ? subchapter.__data__ : subchapter.toObject(); |
20 | | - }); |
| 50 | + contentsToObject(contents) { |
| 51 | + return contents.map(this.contentToObject); |
21 | 52 | } |
22 | 53 |
|
23 | 54 | toObject() { |
24 | 55 | return { |
25 | 56 | ...this.__data__, |
26 | | - items: this.itemsToObject(), |
27 | | - contents: this.contentsToObject(), |
28 | | - subChapters: this.subChapters.map(subChapter => subChapter.__data__ ? subChapter.__data__ : subChapter) |
| 57 | + contents: this.contentsToObject(this.__data__.contents), |
29 | 58 | }; |
30 | 59 | } |
31 | 60 |
|
32 | 61 | toHTML() { |
33 | 62 | return buildHTMLFromChapter(this.__data__); |
34 | 63 | } |
35 | 64 |
|
36 | | - /** |
37 | | - * @returns {EPubSubChapterData[]} |
38 | | - */ |
39 | | - get subChapters() { |
40 | | - return this.__data__.subChapters; |
| 65 | + static untitledChapterNum = 0; |
| 66 | + static buildContentsFromItems(items, sourceId) { |
| 67 | + const content = []; |
| 68 | + for (const item of items) { |
| 69 | + if (item !== undefined) { |
| 70 | + if (item.image) { // if there is an image |
| 71 | + const imageData = (sourceId ? EPubImageData.createWithTimestamp(item, sourceId) : EPubImageData.create(item)); |
| 72 | + content.push(imageData) |
| 73 | + } |
| 74 | + if (item.text) { // if there is text |
| 75 | + const text = item.text |
| 76 | + if (_.trim(text)) { |
| 77 | + content.push(text); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return content; |
41 | 83 | } |
42 | | - |
43 | | - set subChapters(subChapters) { |
44 | | - this.__data__.subChapters = subChapters; |
| 84 | + static createChapterTitle() { |
| 85 | + EPubChapterData.untitledChapterNum += 1; |
| 86 | + let chapterNum = EPubChapterData.untitledChapterNum > 0 ? ` (${EPubChapterData.untitledChapterNum})` : ''; |
| 87 | + return `Untitled Chapter${chapterNum}`; |
45 | 88 | } |
46 | | - |
47 | | - static __buildHTMLFromChapter = buildHTMLFromChapter; |
48 | 89 | } |
49 | 90 |
|
50 | 91 | export default EPubChapterData; |
0 commit comments