Skip to content

Commit

Permalink
Enable webviews to change their title and icon url (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcouch-sil authored Oct 23, 2023
2 parents 5d44822 + 6044270 commit 379306d
Show file tree
Hide file tree
Showing 16 changed files with 1,096 additions and 351 deletions.
32 changes: 24 additions & 8 deletions c-sharp/NetworkObjects/UsfmDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected override ResponseToRequest HandleRequest(string functionName, JsonArra
"getBookNames" => GetBookNames(),
"getChapter" => GetChapter(args[0]!.ToJsonString()),
"getChapterUsx" => GetChapterUsx(args[0]!.ToJsonString()),
"getBookUsx" => GetBookUsx(args[0]!.ToJsonString()),
"getVerse" => GetVerse(args[0]!.ToJsonString()),
_ => ResponseToRequest.Failed($"Unexpected function: {functionName}")
};
Expand All @@ -69,7 +70,14 @@ private ResponseToRequest GetChapter(string args)
private ResponseToRequest GetChapterUsx(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(GetUsx(verseRef))
? ResponseToRequest.Succeeded(GetUsxForChapter(verseRef))
: ResponseToRequest.Failed(errorMsg);
}

private ResponseToRequest GetBookUsx(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(GetUsxForBook(verseRef))
: ResponseToRequest.Failed(errorMsg);
}

Expand All @@ -80,16 +88,18 @@ private ResponseToRequest GetVerse(string args)
: ResponseToRequest.Failed(errorMsg);
}

public string GetUsx(VerseRef vref)
private string GetUsxForChapter(VerseRef vref)
{
XmlDocument usx = GetUsxForChapter(vref.BookNum, vref.ChapterNum);
XmlDocument usx = ConvertUsfmToUsx(GetUsfm(vref.BookNum, vref.ChapterNum), vref.BookNum);
string contents = usx.OuterXml ?? string.Empty;
return contents;
}

private XmlDocument GetUsxForChapter(int bookNum, int chapterNum)
private string GetUsxForBook(VerseRef vref)
{
return ConvertUsfmToUsx(GetUsfmForChapter(bookNum, chapterNum), bookNum);
XmlDocument usx = ConvertUsfmToUsx(GetUsfm(vref.BookNum), vref.BookNum);
string contents = usx.OuterXml ?? string.Empty;
return contents;
}

/// <summary>
Expand All @@ -111,10 +121,16 @@ private XmlDocument ConvertUsfmToUsx(string usfm, int bookNum)
return doc;
}

private string GetUsfmForChapter(int bookNum, int chapterNum)
/// <summary>
/// Gets USFM for a book or chapter.
/// </summary>
/// <param name="bookNum">The book for which to get USFM</param>
/// <param name="chapterNum">The chapter for which to get USFM. Do not specify or specify `null` for the whole book</param>
/// <returns>USFM</returns>
private string GetUsfm(int bookNum, int? chapterNum = null)
{
VerseRef vref = new(bookNum, chapterNum, 0, _scrText!.Settings.Versification);
VerseRef vref = new(bookNum, chapterNum ?? 0, 0, _scrText!.Settings.Versification);
ScrText projectToUse = _scrText!.GetJoinedText(bookNum);
return projectToUse.GetText(vref, true, true);
return projectToUse.GetText(vref, chapterNum.HasValue, true);
}
}
2 changes: 1 addition & 1 deletion extensions/src/hello-world/hello-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ const reactWebViewProvider: IWebViewProviderWithType = {
`${this.webViewType} provider received request to provide a ${savedWebView.webViewType} web view`,
);
return {
...savedWebView,
iconUrl: 'papi-extension://hello-world/assets/offline.svg',
title: 'Hello World React',
...savedWebView,
content: helloWorldReactWebView,
styles: helloWorldReactWebViewStyles,
state: {
Expand Down
18 changes: 15 additions & 3 deletions extensions/src/hello-world/web-views/hello-world.web-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
import type { QuickVerseDataTypes } from 'quick-verse';
import type { PeopleDataProvider, PeopleDataTypes } from 'hello-someone';
import type { UsfmProviderDataTypes } from 'usfm-data-provider';
import { Key, useCallback, useContext, useMemo, useRef, useState } from 'react';
import { Key, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import type { HelloWorldEvent } from 'hello-world';
import type { DialogTypes } from 'renderer/components/dialogs/dialog-definition.model';
import type { WebViewProps } from 'shared/data/web-view.model';
import Clock from './components/clock.component';

type Row = {
Expand Down Expand Up @@ -57,10 +58,14 @@ papi
.fetch('https://www.example.com', { mode: 'no-cors' })
.catch((e) => logger.error(`Could not get data from example.com! Reason: ${e}`));

globalThis.webViewComponent = function HelloWorld() {
globalThis.webViewComponent = function HelloWorld({
useWebViewState,
getWebViewDefinitionUpdatableProperties,
updateWebViewDefinition,
}: WebViewProps) {
const test = useContext(TestContext) || "Context didn't work!! :(";

const [clicks, setClicks] = globalThis.useWebViewState<number>('clicks', 0);
const [clicks, setClicks] = useWebViewState<number>('clicks', 0);
const [rows, setRows] = useState(initializeRows());
const [selectedRows, setSelectedRows] = useState(new Set<Key>());
const [scrRef, setScrRef] = useSetting('platform.verseRef', defaultScrRef);
Expand All @@ -80,6 +85,13 @@ globalThis.webViewComponent = function HelloWorld() {
),
);

useEffect(() => {
logger.log(
`Hello World WebView previous title: ${getWebViewDefinitionUpdatableProperties()?.title}`,
);
updateWebViewDefinition({ title: `Hello World ${clicks}` });
}, [getWebViewDefinitionUpdatableProperties, updateWebViewDefinition, clicks]);

const [echoResult] = usePromise(
useCallback(async () => {
// Not using the promise's resolved value
Expand Down
1 change: 1 addition & 0 deletions extensions/src/usfm-data-provider/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare module 'usfm-data-provider' {
BookNames: DataProviderDataType<boolean, string[], never>;
Chapter: DataProviderDataType<VerseRef, string | undefined, never>;
ChapterUsx: DataProviderDataType<VerseRef, string | undefined, never>;
BookUsx: DataProviderDataType<VerseRef, string | undefined, never>;
Verse: DataProviderDataType<VerseRef, string | undefined, never>;
};

Expand Down
Loading

0 comments on commit 379306d

Please sign in to comment.