Skip to content

Commit

Permalink
refactor: split renderInfoview
Browse files Browse the repository at this point in the history
  • Loading branch information
alissa-tung committed Dec 16, 2023
1 parent a323a39 commit 64a1a3e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lean4-infoview/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export { importWidgetModule, DynamicComponent, DynamicComponentProps,
PanelWidgetProps } from './infoview/userWidget';

export { InteractiveCode, InteractiveCodeProps } from './infoview/interactiveCode';
export { renderInfoview } from './infoview/main';
export { renderInfoview, mkInfoviewApi, Infoview } from './infoview/main';

export { MessageData };

Expand Down
30 changes: 23 additions & 7 deletions lean4-infoview/src/infoview/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ function Main(props: {}) {
* @returns a collection of methods which must be invoked when the relevant editor events occur.
*/
export function renderInfoview(editorApi: EditorApi, uiElement: HTMLElement): InfoviewApi {
const [editorEvents, infoviewApi] = mkInfoviewApi();
connInfoview(editorApi, editorEvents, uiElement);
return infoviewApi;
}

export const mkInfoviewApi = (): [EditorEvents, InfoviewApi] => {
const editorEvents: EditorEvents = {
initialize: new EventEmitter(),
gotServerNotification: new EventEmitter(),
Expand Down Expand Up @@ -125,16 +131,26 @@ export function renderInfoview(editorApi: EditorApi, uiElement: HTMLElement): In
getInfoviewHtml: async () => document.body.innerHTML,
};

const ec = new EditorConnection(editorApi, editorEvents);
return [editorEvents, infoviewApi]
}

editorEvents.initialize.on((loc: Location) => ec.events.changedCursorLocation.fire(loc))
export const Infoview:
React.FC<{ editorApi: EditorApi, editorEvents: EditorEvents }> =
({ editorApi, editorEvents }) => {
const ecRef = React.useRef(new EditorConnection(editorApi, editorEvents));
const ec = ecRef.current;
React.useEffect(() => {
editorEvents.initialize.on((loc: Location) => ec.events.changedCursorLocation.fire(loc))
}, [])

Check warning on line 144 in lean4-infoview/src/infoview/main.tsx

View workflow job for this annotation

GitHub Actions / Windows

React Hook React.useEffect has missing dependencies: 'ec.events.changedCursorLocation' and 'editorEvents.initialize'. Either include them or remove the dependency array

return (<EditorContext.Provider value={ec}>
<Main />
</EditorContext.Provider>)
}

const connInfoview = (editorApi: EditorApi, editorEvents: EditorEvents, uiElement: HTMLElement) => {
const root = ReactDOM.createRoot(uiElement)
root.render(<React.StrictMode>
<EditorContext.Provider value={ec}>
<Main/>
</EditorContext.Provider>
<Infoview editorApi={editorApi} editorEvents={editorEvents} ></Infoview>
</React.StrictMode>)

return infoviewApi;
}

0 comments on commit 64a1a3e

Please sign in to comment.