Save playground state in URL, for easy sharing

fix ssr
This commit is contained in:
Recursing 2022-06-21 23:09:42 +02:00
parent f365cdef70
commit 35a6a0c4e5
2 changed files with 38 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import React, { FC, Fragment, useState } from "react"; import React, { FC, Fragment, useState, useEffect } from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import { Path, useForm, UseFormRegister, useWatch } from "react-hook-form"; import { Path, useForm, UseFormRegister, useWatch } from "react-hook-form";
import * as yup from "yup"; import * as yup from "yup";
@ -35,6 +35,7 @@ interface PlaygroundProps {
/** If code is set, component becomes controlled */ /** If code is set, component becomes controlled */
code?: string; code?: string;
onCodeChange?(expr: string): void; onCodeChange?(expr: string): void;
onSettingsChange?(settings: any): void;
/** Should we show the editor? */ /** Should we show the editor? */
showEditor?: boolean; showEditor?: boolean;
} }
@ -205,6 +206,7 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
showSummary = false, showSummary = false,
code: controlledCode, code: controlledCode,
onCodeChange, onCodeChange,
onSettingsChange,
showEditor = true, showEditor = true,
}) => { }) => {
const [uncontrolledCode, setUncontrolledCode] = useState( const [uncontrolledCode, setUncontrolledCode] = useState(
@ -233,6 +235,11 @@ export const SquigglePlayground: FC<PlaygroundProps> = ({
const vars = useWatch({ const vars = useWatch({
control, control,
}); });
useEffect(() => {
onSettingsChange?.(vars);
}, [vars]);
const chartSettings = { const chartSettings = {
start: Number(vars.diagramStart), start: Number(vars.diagramStart),
stop: Number(vars.diagramStop), stop: Number(vars.diagramStop),

View File

@ -2,7 +2,36 @@ import React from "react";
import Layout from "@theme/Layout"; import Layout from "@theme/Layout";
import { SquigglePlayground } from "../components/SquigglePlayground"; import { SquigglePlayground } from "../components/SquigglePlayground";
function getHashData() {
if (typeof window === "undefined") {
return {};
}
try {
return JSON.parse(window.decodeURIComponent(window.location.hash.slice(1)));
} catch (err) {
console.error(err);
return {};
}
}
function setHashData(Data) {
window.location.hash = window.encodeURIComponent(
JSON.stringify({ ...getHashData(), ...Data })
);
}
export default function PlaygroundPage() { 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 });
},
};
return ( return (
<Layout title="Playground" description="Squiggle Playground"> <Layout title="Playground" description="Squiggle Playground">
<div <div
@ -10,11 +39,7 @@ export default function PlaygroundPage() {
maxWidth: 2000, maxWidth: 2000,
}} }}
> >
<SquigglePlayground <SquigglePlayground {...playgroundProps} />
initialSquiggleString="normal(0,1)"
height={700}
showTypes={true}
/>
</div> </div>
</Layout> </Layout>
); );