Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write scripture, more supported markers, misc tidying up #8

Merged
merged 8 commits into from
Oct 19, 2022
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
2 changes: 1 addition & 1 deletion react-electron-poc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion react-electron-poc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
}
},
"dependencies": {
"@types/is-hotkey": "^0.1.7",
"dockview": "^1.5.1",
"electron-debug": "^3.2.0",
"electron-log": "^4.4.8",
Expand All @@ -131,6 +130,7 @@
"@teamsupercell/typings-for-css-modules-loader": "^2.5.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.3.0",
"@types/is-hotkey": "^0.1.7",
"@types/jest": "^28.1.7",
"@types/node": "18.7.6",
"@types/react": "^18.0.17",
Expand Down
88 changes: 88 additions & 0 deletions react-electron-poc/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,41 @@ async function getFilesText(filePaths: string[], delay = 0): Promise<string[]> {
);
}

/**
* Writes the string to a file asynchronously. Delays 1ms or more if desired
* @param filePath Path to file from assets
* @param fileContents string to write into the file
* @param delay delay before resolving promise in ms
* @returns promise that resolves after delay ms and then writing the fil
*/
async function writeFileText(
filePath: string,
fileContents: string,
delay = 0,
): Promise<void> {
return delayPromise<void>((resolve, reject) => {
const start = performance.now();
fs.writeFile(getAssetPath(filePath), fileContents, (err) => {
if (err) reject(err.message);
else resolve();
console.log(
`Writing ${filePath} took ${performance.now() - start} ms`,
);
});
}, delay);
}

/* async function writeFilesText(
files: { filePath: string; fileContents: string }[],
delay = 0,
): Promise<void[]> {
return Promise.all(
files.map(({ filePath, fileContents }) =>
writeFileText(filePath, fileContents, delay),
),
);
} */

/** Simulating how long it may take Paratext to load and serve the Scriptures */
const getScriptureDelay = 75;
/** Simulating how long it may take Paratext to serve the resource info */
Expand Down Expand Up @@ -283,6 +318,38 @@ async function handleGetScriptureChapter(
}
}

async function handleWriteScriptureBook(
_event: IpcMainInvokeEvent,
_fileExtension: string,
_shortName: string,
_bookNum: number,
_contents: ScriptureChapter[],
): Promise<void> {
throw new Error(
'writeScriptureBook was deemed not necessary for this POC and was not implemented',
);
}

async function handleWriteScriptureChapter(
_event: IpcMainInvokeEvent,
fileExtension: string,
shortName: string,
bookNum: number,
chapter: number,
contents: ScriptureChapter,
): Promise<void> {
try {
return await writeFileText(
`testScripture/${shortName}/${bookNum}-${chapter}.${fileExtension}`,
contents.contents as string,
getScriptureDelay,
);
} catch (e) {
console.log(e);
throw new Error(`Failed to write ${shortName} ${bookNum} ${chapter}`);
}
}

/** These test files are from breakpointing at UsfmSinglePaneControl.cs at the line that gets Css in LoadUsfm. */
async function handleGetScriptureStyle(
_event: IpcMainInvokeEvent,
Expand Down Expand Up @@ -359,6 +426,27 @@ const ipcHandlers: {
bookNum: number,
chapter: number,
) => handleGetScriptureChapter(event, 'json', shortName, bookNum, chapter),
'ipc-scripture:writeScriptureBook': (
event,
shortName: string,
bookNum: number,
contents: ScriptureChapter[],
) => handleWriteScriptureBook(event, 'json', shortName, bookNum, contents),
'ipc-scripture:writeScriptureChapter': (
event,
shortName: string,
bookNum: number,
chapter: number,
contents: ScriptureChapter,
) =>
handleWriteScriptureChapter(
event,
'json',
shortName,
bookNum,
chapter,
contents,
),
'ipc-scripture:getScriptureBookRaw': (
event,
shortName: string,
Expand Down
24 changes: 24 additions & 0 deletions react-electron-poc/src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ contextBridge.exposeInMainWorld('electronAPI', {
bookNum,
chapter,
),
writeScriptureBook: (
shortName: string,
bookNum: number,
contents: ScriptureChapterContent[],
): Promise<void> =>
ipcRenderer.invoke(
'ipc-scripture:writeScriptureBook',
shortName,
bookNum,
contents,
),
writeScriptureChapter: (
shortName: string,
bookNum: number,
chapter: number,
contents: ScriptureChapterContent,
): Promise<void> =>
ipcRenderer.invoke(
'ipc-scripture:writeScriptureChapter',
shortName,
bookNum,
chapter,
contents,
),
getScriptureBookRaw: (
shortName: string,
bookNum: number,
Expand Down
5 changes: 5 additions & 0 deletions react-electron-poc/src/renderer/components/Components.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
137 changes: 80 additions & 57 deletions react-electron-poc/src/renderer/components/ScrRefSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
memo,
PropsWithChildren,
useCallback,
useEffect,
useState,
} from 'react';
import usePromise from 'renderer/hooks/usePromise';
Expand Down
Loading