@@ -83,7 +83,8 @@ window.addEventListener("load", () => {
83
83
localStorage . setItem ( "usedSandbox" , "true" )
84
84
}
85
85
86
- let code = node . textContent
86
+ const codeId = node . firstChild . id
87
+ let code = localStorage . getItem ( codeId ) || node . textContent
87
88
let wrap = node . parentNode . insertBefore ( elt ( "div" , { "class" : "editor-wrap" } ) , node )
88
89
let editor = CodeMirror ( div => wrap . insertBefore ( div , wrap . firstChild ) , {
89
90
value : code ,
@@ -103,6 +104,7 @@ window.addEventListener("load", () => {
103
104
clearTimeout ( pollingScroll )
104
105
pollingScroll = setTimeout ( pollScroll , 500 )
105
106
} )
107
+ editor . on ( "change" , debounce ( ( ) => localStorage . setItem ( codeId , editor . getValue ( ) ) , 250 ) )
106
108
wrap . style . marginLeft = wrap . style . marginRight = - Math . min ( article . offsetLeft , 100 ) + "px"
107
109
setTimeout ( ( ) => editor . refresh ( ) , 600 )
108
110
if ( e ) {
@@ -228,4 +230,12 @@ window.addEventListener("load", () => {
228
230
window . scrollBy ( 0 , newBot - bot )
229
231
}
230
232
}
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
+ }
231
241
} )
0 commit comments