From 0e1f7bfd66dca4b239ef05d5e597de33fc68e4e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Rochala?= <48657087+rochala@users.noreply.github.com> Date: Tue, 2 Jan 2024 16:21:34 +0100 Subject: [PATCH] Snippet loading fix (#1017) * Snippet loading should now work correctly * Scala.js should now work after multiple runs if something is exported * Format change --- .../components/Scastie.scala | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/client/src/main/scala/com.olegych.scastie.client/components/Scastie.scala b/client/src/main/scala/com.olegych.scastie.client/components/Scastie.scala index 590237c9e..2982bc82c 100644 --- a/client/src/main/scala/com.olegych.scastie.client/components/Scastie.scala +++ b/client/src/main/scala/com.olegych.scastie.client/components/Scastie.scala @@ -165,6 +165,8 @@ object Scastie { initialState >> backend.loadUser } + val playgroundMainRegex = "let ScastiePlaygroundMain".r + private def executeScalaJs(scastieId: UUID, state: ScastieState): CallbackTo[Unit] = { val scalaJsRunId = "scastie-scalajs-playground-run" @@ -213,7 +215,7 @@ object Scastie { state.snippetState.scalaJsContent.foreach { content => println("== Loading Scala.js! ==") val scalaJsScriptElement = createScript(scalaJsId) - val fixedContent = content.replace("let ScastiePlaygroundMain;", "var ScastiePlaygroundMain;") + val fixedContent = playgroundMainRegex.replaceAllIn(content, "var ScastiePlaygroundMain") val scriptTextNode = dom.document.createTextNode(fixedContent) scalaJsScriptElement.appendChild(scriptTextNode) runScalaJs() @@ -227,14 +229,13 @@ object Scastie { .builder[Scastie]("Scastie") .initialStateFromProps { props => val state = { - val loadedState = - LocalStorage.load.map( - _.copy(isEmbedded = props.isEmbedded, modalState = if (props.isEmbedded) ModalState.allClosed else ModalState.default) - ).getOrElse(ScastieState.default(props.isEmbedded)).copy(inputs = Inputs.default.copy(code = "")) + val scheme = LocalStorage.load.map(_.isDarkTheme) + val loadedState = ScastieState.default(props.isEmbedded) + val loadedStateWithScheme = scheme.map(theme => loadedState.copy(isDarkTheme = theme)).getOrElse(loadedState) if (!props.isEmbedded) { - loadedState + loadedStateWithScheme } else { - loadedState.setCleanInputs.clearOutputs + loadedStateWithScheme.setCleanInputs.clearOutputs } }