Skip to content
Merged
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
62 changes: 43 additions & 19 deletions renderer/src/components/EditorPage/AudioEditor/AudioEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ const loadVerseStructureFromFile = (projectsDir, bookId, chapter) => {
}
};

const parsedUsfmCache = new Map();

const getParsedUsfmBook = (usfmPath) => {
const fs = window.require('fs');

const stat = fs.statSync(usfmPath);
const cached = parsedUsfmCache.get(usfmPath);

if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
logger.debug('Using cached parsed USFM:', usfmPath);
return cached.bookContent;
}

const usfm = fs.readFileSync(usfmPath, 'utf8');
const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED);
const jsonOutput = myUsfmParser.toJSON();
const bookContent = jsonOutput.chapters;

parsedUsfmCache.set(usfmPath, {
mtimeMs: stat.mtimeMs,
size: stat.size,
bookContent,
});

return bookContent;
};

// Priority 2: Load from USFM
const loadVersesFromUSFM = (projectsDir, bookId, chapter) => {
try {
Expand All @@ -116,19 +143,7 @@ const loadVersesFromUSFM = (projectsDir, bookId, chapter) => {
};
}

const usfm = fs.readFileSync(usfmPath, 'utf8');
const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED);
const isJsonValid = myUsfmParser.validate();

if (!isJsonValid) {
logger.error('Invalid USFM file');
return {
success: false, bookContent: null, verses: null, source: null,
};
}

const jsonOutput = myUsfmParser.toJSON();
const bookContent = jsonOutput.chapters;
const bookContent = getParsedUsfmBook(usfmPath);

const chapterData = bookContent.find(
(ch) => ch.chapterNumber === chapter.toString(),
Expand Down Expand Up @@ -495,6 +510,8 @@ const AudioEditor = ({ editor }) => {
useEffect(() => {
if (!isElectron()) { return; }

let cancelled = false;

structureAppliedRef.current = false;

setIsLoading(true);
Expand Down Expand Up @@ -605,6 +622,8 @@ const AudioEditor = ({ editor }) => {
logger.debug('Data source:', dataSource);
logger.debug('Verse count:', finalVerses.length);

if (cancelled) { return; }

setOriginalBookContent(bookContent);

const versesWithAudios = structureAppliedRef.current
Expand All @@ -629,25 +648,30 @@ const AudioEditor = ({ editor }) => {
}),
);

if (_books.includes(bookId.toUpperCase()) === false) {
if (!cancelled && _books.includes(bookId.toUpperCase()) === false) {
setAudioContent();
setDisplayScreen(true);
setIsLoading(false);
}
}
} catch (error) {
logger.error('Error in useEffect:', error);
setIsLoading(false);
setDisplayScreen(true);
if (!cancelled) {
setIsLoading(false);
setDisplayScreen(true);
}
}
});

return () => { cancelled = true; };
}, [bookId, chapter]);

return (
<div id="editor">
<Editor callFrom="textTranslation" editor={editor}>
{((isLoading || !audioContent) && displyScreen) && <EmptyScreen call="audio" />}
{isLoading && !displyScreen && <LoadingScreen />}
{audioContent && isLoading === false
{isLoading && <LoadingScreen />}
{!isLoading && displyScreen && <EmptyScreen call="audio" />}
{!isLoading && audioContent
&& (
<EditorPage
verse={verse}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const VerseContextMenu = ({
}) => {
const { t } = useTranslation();

if (!visible) {
const hasMenuItems = (!isFirstVerse && !isJoinedVerse)
|| isJoinedVerse
|| canOpenComments;

if (!visible || !hasMenuItems) {
return null;
}

Expand Down
62 changes: 43 additions & 19 deletions renderer/src/components/EditorPage/VideoEditor/VideoEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ const loadVerseStructureFromFile = (projectsDir, bookId, chapter) => {
}
};

const parsedUsfmCache = new Map();

const getParsedUsfmBook = (usfmPath) => {
const fs = window.require('fs');

const stat = fs.statSync(usfmPath);
const cached = parsedUsfmCache.get(usfmPath);

if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
logger.debug('Using cached parsed USFM:', usfmPath);
return cached.bookContent;
}

const usfm = fs.readFileSync(usfmPath, 'utf8');
const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED);
const jsonOutput = myUsfmParser.toJSON();
const bookContent = jsonOutput.chapters;

parsedUsfmCache.set(usfmPath, {
mtimeMs: stat.mtimeMs,
size: stat.size,
bookContent,
});

return bookContent;
};

const loadVersesFromUSFM = (projectsDir, bookId, chapter) => {
try {
const fs = window.require('fs');
Expand All @@ -137,19 +164,7 @@ const loadVersesFromUSFM = (projectsDir, bookId, chapter) => {
};
}

const usfm = fs.readFileSync(usfmPath, 'utf8');
const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED);
const isJsonValid = myUsfmParser.validate();

if (!isJsonValid) {
logger.error('Invalid USFM file');
return {
success: false, bookContent: null, verses: null, source: null,
};
}

const jsonOutput = myUsfmParser.toJSON();
const bookContent = jsonOutput.chapters;
const bookContent = getParsedUsfmBook(usfmPath);

const chapterData = bookContent.find(
(ch) => ch.chapterNumber === chapter.toString(),
Expand Down Expand Up @@ -609,6 +624,8 @@ const VideoEditor = ({ editor }) => {
modelClose();
};
useEffect(() => {
let cancelled = false;

if (isElectron()) {
setIsLoading(true);
setDisplayScreen(false);
Expand Down Expand Up @@ -728,6 +745,8 @@ const VideoEditor = ({ editor }) => {
chapter,
);

if (cancelled) { return; }

setOriginalBookContent(bookContent);

logger.debug('Starting video attachment process...');
Expand Down Expand Up @@ -760,18 +779,23 @@ const VideoEditor = ({ editor }) => {
}),
);

if (_books.includes(bookId.toUpperCase()) === false) {
if (!cancelled && _books.includes(bookId.toUpperCase()) === false) {
setVideoContent();
setDisplayScreen(true);
setIsLoading(false);
}
}
} catch (error) {
logger.error('Error in useEffect:', error);
setIsLoading(false);
setDisplayScreen(true);
if (!cancelled) {
setIsLoading(false);
setDisplayScreen(true);
}
}
});
}

return () => { cancelled = true; };
}, [bookId, chapter]);

useEffect(() => {
Expand Down Expand Up @@ -808,9 +832,9 @@ const VideoEditor = ({ editor }) => {

return (
<Editor callFrom="textTranslation" editor={editor}>
{((isLoading || !videoContent) && displyScreen) && <EmptyScreen call="video" />}
{isLoading && !displyScreen && <LoadingScreen />}
{videoContent && isLoading === false
{isLoading && <LoadingScreen />}
{!isLoading && displyScreen && <EmptyScreen call="video" />}
{!isLoading && videoContent
&& (
<div id="video-editor" className="h-full overflow-auto">
<VideoPlayer
Expand Down
Loading
Loading