Notes/src/share/shaca/shaca-interface.ts

96 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-04-09 22:39:43 +03:00
import SAttachment = require("./entities/sattachment");
import SAttribute = require("./entities/sattribute");
import SBranch = require("./entities/sbranch");
import SNote = require("./entities/snote");
export default class Shaca {
notes!: Record<string, SNote>;
branches!: Record<string, SBranch>;
childParentToBranch!: Record<string, SBranch>;
private attributes!: Record<string, SAttribute>;
attachments!: Record<string, SAttachment>;
private aliasToNote!: Record<string, SNote>;
private shareRootNote!: SNote | null;
/** true if the index of all shared subtrees is enabled */
private shareIndexEnabled!: boolean;
loaded!: boolean;
2021-10-16 22:13:34 +02:00
constructor() {
this.reset();
}
reset() {
this.notes = {};
this.branches = {};
this.childParentToBranch = {};
this.attributes = {};
2023-06-05 23:05:05 +02:00
this.attachments = {};
2021-12-22 10:57:02 +01:00
this.aliasToNote = {};
2021-10-16 22:13:34 +02:00
this.shareRootNote = null;
this.shareIndexEnabled = false;
2021-10-16 22:13:34 +02:00
this.loaded = false;
}
2024-04-09 22:39:43 +03:00
getNote(noteId: string) {
2021-10-16 22:13:34 +02:00
return this.notes[noteId];
}
2024-04-09 22:39:43 +03:00
hasNote(noteId: string) {
2021-12-22 10:57:02 +01:00
return noteId in this.notes;
}
2024-04-09 22:39:43 +03:00
getNotes(noteIds: string[], ignoreMissing = false) {
2021-10-16 22:13:34 +02:00
const filteredNotes = [];
for (const noteId of noteIds) {
const note = this.notes[noteId];
if (!note) {
if (ignoreMissing) {
continue;
}
throw new Error(`Note '${noteId}' was not found in shaca.`);
2021-10-16 22:13:34 +02:00
}
filteredNotes.push(note);
}
return filteredNotes;
}
2024-04-09 22:39:43 +03:00
getBranch(branchId: string) {
2021-10-16 22:13:34 +02:00
return this.branches[branchId];
}
2024-04-09 22:39:43 +03:00
getBranchFromChildAndParent(childNoteId: string, parentNoteId: string) {
2021-10-16 22:13:34 +02:00
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
}
2024-04-09 22:39:43 +03:00
getAttribute(attributeId: string) {
return this.attributes[attributeId];
}
2024-04-09 22:39:43 +03:00
getAttachment(attachmentId: string) {
2023-06-05 23:05:05 +02:00
return this.attachments[attachmentId];
}
2024-04-09 22:39:43 +03:00
getEntity(entityName: string, entityId: string) {
2021-10-16 22:13:34 +02:00
if (!entityName || !entityId) {
return null;
}
const camelCaseEntityName = entityName.toLowerCase().replace(/(_[a-z])/g,
group =>
group
.toUpperCase()
.replace('_', '')
);
2024-04-09 22:39:43 +03:00
return (this as any)[camelCaseEntityName][entityId];
2021-10-16 22:13:34 +02:00
}
2024-04-09 22:39:43 +03:00
}