6969</template >
7070
7171<script setup>
72+ import { useClipboard , useStorage } from ' @vueuse/core' ;
7273import { useRoute } from ' vitepress' ;
7374import { onBeforeUnmount , onMounted , ref , watch } from ' vue' ;
7475
@@ -97,8 +98,8 @@ const DEFAULT_VALIDATE = `//Insert data to validate here
9798
9899
99100const route = useRoute ();
100- const schema = ref ( DEFAULT_SCHEMA );
101- const validate = ref ( DEFAULT_VALIDATE );
101+ const schema = useStorage ( ' joi-sandbox-schema ' , DEFAULT_SCHEMA );
102+ const validate = useStorage ( ' joi-sandbox-validate ' , DEFAULT_VALIDATE );
102103const result = ref (' ' );
103104const resultEditorContent = ref (' ' );
104105const errorDetails = ref ([]);
@@ -109,7 +110,7 @@ const version = ref('');
109110const joiInstance = ref (null );
110111const isLoading = ref (false );
111112const latestVersion = ref (joiInfo .versionsArray [0 ]);
112- const isCopied = ref ( false );
113+ const { copy , copied : isCopied } = useClipboard ( );
113114const showResetConfirm = ref (false );
114115
115116
@@ -176,33 +177,12 @@ onMounted(async () => {
176177 validate .value = state .data ;
177178 }
178179 } catch {}
179- } else {
180- // Check if we have saved state in localStorage
181- const savedSchema = localStorage .getItem (' joi-sandbox-schema' );
182- const savedValidate = localStorage .getItem (' joi-sandbox-validate' );
183- if (savedSchema !== null ) {
184- schema .value = savedSchema;
185- }
186- if (savedValidate !== null ) {
187- validate .value = savedValidate;
188- }
189180 }
190181
191182 await loadJoi (v);
192183});
193184
194185
195- onBeforeUnmount (() => {
196- saveState ();
197- });
198-
199-
200- const saveState = () => {
201- localStorage .setItem (' joi-sandbox-schema' , schema .value );
202- localStorage .setItem (' joi-sandbox-validate' , validate .value );
203- };
204-
205-
206186watch (
207187 () => route .path ,
208188 async (newPath ) => {
@@ -211,7 +191,6 @@ watch(
211191 if (testerMatch) {
212192 const v = testerMatch .at (1 );
213193 if (v !== version .value ) {
214- saveState ();
215194 await loadJoi (v);
216195 }
217196 }
@@ -227,10 +206,6 @@ const onValidateClick = () => {
227206 }
228207
229208
230- // Save to localStorage whenever we validate
231- saveState ();
232-
233-
234209 try {
235210 // oxlint-disable-next-line no-new-func
236211 const dataToValidate = new Function (` "use strict";return (${ validate .value } )` )();
@@ -291,8 +266,6 @@ const cancelReset = () => {
291266
292267const confirmReset = () => {
293268 showResetConfirm .value = false ;
294- localStorage .removeItem (' joi-sandbox-schema' );
295- localStorage .removeItem (' joi-sandbox-validate' );
296269
297270
298271 schema .value = DEFAULT_SCHEMA ;
@@ -307,10 +280,6 @@ const confirmReset = () => {
307280
308281
309282const onShareClick = async () => {
310- // Also save to localStorage when sharing
311- saveState ();
312-
313-
314283 const state = JSON .stringify ({
315284 data: validate .value ,
316285 schema: schema .value ,
@@ -321,11 +290,7 @@ const onShareClick = async () => {
321290 const encoded = btoa (unescape (encodeURIComponent (state)));
322291 const url = new URL (window .location .href );
323292 url .hash = ` share=${ encoded} ` ;
324- await navigator .clipboard .writeText (url .toString ());
325- isCopied .value = true ;
326- setTimeout (() => {
327- isCopied .value = false ;
328- }, 2000 );
293+ await copy (url .toString ());
329294 } catch {}
330295};
331296 </script >
0 commit comments