-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sandbox web languages using iframes (#268)
Fixes #239 Load web libraries in separate pages and embed them using iframes. This way, these libraries do not pollute the global Window from the session page and there are no collisions between the globals each library define. Incidentally, this also fixes #193: Because the web language libraries are only loaded inside the iframe and thus in a separate JS context, when unloading the Iframe DOM element, sound and visuals stop. Strudel also adds some extensions to the Codemirror editor and has an animation frame to update decorators. For some reason, these updates need to run in the same context as the editor, and can't be modified from within the Iframe page.
- Loading branch information
Showing
16 changed files
with
419 additions
and
255 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,47 @@ | ||
import { useQuery } from "@/hooks/use-query"; | ||
import { EvalMessage, Session } from "@flok-editor/session"; | ||
import { useEffect, useRef } from "react"; | ||
|
||
export interface WebTargetIframeProps { | ||
target: string; | ||
session: Session | null; | ||
} | ||
|
||
export const WebTargetIframe = ({ target, session }: WebTargetIframeProps) => { | ||
const ref = useRef<HTMLIFrameElement | null>(null); | ||
|
||
const query = useQuery(); | ||
const noWebEval = query.get("noWebEval")?.split(",") || []; | ||
|
||
// Check if we should load the target | ||
if (noWebEval.includes(target) || noWebEval.includes("*")) { | ||
return null; | ||
} | ||
|
||
// Handle evaluation messages from session | ||
useEffect(() => { | ||
if (!session || !ref.current) return; | ||
|
||
const handler = (msg: EvalMessage) => { | ||
const payload = { | ||
type: "eval", | ||
body: msg, | ||
}; | ||
ref.current?.contentWindow?.postMessage(payload, "*"); | ||
}; | ||
|
||
session.on(`eval:${target}`, handler); | ||
|
||
return () => { | ||
session.off(`eval:${target}`, handler); | ||
}; | ||
}, [session, ref]); | ||
|
||
return ( | ||
<iframe | ||
ref={ref} | ||
src={`/frames/${target}`} | ||
className="absolute inset-0 w-full h-full" | ||
/> | ||
); | ||
}; |
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,21 @@ | ||
import { useRef, useEffect, useCallback } from "react"; | ||
|
||
export function useAnimationFrame(callback: (timestamp: number) => void) { | ||
const requestId = useRef<number>(); | ||
|
||
const animate = useCallback( | ||
(timestamp: number) => { | ||
callback(timestamp); | ||
requestId.current = requestAnimationFrame(animate); | ||
}, | ||
[callback] | ||
); | ||
|
||
useEffect(() => { | ||
requestId.current = requestAnimationFrame(animate); | ||
|
||
return () => { | ||
cancelAnimationFrame(requestId.current!); | ||
}; | ||
}, [animate]); | ||
} |
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,21 @@ | ||
import { EvalMessage } from "@flok-editor/session"; | ||
import { useEffect } from "react"; | ||
|
||
export function useEvalHandler(callback: (message: EvalMessage) => void) { | ||
useEffect(() => { | ||
const handleEval = (event: MessageEvent) => { | ||
if (event.data.type === "eval") { | ||
const msg = event.data.body as EvalMessage; | ||
callback(msg); | ||
} | ||
}; | ||
|
||
window.addEventListener("message", handleEval); | ||
|
||
return () => { | ||
window.removeEventListener("message", handleEval); | ||
}; | ||
}, [callback]); | ||
|
||
return; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
packages/web/src/hooks/use-strudel-codemirror-extensions.ts
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,31 @@ | ||
import { Session } from "@flok-editor/session"; | ||
import { | ||
highlightMiniLocations, | ||
updateMiniLocations, | ||
} from "@strudel/codemirror"; | ||
import { ReactCodeMirrorRef } from "@uiw/react-codemirror"; | ||
import { useCallback } from "react"; | ||
import { useAnimationFrame } from "./use-animation-frame"; | ||
import { forEachDocumentContext } from "@/lib/utils"; | ||
|
||
export function useStrudelCodemirrorExtensions( | ||
session: Session | null, | ||
editorRefs: React.RefObject<ReactCodeMirrorRef>[] | ||
) { | ||
useAnimationFrame( | ||
useCallback(() => { | ||
if (!session) return; | ||
|
||
forEachDocumentContext( | ||
(ctx, editor) => { | ||
const view = editor?.view; | ||
if (!view) return; | ||
updateMiniLocations(view, ctx.miniLocations || []); | ||
highlightMiniLocations(view, ctx.phase || 0, ctx.haps || []); | ||
}, | ||
session, | ||
editorRefs | ||
); | ||
}, [session, editorRefs]) | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.