-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27b42ea
commit 4e2b5bd
Showing
5 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useEffect, useRef, useState } from "react" | ||
|
||
export type AutoSaveConfig = { | ||
cacheKey:string | ||
} | ||
|
||
export type AutoSaveReturn = { | ||
config:AutoSaveConfig, | ||
autosave: (text:string)=>void, | ||
getAutosavedText: ()=>string | ||
} | ||
|
||
export function useEditorAutosave( config:AutoSaveConfig ):AutoSaveReturn { | ||
|
||
let interval = useRef<number>() | ||
|
||
useEffect(()=>{ | ||
|
||
return () => { | ||
clearTimeout( interval.current ); | ||
} | ||
|
||
}, []); | ||
|
||
return { | ||
config, | ||
autosave: text => { | ||
|
||
clearTimeout( interval.current ); | ||
|
||
interval.current = window.setTimeout(()=>{ | ||
try | ||
{ | ||
localStorage.setItem( config.cacheKey, text ); | ||
} | ||
catch(error) { | ||
|
||
// if (error instanceof DOMException && error.name === 'QuotaExceededError') { | ||
// console.error('LocalStorage quota exceeded'); | ||
// } else { | ||
// console.error('An error occurred while accessing localStorage', error); | ||
// } | ||
|
||
} | ||
|
||
}, 1000 ); | ||
|
||
}, | ||
getAutosavedText: ()=> { | ||
let text = localStorage.getItem( config.cacheKey ) || "" | ||
//localStorage.removeItem( config.cacheKey ); | ||
return text; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"buildMajor":"2","buildMinor":29,"buildRevision":1,"buildTag":"RELEASE","when":"Sun, 28 Apr 2024 11:41:43 GMT"} | ||
{"buildMajor":"2","buildMinor":30,"buildRevision":0,"buildTag":"RELEASE","when":"Sun, 21 Jul 2024 12:32:10 GMT"} |