Skip to content

Automatically sync playground URL #937

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 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions src/Playground.res
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ module ControlPanel = {
| CopySuccess

@react.component
let make = (~createShareLink: unit => string, ~actionIndicatorKey: string) => {
let make = (~actionIndicatorKey: string) => {
let (state, setState) = React.useState(() => Init)

React.useEffect(() => {
Expand All @@ -1112,8 +1112,7 @@ module ControlPanel = {

let onClick = evt => {
ReactEvent.Mouse.preventDefault(evt)
let url = createShareLink()
let ret = copyToClipboard(url)
let ret = copyToClipboard(Webapi.Window.Location.href)
if ret {
setState(_ => CopySuccess)
}
Expand All @@ -1134,7 +1133,6 @@ module ControlPanel = {
}
}

@val @scope(("window", "location")) external origin: string = "origin"
@react.component
let make = (
~actionIndicatorKey: string,
Expand All @@ -1144,46 +1142,22 @@ module ControlPanel = {
~runOutput,
~toggleRunOutput,
) => {
let router = Next.Router.useRouter()

let children = switch state {
| Init => React.string("Initializing...")
| SwitchingCompiler(_ready, _version) => React.string("Switching Compiler...")
| Compiling(ready, _)
| Ready(ready) =>
| Compiling(_, _)
| Ready(_) =>
let onFormatClick = evt => {
ReactEvent.Mouse.preventDefault(evt)
dispatch(Format(editorCode.current))
}

let createShareLink = () => {
let params = switch ready.targetLang {
| Res => []
| lang => [("ext", Api.Lang.toExt(lang))]
}

let version = ready.selected.compilerVersion

Array.push(params, ("version", "v" ++ version))->ignore

Array.push(
params,
("code", editorCode.current->LzString.compressToEncodedURIComponent),
)->ignore

let querystring = params->Array.map(((key, value)) => key ++ "=" ++ value)->Array.join("&")

let url = origin ++ router.route ++ "?" ++ querystring
Next.Router.replace(router, url)
url
}

<div className="flex flex-row gap-x-2">
<ToggleButton checked=runOutput onChange={_ => toggleRunOutput()}>
{React.string("Auto-run")}
</ToggleButton>
<Button onClick=onFormatClick> {React.string("Format")} </Button>
<ShareButton actionIndicatorKey createShareLink />
<ShareButton actionIndicatorKey />
</div>
| _ => React.null
}
Expand Down Expand Up @@ -1455,6 +1429,8 @@ let make = (~versions: array<string>) => {
| _ => Api.Lang.Res
}

let initialModuleSystem = Dict.get(router.query, "module")

let initialContent = switch (Dict.get(router.query, "code"), initialLang) {
| (Some(compressedCode), _) => LzString.decompressToEncodedURIComponent(compressedCode)
| (None, Reason) => initialReContent
Expand All @@ -1473,6 +1449,7 @@ let make = (~versions: array<string>) => {
let onAction = _ => setActionCount(prev => prev > 1000000 ? 0 : prev + 1)
let (compilerState, compilerDispatch) = useCompilerManager(
~initialVersion?,
~initialModuleSystem?,
~initialLang,
~onAction,
~versions,
Expand Down
135 changes: 0 additions & 135 deletions src/bindings/Next.resi

This file was deleted.

11 changes: 11 additions & 0 deletions src/bindings/Webapi.res
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ module Window = {
@scope("window") @val external innerWidth: int = "innerWidth"
@scope("window") @val external innerHeight: int = "innerHeight"
@scope("window") @val external scrollY: int = "scrollY"

module History = {
@scope(("window", "history")) @val
external pushState: (nullable<'a>, @as(json`""`) _, ~url: string=?) => unit = "pushState"
@scope(("window", "history")) @val
external replaceState: (nullable<'a>, @as(json`""`) _, ~url: string=?) => unit = "replaceState"
}

module Location = {
@scope(("window", "location")) @val external href: string = "href"
}
}

module Fetch = {
Expand Down
31 changes: 27 additions & 4 deletions src/common/CompilerManagerHook.res
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ type action =
| CompileCode(Lang.t, string)
| UpdateConfig(Config.t)

let createUrl = (pathName, ready) => {
let params = switch ready.targetLang {
| Res => []
| lang => [("ext", RescriptCompilerApi.Lang.toExt(lang))]
}
Array.push(params, ("version", "v" ++ ready.selected.compilerVersion))
Array.push(params, ("module", ready.selected.config.module_system))
Array.push(params, ("code", ready.code->LzString.compressToEncodedURIComponent))
let querystring = params->Array.map(((key, value)) => key ++ "=" ++ value)->Array.join("&")
let url = pathName ++ "?" ++ querystring
url
}

let defaultModuleSystem = "esmodule"

// ~initialLang:
// The target language the compiler should be set to during
// playground initialization. If the compiler doesn't support the language, it
Expand All @@ -255,12 +270,14 @@ type action =
// cases where the output didn't visually change)
let useCompilerManager = (
~initialVersion: option<Semver.t>=?,
~initialModuleSystem=defaultModuleSystem,
~initialLang: Lang.t=Res,
~onAction: option<action => unit>=?,
~versions: array<Semver.t>,
(),
) => {
let (state, setState) = React.useState(_ => Init)
let router = Next.Router.useRouter()

// Dispatch method for the public interface
let dispatch = (action: action): unit => {
Expand Down Expand Up @@ -417,7 +434,7 @@ let useCompilerManager = (
// internal compiler state with our playground state.
let config = {
...instance->Compiler.getConfig,
module_system: "esmodule",
module_system: initialModuleSystem,
?open_modules,
}
instance->Compiler.setConfig(config)
Expand Down Expand Up @@ -470,7 +487,11 @@ let useCompilerManager = (
let apiVersion = apiVersion->Version.fromString
let open_modules = getOpenModules(~apiVersion, ~libraries)

let config = {...instance->Compiler.getConfig, ?open_modules}
let config = {
...instance->Compiler.getConfig,
module_system: defaultModuleSystem,
?open_modules,
}
instance->Compiler.setConfig(config)

let selected = {
Expand Down Expand Up @@ -528,8 +549,10 @@ let useCompilerManager = (
}

setState(_ => Ready({...ready, result: FinalResult.Comp(compResult)}))
| SetupFailed(_)
| Ready(_) => ()
| SetupFailed(_) => ()
| Ready(ready) =>
let url = createUrl(router.route, ready)
Webapi.Window.History.replaceState(null, ~url)
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/common/CompilerManagerHook.resi
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ type action =

let useCompilerManager: (
~initialVersion: Semver.t=?,
~initialModuleSystem: string=?,
~initialLang: Lang.t=?,
~onAction: action => unit=?,
~versions: array<Semver.t>,
unit,
) => (state, action => unit)

let createUrl: (string, ready) => string