squiggle/packages/website/src/pages/playground.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

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";
function getHashData() {
2022-06-21 23:38:07 +00:00
if (typeof window === "undefined" || !window.location.hash) {
return {};
}
try {
2022-06-21 23:38:07 +00:00
const compressed = toByteArray(
decodeURIComponent(window.location.hash.slice(1))
);
const text = inflate(compressed, { to: "string" });
return JSON.parse(text);
} 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 });
window.location.hash = encodeURIComponent(fromByteArray(compressed));
}
2022-04-11 01:48:17 +00:00
export default function PlaygroundPage() {
const playgroundProps = {
initialSquiggleString: "normal(0,1)",
height: 700,
showTypes: true,
...getHashData(),
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,
}}
>
<SquigglePlayground {...playgroundProps} />
2022-04-11 01:48:17 +00:00
</div>
</Layout>
);
}