mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 11:02:28 +08:00
make sure nothing breaks on fresh empty note or note that has already content
This commit is contained in:
parent
7f01032b6d
commit
f1c9dda366
@ -41,6 +41,9 @@ const TPL = `
|
|||||||
* FIXME: FONTS from unpkg.com are loaded. Change font to HELVETICA?
|
* FIXME: FONTS from unpkg.com are loaded. Change font to HELVETICA?
|
||||||
* See: https://www.npmjs.com/package/@excalidraw/excalidraw => FONT_FAMILY
|
* 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?
|
* 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
|
// 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);
|
this.log("doRefresh note KKKK", this.currentNoteId, note.noteId);
|
||||||
const noteChanged = this.currentNoteId !== note.noteId;
|
const noteChanged = this.currentNoteId !== note.noteId;
|
||||||
// reset scene to omit unnecessary onchange handler
|
|
||||||
if (noteChanged) {
|
if (noteChanged) {
|
||||||
this.log("doRefresh resetCurrentSceneVersion = -1");
|
this.log("doRefresh resetCurrentSceneVersion = -1");
|
||||||
|
// reset scene to omit unnecessary onchange handler
|
||||||
this.currentSceneVersion = -1;
|
this.currentSceneVersion = -1;
|
||||||
}
|
}
|
||||||
this.currentNoteId = note.noteId;
|
this.currentNoteId = note.noteId;
|
||||||
@ -149,8 +152,18 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
|
|||||||
await sleep(200);
|
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 content = JSON.parse(noteComplement.content || "");
|
||||||
|
|
||||||
const {elements, appState, files} = content;
|
const {elements, appState, files} = content;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -190,13 +203,16 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
|
|||||||
|
|
||||||
this.excalidrawRef.current.updateScene(sceneData);
|
this.excalidrawRef.current.updateScene(sceneData);
|
||||||
this.excalidrawRef.current.addFiles(fileArray);
|
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) {
|
if (this.currentSceneVersion === -1) {
|
||||||
this.currentSceneVersion = this.getSceneVersion();
|
this.currentSceneVersion = this.getSceneVersion();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* (trilium)
|
* (trilium)
|
||||||
@ -218,7 +234,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
|
|||||||
elements,
|
elements,
|
||||||
appState,
|
appState,
|
||||||
files,
|
files,
|
||||||
time
|
time,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.log('getContent()', content);
|
this.log('getContent()', content);
|
||||||
@ -247,15 +263,16 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
|
|||||||
// is desired to save? (add)
|
// is desired to save? (add)
|
||||||
|
|
||||||
// upon updateScene, onchange is called, even though "nothing really changed" that is worth saving
|
// 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) {
|
if (shouldSave) {
|
||||||
this.updateSceneVersion();
|
this.updateSceneVersion();
|
||||||
this.saveData();
|
this.saveData();
|
||||||
} else {
|
} else {
|
||||||
// do nothing
|
// do nothing
|
||||||
|
this.log("onChangeHandler() no update", isNewSceneVersion, isNotInitialScene);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user