@@ -76,6 +76,9 @@ export default function Playground() {
76
76
let [ codeSrc , setCodeSrc ] = useState < string | undefined > ( ) ;
77
77
const [ isEmpty , setIsEmpty ] = useState < boolean > ( true ) ;
78
78
const subdomain = useRef < string > ( crypto . randomUUID ( ) ) ;
79
+ const [ initialContent , setInitialContent ] = useState < EditorContent | null > (
80
+ null
81
+ ) ;
79
82
let { data : initialCode } = useSWRImmutable < EditorContent > (
80
83
! shared && gistId ? `/api/v1/play/${ encodeURIComponent ( gistId ) } ` : null ,
81
84
async ( url ) => {
@@ -105,8 +108,13 @@ export default function Playground() {
105
108
const diaRef = useRef < HTMLDialogElement | null > ( null ) ;
106
109
107
110
useEffect ( ( ) => {
108
- initialCode && store ( SESSION_KEY , initialCode ) ;
109
- } , [ initialCode ] ) ;
111
+ if ( initialCode ) {
112
+ store ( SESSION_KEY , initialCode ) ;
113
+ if ( Object . values ( initialCode ) . some ( Boolean ) ) {
114
+ setInitialContent ( structuredClone ( initialCode ) ) ;
115
+ }
116
+ }
117
+ } , [ initialCode , setInitialContent ] ) ;
110
118
111
119
const getEditorContent = useCallback ( ( ) => {
112
120
const code = {
@@ -181,6 +189,17 @@ export default function Playground() {
181
189
setSearchParams ( [ ] , { replace : true } ) ;
182
190
setCodeSrc ( undefined ) ;
183
191
setEditorContent ( { html : HTML_DEFAULT , css : CSS_DEFAULT , js : JS_DEFAULT } ) ;
192
+ setInitialContent ( null ) ;
193
+
194
+ updateWithEditorContent ( ) ;
195
+ } ;
196
+
197
+ const reset = async ( ) => {
198
+ setEditorContent ( {
199
+ html : initialContent ?. html || HTML_DEFAULT ,
200
+ css : initialContent ?. css || CSS_DEFAULT ,
201
+ js : initialContent ?. js || JS_DEFAULT ,
202
+ } ) ;
184
203
185
204
updateWithEditorContent ( ) ;
186
205
} ;
@@ -192,6 +211,13 @@ export default function Playground() {
192
211
}
193
212
} ;
194
213
214
+ const resetConfirm = async ( ) => {
215
+ if ( window . confirm ( "Do you really want to revert your changes?" ) ) {
216
+ gleanClick ( `${ PLAYGROUND } : revert-click` ) ;
217
+ await reset ( ) ;
218
+ }
219
+ } ;
220
+
195
221
const updateWithEditorContent = ( ) => {
196
222
const { html, css, js } = getEditorContent ( ) ;
197
223
setIsEmpty ( ! html && ! css && ! js ) ;
@@ -314,6 +340,16 @@ export default function Playground() {
314
340
>
315
341
clear
316
342
</ Button >
343
+ { initialContent && (
344
+ < Button
345
+ type = "secondary"
346
+ id = "reset"
347
+ extraClasses = "red"
348
+ onClickHandler = { resetConfirm }
349
+ >
350
+ reset
351
+ </ Button >
352
+ ) }
317
353
</ menu >
318
354
</ aside >
319
355
< Editor
0 commit comments