Skip to content

Commit

Permalink
add option to load code via POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
nimec01 authored and marcellussiegburg committed Oct 18, 2024
1 parent fb1a373 commit d2305a7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions codeworld-server/codeworld-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Executable codeworld-server
cryptonite,
data-default,
directory,
extra,
fast-logger,
filelock,
filepath,
Expand Down
12 changes: 11 additions & 1 deletion codeworld-server/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import Data.ByteString.Builder (toLazyByteString)
import qualified Data.ByteString.Lazy as LB
import Data.Char (isSpace)
import Data.List
import Data.List.Extra (replace)
import Data.Maybe
import Data.Monoid
import Data.Text (Text)
Expand Down Expand Up @@ -168,7 +169,8 @@ site ctx =
("runJS", runHandler ctx),
("runBaseJS", runBaseHandler ctx),
("runMsg", runMessageHandler ctx),
("haskell", serveFile "web/env.html"),
-- ("haskell", serveFile "web/env.html"),
("haskell", serveEditor ctx),
-- ("blocks", serveFile "web/blocks.html"),
-- ("funblocks", serveFile "web/blocks.html"),
("indent", indentHandler ctx),
Expand Down Expand Up @@ -494,6 +496,14 @@ runMessageHandler = public $ \ctx -> do
modifyResponse $ setContentType "text/plain"
serveFile (buildRootDir mode </> resultFile programId)

serveEditor :: CodeWorldHandler
serveEditor = public $ \ctx -> do
msource <- getParam "source"
modifyResponse $ setContentType "text/html"
template <- liftIO $ readFile "web/env.html"
let content = replace "/*CODE_TO_BE_LOADED_BY_DEFAULT*/" (maybe "" (T.unpack . T.decodeUtf8) msource) template
writeBS $ T.encodeUtf8 $ T.pack content

indentHandler :: CodeWorldHandler
indentHandler = public $ \ctx -> do
mode <- getBuildMode
Expand Down
3 changes: 3 additions & 0 deletions web/env.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
rel="stylesheet"
href="mirrored/ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"
/>
<script>
window.preloadCode = `/*CODE_TO_BE_LOADED_BY_DEFAULT*/`;
</script>
</head>

<body>
Expand Down
2 changes: 2 additions & 0 deletions web/js/codeworld.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ picture = ...
);
}
}

if(window.preloadCode && window.buildMode === 'haskell')setCode(window.preloadCode);
}

function initCodeworld() {
Expand Down

0 comments on commit d2305a7

Please sign in to comment.