@@ -83,7 +83,8 @@ window.addEventListener("load", () => {
8383 localStorage . setItem ( "usedSandbox" , "true" )
8484 }
8585
86- let code = node . textContent
86+ const codeId = node . firstChild . id
87+ let code = localStorage . getItem ( codeId ) || node . textContent
8788 let wrap = node . parentNode . insertBefore ( elt ( "div" , { "class" : "editor-wrap" } ) , node )
8889 let editor = CodeMirror ( div => wrap . insertBefore ( div , wrap . firstChild ) , {
8990 value : code ,
@@ -103,6 +104,7 @@ window.addEventListener("load", () => {
103104 clearTimeout ( pollingScroll )
104105 pollingScroll = setTimeout ( pollScroll , 500 )
105106 } )
107+ editor . on ( "change" , debounce ( ( ) => localStorage . setItem ( codeId , editor . getValue ( ) ) , 250 ) )
106108 wrap . style . marginLeft = wrap . style . marginRight = - Math . min ( article . offsetLeft , 100 ) + "px"
107109 setTimeout ( ( ) => editor . refresh ( ) , 600 )
108110 if ( e ) {
@@ -228,4 +230,12 @@ window.addEventListener("load", () => {
228230 window . scrollBy ( 0 , newBot - bot )
229231 }
230232 }
233+
234+ function debounce ( fn , delay = 50 ) {
235+ let timeout
236+ return ( ) => {
237+ if ( timeout ) clearTimeout ( timeout )
238+ timeout = setTimeout ( ( ) => fn . apply ( null , arguments ) , delay )
239+ }
240+ }
231241} )
0 commit comments