Skip to content

Commit

Permalink
Add listener for files from parent window
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Jul 17, 2024
1 parent ad1b50d commit c1e5d88
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,26 @@ export function App({
})();
}, [proxyHandle.ready, currentFiles]);

// Set up the listener for file messages posted from the parent window.
// The useRef is used to ensure that the listener is only set up once.
const fileMessageListenerInitialized = React.useRef(false);

React.useEffect(() => {
if (fileMessageListenerInitialized.current) {
return;
}
fileMessageListenerInitialized.current = true;

window.addEventListener("message", (event) => {
if (event.source !== window.parent) {
return;
}
if (event.data.files) {
setCurrentFiles(event.data.files);
}
});
}, []);

const [utilityMethods, setUtilityMethods] = React.useState<UtilityMethods>({
formatCode: async (code: string) => {
return code;
Expand Down

0 comments on commit c1e5d88

Please sign in to comment.