mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	frontend sync through websocket
This commit is contained in:
		
							
								
								
									
										6
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							| @@ -8990,9 +8990,9 @@ | |||||||
|       } |       } | ||||||
|     }, |     }, | ||||||
|     "semver": { |     "semver": { | ||||||
|       "version": "7.1.1", |       "version": "7.1.2", | ||||||
|       "resolved": "https://registry.npmjs.org/semver/-/semver-7.1.1.tgz", |       "resolved": "https://registry.npmjs.org/semver/-/semver-7.1.2.tgz", | ||||||
|       "integrity": "sha512-WfuG+fl6eh3eZ2qAf6goB7nhiCd7NPXhmyFxigB/TOkQyeLP8w8GsVehvtGNtnNmyboz4TgeK40B1Kbql/8c5A==" |       "integrity": "sha512-BJs9T/H8sEVHbeigqzIEo57Iu/3DG6c4QoqTfbQB3BPA4zgzAomh/Fk9E7QtjWQ8mx2dgA9YCfSF4y9k9bHNpQ==" | ||||||
|     }, |     }, | ||||||
|     "semver-compare": { |     "semver-compare": { | ||||||
|       "version": "1.0.0", |       "version": "1.0.0", | ||||||
|   | |||||||
| @@ -62,7 +62,7 @@ | |||||||
|     "rimraf": "3.0.1", |     "rimraf": "3.0.1", | ||||||
|     "sanitize-filename": "1.6.3", |     "sanitize-filename": "1.6.3", | ||||||
|     "sax": "1.2.4", |     "sax": "1.2.4", | ||||||
|     "semver": "7.1.1", |     "semver": "7.1.2", | ||||||
|     "serve-favicon": "2.5.0", |     "serve-favicon": "2.5.0", | ||||||
|     "session-file-store": "1.4.0", |     "session-file-store": "1.4.0", | ||||||
|     "simple-node-logger": "18.12.24", |     "simple-node-logger": "18.12.24", | ||||||
|   | |||||||
| @@ -13,9 +13,8 @@ class NoteShort { | |||||||
|     /** |     /** | ||||||
|      * @param {TreeCache} treeCache |      * @param {TreeCache} treeCache | ||||||
|      * @param {Object.<string, Object>} row |      * @param {Object.<string, Object>} row | ||||||
|      * @param {Branch[]} branches - all relevant branches, i.e. where this note is either child or parent |  | ||||||
|      */ |      */ | ||||||
|     constructor(treeCache, row, branches) { |     constructor(treeCache, row) { | ||||||
|         this.treeCache = treeCache; |         this.treeCache = treeCache; | ||||||
|  |  | ||||||
|         /** @type {string[]} */ |         /** @type {string[]} */ | ||||||
| @@ -35,10 +34,10 @@ class NoteShort { | |||||||
|         /** @type {Object.<string, string>} */ |         /** @type {Object.<string, string>} */ | ||||||
|         this.childToBranch = {}; |         this.childToBranch = {}; | ||||||
|  |  | ||||||
|         this.update(row, branches); |         this.update(row); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     update(row, branches = []) { |     update(row) { | ||||||
|         /** @param {string} */ |         /** @param {string} */ | ||||||
|         this.noteId = row.noteId; |         this.noteId = row.noteId; | ||||||
|         /** @param {string} */ |         /** @param {string} */ | ||||||
| @@ -53,20 +52,6 @@ class NoteShort { | |||||||
|         this.mime = row.mime; |         this.mime = row.mime; | ||||||
|         /** @param {boolean} */ |         /** @param {boolean} */ | ||||||
|         this.isDeleted = row.isDeleted; |         this.isDeleted = row.isDeleted; | ||||||
|  |  | ||||||
|         for (const branch of branches) { |  | ||||||
|             if (this.noteId === branch.noteId) { |  | ||||||
|                 this.parents.push(branch.parentNoteId); |  | ||||||
|                 this.parentToBranch[branch.parentNoteId] = branch.branchId; |  | ||||||
|             } |  | ||||||
|             else if (this.noteId === branch.parentNoteId) { |  | ||||||
|                 this.children.push(branch.noteId); |  | ||||||
|                 this.childToBranch[branch.noteId] = branch.branchId; |  | ||||||
|             } |  | ||||||
|             else { |  | ||||||
|                 throw new Error(`Unknown branch ${branch.branchId} for note ${this.noteId}`); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     addParent(parentNoteId, branchId) { |     addParent(parentNoteId, branchId) { | ||||||
|   | |||||||
| @@ -35,18 +35,6 @@ class TreeCache { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     addResp(noteRows, branchRows, attributeRows) { |     addResp(noteRows, branchRows, attributeRows) { | ||||||
|         const branchesByNotes = {}; |  | ||||||
|  |  | ||||||
|         for (const branchRow of branchRows) { |  | ||||||
|             const branch = new Branch(this, branchRow); |  | ||||||
|  |  | ||||||
|             branchesByNotes[branch.noteId] = branchesByNotes[branch.noteId] || []; |  | ||||||
|             branchesByNotes[branch.noteId].push(branch); |  | ||||||
|  |  | ||||||
|             branchesByNotes[branch.parentNoteId] = branchesByNotes[branch.parentNoteId] || []; |  | ||||||
|             branchesByNotes[branch.parentNoteId].push(branch); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         for (const noteRow of noteRows) { |         for (const noteRow of noteRows) { | ||||||
|             const {noteId} = noteRow; |             const {noteId} = noteRow; | ||||||
|  |  | ||||||
| @@ -76,28 +64,26 @@ class TreeCache { | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             for (const branch of branchesByNotes[noteId] || []) { // can be empty for deleted notes |             const note = new NoteShort(this, noteRow); | ||||||
|                 this.branches[branch.branchId] = branch; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             const note = new NoteShort(this, noteRow, branchesByNotes[noteId] || []); |  | ||||||
|  |  | ||||||
|             this.notes[note.noteId] = note; |             this.notes[note.noteId] = note; | ||||||
|  |         } | ||||||
|  |  | ||||||
|             for (const childNoteId of note.children) { |         for (const branchRow of branchRows) { | ||||||
|                 const childNote = this.notes[childNoteId]; |             const branch = new Branch(this, branchRow); | ||||||
|  |  | ||||||
|  |             this.branches[branch.branchId] = branch; | ||||||
|  |  | ||||||
|  |             const childNote = this.notes[branch.noteId]; | ||||||
|  |  | ||||||
|             if (childNote) { |             if (childNote) { | ||||||
|                     childNote.addParent(noteId, note.childToBranch[childNoteId]); |                 childNote.addParent(branch.parentNoteId, branch.branchId); | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             for (const parentNoteId of note.parents) { |             const parentNote = this.notes[branch.parentNoteId]; | ||||||
|                 const parentNote = this.notes[parentNoteId]; |  | ||||||
|  |  | ||||||
|             if (parentNote) { |             if (parentNote) { | ||||||
|                     parentNote.addChild(noteId, note.parentToBranch[parentNoteId]); |                 parentNote.addChild(branch.noteId, branch.branchId); | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -244,11 +230,50 @@ class TreeCache { | |||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => { |         syncRows.filter(sync => sync.entityName === 'branches').forEach(sync => { | ||||||
|             const branch = this.branches[sync.entityId]; |             let branch = this.branches[sync.entityId]; | ||||||
|  |             const childNote = this.notes[sync.entity.noteId]; | ||||||
|  |             const parentNote = this.notes[sync.entity.parentNoteId]; | ||||||
|  |  | ||||||
|             if (branch) { |             if (branch) { | ||||||
|  |                 if (sync.entity.isDeleted) { | ||||||
|  |                     if (childNote) { | ||||||
|  |                         childNote.parents = childNote.parents.filter(parentNoteId => parentNoteId !== sync.entity.parentNoteId); | ||||||
|  |                         delete childNote.parentToBranch[sync.entity.parentNoteId]; | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     if (parentNote) { | ||||||
|  |                         parentNote.children = parentNote.children.filter(childNoteId => childNoteId !== sync.entity.noteId); | ||||||
|  |                         delete parentNote.childToBranch[sync.entity.noteId]; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 else { | ||||||
|                     branch.update(sync.entity); |                     branch.update(sync.entity); | ||||||
|                     loadResults.addBranch(sync.entityId, sync.sourceId); |                     loadResults.addBranch(sync.entityId, sync.sourceId); | ||||||
|  |  | ||||||
|  |                     if (childNote) { | ||||||
|  |                         childNote.addParent(branch.parentNoteId, branch.branchId); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     if (parentNote) { | ||||||
|  |                         parentNote.addChild(branch.noteId, branch.branchId); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else if (!sync.entity.isDeleted) { | ||||||
|  |                 if (childNote || parentNote) { | ||||||
|  |                     branch = new Branch(this, sync.entity); | ||||||
|  |                     this.branches[branch.branchId] = branch; | ||||||
|  |  | ||||||
|  |                     loadResults.addBranch(sync.entityId, sync.sourceId); | ||||||
|  |  | ||||||
|  |                     if (childNote) { | ||||||
|  |                         childNote.addParent(branch.parentNoteId, branch.branchId); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     if (parentNote) { | ||||||
|  |                         parentNote.addChild(branch.noteId, branch.branchId); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
| @@ -266,11 +291,40 @@ class TreeCache { | |||||||
|  |  | ||||||
|         // missing reloading the relation target note |         // missing reloading the relation target note | ||||||
|         syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => { |         syncRows.filter(sync => sync.entityName === 'attributes').forEach(sync => { | ||||||
|             const attribute = this.attributes[sync.entityId]; |             let attribute = this.attributes[sync.entityId]; | ||||||
|  |             const sourceNote = this.notes[sync.entity.noteId]; | ||||||
|  |             const targetNote = sync.entity.type === 'relation' && this.notes[sync.entity.value]; | ||||||
|  |  | ||||||
|             if (attribute) { |             if (attribute) { | ||||||
|                 attribute.update(sync.entity); |                 attribute.update(sync.entity); | ||||||
|                 loadResults.addAttribute(sync.entityId, sync.sourceId); |                 loadResults.addAttribute(sync.entityId, sync.sourceId); | ||||||
|  |  | ||||||
|  |                 if (sync.entity.isDeleted) { | ||||||
|  |                     if (sourceNote) { | ||||||
|  |                         sourceNote.attributes = sourceNote.attributes.filter(attributeId => attributeId !== attribute.attributeId); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     if (targetNote) { | ||||||
|  |                         targetNote.targetRelations = targetNote.targetRelations.filter(attributeId => attributeId !== attribute.value); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             else if (!sync.entity.isDeleted) { | ||||||
|  |                 if (sourceNote || targetNote) { | ||||||
|  |                     attribute = new Attribute(this, sync.entity); | ||||||
|  |  | ||||||
|  |                     this.attributes[attribute.attributeId] = attribute; | ||||||
|  |  | ||||||
|  |                     loadResults.addAttribute(sync.entityId, sync.sourceId); | ||||||
|  |  | ||||||
|  |                     if (sourceNote && !sourceNote.attributes.includes(attribute.attributeId)) { | ||||||
|  |                         sourceNote.attributes.push(attribute.attributeId); | ||||||
|  |                     } | ||||||
|  |  | ||||||
|  |                     if (targetNote && !targetNote.attributes.includes(attribute.attributeId)) { | ||||||
|  |                         targetNote.attributes.push(attribute.attributeId); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -89,11 +89,7 @@ class AttributesWidget extends StandardWidget { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     entitiesReloadedListener({loadResults}) { |     entitiesReloadedListener({loadResults}) { | ||||||
|         console.log("CHECK ATTRS", loadResults.getAttributes()); |  | ||||||
|  |  | ||||||
|         if (loadResults.getAttributes().find(attr => attr.noteId === this.noteId)) { |         if (loadResults.getAttributes().find(attr => attr.noteId === this.noteId)) { | ||||||
|             console.log("JAAAJ"); |  | ||||||
|  |  | ||||||
|             this.refresh(); |             this.refresh(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user