-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for loading HTML from URI
- Loading branch information
Showing
10 changed files
with
152 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React, { ReactElement, useEffect, useState } from 'react'; | ||
import { ActivityIndicator, Text, View } from 'react-native'; | ||
import { RenderHTMLProps } from './shared-types'; | ||
|
||
interface HTMLLoaderProps extends RenderHTMLProps { | ||
children: (resolvedHTML: string) => ReactElement; | ||
} | ||
|
||
interface LoaderInternalState { | ||
loading: boolean; | ||
error: boolean; | ||
resolvedHTML: string | null; | ||
} | ||
|
||
const ERROR_STATE = { | ||
error: true, | ||
loading: false, | ||
resolvedHTML: null | ||
}; | ||
|
||
async function loadHTMLResource(uri: string): Promise<LoaderInternalState> { | ||
const response = await fetch(uri); | ||
if (response.ok) { | ||
const html = await response.text(); | ||
return { | ||
resolvedHTML: html, | ||
error: false, | ||
loading: false | ||
}; | ||
} | ||
return ERROR_STATE; | ||
} | ||
|
||
function useLoader(props: HTMLLoaderProps) { | ||
const { uri, html } = props; | ||
const [loadState, setState] = useState<LoaderInternalState>({ | ||
error: false, | ||
loading: false, | ||
resolvedHTML: html || null | ||
}); | ||
const { error } = loadState; | ||
useEffect(() => { | ||
let cancelled = false; | ||
if (!error && uri) { | ||
setState({ loading: true, error: false, resolvedHTML: null }); | ||
loadHTMLResource(uri) | ||
.then((state) => { | ||
!cancelled && setState(state); | ||
}) | ||
.catch(() => { | ||
!cancelled && setState(ERROR_STATE); | ||
}); | ||
} | ||
return () => { | ||
cancelled = true; | ||
}; | ||
}, [error, uri]); | ||
return loadState; | ||
} | ||
|
||
function defaultRenderError(props: RenderHTMLProps) { | ||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Text style={{ fontStyle: 'italic', fontSize: 16 }}> | ||
Failed to load {props.uri} | ||
</Text> | ||
</View> | ||
); | ||
} | ||
|
||
function defaultRenderLoading() { | ||
return ( | ||
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> | ||
<ActivityIndicator /> | ||
</View> | ||
); | ||
} | ||
export default function LoadHTML(props: HTMLLoaderProps): ReactElement | null { | ||
const { remoteErrorView, remoteLoadingView, children } = props; | ||
const { resolvedHTML, error, loading } = useLoader(props); | ||
if (error) { | ||
return remoteErrorView!.call(null, props); | ||
} | ||
if (loading) { | ||
return remoteLoadingView!.call(null, props); | ||
} | ||
return children?.call(null, resolvedHTML!) || null; | ||
} | ||
|
||
LoadHTML.defaultProps = { | ||
remoteErrorView: defaultRenderError, | ||
remoteLoadingView: defaultRenderLoading | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { TBlock } from '@native-html/transient-render-engine'; | ||
import { TNodeGenericRendererProps } from './shared-types'; | ||
|
||
const TDocumentRenderer = ({}: TNodeGenericRendererProps<TBlock>) => { | ||
return null; | ||
}; | ||
|
||
export default TDocumentRenderer; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import RenderHTML from './RenderHTML'; | ||
export * from './shared-types'; | ||
export default RenderHTML; | ||
|
||
export { default as useTRenderEngine } from './useTRenderEngine'; | ||
export { default as useTTree } from './useTTree'; |
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