Skip to content

Build your personal knowledge base with Trilium Notes

refactoring

zadamae42e0efc77e
diff --git a/src/becca/becca_loader.js b/src/becca/becca_loader.js
index f7659e9647..82dd09e426 100644
--- a/src/becca/becca_loader.js
+++ b/src/becca/becca_loader.js
@@ -69,15 +69,22 @@ function reload() {
require('../services/ws').reloadFrontend();
}
-function postProcessEntityUpdate(entityName, entity) {
+/**
+ * This gets run on entity being created or updated.
+ *
+ * @param entityName
+ * @param entityRow - can be a becca entity (change comes from this trilium instance) or just a row (from sync).
+ * Should be therefore treated as a row.
+ */
+function postProcessEntityUpdate(entityName, entityRow) {
if (entityName === 'notes') {
- noteUpdated(entity);
+ noteUpdated(entityRow);
} else if (entityName === 'branches') {
- branchUpdated(entity);
+ branchUpdated(entityRow);
} else if (entityName === 'attributes') {
- attributeUpdated(entity);
+ attributeUpdated(entityRow);
} else if (entityName === 'note_reordering') {
- noteReorderingUpdated(entity);
+ noteReorderingUpdated(entityRow);
}
}
@@ -163,8 +170,8 @@ function branchDeleted(branchId) {
delete becca.branches[branch.branchId];
}
-function noteUpdated(entity) {
- const note = becca.notes[entity.noteId];
+function noteUpdated(entityRow) {
+ const note = becca.notes[entityRow.noteId];
if (note) {
// type / mime could have been changed, and they are present in flatTextCache
@@ -172,15 +179,15 @@ function noteUpdated(entity) {
}
}
-function branchUpdated(branch) {
- const childNote = becca.notes[branch.noteId];
+function branchUpdated(branchRow) {
+ const childNote = becca.notes[branchRow.noteId];
if (childNote) {
childNote.flatTextCache = null;
childNote.sortParents();
}
- const parentNote = becca.notes[branch.parentNoteId];
+ const parentNote = becca.notes[branchRow.parentNoteId];
if (parentNote) {
parentNote.sortChildren();
@@ -222,8 +229,10 @@ function attributeDeleted(attributeId) {
}
}
-function attributeUpdated(attribute) {
- const note = becca.notes[attribute.noteId];
+/** @param {BAttribute} attributeRow */
+function attributeUpdated(attributeRow) {
+ const attribute = becca.attributes[attributeRow.attributeId];
+ const note = becca.notes[attributeRow.noteId];
if (note) {
if (attribute.isAffectingSubtree || note.isInherited()) {
diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js
index ec24fe08e7..657de1e66b 100644
--- a/src/becca/entities/bnote.js
+++ b/src/becca/entities/bnote.js
@@ -84,7 +84,7 @@ class BNote extends AbstractBeccaEntity {
this.decrypt();
/** @type {string|null} */
- this.flatTextCache = null;
+ this.__flatTextCache = null;
return this;
}
@@ -118,7 +118,7 @@ class BNote extends AbstractBeccaEntity {
/** @type {BNote[]|null}
* @private */
- this.ancestorCache = null;
+ this.__ancestorCache = null;
// following attributes are filled during searching from database
@@ -813,40 +813,40 @@ class BNote extends AbstractBeccaEntity {
* @returns {string} - returns flattened textual representation of note, prefixes and attributes
*/
getFlatText() {
- if (!this.flatTextCache) {
- this.flatTextCache = `${this.noteId} ${this.type} ${this.mime} `;
+ if (!this.__flatTextCache) {
+ this.__flatTextCache = `${this.noteId} ${this.type} ${this.mime} `;
for (const branch of this.parentBranches) {
if (branch.prefix) {
- this.flatTextCache += `${branch.prefix} `;
+ this.__flatTextCache += `${branch.prefix} `;
}
}
- this.flatTextCache += `${this.title} `;
+ this.__flatTextCache += `${this.title} `;
for (const attr of this.getAttributes()) {
// it's best to use space as separator since spaces are filtered from the search string by the tokenization into words
- this.flatTextCache += `${attr.type === 'label' ? '#' : '~'}${attr.name}`;
+ this.__flatTextCache += `${attr.type === 'label' ? '#' : '~'}${attr.name}`;
if (attr.value) {
- this.flatTextCache += `=${attr.value}`;
+ this.__flatTextCache += `=${attr.value}`;
}
- this.flatTextCache += ' ';
+ this.__flatTextCache += ' ';
}
- this.flatTextCache = utils.normalize(this.flatTextCache);
+ this.__flatTextCache = utils.normalize(this.__flatTextCache);
}
- return this.flatTextCache;
+ return this.__flatTextCache;
}
invalidateThisCache() {
- this.flatTextCache = null;
+ this.__flatTextCache = null;
this.__attributeCache = null;
this.__inheritableAttributeCache = null;
- this.ancestorCache = null;
+ this.__ancestorCache = null;
}
invalidateSubTree(path = []) {
@@ -875,24 +875,6 @@ class BNote extends AbstractBeccaEntity {
}
}
- invalidateSubtreeFlatText() {
- this.flatTextCache = null;
-
- for (const childNote of this.children) {
- childNote.invalidateSubtreeFlatText();
- }
-
- for (const targetRelation of this.targetRelations) {
- if (targetRelation.name === 'template' || targetRelation.name === 'inherit') {
- const note = targetRelation.note;
-
- if (note) {
- note.invalidateSubtreeFlatText();
- }
- }
- }
- }
-
getRelationDefinitions() {
return this.getLabels()
.filter(l => l.name.startsWith("relation:"));
@@ -1083,28 +1065,28 @@ class BNote extends AbstractBeccaEntity {
/** @returns {BNote[]} */
getAncestors() {
- if (!this.ancestorCache) {
+ if (!this.__ancestorCache) {
const noteIds = new Set();
- this.ancestorCache = [];
+ this.__ancestorCache = [];
for (const parent of this.parents) {
if (noteIds.has(parent.noteId)) {
continue;
}
- this.ancestorCache.push(parent);
+ this.__ancestorCache.push(parent);
noteIds.add(parent.noteId);
for (const ancestorNote of parent.getAncestors()) {
if (!noteIds.has(ancestorNote.noteId)) {
- this.ancestorCache.push(ancestorNote);
+ this.__ancestorCache.push(ancestorNote);
noteIds.add(ancestorNote.noteId);
}
}
}
}
- return this.ancestorCache;
+ return this.__ancestorCache;
}
/** @returns {boolean} */
@@ -1491,7 +1473,7 @@ class BNote extends AbstractBeccaEntity {
if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {
try {
this.title = protectedSessionService.decryptString(this.title);
- this.flatTextCache = null;
+ this.__flatTextCache = null;
this.isDecrypted = true;
}
diff --git a/src/public/app/widgets/title_bar_buttons.js b/src/public/app/widgets/title_bar_buttons.js
index 6a4f73ad51..89dd436017 100644
--- a/src/public/app/widgets/title_bar_buttons.js
+++ b/src/public/app/widgets/title_bar_buttons.js
@@ -56,13 +56,15 @@ export default class TitleBarButtonsWidget extends BasicWidget {
const $maximizeBtn = this.$widget.find(".maximize-btn");
const $closeBtn = this.$widget.find(".close-btn");
- //When the window is restarted, the window will not be reset when it is set to the top, so get the window status and set the icon background
- (function () {
+ // When the window is restarted, the window will not be reset when it is set to the top,
+ // so get the window status and set the icon background
+ setTimeout(() => {
const remote = utils.dynamicRequire('@electron/remote');
- if (remote.BrowserWindow.getFocusedWindow().isAlwaysOnTop()) {
+ if (remote.BrowserWindow.getFocusedWindow()?.isAlwaysOnTop()) {
$topBtn.addClass('active');
}
- }());
+ }, 1000);
+
$topBtn.on('click', () => {
$topBtn.trigger('blur');
const remote = utils.dynamicRequire('@electron/remote');
diff --git a/src/services/notes.js b/src/services/notes.js
index f3d2191458..c9343bc4e9 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -54,11 +54,10 @@ function deriveMime(type, mime) {
}
function copyChildAttributes(parentNote, childNote) {
- const hasAlreadyTemplate = childNote.hasRelation('template');
-
for (const attr of parentNote.getAttributes()) {
if (attr.name.startsWith("child:")) {
const name = attr.name.substr(6);
+ const hasAlreadyTemplate = childNote.hasRelation('template');
if (hasAlreadyTemplate && attr.type === 'relation' && name === 'template') {
// if the note already has a template, it means the template was chosen by the user explicitly
@@ -174,7 +173,7 @@ function createNewNote(params) {
// TODO: think about what can happen if the note already exists with the forced ID
// I guess on DB it's going to be fine, but becca references between entities
- // might get messed up (two Note instance for the same ID existing in the references)
+ // might get messed up (two note instances for the same ID existing in the references)
note = new BNote({
noteId: params.noteId, // optionally can force specific noteId
title: params.title,
@@ -195,7 +194,7 @@ function createNewNote(params) {
}
finally {
if (!isEntityEventsDisabled) {
- // re-enable entity events only if there were previously enabled
+ // re-enable entity events only if they were previously enabled
// (they can be disabled in case of import)
cls.enableEntityEvents();
}
@@ -215,27 +214,14 @@ function createNewNote(params) {
copyChildAttributes(parentNote, note);
+ eventService.emit(eventService.ENTITY_CREATED, { entityName: 'notes', entity: note });
+ eventService.emit(eventService.ENTITY_CHANGED, { entityName: 'notes', entity: note });
triggerNoteTitleChanged(note);
-
- eventService.emit(eventService.ENTITY_CREATED, {
- entityName: 'notes',
- entity: note
- });
-
- eventService.emit(eventService.ENTITY_CREATED, {
- entityName: 'note_contents',
- entity: note
- });
-
- eventService.emit(eventService.ENTITY_CREATED, {
- entityName: 'branches',
- entity: branch
- });
-
- eventService.emit(eventService.CHILD_NOTE_CREATED, {
- childNote: note,
- parentNote: parentNote
- });
+ // note_contents doesn't use "created" event
+ eventService.emit(eventService.ENTITY_CHANGED, { entityName: 'note_contents', entity: note });
+ eventService.emit(eventService.ENTITY_CREATED, { entityName: 'branches', entity: branch });
+ eventService.emit(eventService.ENTITY_CHANGED, { entityName: 'branches', entity: branch });
+ eventService.emit(eventService.CHILD_NOTE_CREATED, { childNote: note, parentNote: parentNote });
log.info(`Created new note '${note.noteId}', branch '${branch.branchId}' of type '${note.type}', mime '${note.mime}'`);