Skip to content

Commit

Permalink
autosave on jeditor
Browse files Browse the repository at this point in the history
  • Loading branch information
bandinopla committed Jul 21, 2024
1 parent 27b42ea commit 4e2b5bd
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 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.0 : 2024-07-21
+ Added an auto-save mechanic on the log's editor. As pointed by @jimr1603 sometimes the browser will refresh the page if you leave it on too long and all logged data will be lost. Now, the editor will save whatever you type and if you open it again it will detect previously autosaved data and ask you if you want to use that.

- 2.29.1 : 2024-04-28
* bug fix in backend: thanks to @Kandrase for pointing this out. Some journals failed to load due to trying to access an undefined property (Cannot read property)

Expand Down
24 changes: 20 additions & 4 deletions src/codemirror/LogTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ const __lintEditor = {
}
};

export const LogTextEditor = ({ usekg, exercises, tags, value, getDocRef, getShowErrorRef, defaultYMD, utags, hintTriggerRef })=> {
export const LogTextEditor = ({ usekg, exercises, tags, value, getDocRef, getShowErrorRef, defaultYMD, utags, hintTriggerRef, onCodeMirrorReady, valueAsTextHook })=> {

const classes = useStyles();
const txt = useRef();
Expand All @@ -353,14 +353,28 @@ export const LogTextEditor = ({ usekg, exercises, tags, value, getDocRef, getSho
*/
const valueAsText = useMemo( ()=>{

let txt = "";

if( typeof value == 'string' )
{
return value;
txt = value;
}
else if( typeof value == 'object')
{
txt = __convertJEditorDataToText(value, usekg, utags);
}

if( !value ) return "";
if( valueAsTextHook )
{
let txtHooked = valueAsTextHook(txt);

if( txtHooked )
{
return txtHooked;
}
}

return __convertJEditorDataToText(value, usekg, utags);
return txt;

}, [value] );

Expand Down Expand Up @@ -433,6 +447,8 @@ export const LogTextEditor = ({ usekg, exercises, tags, value, getDocRef, getSho

myCodeMirror.focus();

onCodeMirrorReady && onCodeMirrorReady(myCodeMirror)

//
// on dismount...
//
Expand Down
56 changes: 56 additions & 0 deletions src/componentes/journal/editor-autosave.ts
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;
}
}

}
15 changes: 15 additions & 0 deletions src/componentes/journal/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { OpenConfirmModal } from "../Dialog";
import { useGetSession } from "../../session/session-handler";
import { OpenJeditorSaveBackdrop } from "./editor-save-backdrop";
import LoadCopyOfWorkoutModal from "./editor-copy-journal";
import { useEditorAutosave } from "./editor-autosave";

const $jeditorError = makeVar();

Expand All @@ -34,6 +35,10 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo
const saveError = useReactiveVar($jeditorError);
const [jeditorData, setJeditorData] = useState();

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

const [saveEditor, {client}] = useSaveJEditorMutation();

const { data, loading, error, refetch } = useGetJEditorDataQuery({
Expand Down Expand Up @@ -263,6 +268,16 @@ export const JEditor = ({ ymd, range, onClose, saveTrigger, hintTriggerRef, onLo
getShowErrorRef={showDocError}
hintTriggerRef={hintTriggerRef}
utags={jeditorData.jeditor.utags}
onCodeMirrorReady = { cm =>cm.on("change", ()=>autosave(cm.getValue()) ) }
valueAsTextHook = { value => {

let saved = getAutosavedText();

if( saved!=="" && saved!==value && window.confirm("An autosaved text is available. Would you like to load it?"))
{
return saved;
}
}}
/>
{ saveError && <Alert severity="error">{parseError(saveError)}</Alert> }
</>
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":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"}

0 comments on commit 4e2b5bd

Please sign in to comment.