Skip to content

Commit 915053e

Browse files
committed
update playground URL continuously
1 parent eee4fbd commit 915053e

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

src/Playground.res

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ module ControlPanel = {
11021102
| CopySuccess
11031103

11041104
@react.component
1105-
let make = (~createShareLink: unit => string, ~actionIndicatorKey: string) => {
1105+
let make = (~actionIndicatorKey: string) => {
11061106
let (state, setState) = React.useState(() => Init)
11071107

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

11131113
let onClick = evt => {
11141114
ReactEvent.Mouse.preventDefault(evt)
1115-
let url = createShareLink()
1116-
let ret = copyToClipboard(url)
1115+
let ret = copyToClipboard(Webapi.Window.Location.href)
11171116
if ret {
11181117
setState(_ => CopySuccess)
11191118
}
@@ -1143,46 +1142,22 @@ module ControlPanel = {
11431142
~runOutput,
11441143
~toggleRunOutput,
11451144
) => {
1146-
let router = Next.Router.useRouter()
1147-
11481145
let children = switch state {
11491146
| Init => React.string("Initializing...")
11501147
| SwitchingCompiler(_ready, _version) => React.string("Switching Compiler...")
1151-
| Compiling(ready, _)
1152-
| Ready(ready) =>
1148+
| Compiling(_, _)
1149+
| Ready(_) =>
11531150
let onFormatClick = evt => {
11541151
ReactEvent.Mouse.preventDefault(evt)
11551152
dispatch(Format(editorCode.current))
11561153
}
11571154

1158-
let createShareLink = () => {
1159-
let params = switch ready.targetLang {
1160-
| Res => []
1161-
| lang => [("ext", Api.Lang.toExt(lang))]
1162-
}
1163-
1164-
let version = ready.selected.compilerVersion
1165-
1166-
Array.push(params, ("version", "v" ++ version))->ignore
1167-
1168-
Array.push(
1169-
params,
1170-
("code", editorCode.current->LzString.compressToEncodedURIComponent),
1171-
)->ignore
1172-
1173-
let querystring = params->Array.map(((key, value)) => key ++ "=" ++ value)->Array.join("&")
1174-
1175-
let url = router.route ++ "?" ++ querystring
1176-
Webapi.Window.History.replaceState(null, ~url)
1177-
url
1178-
}
1179-
11801155
<div className="flex flex-row gap-x-2">
11811156
<ToggleButton checked=runOutput onChange={_ => toggleRunOutput()}>
11821157
{React.string("Auto-run")}
11831158
</ToggleButton>
11841159
<Button onClick=onFormatClick> {React.string("Format")} </Button>
1185-
<ShareButton actionIndicatorKey createShareLink />
1160+
<ShareButton actionIndicatorKey />
11861161
</div>
11871162
| _ => React.null
11881163
}

src/bindings/Webapi.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ module Window = {
5555
@scope(("window", "history")) @val
5656
external replaceState: (nullable<'a>, @as(json`""`) _, ~url: string=?) => unit = "replaceState"
5757
}
58+
59+
module Location = {
60+
@scope(("window", "location")) @val external href: string = "href"
61+
}
5862
}
5963

6064
module Fetch = {

src/common/CompilerManagerHook.res

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ type action =
242242
| CompileCode(Lang.t, string)
243243
| UpdateConfig(Config.t)
244244

245+
let createUrl = (pathName, {code, selected: {compilerVersion}, targetLang}) => {
246+
let params = switch targetLang {
247+
| Res => []
248+
| lang => [("ext", RescriptCompilerApi.Lang.toExt(lang))]
249+
}
250+
Array.push(params, ("version", "v" ++ compilerVersion))
251+
Array.push(params, ("code", code->LzString.compressToEncodedURIComponent))
252+
let querystring = params->Array.map(((key, value)) => key ++ "=" ++ value)->Array.join("&")
253+
let url = pathName ++ "?" ++ querystring
254+
url
255+
}
256+
245257
// ~initialLang:
246258
// The target language the compiler should be set to during
247259
// playground initialization. If the compiler doesn't support the language, it
@@ -261,6 +273,7 @@ let useCompilerManager = (
261273
(),
262274
) => {
263275
let (state, setState) = React.useState(_ => Init)
276+
let router = Next.Router.useRouter()
264277

265278
// Dispatch method for the public interface
266279
let dispatch = (action: action): unit => {
@@ -528,8 +541,10 @@ let useCompilerManager = (
528541
}
529542

530543
setState(_ => Ready({...ready, result: FinalResult.Comp(compResult)}))
531-
| SetupFailed(_)
532-
| Ready(_) => ()
544+
| SetupFailed(_) => ()
545+
| Ready(ready) =>
546+
let url = createUrl(router.route, ready)
547+
Webapi.Window.History.replaceState(null, ~url)
533548
}
534549
}
535550

src/common/CompilerManagerHook.resi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ let useCompilerManager: (
6464
~versions: array<Semver.t>,
6565
unit,
6666
) => (state, action => unit)
67+
68+
let createUrl: (string, ready) => string

0 commit comments

Comments
 (0)