Skip to content

Commit f3e4618

Browse files
authored
Merge pull request rescript-lang#58 from reason-association/playground-share
Playground share functionality
2 parents df6f6c2 + 1916b0d commit f3e4618

File tree

6 files changed

+283
-172
lines changed

6 files changed

+283
-172
lines changed

bindings/Bs_platform_api.re

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,22 @@ module Compiler = {
430430
// General format function
431431
let convertSyntax =
432432
(t, ~fromLang: Lang.t, ~toLang: Lang.t, ~code: string)
433-
: ConversionResult.t => {
434-
convertSyntax(t, Lang.toExt(fromLang), Lang.toExt(toLang), code)
435-
->ConversionResult.decode(~fromLang, ~toLang);
436-
};
433+
: ConversionResult.t =>
434+
435+
// TODO: There is an issue where trying to convert an empty Reason code
436+
// to ReScript code would throw an unhandled JSOO exception
437+
// we'd either need to special case the empty Reason code parsing,
438+
// or handle the error on the JSOO bundle side more gracefully
439+
try(
440+
convertSyntax(t, Lang.toExt(fromLang), Lang.toExt(toLang), code)
441+
->ConversionResult.decode(~fromLang, ~toLang)
442+
) {
443+
| Js.Exn.Error(obj) =>
444+
switch (Js.Exn.message(obj)) {
445+
| Some(m) => ConversionResult.UnexpectedError(m)
446+
| None => UnexpectedError("")
447+
}
448+
};
437449
};
438450

439451
[@bs.val] [@bs.scope "bs_platform"]

bindings/Next.re

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ module Router = {
102102
route: string,
103103
asPath: string,
104104
events: Events.t,
105+
query: Js.Dict.t(string),
105106
};
106107

107-
[@bs.send] external push: (router, string ) => unit = "push";
108+
[@bs.send] external push: (router, string) => unit = "push";
108109

109110
[@bs.module "next/router"] external useRouter: unit => router = "useRouter";
111+
[@bs.send] external replace: (router, string) => unit = "replace";
110112
};
111113

112114
module Head = {

common/CompilerManagerHook.re

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type ready = {
142142
type state =
143143
| Init
144144
| SetupFailed(string)
145-
| SwitchingCompiler(ready, string, array(string))
145+
| SwitchingCompiler(ready, string, array(string)) // (ready, targetId, libraries)
146146
| Ready(ready)
147147
| Compiling(ready, (Lang.t, string));
148148

@@ -159,13 +159,19 @@ type action =
159159
| CompileCode(Lang.t, string)
160160
| UpdateConfig(Config.t);
161161

162-
// onAction: This function is especially useful if you want to maintain
163-
// state that depends on any action happening in the compiler, no
164-
// matter if a state transition happened, or not.
165-
// We need that for a ActivityIndicator component to give feedback
166-
// to the user that an action happened (useful in cases where the output
167-
// didn't visually change)
168-
let useCompilerManager = (~onAction: option(action => unit)=?, ()) => {
162+
// ~initialLang:
163+
// The target language the compiler should be set to during
164+
// playground initialization. If the compiler doesn't support the language, it
165+
// will default to ReScript syntax
166+
//
167+
// ~onAction:
168+
// This function is especially useful if you want to maintain state that
169+
// depends on any action happening in the compiler, no matter if a state
170+
// transition happened, or not. We need that for a ActivityIndicator
171+
// component to give feedback to the user that an action happened (useful in
172+
// cases where the output didn't visually change)
173+
let useCompilerManager =
174+
(~initialLang: Lang.t=Res, ~onAction: option(action => unit)=?, ()) => {
169175
let (state, setState) = React.useState(_ => Init);
170176

171177
// Dispatch method for the public interface
@@ -365,10 +371,17 @@ let useCompilerManager = (~onAction: option(action => unit)=?, ()) => {
365371
instance,
366372
};
367373

374+
let targetLang =
375+
Version.availableLanguages(apiVersion)
376+
->Js.Array2.find(l => {l === initialLang})
377+
->Belt.Option.getWithDefault(
378+
Version.defaultTargetLang(apiVersion),
379+
);
380+
368381
setState(_ => {
369382
Ready({
370383
selected,
371-
targetLang: Version.defaultTargetLang(apiVersion),
384+
targetLang,
372385
versions,
373386
errors: [||],
374387
result: FinalResult.Nothing,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"glob": "^7.1.4",
2121
"gray-matter": "^4.0.2",
2222
"highlight.js": "^9.15.10",
23+
"lz-string": "^1.4.4",
2324
"next": "^9.5.1",
2425
"next-transpile-modules": "^2.3.1",
2526
"prettier": "^1.18.2",

0 commit comments

Comments
 (0)