Skip to content

Commit

Permalink
autosave popup fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bandinopla committed Aug 20, 2024
1 parent 2f8946d commit 34da94b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
3 changes: 3 additions & 0 deletions public/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
- 2.30.6 : 2024-08-20
* Reported by @Duke309 : The autosave popup is appearing unexpectedly. I reviewed the code and made some adjustments trying to limit the reasons why it whould appear and making sure it doesn't in known scenarios.

- 2.30.5 : 2024-08-12
* Improved the banners using Flux(pro) AI

Expand Down
35 changes: 20 additions & 15 deletions src/componentes/journal/editor-autosave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ export type AutoSaveConfig = {
export type AutoSaveReturn = {
config:AutoSaveConfig,
autosave: (text:string)=>void,
getAutosavedText: ()=>string
getAutosavedText: ()=>string,
clear: ( stopAutosaving:boolean )=>void
}

class LocalStoragePolyfill {
setItem = (key: string, value: string) => localStorage?.setItem(key, value);
getItem = (key: string) => localStorage?.getItem(key) ?? null;
removeItem = (key: string) => localStorage?.removeItem(key);
clear = ( ) => localStorage?.clear();
}

const $lstorage = new LocalStoragePolyfill();

export function useEditorAutosave( config:AutoSaveConfig ):AutoSaveReturn {

let interval = useRef<number>()
Expand All @@ -29,32 +39,27 @@ export function useEditorAutosave( config:AutoSaveConfig ):AutoSaveReturn {
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);
// }

}
$lstorage.setItem(config.cacheKey, text);

}, 1000 );

},
getAutosavedText: ()=> {
let text = localStorage.getItem( config.cacheKey ) || ""
let text = $lstorage.getItem( config.cacheKey ) || ""
//localStorage.removeItem( config.cacheKey );
let defaultPattern = /^\d{4}-\d{2}-\d{2}\n@ *\d+( *bw)?\s*$/i;
if( defaultPattern.test(text) )
{
return ""; //ignore...
}
return text;
},
clear: ( stopAutosaving :boolean )=>{
$lstorage.removeItem(config.cacheKey)
if( stopAutosaving )
{
clearTimeout( interval.current );
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/componentes/journal/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo
const saveError = useReactiveVar($jeditorError);
const [jeditorData, setJeditorData] = useState();

const { autosave, getAutosavedText } = useEditorAutosave({
const { autosave, getAutosavedText, clear:clearAutosave } = useEditorAutosave({
cacheKey: `${session.user.id}-autosave`
});

Expand Down Expand Up @@ -63,7 +63,10 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo

window.addEventListener('jeditor:data', onEventData);

return ()=>window.removeEventListener('jeditor:data', onEventData )
return ()=>{
window.removeEventListener('jeditor:data', onEventData )
clearAutosave(); // if the user closes the editor, he knows what he is doing...
}

}, []);

Expand Down Expand Up @@ -169,7 +172,9 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo
throw new Error("Unexpected error...");
}
else
{
{
clearAutosave(true); //if everything was saved, clear it since it is already saved...

// full reload...
setTimeout( ()=> window.open( "/journal/"+session.user.uname+"/"+__ymd , "_self"), 2500 );
;
Expand Down
2 changes: 1 addition & 1 deletion src/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"buildMajor":"2","buildMinor":30,"buildRevision":5,"buildTag":"RELEASE","when":"Mon, 12 Aug 2024 18:20:37 GMT"}
{"buildMajor":"2","buildMinor":30,"buildRevision":6,"buildTag":"RELEASE","when":"Tue, 20 Aug 2024 16:12:35 GMT"}

0 comments on commit 34da94b

Please sign in to comment.