Skip to content

Commit b4576a5

Browse files
authored
Merge pull request #847 from classtranscribe/update-epub-frontend
Update epub frontend
2 parents 6620c61 + 37300b3 commit b4576a5

44 files changed

Lines changed: 1501 additions & 1881 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/components/CTEPubListScreen/controllers/EPubListController.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
/* eslint-disable no-unreachable */
12
import SourceTypes from 'entities/SourceTypes';
23
import ErrorTypes from 'entities/ErrorTypes';
3-
import { api, prompt, links } from 'utils';
4+
import { api, prompt, links, uurl } from 'utils';
45
import { EPubData } from 'entities/EPubs/structs';
56
import Constants from 'screens/EPub/controllers/constants/EPubConstants';
67
import { LanguageConstants } from '../../CTPlayer';
@@ -22,6 +23,8 @@ class EPubListController {
2223
async createEPub(sourceType, sourceId, data) {
2324
prompt.addOne({ text: 'Creating I-Note...', timeout: 4000 });
2425
const rawEPubData = await this.getRawEPubData(sourceType, sourceId, data.language);
26+
27+
2528
if (rawEPubData === ErrorTypes.NotFound404) {
2629
prompt.error('Failed to create the I-Note.');
2730
return false;
@@ -32,6 +35,8 @@ class EPubListController {
3235
sourceType, sourceId, ...data
3336
}).toObject();
3437

38+
// throw Error();
39+
3540
delete ePubData.id;
3641
// console.log(ePubData);
3742

@@ -43,9 +48,9 @@ class EPubListController {
4348
}
4449
const url = links.epub(newEPubData.id, Constants.EditINote, Constants.HFromNew);
4550

46-
window.location.href = url;
51+
// window.location.href = url;
4752
// Don't open in new tab; the user may not have enabled that.
48-
// nope: uurl.openNewTab(url)
53+
uurl.openNewTab(url)
4954

5055
return newEPubData;
5156
}

src/components/CTEPubListScreen/controllers/helpers.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { ARRAY_INIT } from 'utils/constants';
44
import { LanguageConstants } from '../../CTPlayer';
55

66
export function _filterTrivalItems(epubData) {
7-
return [...epubData];
8-
// return _.filter(epubData, (item) => Boolean(_.trim(item.text)));
7+
// return [...epubData];
8+
return _.filter(epubData, (item) => Boolean(_.trim(item.text)));
99
}
1010
function getLastPunctuationIndex(sentence) {
1111
let lastPunctuationIndex = -1;
@@ -18,21 +18,22 @@ function getLastPunctuationIndex(sentence) {
1818
return lastPunctuationIndex;
1919
}
2020

21+
// eslint-disable-next-line no-unused-vars
2122
function _parseRawEPubDataSplittingOnPunctuation(rawEPubData) {
22-
return null;
23-
// let buffer = "";
24-
// for (let i = 0; i < rawEPubData.length; i += 1) {
25-
// let curr = (buffer + rawEPubData[i].text).trim();
26-
// let idx = getLastPunctuationIndex(curr);
27-
// if (idx === curr.length - 1) {
28-
// rawEPubData[i].text = curr;
29-
// buffer = "";
30-
// } else {
31-
// buffer = `${curr.substring(getLastPunctuationIndex(curr) + 1, curr.length)} `;
32-
// rawEPubData[i].text = curr.substring(0, getLastPunctuationIndex(curr) + 1);
33-
// }
34-
// }
3523
// return null;
24+
let buffer = "";
25+
for (let i = 0; i < rawEPubData.length; i += 1) {
26+
let curr = (buffer + rawEPubData[i].text).trim();
27+
let idx = getLastPunctuationIndex(curr);
28+
if (idx === curr.length - 1) {
29+
rawEPubData[i].text = curr;
30+
buffer = "";
31+
} else {
32+
buffer = `${curr.substring(getLastPunctuationIndex(curr) + 1, curr.length)} `;
33+
rawEPubData[i].text = curr.substring(0, getLastPunctuationIndex(curr) + 1);
34+
}
35+
}
36+
return null;
3637
}
3738
export function _parseRawEPubData(rawEPubData) {
3839
let a = _.map(_filterTrivalItems(rawEPubData), item => ({ ...item, id: _buildID() }));

src/components/CTImagePickerModal/ImagesTab.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import { CTText } from 'layout';
44
import { uurl } from 'utils/use-url';
55
import Image from 'components/Image';
6+
import _ from 'lodash';
67
import ImagePreview from './ImagePreview';
78

89
function ImagesTab(props) {
@@ -19,8 +20,8 @@ function ImagesTab(props) {
1920
{description && <CTText margin="5">{description}</CTText>}
2021
<div role="list" className="ct-img-picker-imgs">
2122
{images.map(img => (
22-
<div
23-
key={img}
23+
<div
24+
key={img}
2425
tabIndex={0}
2526
className="ct-img-picker-img-con"
2627
data-current={img === imgUrl}

src/entities/EPubs/html-converters.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,21 @@ export async function buildMDFromContent(content) {
2727
// Most likely cause is CORS policy when running local dev server
2828
}
2929

30+
let link_url = content.link;
31+
// if (content.timestamp) {
32+
// link_url = links.watch(epub.sourceId, { begin: TimeString.toSeconds(timestamp) })
33+
// }
34+
3035
if (img_data_url === null) {
31-
let despId = _buildID();
32-
return [
33-
'<div class="img-block">',
34-
`\t<img src="${""}" alt="${content.alt}" aria-describedby="${despId}" />`,
35-
`\t<div id="${despId}">${html.markdown(content.descriptions.join("\n"))}</div>`,
36-
'</div>'
37-
].join('\n');
38-
}
39-
if (content.descriptions.length !== 0) {
40-
let despId = _buildID();
41-
return [
42-
'<div class="img-block">',
43-
`\t<img src="${img_data_url}" alt="${content.alt}" aria-describedby="${despId}" />`,
44-
`\t<div id="${despId}">${html.markdown(content.descriptions.join("\n"))}</div>`,
45-
'</div>'
46-
].join('\n');
36+
img_data_url = "";
4737
}
38+
let despId = _buildID();
4839
return [
4940
'<div class="img-block">',
50-
`\t<img src="${img_data_url}" alt="${content.alt}" />`,
41+
(link_url && link_url !== "") ? `<a href="${link_url}">` : "",
42+
`\t<img src="${img_data_url}" alt="${content.alt}" aria-describedby="${despId}" />`,
43+
(link_url && link_url !== "") ? `</a>` : "",
44+
content.descriptions.length !== 0 ? `\t<div id="${despId}">${html.markdown(content.descriptions.join("\n"))}</div>` : "",
5145
'</div>'
5246
].join('\n');
5347
}
Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,91 @@
1+
import { _buildID } from 'utils';
2+
import _ from 'lodash';
13
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+
}
1145

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+
}
1549

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);
2152
}
2253

2354
toObject() {
2455
return {
2556
...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),
2958
};
3059
}
3160

3261
toHTML() {
3362
return buildHTMLFromChapter(this.__data__);
3463
}
3564

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;
4183
}
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}`;
4588
}
46-
47-
static __buildHTMLFromChapter = buildHTMLFromChapter;
4889
}
4990

5091
export default EPubChapterData;

0 commit comments

Comments
 (0)