2024-07-18 21:35:17 +03:00
|
|
|
import BNote from "../../src/becca/entities/bnote.js";
|
|
|
|
import BBranch from "../../src/becca/entities/bbranch.js";
|
|
|
|
import BAttribute from "../../src/becca/entities/battribute.js";
|
|
|
|
import becca from "../../src/becca/becca.js";
|
2024-07-18 21:37:45 +03:00
|
|
|
import randtoken from "rand-token";
|
2024-07-18 21:35:17 +03:00
|
|
|
import SearchResult from "../../src/services/search/search_result.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { NoteType } from "../../src/becca/entities/rows.js";
|
2024-05-08 23:59:11 +02:00
|
|
|
randtoken.generator({ source: "crypto" });
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
function findNoteByTitle(searchResults: Array<SearchResult>, title: string): BNote | undefined {
|
|
|
|
return searchResults.map((sr) => becca.notes[sr.noteId]).find((note) => note.title === title);
|
2024-05-08 23:59:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class NoteBuilder {
|
2025-01-09 18:07:02 +02:00
|
|
|
note: BNote;
|
|
|
|
constructor(note: BNote) {
|
|
|
|
this.note = note;
|
|
|
|
}
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
label(name: string, value = "", isInheritable = false) {
|
|
|
|
new BAttribute({
|
|
|
|
attributeId: id(),
|
|
|
|
noteId: this.note.noteId,
|
|
|
|
type: "label",
|
|
|
|
isInheritable,
|
|
|
|
name,
|
|
|
|
value
|
|
|
|
});
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return this;
|
|
|
|
}
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
relation(name: string, targetNote: BNote) {
|
|
|
|
new BAttribute({
|
|
|
|
attributeId: id(),
|
|
|
|
noteId: this.note.noteId,
|
|
|
|
type: "relation",
|
|
|
|
name,
|
|
|
|
value: targetNote.noteId
|
|
|
|
});
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return this;
|
|
|
|
}
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
child(childNoteBuilder: NoteBuilder, prefix = "") {
|
|
|
|
new BBranch({
|
|
|
|
branchId: id(),
|
|
|
|
noteId: childNoteBuilder.note.noteId,
|
|
|
|
parentNoteId: this.note.noteId,
|
|
|
|
prefix,
|
|
|
|
notePosition: 10
|
|
|
|
});
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return this;
|
|
|
|
}
|
2024-05-08 23:59:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function id() {
|
2025-01-09 18:07:02 +02:00
|
|
|
return randtoken.generate(10);
|
2024-05-08 23:59:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function note(title: string, extraParams = {}) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const row = Object.assign(
|
|
|
|
{
|
|
|
|
noteId: id(),
|
|
|
|
title: title,
|
|
|
|
type: "text" as NoteType,
|
|
|
|
mime: "text/html"
|
|
|
|
},
|
|
|
|
extraParams
|
|
|
|
);
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const note = new BNote(row);
|
2024-05-08 23:59:11 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return new NoteBuilder(note);
|
2024-05-08 23:59:11 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2025-01-09 18:07:02 +02:00
|
|
|
NoteBuilder,
|
|
|
|
findNoteByTitle,
|
|
|
|
note
|
2024-05-08 23:59:11 +02:00
|
|
|
};
|