Browse Source

include seed in hash data

Getty Ritter 1 year ago
parent
commit
64058f43e7
2 changed files with 30 additions and 8 deletions
  1. 9 2
      index.html
  2. 21 6
      js/index.js

+ 9 - 2
index.html

@@ -1,6 +1,14 @@
 <!DOCTYPE html>
 <html>
   <head>
+    <title>Matzo Playground</title>
+
+    <meta property="og:title" content="Matzo Playground" />
+    <meta property="og:type" content="website" />
+    <meta property="og:url" content="https://gdritter.com/mtz.html" />
+    <meta property="og:image" content="https://gdritter.com/imgs/matzo.png" />
+
+    <meta charset="utf-8">
     <script type="text/javascript">
      const sources = %SOURCES%;
     </script>
@@ -25,7 +33,6 @@
      }
 
      .all > div {
-       border: 1px solid;
        width: 50%;
        height: 100%;
        max-height: 100%;
@@ -106,7 +113,7 @@
     </div>
     <footer>
       <span>
-        random seed: <input type="text" id="seed" value="500" />
+        random seed: <input type="text" id="seed" value="12345" />
       </span>
       <span>
         <input type="submit" id="run" value="Reroll" />

+ 21 - 6
js/index.js

@@ -15,8 +15,12 @@ window.onload = () => {
 
       const updateCode = () => {
         const code = editor.state.doc.toString();
+        const data = JSON.stringify({
+          'source': code,
+          'seed': Number(seed.value),
+        });
         // update the URL hash
-        location.hash = '#' + encodeURIComponent(code);
+        location.hash = '#' + encodeURIComponent(data);
         // run the code
         updateOutput(code);
 
@@ -44,9 +48,20 @@ window.onload = () => {
 
       if (location.hash) {
         const encoded = location.hash.substring(1);
-        editor.dispatch({
-          changes: {from: 0, insert: decodeURIComponent(encoded)}
-        });
+
+        try {
+          const data = JSON.parse(decodeURIComponent(encoded));
+          editor.dispatch({
+            changes: {from: 0, insert: data.source}
+          });
+          seed.value = Number(data.seed);
+          console.log(`Setting seed value to ${seed.value}`);
+        } catch (exn) {
+          editor.dispatch({
+            changes: {from: 0, insert: sources['simple']}
+          });
+          seed.value = 12345;
+        }
       } else {
         editor.dispatch({
           changes: {from: 0, insert: sources['simple']}
@@ -59,12 +74,12 @@ window.onload = () => {
       };
 
       seed.onchange = () => {
-        updateOutput(editor.state.doc.toString());
+        updateCode();
       }
 
       run.onclick = () => {
         seed.value = Math.floor(Math.random() * 0xffffffff);
-        updateOutput(editor.state.doc.toString());
+        updateCode();
       }
     });
 };