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 1111 "@code-hike/mdx" : " ^0.5.1" ,
1212 "@mdx-js/mdx" : " ^2.1.1" ,
1313 "@monaco-editor/react" : " ^4.4.5" ,
14+ "lz-string" : " ^1.4.4" ,
1415 "react" : " ^17.0.2" ,
1516 "react-dom" : " ^17.0.2" ,
1617 "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" ;
22import { Editor } from "./editor" ;
3+ import { readHash , writeHash } from "./hash" ;
34import { Preview } from "./preview" ;
45
56const defaultCode = `
@@ -14,15 +15,22 @@ print("This is Code Hike")
1415
1516` ;
1617
17- const defaultInput = {
18- mdx : defaultCode ,
19- css : ".preview-container { border: 1px solid blue; }" ,
20- config : { lineNumbers : false , showCopyButton : false } ,
21- } ;
22-
2318function 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+ } , [ ] ) ;
2428 const [ input , setInput ] = useState ( defaultInput ) ;
2529
30+ React . useEffect ( ( ) => {
31+ writeHash ( input ) ;
32+ } , [ input ] ) ;
33+
2634 return (
2735 < div className = "app" >
2836 < 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