@@ -7,22 +7,62 @@ import htm from "../js/htm.js";
77import { init_draw2d } from "./wdk_draw2d.js" ;
88import { calculateMNA , calcBilinear } from "./mna.js" ;
99
10+ //Decode the URL before starting and REACT stuff
11+ // Extract the query parameter
12+
13+ var initialState = {
14+ elements : { } ,
15+ fmin : {
16+ value : 1 ,
17+ unit : "K" ,
18+ } ,
19+ fmax : {
20+ value : 100 ,
21+ unit : "G" ,
22+ } ,
23+ schematic : startupSchematic ,
24+ } ;
25+ var startState = { ...initialState } ;
26+
27+ const urlParams = new URLSearchParams ( window . location . search ) ;
28+ const encodedCompressed = urlParams . get ( "state" ) ;
29+ if ( encodedCompressed ) {
30+ // Decode the Base64 string back into a Uint8Array
31+ const compressedBinary = Uint8Array . from ( atob ( decodeURIComponent ( encodedCompressed ) ) , ( char ) =>
32+ char . charCodeAt ( 0 )
33+ ) ;
34+ const decompressed = pako . inflate ( compressedBinary , { to : "string" } ) ; // Decompress the data using pako
35+ const decodedObject = JSON . parse ( decompressed ) ; // Parse the decompressed JSON string into an object
36+
37+ if ( 'schematic' in decodedObject ) startState . schematic = decodedObject . schematic ;
38+ if ( 'elements' in decodedObject ) startState . elements = decodedObject . elements ;
39+ if ( 'fmin' in decodedObject ) startState . fmin = decodedObject . fmin ;
40+ if ( 'fmax' in decodedObject ) startState . fmax = decodedObject . fmax ;
41+
42+ }
1043const html = htm . bind ( React . createElement ) ;
1144
1245function navBar ( props ) {
1346 return html ` < div className ="w-100 p-3 bg-navy text-white " key ="cont_w100 ">
1447 < div className ="container-xl " key ="cont ">
1548 < div className ="row " key ="r1 ">
1649 < div className ="col " key ="title ">
17- < h4 className ="mb-0 " key ="head "> < strong > ${ props . title } </ strong > </ h4 >
50+ < h4 className ="mb-0 " key ="head "> < strong > < a href =" / " style = ${ { 'color' : '#fff' , 'text-decoration' : 'none' } } > ${ props . title } </ a > </ strong > </ h4 >
1851 </ div >
19- < div className ="col-1 " key ="undo ">
52+ < div className ="col-2 " key ="undo ">
2053 < button type ="button " className ="btn btn-secondary py-0 " title ="undo " onClick =${ ( e ) => props . onClickUndo ( e ) } key ="undoB">
2154 < svg xmlns ="http://www.w3.org/2000/svg " width ="16 " height ="16 " fill ="currentColor " viewBox ="0 0 16 16 " className ="bi ">
2255 < path d ="M8 3a5 5 0 1 1-4.546 2.914.5.5 0 0 0-.908-.417A6 6 0 1 0 8 2v1z "> </ path >
2356 < path d ="M8 4.466V.534a.25.25 0 0 0-.41-.192L5.23 2.308a.25.25 0 0 0 0 .384l2.36 1.966A.25.25 0 0 0 8 4.466z "> </ path >
2457 </ svg >
2558 </ button >
59+ < a href ="/ " style =${ { 'color' :'#fff' , 'text-decoration' :'none' } } >
60+ < button type ="button " className ="btn btn-secondary py-0 ms-2 " title ="restart " key ="undoC ">
61+ < svg xmlns ="http://www.w3.org/2000/svg " width ="16 " height ="16 " fill ="currentColor " class ="bi bi-x-circle " viewBox ="0 0 16 16 ">
62+ < path d ="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16 "/>
63+ < path d ="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708 "/>
64+ </ svg >
65+ </ button > </ a >
2666 </ div >
2767 < div className ="col d-grid d-md-flex justify-content-md-end " key ="navButtons ">
2868 < a className ="btn btn-light py-0 " title ="home " href ="../ " key ="home ">
@@ -627,18 +667,7 @@ class Game extends React.Component {
627667 super ( props ) ;
628668 this . state = {
629669 history : [
630- {
631- elements : { } ,
632- fmin : {
633- value : 1 ,
634- unit : "K" ,
635- } ,
636- fmax : {
637- value : 100 ,
638- unit : "G" ,
639- } ,
640- schematic : startupSchematic ,
641- } ,
670+ { ...startState } ,
642671 ] ,
643672 latex : null ,
644673 iinOrVin : "vin" ,
@@ -969,15 +998,7 @@ class Game extends React.Component {
969998 {
970999 history : this . state . history . concat ( [ current ] ) ,
9711000 } ,
972- // this.redrawSchematic(current)
9731001 ( ) => this . redrawSchematic ( current )
974- // {
975- // this.preventNewState = true;
976- // this.state.elOnSchematic = {};
977- // this.wdk_draw2d.reUpdateCanvas(current.schematic, (b) => this.handleCanvasChange(b));
978- // this.preventNewState = false;
979- // this.calculateTF;
980- // }
9811002 ) ;
9821003
9831004 // this.setState(
@@ -1003,18 +1024,6 @@ class Game extends React.Component {
10031024 this . setState ( {
10041025 history : this . state . history ,
10051026 } ) ;
1006-
1007- // this.setState(
1008- // {
1009- // history: this.state.history.concat([current]),
1010- // },
1011- // // this.redrawSchematic(current)
1012- // () => this.redrawSchematic(current)
1013- // this.preventNewState = true;
1014- // this.state.elOnSchematic = {};
1015- // this.wdk_draw2d.reUpdateCanvas(current.schematic, (b) => this.handleCanvasChange(b));
1016- // this.preventNewState = false;
1017- // this.calculateTF;
10181027 }
10191028 }
10201029
@@ -1024,14 +1033,25 @@ class Game extends React.Component {
10241033
10251034 render ( ) {
10261035 const current = this . state . history [ this . state . history . length - 1 ] ;
1027- // console.log(this.state)
1036+ // console.log("state", this.state)
1037+
1038+ //put the state into the URL
1039+ // Convert the object to a JSON string and encode it
1040+ const jsonString = JSON . stringify ( current ) ;
1041+ const compressed = pako . deflate ( jsonString , { to : "string" } ) ;
1042+ // Encode the compressed data to make it URL-safe
1043+ const encodedCompressed = encodeURIComponent ( btoa ( String . fromCharCode ( ...compressed ) ) ) ;
1044+ const url = new URL ( window . location . href ) ;
1045+ // Use URLSearchParams to set the compressed data
1046+ url . searchParams . set ( "state" , encodedCompressed ) ;
1047+ window . history . pushState ( { } , "" , url . toString ( ) ) ; // Update the browser URL without reloading
10281048
10291049 // // Use state (variable containing all user inputs) to do MNA (modified nodal analysis)
10301050 //
10311051
10321052 // Update the DOM
10331053 return html `
1034- < ${ navBar } title ="ONLINE CIRCUIT SOLVER" key="navBar" onClickUndo=${ ( ) => this . handleUndo ( true ) } />
1054+ < ${ navBar } title ="ONLINE CIRCUIT SOLVER" key="navBar" onClickUndo=${ ( ) => this . handleUndo ( true ) } />
10351055 < ${ Toasts } key ="toasts" toastMxVIsource=${ this . toastMxVIsource } toastCopiedLatex=${ this . toastCopiedLatex } toastCopiedMathML=${ this . toastCopiedMathML } />
10361056 < div className ="w-100 p-2 bg-green " key ="wrapper ">
10371057 < div className ="container-xl " key ="topContainer ">
0 commit comments