-
Notifications
You must be signed in to change notification settings - Fork 86
feat: user widgets #194
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
Merged
Merged
feat: user widgets #194
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9aa9e08
feat: widgets
EdAyers e15dcd4
fix: simplify memoising logic of userWidget
EdAyers b6dffe9
fix: bad import
EdAyers e7c0b91
feat: use api from RFC
EdAyers 0c7f74b
refactor: Lean.Widget.getWidgetInfos → getWidgets
EdAyers 0b680ac
feat: WidgetSource content addressing
EdAyers 8ff0ecc
feat: export InteractiveCode
Vtec234 7aec23a
feat: export InteractiveCode for widgets
Vtec234 00e14a6
refactor: move RPC calls to rpcInterface.ts
EdAyers e102747
feat: if multiple widgets are present at a pos then display them all
EdAyers db47eb6
doc: docstrings for rpc
EdAyers 012ab62
fix: issue where widgets would not update if code changed
EdAyers ece1385
fix: user widget loader uses same mechanism as in InfoAux
EdAyers 9514743
fix: some PR issues
EdAyers 9501eba
fix: remove deps on Widget_getWidgetSource useAsync
EdAyers d1eef2d
fix: refactor userwidget api
EdAyers 3ba485e
chore: export utils
Vtec234 9e022b2
Merge branch 'master' into widget-pr
Vtec234 6b61ae3
chore: adapt after merge
Vtec234 9080a4f
fix: typing bug
Vtec234 e400a53
fix: import
Vtec234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.