2022-06-21 23:38:07 +00:00
|
|
|
import { deflate, inflate } from "pako";
|
|
|
|
import { toByteArray, fromByteArray } from "base64-js";
|
2022-04-11 01:48:17 +00:00
|
|
|
import React from "react";
|
|
|
|
import Layout from "@theme/Layout";
|
|
|
|
import { SquigglePlayground } from "../components/SquigglePlayground";
|
|
|
|
|
2022-06-22 19:36:19 +00:00
|
|
|
const HASH_PREFIX = "#code=";
|
2022-06-21 21:09:42 +00:00
|
|
|
function getHashData() {
|
2022-06-22 19:36:19 +00:00
|
|
|
if (typeof window === "undefined") {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const hash = window.location.hash;
|
|
|
|
if (!hash.startsWith(HASH_PREFIX)) {
|
2022-06-21 21:09:42 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
try {
|
2022-06-21 23:38:07 +00:00
|
|
|
const compressed = toByteArray(
|
2022-06-22 19:36:19 +00:00
|
|
|
decodeURIComponent(hash.slice(HASH_PREFIX.length))
|
2022-06-21 23:38:07 +00:00
|
|
|
);
|
|
|
|
const text = inflate(compressed, { to: "string" });
|
|
|
|
return JSON.parse(text);
|
2022-06-21 21:09:42 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 23:38:07 +00:00
|
|
|
function setHashData(data) {
|
|
|
|
const text = JSON.stringify({ ...getHashData(), ...data });
|
|
|
|
const compressed = deflate(text, { level: 9 });
|
2022-06-22 18:19:57 +00:00
|
|
|
window.history.replaceState(
|
|
|
|
undefined,
|
|
|
|
"",
|
2022-06-22 19:36:19 +00:00
|
|
|
HASH_PREFIX + encodeURIComponent(fromByteArray(compressed))
|
2022-06-22 18:19:57 +00:00
|
|
|
);
|
2022-06-21 21:09:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 01:48:17 +00:00
|
|
|
export default function PlaygroundPage() {
|
2022-07-06 15:34:39 +00:00
|
|
|
const hashData = getHashData();
|
|
|
|
if (hashData.initialSquiggleString) {
|
|
|
|
hashData.defaultCode = String(hashData.initialSquiggleString);
|
|
|
|
delete hashData.initialSquiggleString;
|
|
|
|
}
|
2022-06-21 21:09:42 +00:00
|
|
|
const playgroundProps = {
|
2022-06-27 07:07:09 +00:00
|
|
|
defaultCode: "normal(0,1)",
|
2022-06-21 21:09:42 +00:00
|
|
|
height: 700,
|
|
|
|
showTypes: true,
|
2022-07-06 15:34:39 +00:00
|
|
|
...hashData,
|
2022-06-21 21:09:42 +00:00
|
|
|
onCodeChange: (code) => setHashData({ initialSquiggleString: code }),
|
|
|
|
onSettingsChange: (settings) => {
|
|
|
|
const { showTypes, showControls, showSummary, showEditor } = settings;
|
|
|
|
setHashData({ showTypes, showControls, showSummary, showEditor });
|
|
|
|
},
|
|
|
|
};
|
2022-04-11 01:48:17 +00:00
|
|
|
return (
|
|
|
|
<Layout title="Playground" description="Squiggle Playground">
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
maxWidth: 2000,
|
|
|
|
}}
|
|
|
|
>
|
2022-06-21 21:09:42 +00:00
|
|
|
<SquigglePlayground {...playgroundProps} />
|
2022-04-11 01:48:17 +00:00
|
|
|
</div>
|
|
|
|
</Layout>
|
|
|
|
);
|
|
|
|
}
|