refactoring

This commit is contained in:
zadam 2023-06-01 00:07:57 +02:00
parent b5cfc28912
commit ae42e0efc7
4 changed files with 56 additions and 77 deletions

View File

@ -69,15 +69,22 @@ function reload() {
require('../services/ws').reloadFrontend(); 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') { if (entityName === 'notes') {
noteUpdated(entity); noteUpdated(entityRow);
} else if (entityName === 'branches') { } else if (entityName === 'branches') {
branchUpdated(entity); branchUpdated(entityRow);
} else if (entityName === 'attributes') { } else if (entityName === 'attributes') {
attributeUpdated(entity); attributeUpdated(entityRow);
} else if (entityName === 'note_reordering') { } else if (entityName === 'note_reordering') {
noteReorderingUpdated(entity); noteReorderingUpdated(entityRow);
} }
} }
@ -163,8 +170,8 @@ function branchDeleted(branchId) {
delete becca.branches[branch.branchId]; delete becca.branches[branch.branchId];
} }
function noteUpdated(entity) { function noteUpdated(entityRow) {
const note = becca.notes[entity.noteId]; const note = becca.notes[entityRow.noteId];
if (note) { if (note) {
// type / mime could have been changed, and they are present in flatTextCache // type / mime could have been changed, and they are present in flatTextCache
@ -172,15 +179,15 @@ function noteUpdated(entity) {
} }
} }
function branchUpdated(branch) { function branchUpdated(branchRow) {
const childNote = becca.notes[branch.noteId]; const childNote = becca.notes[branchRow.noteId];
if (childNote) { if (childNote) {
childNote.flatTextCache = null; childNote.flatTextCache = null;
childNote.sortParents(); childNote.sortParents();
} }
const parentNote = becca.notes[branch.parentNoteId]; const parentNote = becca.notes[branchRow.parentNoteId];
if (parentNote) { if (parentNote) {
parentNote.sortChildren(); parentNote.sortChildren();
@ -222,8 +229,10 @@ function attributeDeleted(attributeId) {
} }
} }
function attributeUpdated(attribute) { /** @param {BAttribute} attributeRow */
const note = becca.notes[attribute.noteId]; function attributeUpdated(attributeRow) {
const attribute = becca.attributes[attributeRow.attributeId];
const note = becca.notes[attributeRow.noteId];
if (note) { if (note) {
if (attribute.isAffectingSubtree || note.isInherited()) { if (attribute.isAffectingSubtree || note.isInherited()) {

View File

@ -84,7 +84,7 @@ class BNote extends AbstractBeccaEntity {
this.decrypt(); this.decrypt();
/** @type {string|null} */ /** @type {string|null} */
this.flatTextCache = null; this.__flatTextCache = null;
return this; return this;
} }
@ -118,7 +118,7 @@ class BNote extends AbstractBeccaEntity {
/** @type {BNote[]|null} /** @type {BNote[]|null}
* @private */ * @private */
this.ancestorCache = null; this.__ancestorCache = null;
// following attributes are filled during searching from database // 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 * @returns {string} - returns flattened textual representation of note, prefixes and attributes
*/ */
getFlatText() { getFlatText() {
if (!this.flatTextCache) { if (!this.__flatTextCache) {
this.flatTextCache = `${this.noteId} ${this.type} ${this.mime} `; this.__flatTextCache = `${this.noteId} ${this.type} ${this.mime} `;
for (const branch of this.parentBranches) { for (const branch of this.parentBranches) {
if (branch.prefix) { 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()) { 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 // 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) { 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() { invalidateThisCache() {
this.flatTextCache = null; this.__flatTextCache = null;
this.__attributeCache = null; this.__attributeCache = null;
this.__inheritableAttributeCache = null; this.__inheritableAttributeCache = null;
this.ancestorCache = null; this.__ancestorCache = null;
} }
invalidateSubTree(path = []) { 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() { getRelationDefinitions() {
return this.getLabels() return this.getLabels()
.filter(l => l.name.startsWith("relation:")); .filter(l => l.name.startsWith("relation:"));
@ -1083,28 +1065,28 @@ class BNote extends AbstractBeccaEntity {
/** @returns {BNote[]} */ /** @returns {BNote[]} */
getAncestors() { getAncestors() {
if (!this.ancestorCache) { if (!this.__ancestorCache) {
const noteIds = new Set(); const noteIds = new Set();
this.ancestorCache = []; this.__ancestorCache = [];
for (const parent of this.parents) { for (const parent of this.parents) {
if (noteIds.has(parent.noteId)) { if (noteIds.has(parent.noteId)) {
continue; continue;
} }
this.ancestorCache.push(parent); this.__ancestorCache.push(parent);
noteIds.add(parent.noteId); noteIds.add(parent.noteId);
for (const ancestorNote of parent.getAncestors()) { for (const ancestorNote of parent.getAncestors()) {
if (!noteIds.has(ancestorNote.noteId)) { if (!noteIds.has(ancestorNote.noteId)) {
this.ancestorCache.push(ancestorNote); this.__ancestorCache.push(ancestorNote);
noteIds.add(ancestorNote.noteId); noteIds.add(ancestorNote.noteId);
} }
} }
} }
} }
return this.ancestorCache; return this.__ancestorCache;
} }
/** @returns {boolean} */ /** @returns {boolean} */
@ -1491,7 +1473,7 @@ class BNote extends AbstractBeccaEntity {
if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {
try { try {
this.title = protectedSessionService.decryptString(this.title); this.title = protectedSessionService.decryptString(this.title);
this.flatTextCache = null; this.__flatTextCache = null;
this.isDecrypted = true; this.isDecrypted = true;
} }

View File

@ -56,13 +56,15 @@ export default class TitleBarButtonsWidget extends BasicWidget {
const $maximizeBtn = this.$widget.find(".maximize-btn"); const $maximizeBtn = this.$widget.find(".maximize-btn");
const $closeBtn = this.$widget.find(".close-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 // When the window is restarted, the window will not be reset when it is set to the top,
(function () { // so get the window status and set the icon background
setTimeout(() => {
const remote = utils.dynamicRequire('@electron/remote'); const remote = utils.dynamicRequire('@electron/remote');
if (remote.BrowserWindow.getFocusedWindow().isAlwaysOnTop()) { if (remote.BrowserWindow.getFocusedWindow()?.isAlwaysOnTop()) {
$topBtn.addClass('active'); $topBtn.addClass('active');
} }
}()); }, 1000);
$topBtn.on('click', () => { $topBtn.on('click', () => {
$topBtn.trigger('blur'); $topBtn.trigger('blur');
const remote = utils.dynamicRequire('@electron/remote'); const remote = utils.dynamicRequire('@electron/remote');

View File

@ -54,11 +54,10 @@ function deriveMime(type, mime) {
} }
function copyChildAttributes(parentNote, childNote) { function copyChildAttributes(parentNote, childNote) {
const hasAlreadyTemplate = childNote.hasRelation('template');
for (const attr of parentNote.getAttributes()) { for (const attr of parentNote.getAttributes()) {
if (attr.name.startsWith("child:")) { if (attr.name.startsWith("child:")) {
const name = attr.name.substr(6); const name = attr.name.substr(6);
const hasAlreadyTemplate = childNote.hasRelation('template');
if (hasAlreadyTemplate && attr.type === 'relation' && name === '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 // 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 // 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 // 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({ note = new BNote({
noteId: params.noteId, // optionally can force specific noteId noteId: params.noteId, // optionally can force specific noteId
title: params.title, title: params.title,
@ -195,7 +194,7 @@ function createNewNote(params) {
} }
finally { finally {
if (!isEntityEventsDisabled) { 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) // (they can be disabled in case of import)
cls.enableEntityEvents(); cls.enableEntityEvents();
} }
@ -215,27 +214,14 @@ function createNewNote(params) {
copyChildAttributes(parentNote, note); copyChildAttributes(parentNote, note);
eventService.emit(eventService.ENTITY_CREATED, { entityName: 'notes', entity: note });
eventService.emit(eventService.ENTITY_CHANGED, { entityName: 'notes', entity: note });
triggerNoteTitleChanged(note); triggerNoteTitleChanged(note);
// note_contents doesn't use "created" event
eventService.emit(eventService.ENTITY_CREATED, { eventService.emit(eventService.ENTITY_CHANGED, { entityName: 'note_contents', entity: note });
entityName: 'notes', eventService.emit(eventService.ENTITY_CREATED, { entityName: 'branches', entity: branch });
entity: note eventService.emit(eventService.ENTITY_CHANGED, { entityName: 'branches', entity: branch });
}); eventService.emit(eventService.CHILD_NOTE_CREATED, { childNote: note, parentNote: parentNote });
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
});
log.info(`Created new note '${note.noteId}', branch '${branch.branchId}' of type '${note.type}', mime '${note.mime}'`); log.info(`Created new note '${note.noteId}', branch '${branch.branchId}' of type '${note.type}', mime '${note.mime}'`);