Skip to content
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

Optionen um Code vorabzuladen #17

Merged
merged 2 commits into from
Oct 18, 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
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
41 changes: 41 additions & 0 deletions web/js/codeworld.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,47 @@ program = drawingOf(picture & coordinatePlane)
picture = ...
`);
}

const currentUrl = new URL(window.location);
const searchParams = currentUrl.searchParams;
const codeSrc = searchParams.get("loadSrc");
if(codeSrc) {
const fetchController = new AbortController();
sweetAlert({
title: Alert.title('Loading code'),
text: 'The code is being fetched. Please wait...',
onOpen: () => {
sweetAlert.showLoading();
sweetAlert.getCancelButton().disabled = false;
},
showConfirmButton: false,
showCancelButton: true,
showCloseButton: false,
allowOutsideClick: false,
allowEscapeKey: false,
allowEnterKey: false,
}).then(() => {
fetchController.abort();
});
try {
const response = await fetch(codeSrc, {
signal: fetchController.signal,
});
const code = await response.text();
setCode(code);
sweetAlert.close();
searchParams.delete("loadSrc");
window.history.replaceState(window.history.state, "", currentUrl.toString());
} catch (error) {
sweetAlert(
'Oops!',
'Could not load the code from source. Please try again.',
'error'
);
}
}

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

function initCodeworld() {
Expand Down