Skip to content

Commit

Permalink
Prevent ScrRefSelector from going below lowest values
Browse files Browse the repository at this point in the history
tjcouch-sil committed Oct 14, 2022
1 parent 0489bda commit 43338f2
Showing 5 changed files with 121 additions and 70 deletions.
5 changes: 5 additions & 0 deletions react-electron-poc/src/renderer/components/Components.css
Original file line number Diff line number Diff line change
@@ -33,6 +33,11 @@
width: 4ch
}

.scr-toolbar .selector-area .change-btns {
width: auto;
margin: 0px;
}

.scr-toolbar .selector-area .change-btn {
padding: 2px 1px;
border-right: 1px solid #505070;
137 changes: 80 additions & 57 deletions react-electron-poc/src/renderer/components/ScrRefSelector.tsx
Original file line number Diff line number Diff line change
@@ -7,6 +7,9 @@ import {
offsetBook,
offsetChapter,
offsetVerse,
FIRST_SCR_BOOK_NUM,
FIRST_SCR_CHAPTER_NUM,
FIRST_SCR_VERSE_NUM,
} from '@util/ScriptureUtil';
import React, { useCallback, useEffect, useState } from 'react';
import './Components.css';
@@ -49,65 +52,85 @@ export default ({ scrRef, handleSubmit }: ScrRefSelectorProps) => {
<span className="book">
{getBookLongNameFromNum(scrRef.book)}
</span>
<button
type="button"
className="change-btn left"
onClick={() => handleSubmit(offsetBook(scrRef, -1))}
disabled={isScrRefChanged}
>
&lt;
</button>
<span
className={`splitter${isScrRefChanged ? ' changed' : ''}`}
/>
<button
type="button"
className="change-btn"
onClick={() => handleSubmit(offsetBook(scrRef, 1))}
disabled={isScrRefChanged}
>
&gt;
</button>
<span className="change-btns">
<button
type="button"
className="change-btn left"
onClick={() => handleSubmit(offsetBook(scrRef, -1))}
disabled={
isScrRefChanged || scrRef.book <= FIRST_SCR_BOOK_NUM
}
>
&lt;
</button>
<span
className={`splitter${
isScrRefChanged ? ' changed' : ''
}`}
/>
<button
type="button"
className="change-btn"
onClick={() => handleSubmit(offsetBook(scrRef, 1))}
disabled={isScrRefChanged}
>
&gt;
</button>
</span>
<span className="chapter">{scrRef.chapter}:</span>
<button
type="button"
className="change-btn left"
onClick={() => handleSubmit(offsetChapter(scrRef, -1))}
disabled={isScrRefChanged}
>
&lt;
</button>
<span
className={`splitter${isScrRefChanged ? ' changed' : ''}`}
/>
<button
type="button"
className="change-btn"
onClick={() => handleSubmit(offsetChapter(scrRef, 1))}
disabled={isScrRefChanged}
>
&gt;
</button>
<span className="change-btns">
<button
type="button"
className="change-btn left"
onClick={() => handleSubmit(offsetChapter(scrRef, -1))}
disabled={
isScrRefChanged ||
scrRef.chapter <= FIRST_SCR_CHAPTER_NUM
}
>
&lt;
</button>
<span
className={`splitter${
isScrRefChanged ? ' changed' : ''
}`}
/>
<button
type="button"
className="change-btn"
onClick={() => handleSubmit(offsetChapter(scrRef, 1))}
disabled={isScrRefChanged}
>
&gt;
</button>
</span>
<span>{scrRef.verse}</span>
<button
type="button"
className="change-btn left"
onClick={() => handleSubmit(offsetVerse(scrRef, -1))}
disabled={isScrRefChanged}
>
&lt;
</button>
<span
className={`splitter${isScrRefChanged ? ' changed' : ''}`}
/>
<button
type="button"
className="change-btn"
onClick={() => handleSubmit(offsetVerse(scrRef, 1))}
disabled={isScrRefChanged}
>
&gt;
</button>
<span className="change-btns">
<button
type="button"
className="change-btn left"
onClick={() => handleSubmit(offsetVerse(scrRef, -1))}
disabled={
isScrRefChanged ||
scrRef.verse <= FIRST_SCR_VERSE_NUM
}
>
&lt;
</button>
<span
className={`splitter${
isScrRefChanged ? ' changed' : ''
}`}
/>
<button
type="button"
className="change-btn"
onClick={() => handleSubmit(offsetVerse(scrRef, 1))}
disabled={isScrRefChanged}
>
&gt;
</button>
</span>
</span>
<span className="input-area">
<input
Original file line number Diff line number Diff line change
@@ -1029,7 +1029,8 @@ export const ScriptureTextPanelSlate = ScriptureTextPanelHOC(
/** When we get new Scripture project contents, partition the chapters into smaller chunks and create an editor for each chunk */
const scrChaptersChunked = useMemo<ScriptureContentChunk[]>(() => {
if (scrChapters && scrChapters.length > 0) {
return scrChapters.flatMap((scrChapter) => {
const start = performance.now();
const scrChapterChunks = scrChapters.flatMap((scrChapter) => {
// TODO: When loading, the contents come as a string. Consider how to improve the loading value in ScriptureTextPanelHOC
const scrChapterContents = isString(scrChapter.contents)
? [
@@ -1049,6 +1050,12 @@ export const ScriptureTextPanelSlate = ScriptureTextPanelHOC(
chunkSize,
);
});
console.log(
`Performance: chunking scrChapters took ${
performance.now() - start
} ms`,
);
return scrChapterChunks;
}
return [];
}, [scrChapters, useVirtualization]);
11 changes: 8 additions & 3 deletions react-electron-poc/src/renderer/services/ScriptureService.ts
Original file line number Diff line number Diff line change
@@ -36,10 +36,9 @@ export const getScripture = async (
),
}),
);
const end = performance.now();
console.log(
`Performance: Parsing JSON for getScripture(${shortName}, ${bookNum}, ${chapter}) took ${
end - start
performance.now() - start
} ms`,
);
return scrChapterContentsParsed;
@@ -74,10 +73,16 @@ export const writeScripture = async (
contents: ScriptureChapterContent[],
): Promise<boolean> => {
try {
const start = performance.now();
const contentsJSON = contents.map((content) => ({
...content,
contents: JSON.stringify(content.contents, null, 4),
contents: JSON.stringify(content.contents),
})) as unknown as ScriptureChapterContent[];
console.log(
`Performance: Stringifying ${shortName} ${bookNum}:${chapter} took ${
performance.now() - start
} ms`,
);
if (chapter >= 0)
await window.electronAPI.scripture.writeScriptureChapter(
shortName,
29 changes: 20 additions & 9 deletions react-electron-poc/src/renderer/util/ScriptureUtil.ts
Original file line number Diff line number Diff line change
@@ -77,8 +77,10 @@ const scrBookNames: string[][] = [
['JUD', 'Jude'],
['REV', 'Revelation'],
];
const firstScrBookNum = 1;
const lastScrBookNum = scrBookNames.length - 1;
export const FIRST_SCR_BOOK_NUM = 1;
export const LAST_SCR_BOOK_NUM = scrBookNames.length - 1;
export const FIRST_SCR_CHAPTER_NUM = 1;
export const FIRST_SCR_VERSE_NUM = 0;

export const getBookNumFromName = (bookName: string): number => {
return scrBookNames.findIndex((bookNames) => bookNames.includes(bookName));
@@ -87,20 +89,26 @@ export const getBookNumFromName = (bookName: string): number => {
export const getAllBookNamesFromNum = (bookNum: number): string[] => {
return [
...scrBookNames[
bookNum < firstScrBookNum || bookNum > lastScrBookNum ? 0 : bookNum
bookNum < FIRST_SCR_BOOK_NUM || bookNum > LAST_SCR_BOOK_NUM
? 0
: bookNum
],
];
};

export const getBookShortNameFromNum = (bookNum: number): string => {
return scrBookNames[
bookNum < firstScrBookNum || bookNum > lastScrBookNum ? 0 : bookNum
bookNum < FIRST_SCR_BOOK_NUM || bookNum > LAST_SCR_BOOK_NUM
? 0
: bookNum
][0];
};

export const getBookLongNameFromNum = (bookNum: number): string => {
return scrBookNames[
bookNum < firstScrBookNum || bookNum > lastScrBookNum ? 0 : bookNum
bookNum < FIRST_SCR_BOOK_NUM || bookNum > LAST_SCR_BOOK_NUM
? 0
: bookNum
][1];
};

@@ -109,8 +117,8 @@ export const offsetBook = (
offset: number,
): ScriptureReference => ({
book: Math.max(
firstScrBookNum,
Math.min(scrRef.book + offset, lastScrBookNum),
FIRST_SCR_BOOK_NUM,
Math.min(scrRef.book + offset, LAST_SCR_BOOK_NUM),
),
chapter: 1,
verse: 1,
@@ -121,14 +129,17 @@ export const offsetChapter = (
offset: number,
): ScriptureReference => ({
...scrRef,
chapter: scrRef.chapter + offset,
chapter: Math.max(FIRST_SCR_CHAPTER_NUM, scrRef.chapter + offset),
verse: 1,
});

export const offsetVerse = (
scrRef: ScriptureReference,
offset: number,
): ScriptureReference => ({ ...scrRef, verse: scrRef.verse + offset });
): ScriptureReference => ({
...scrRef,
verse: Math.max(FIRST_SCR_VERSE_NUM, scrRef.verse + offset),
});

/** Parse a verse number from a string */
export const parseVerse = (verseText: string): number | undefined => {

0 comments on commit 43338f2

Please sign in to comment.