-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add communication to web views (Web View Controllers and `postMessage…
…ToWebView`) (#1300)
- Loading branch information
Showing
26 changed files
with
2,577 additions
and
975 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 39 additions & 10 deletions
49
...src/hello-world/src/web-views/hello-world-project/hello-world-project-viewer.web-view.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,55 @@ | ||
import { WebViewProps } from '@papi/core'; | ||
import { useProjectData, useProjectSetting } from '@papi/frontend/react'; | ||
import { useProjectData, useProjectSetting, useWebViewController } from '@papi/frontend/react'; | ||
import { Button } from 'platform-bible-react'; | ||
import { CSSProperties, useMemo } from 'react'; | ||
|
||
const namesDefault: string[] = []; | ||
|
||
globalThis.webViewComponent = function HelloWorldProjectViewer({ projectId }: WebViewProps) { | ||
globalThis.webViewComponent = function HelloWorldProjectViewer({ | ||
projectId, | ||
useWebViewState, | ||
}: WebViewProps) { | ||
const [callerWebViewId] = useWebViewState<string | undefined>('callerWebViewId', undefined); | ||
|
||
const callerWebViewController = useWebViewController( | ||
'helloWorld.projectWebView', | ||
callerWebViewId, | ||
); | ||
|
||
const [names] = useProjectData('helloWorld', projectId).Names(undefined, namesDefault); | ||
|
||
const [headerSize] = useProjectSetting(projectId, 'helloWorld.headerSize', 15); | ||
|
||
const [headerColor] = useProjectSetting(projectId, 'helloWorld.headerColor', 'Black'); | ||
|
||
const headerStyle = useMemo<CSSProperties>( | ||
() => ({ fontSize: `${headerSize}pt`, color: headerColor }), | ||
[headerSize, headerColor], | ||
); | ||
const headerStyle = useMemo<CSSProperties>(() => { | ||
const colorPropertyName = callerWebViewController ? 'backgroundColor' : 'color'; | ||
return { fontSize: `${headerSize}pt`, [colorPropertyName]: headerColor }; | ||
}, [callerWebViewController, headerSize, headerColor]); | ||
|
||
return ( | ||
<div className="top"> | ||
{names.map((name) => ( | ||
<div style={headerStyle}>Hello, {name}!</div> | ||
))} | ||
<div className="tw-m-3 [&>*]:tw-mb-3"> | ||
{callerWebViewController && ( | ||
<div>Click a name to focus it on the Hello World Project web view!</div> | ||
)} | ||
{names.map((name) => { | ||
const textContent = `Hello, ${name}!`; | ||
return callerWebViewController ? ( | ||
<div key={name}> | ||
<Button | ||
className="tw-text-foreground" | ||
style={headerStyle} | ||
onClick={() => callerWebViewController?.focusName(name)} | ||
> | ||
{textContent} | ||
</Button> | ||
</div> | ||
) : ( | ||
<div key={name} style={headerStyle}> | ||
{textContent} | ||
</div> | ||
); | ||
})} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.