Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-unreachable */
import SourceTypes from 'entities/SourceTypes';
import ErrorTypes from 'entities/ErrorTypes';
import { api, prompt, links } from 'utils';
import { api, prompt, links, uurl } from 'utils';
import { EPubData } from 'entities/EPubs/structs';
import Constants from 'screens/EPub/controllers/constants/EPubConstants';
import { LanguageConstants } from '../../CTPlayer';
Expand All @@ -22,6 +23,8 @@ class EPubListController {
async createEPub(sourceType, sourceId, data) {
prompt.addOne({ text: 'Creating I-Note...', timeout: 4000 });
const rawEPubData = await this.getRawEPubData(sourceType, sourceId, data.language);


if (rawEPubData === ErrorTypes.NotFound404) {
prompt.error('Failed to create the I-Note.');
return false;
Expand All @@ -32,6 +35,8 @@ class EPubListController {
sourceType, sourceId, ...data
}).toObject();

// throw Error();

delete ePubData.id;
// console.log(ePubData);

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

window.location.href = url;
// window.location.href = url;
// Don't open in new tab; the user may not have enabled that.
// nope: uurl.openNewTab(url)
uurl.openNewTab(url)

return newEPubData;
}
Expand Down
31 changes: 16 additions & 15 deletions src/components/CTEPubListScreen/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ARRAY_INIT } from 'utils/constants';
import { LanguageConstants } from '../../CTPlayer';

