remove magic constants for scene_version and give them a name

This commit is contained in:
Tom 2022-05-09 17:00:24 +02:00
parent 35c4c61d15
commit 06e0f2418c

View File

@ -60,6 +60,10 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
constructor() { constructor() {
super(); super();
// CONSTANTS
this.SCENE_VERSION_INITIAL = -1;
this.SCENE_VERSION_ERROR = -2;
// config // config
this.debounceTimeOnchangeHandler = 750; // ms this.debounceTimeOnchangeHandler = 750; // ms
// ensure that assets are loaded from trilium // ensure that assets are loaded from trilium
@ -67,7 +71,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
// temporary vars // temporary vars
this.currentNoteId = ""; this.currentNoteId = "";
this.currentSceneVersion = -1; this.currentSceneVersion = SCENE_VERSION_INITIAL;
// will be overwritten // will be overwritten
this.excalidrawRef; this.excalidrawRef;
@ -144,7 +148,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
if (noteChanged) { if (noteChanged) {
// this.log("doRefresh resetCurrentSceneVersion = -1"); // this.log("doRefresh resetCurrentSceneVersion = -1");
// reset scene to omit unnecessary onchange handler // reset scene to omit unnecessary onchange handler
this.currentSceneVersion = -1; this.currentSceneVersion = this.SCENE_VERSION_INITIAL;
} }
this.currentNoteId = note.noteId; this.currentNoteId = note.noteId;
@ -226,7 +230,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
} }
// set initial scene version // set initial scene version
if (this.currentSceneVersion === -1) { if (this.currentSceneVersion === this.SCENE_VERSION_INITIAL) {
this.currentSceneVersion = this.getSceneVersion(); this.currentSceneVersion = this.getSceneVersion();
} }
} }
@ -308,7 +312,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
*/ */
// 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 isNotInitialScene = this.currentSceneVersion !== -1; const isNotInitialScene = this.currentSceneVersion !== this.SCENE_VERSION_INITIAL;
const shouldSave = isNewSceneVersion && isNotInitialScene; const shouldSave = isNewSceneVersion && isNotInitialScene;
@ -426,7 +430,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
const sceneVersion = this.getSceneVersion(); const sceneVersion = this.getSceneVersion();
// this.log("isNewSceneVersion()", this.currentSceneVersion, sceneVersion); // this.log("isNewSceneVersion()", this.currentSceneVersion, sceneVersion);
if ( if (
this.currentSceneVersion === -1 // initial scene version update this.currentSceneVersion === this.SCENE_VERSION_INITIAL // initial scene version update
|| this.currentSceneVersion !== sceneVersion || this.currentSceneVersion !== sceneVersion
) { ) {
return true; return true;
@ -441,7 +445,7 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
const sceneVersion = window.Excalidraw.getSceneVersion(elements); const sceneVersion = window.Excalidraw.getSceneVersion(elements);
return sceneVersion; return sceneVersion;
} else { } else {
return -2; return this.SCENE_VERSION_ERROR;
} }
} }