-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from Vtec234/widget-pr
feat: user widgets
- Loading branch information
Showing
7 changed files
with
195 additions
and
35 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
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,48 @@ | ||
import * as React from 'react'; | ||
|
||
import { Widget_getWidgetSource, UserWidget, UserWidgetInstance } from '@lean4/infoview-api'; | ||
import { RpcContext } from './rpcSessions'; | ||
import { DocumentPosition, mapRpcError, useAsync } from './util'; | ||
import { ErrorBoundary } from './errors'; | ||
|
||
function dynamicallyLoadComponent(hash: string, code: string) { | ||
return React.lazy(async () => { | ||
const file = new File([code], `widget_${hash}.js`, { type: 'text/javascript' }) | ||
const url = URL.createObjectURL(file) | ||
return await import(url) | ||
}) | ||
} | ||
|
||
const componentCache = new Map<string, React.LazyExoticComponent<React.ComponentType<any>>>() | ||
|
||
interface UserWidgetProps { | ||
pos: DocumentPosition | ||
widget: UserWidgetInstance | ||
} | ||
|
||
export function UserWidget({ pos, widget }: UserWidgetProps) { | ||
const rs = React.useContext(RpcContext); | ||
const hash = widget.javascriptHash | ||
const [status, component, error] = useAsync( | ||
async () => { | ||
if (componentCache.has(hash)) { | ||
return componentCache.get(hash) | ||
} | ||
const code = await Widget_getWidgetSource(rs, pos, hash) | ||
const component = dynamicallyLoadComponent(hash, code.sourcetext) | ||
componentCache.set(hash, component) | ||
return component | ||
}, | ||
[hash]) | ||
|
||
const componentProps = { pos, ...widget.props } | ||
|
||
return ( | ||
<React.Suspense fallback={`Loading widget: ${widget.id} ${status}.`}> | ||
<ErrorBoundary> | ||
{component && <div>{React.createElement(component, componentProps)}</div>} | ||
{error && <div>{mapRpcError(error).message}</div>} | ||
</ErrorBoundary> | ||
</React.Suspense> | ||
) | ||
} |
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