File tree Expand file tree Collapse file tree 3 files changed +43
-7
lines changed Expand file tree Collapse file tree 3 files changed +43
-7
lines changed Original file line number Diff line number Diff line change 11
11
"@code-hike/mdx" : " ^0.5.1" ,
12
12
"@mdx-js/mdx" : " ^2.1.1" ,
13
13
"@monaco-editor/react" : " ^4.4.5" ,
14
+ "lz-string" : " ^1.4.4" ,
14
15
"react" : " ^17.0.2" ,
15
16
"react-dom" : " ^17.0.2" ,
16
17
"react-error-boundary" : " ^3.1.4"
Original file line number Diff line number Diff line change 1
- import { useState } from "react" ;
1
+ import React , { useState } from "react" ;
2
2
import { Editor } from "./editor" ;
3
+ import { readHash , writeHash } from "./hash" ;
3
4
import { Preview } from "./preview" ;
4
5
5
6
const defaultCode = `
@@ -14,15 +15,22 @@ print("This is Code Hike")
14
15
15
16
` ;
16
17
17
- const defaultInput = {
18
- mdx : defaultCode ,
19
- css : ".preview-container { border: 1px solid blue; }" ,
20
- config : { lineNumbers : false , showCopyButton : false } ,
21
- } ;
22
-
23
18
function App ( ) {
19
+ const defaultInput = React . useMemo ( ( ) => {
20
+ return (
21
+ readHash ( ) || {
22
+ mdx : defaultCode ,
23
+ css : ".preview-container { border: 1px solid blue; }" ,
24
+ config : { lineNumbers : false , showCopyButton : false } ,
25
+ }
26
+ ) ;
27
+ } , [ ] ) ;
24
28
const [ input , setInput ] = useState ( defaultInput ) ;
25
29
30
+ React . useEffect ( ( ) => {
31
+ writeHash ( input ) ;
32
+ } , [ input ] ) ;
33
+
26
34
return (
27
35
< div className = "app" >
28
36
< header >
Original file line number Diff line number Diff line change
1
+ import LZString from "lz-string" ;
2
+
3
+ export function toHash ( input ) {
4
+ return LZString . compressToEncodedURIComponent ( JSON . stringify ( input ) ) ;
5
+ }
6
+
7
+ export function fromHash ( hash ) {
8
+ return JSON . parse ( LZString . decompressFromEncodedURIComponent ( hash ) ) ;
9
+ }
10
+
11
+ export function writeHash ( input ) {
12
+ const hash = toHash ( input ) ;
13
+ window . location . hash = hash ;
14
+ }
15
+
16
+ export function readHash ( ) {
17
+ const hash = document . location . hash . slice ( 1 ) ;
18
+ if ( ! hash ) {
19
+ return null ;
20
+ }
21
+
22
+ try {
23
+ return fromHash ( hash ) ;
24
+ } catch {
25
+ return { } ;
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments