make sure nothing breaks on fresh empty note or note that has already content

This commit is contained in:
Tom 2022-04-10 16:04:32 +02:00
parent 7f01032b6d
commit f1c9dda366

View File

@ -41,6 +41,9 @@ const TPL = `
* FIXME: FONTS from unpkg.com are loaded. Change font to HELVETICA?
* See: https://www.npmjs.com/package/@excalidraw/excalidraw => FONT_FAMILY
*/
/**
* FIXME: somehow the files array keeps increasing. Somehow the files from one note are transferred to another?
*/
/**
* Discussion?: add complete @excalidraw/excalidraw, utils, react, react-dom as library? maybe also node_modules?
*/
@ -130,9 +133,9 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
// see if note changed, since we do not get a new class for a new note
this.log("doRefresh note KKKK", this.currentNoteId, note.noteId);
const noteChanged = this.currentNoteId !== note.noteId;
// reset scene to omit unnecessary onchange handler
if (noteChanged) {
this.log("doRefresh resetCurrentSceneVersion = -1");
// reset scene to omit unnecessary onchange handler
this.currentSceneVersion = -1;
}
this.currentNoteId = note.noteId;
@ -149,8 +152,18 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
await sleep(200);
}
if (this.excalidrawRef.current && noteComplement.content) {
/**
* new and empty note
*/
if (this.excalidrawRef.current && noteComplement.content === "") {
}
/**
* load saved content into excalidraw canvas
*/
else if (this.excalidrawRef.current && noteComplement.content) {
try {
const content = JSON.parse(noteComplement.content || "");
const {elements, appState, files} = content;
/**
@ -190,13 +203,16 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
this.excalidrawRef.current.updateScene(sceneData);
this.excalidrawRef.current.addFiles(fileArray);
} catch(err) {
console.error("Error (note, noteComplement, err)", note, noteComplement, err);
}
}
// set initial version
// set initial scene version
if (this.currentSceneVersion === -1) {
this.currentSceneVersion = this.getSceneVersion();
}
}
}
/**
* (trilium)
@ -218,7 +234,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
elements,
appState,
files,
time
time,
};
this.log('getContent()', content);
@ -247,15 +263,16 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
// is desired to save? (add)
// upon updateScene, onchange is called, even though "nothing really changed" that is worth saving
const isInitialScene = this.currentSceneVersion === -1;
const isNotInitialScene = this.currentSceneVersion !== -1;
const shouldSave = isNewSceneVersion && !isInitialScene;
const shouldSave = isNewSceneVersion && isNotInitialScene;
if (shouldSave) {
this.updateSceneVersion();
this.saveData();
} else {
// do nothing
this.log("onChangeHandler() no update", isNewSceneVersion, isNotInitialScene);
}
}