export function _filterTrivalItems(epubData) {
return [...epubData];
// return _.filter(epubData, (item) => Boolean(_.trim(item.text)));
// return [...epubData];
return _.filter(epubData, (item) => Boolean(_.trim(item.text)));
}
function getLastPunctuationIndex(sentence) {
let lastPunctuationIndex = -1;
Expand All @@ -18,21 +18,22 @@ function getLastPunctuationIndex(sentence) {
return lastPunctuationIndex;
}

// eslint-disable-next-line no-unused-vars
function _parseRawEPubDataSplittingOnPunctuation(rawEPubData) {
return null;
// let buffer = "";
// for (let i = 0; i < rawEPubData.length; i += 1) {
// let curr = (buffer + rawEPubData[i].text).trim();
// let idx = getLastPunctuationIndex(curr);
// if (idx === curr.length - 1) {
// rawEPubData[i].text = curr;
// buffer = "";
// } else {
// buffer = `${curr.substring(getLastPunctuationIndex(curr) + 1, curr.length)} `;
// rawEPubData[i].text = curr.substring(0, getLastPunctuationIndex(curr) + 1);
// }
// }
// return null;
let buffer = "";
for (let i = 0; i < rawEPubData.length; i += 1) {
let curr = (buffer + rawEPubData[i].text).trim();
let idx = getLastPunctuationIndex(curr);
if (idx === curr.length - 1) {
rawEPubData[i].text = curr;
buffer = "";
} else {
buffer = `${curr.substring(getLastPunctuationIndex(curr) + 1, curr.length)} `;
rawEPubData[i].text = curr.substring(0, getLastPunctuationIndex(curr) + 1);
}
}
return null;
}
export function _parseRawEPubData(rawEPubData) {
let a = _.map(_filterTrivalItems(rawEPubData), item => ({ ...item, id: _buildID() }));
Expand Down
5 changes: 3 additions & 2 deletions src/components/CTImagePickerModal/ImagesTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { CTText } from 'layout';
import { uurl } from 'utils/use-url';
import Image from 'components/Image';
import _ from 'lodash';
import ImagePreview from './ImagePreview';

function ImagesTab(props) {
Expand All @@ -19,8 +20,8 @@ function ImagesTab(props) {
{description && <CTText margin="5">{description}</CTText>}
<div role="list" className="ct-img-picker-imgs">
{images.map(img => (
<div
key={img}
<div
key={img}
tabIndex={0}
className="ct-img-picker-img-con"
data-current={img === imgUrl}
Expand Down
28 changes: 11 additions & 17 deletions src/entities/EPubs/html-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,21 @@ export async function buildMDFromContent(content) {
// Most likely cause is CORS policy when running local dev server
}

let link_url = content.link;
// if (content.timestamp) {
// link_url = links.watch(epub.sourceId, { begin: TimeString.toSeconds(timestamp) })
// }

if (img_data_url === null) {
let despId = _buildID();
return [
'<div class="img-block">',
`\t<img src="${""}" alt="${content.alt}" aria-describedby="${despId}" />`,
`\t<div id="${despId}">${html.markdown(content.descriptions.join("\n"))}</div>`,
'</div>'
].join('\n');
}
if (content.descriptions.length !== 0) {
let despId = _buildID();
return [
'<div class="img-block">',
`\t<img src="${img_data_url}" alt="${content.alt}" aria-describedby="${despId}" />`,
`\t<div id="${despId}">${html.markdown(content.descriptions.join("\n"))}</div>`,
'</div>'
].join('\n');
img_data_url = "";
}
let despId = _buildID();
return [
'<div class="img-block">',
`\t<img src="${img_data_url}" alt="${content.alt}" />`,
(link_url && link_url !== "") ? `<a href="${link_url}">` : "",
`\t<img src="${img_data_url}" alt="${content.alt}" aria-describedby="${despId}" />`,
(link_url && link_url !== "") ? `</a>` : "",
content.descriptions.length !== 0 ? `\t<div id="${despId}">${html.markdown(content.descriptions.join("\n"))}</div>` : "",
'</div>'
].join('\n');
}
Expand Down
101 changes: 71 additions & 30 deletions src/entities/EPubs/structs/EPubChapterData.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,91 @@
import { _buildID } from 'utils';
import _ from 'lodash';
import { buildHTMLFromChapter } from '../html-converters';
import EPubChapterLikeData from './EPubChapterLikeData';
import EPubSubChapterData from './EPubSubChapterData';

let untitledChapterNum = 0;
function _createChapterTitle() {
untitledChapterNum += 1;
let chapterNum = untitledChapterNum > 0 ? ` (${untitledChapterNum})` : '';
return `Untitled Chapter${chapterNum}`;
}
import EPubImageData from './EPubImageData';

class EPubChapterData {
__data__ = {
id: '',
title: '',
start: '00:00:00',
end: '00:00:00',
timemerge: '00:00:00',
condition: ['default'],
subChapters: [],
contents: []
};

constructor(data, resetText = true, sourceId) {
if (data instanceof EPubChapterData) {
this.__data__ = data.__data__;
return;
}

let {
id,
title,
items,
contents = [],
start
} = data;

// const { start, end } = findChapterTimeSpan(data); // TODO

this.__data__ = {
...this.__data__,
id: id || _buildID(),
start,
title: title || EPubChapterData.createChapterTitle(),
condition: ['default'],
contents: resetText
? EPubChapterData.buildContentsFromItems(items, sourceId)
: contents.map(con => typeof con === 'string' ? con : { ...con })
};
}

class EPubChapterData extends EPubChapterLikeData {
constructor(chapterLike, resetText) {
super(chapterLike, resetText, _createChapterTitle);
contentToObject(content) {
return content instanceof EPubImageData ? content.toObject() : content;
}

const { subChapters = [] } = chapterLike;
this.subChapters = subChapters.map(sch => {
const subchapter = new EPubSubChapterData(sch, resetText);
return subchapter.__data__ ? subchapter.__data__ : subchapter.toObject();
});
contentsToObject(contents) {
return contents.map(this.contentToObject);
}

toObject() {
return {
...this.__data__,
items: this.itemsToObject(),
contents: this.contentsToObject(),
subChapters: this.subChapters.map(subChapter => subChapter.__data__ ? subChapter.__data__ : subChapter)
contents: this.contentsToObject(this.__data__.contents),
};
}

toHTML() {
return buildHTMLFromChapter(this.__data__);
}

/**
* @returns {EPubSubChapterData[]}
*/
get subChapters() {
return this.__data__.subChapters;
static untitledChapterNum = 0;
static buildContentsFromItems(items, sourceId) {
const content = [];
for (const item of items) {
if (item !== undefined) {
if (item.image) { // if there is an image
const imageData = (sourceId ? EPubImageData.createWithTimestamp(item, sourceId) : EPubImageData.create(item));
content.push(imageData)
}
if (item.text) { // if there is text
const text = item.text
if (_.trim(text)) {
content.push(text);
}
}
}
}
return content;
}

set subChapters(subChapters) {
this.__data__.subChapters = subChapters;
static createChapterTitle() {
EPubChapterData.untitledChapterNum += 1;
let chapterNum = EPubChapterData.untitledChapterNum > 0 ? ` (${EPubChapterData.untitledChapterNum})` : '';
return `Untitled Chapter${chapterNum}`;
}

static __buildHTMLFromChapter = buildHTMLFromChapter;
}

export default EPubChapterData;
Loading
Loading