Skip to content

Commit 34da94b

Browse files
committed
autosave popup fix
1 parent 2f8946d commit 34da94b

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

public/changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- 2.30.6 : 2024-08-20
2+
* 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.
3+
14
- 2.30.5 : 2024-08-12
25
* Improved the banners using Flux(pro) AI
36

src/componentes/journal/editor-autosave.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ export type AutoSaveConfig = {
77
export type AutoSaveReturn = {
88
config:AutoSaveConfig,
99
autosave: (text:string)=>void,
10-
getAutosavedText: ()=>string
10+
getAutosavedText: ()=>string,
11+
clear: ( stopAutosaving:boolean )=>void
1112
}
1213

14+
class LocalStoragePolyfill {
15+
setItem = (key: string, value: string) => localStorage?.setItem(key, value);
16+
getItem = (key: string) => localStorage?.getItem(key) ?? null;
17+
removeItem = (key: string) => localStorage?.removeItem(key);
18+
clear = ( ) => localStorage?.clear();
19+
}
20+
21+
const $lstorage = new LocalStoragePolyfill();
22+
1323
export function useEditorAutosave( config:AutoSaveConfig ):AutoSaveReturn {
1424

1525
let interval = useRef<number>()
@@ -29,32 +39,27 @@ export function useEditorAutosave( config:AutoSaveConfig ):AutoSaveReturn {
2939
clearTimeout( interval.current );
3040

3141
interval.current = window.setTimeout(()=>{
32-
try
33-
{
34-
localStorage.setItem( config.cacheKey, text );
35-
}
36-
catch(error) {
37-
38-
// if (error instanceof DOMException && error.name === 'QuotaExceededError') {
39-
// console.error('LocalStorage quota exceeded');
40-
// } else {
41-
// console.error('An error occurred while accessing localStorage', error);
42-
// }
43-
44-
}
42+
$lstorage.setItem(config.cacheKey, text);
4543

4644
}, 1000 );
4745

4846
},
4947
getAutosavedText: ()=> {
50-
let text = localStorage.getItem( config.cacheKey ) || ""
48+
let text = $lstorage.getItem( config.cacheKey ) || ""
5149
//localStorage.removeItem( config.cacheKey );
5250
let defaultPattern = /^\d{4}-\d{2}-\d{2}\n@ *\d+( *bw)?\s*$/i;
5351
if( defaultPattern.test(text) )
5452
{
5553
return ""; //ignore...
5654
}
5755
return text;
56+
},
57+
clear: ( stopAutosaving :boolean )=>{
58+
$lstorage.removeItem(config.cacheKey)
59+
if( stopAutosaving )
60+
{
61+
clearTimeout( interval.current );
62+
}
5863
}
5964
}
6065

src/componentes/journal/editor.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo
3535
const saveError = useReactiveVar($jeditorError);
3636
const [jeditorData, setJeditorData] = useState();
3737

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

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

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

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

6871
}, []);
6972

@@ -169,7 +172,9 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo
169172
throw new Error("Unexpected error...");
170173
}
171174
else
172-
{
175+
{
176+
clearAutosave(true); //if everything was saved, clear it since it is already saved...
177+
173178
// full reload...
174179
setTimeout( ()=> window.open( "/journal/"+session.user.uname+"/"+__ymd , "_self"), 2500 );
175180
;

src/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"buildMajor":"2","buildMinor":30,"buildRevision":5,"buildTag":"RELEASE","when":"Mon, 12 Aug 2024 18:20:37 GMT"}
1+
{"buildMajor":"2","buildMinor":30,"buildRevision":6,"buildTag":"RELEASE","when":"Tue, 20 Aug 2024 16:12:35 GMT"}

0 commit comments

Comments
 (0)