2025-02-28 19:17:52 +02:00
|
|
|
import utils from "../services/utils.js";
|
|
|
|
import FNote from "../entities/fnote.js";
|
|
|
|
import froca from "../services/froca.js";
|
|
|
|
import FAttribute from "../entities/fattribute.js";
|
2025-02-28 20:11:45 +02:00
|
|
|
import noteAttributeCache from "../services/note_attribute_cache.js";
|
2025-02-28 19:17:52 +02:00
|
|
|
|
2025-02-28 19:55:02 +02:00
|
|
|
type AttributeDefinitions = { [key in `#${string}`]: string; };
|
|
|
|
type RelationDefinitions = { [key in `~${string}`]: string; };
|
2025-02-28 19:44:32 +02:00
|
|
|
|
2025-02-28 19:55:02 +02:00
|
|
|
interface NoteDefinition extends AttributeDefinitions, RelationDefinitions {
|
2025-02-28 19:44:32 +02:00
|
|
|
id?: string | undefined;
|
2025-02-28 19:17:52 +02:00
|
|
|
title: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the given notes with the given title and optionally one or more attributes.
|
|
|
|
*
|
|
|
|
* For a label to be created, simply pass on a key prefixed with `#` and any desired value.
|
|
|
|
*
|
|
|
|
* The notes and attributes will be injected in the froca.
|
|
|
|
*
|
|
|
|
* @param notes
|
|
|
|
* @returns an array containing the IDs of the created notes.
|
|
|
|
* @example
|
|
|
|
* buildNotes([
|
|
|
|
* { title: "A", "#startDate": "2025-05-05" },
|
|
|
|
* { title: "B", "#startDate": "2025-05-07" }
|
|
|
|
* ]);
|
|
|
|
*/
|
|
|
|
export function buildNotes(notes: NoteDefinition[]) {
|
|
|
|
const ids = [];
|
|
|
|
|
|
|
|
for (const noteDef of notes) {
|
2025-02-28 20:34:30 +02:00
|
|
|
ids.push(buildNote(noteDef).noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildNote(noteDef: NoteDefinition) {
|
|
|
|
const note = new FNote(froca, {
|
|
|
|
noteId: noteDef.id ?? utils.randomString(12),
|
|
|
|
title: noteDef.title,
|
|
|
|
type: "text",
|
|
|
|
mime: "text/html",
|
|
|
|
isProtected: false,
|
|
|
|
blobId: ""
|
|
|
|
});
|
|
|
|
froca.notes[note.noteId] = note;
|
2025-02-28 19:17:52 +02:00
|
|
|
|
2025-02-28 20:34:30 +02:00
|
|
|
let position = 0;
|
|
|
|
for (const [ key, value ] of Object.entries(noteDef)) {
|
|
|
|
const attributeId = utils.randomString(12);
|
|
|
|
const name = key.substring(1);
|
2025-02-28 19:55:02 +02:00
|
|
|
|
2025-02-28 20:34:30 +02:00
|
|
|
let attribute: FAttribute | null = null;
|
|
|
|
if (key.startsWith("#")) {
|
|
|
|
attribute = new FAttribute(froca, {
|
|
|
|
noteId: note.noteId,
|
|
|
|
attributeId,
|
|
|
|
type: "label",
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
position,
|
|
|
|
isInheritable: false
|
|
|
|
});
|
|
|
|
}
|
2025-02-28 19:55:02 +02:00
|
|
|
|
2025-02-28 20:34:30 +02:00
|
|
|
if (key.startsWith("~")) {
|
|
|
|
attribute = new FAttribute(froca, {
|
|
|
|
noteId: note.noteId,
|
|
|
|
attributeId,
|
|
|
|
type: "relation",
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
position,
|
|
|
|
isInheritable: false
|
|
|
|
});
|
|
|
|
}
|
2025-02-28 19:17:52 +02:00
|
|
|
|
2025-02-28 20:34:30 +02:00
|
|
|
if (!attribute) {
|
|
|
|
continue;
|
|
|
|
}
|
2025-02-28 19:55:02 +02:00
|
|
|
|
2025-02-28 20:34:30 +02:00
|
|
|
froca.attributes[attributeId] = attribute;
|
|
|
|
note.attributes.push(attributeId);
|
|
|
|
position++;
|
2025-02-28 20:11:45 +02:00
|
|
|
|
2025-02-28 20:34:30 +02:00
|
|
|
// Inject the attribute into the note attribute cache, since FNote.getAttribute() doesn't always work.
|
|
|
|
// If we add support for templates into froca, this might cause issues.
|
|
|
|
if (!noteAttributeCache.attributes[note.noteId]) {
|
|
|
|
noteAttributeCache.attributes[note.noteId] = [];
|
2025-02-28 19:17:52 +02:00
|
|
|
}
|
2025-02-28 20:34:30 +02:00
|
|
|
noteAttributeCache.attributes[note.noteId].push(attribute);
|
2025-02-28 19:17:52 +02:00
|
|
|
}
|
2025-02-28 20:34:30 +02:00
|
|
|
return note;
|
2025-02-28 19:17:52 +02:00
|
|
|
}
|