trilium/spec/search/note_cache_mocking.js

79 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-05-18 04:09:49 +08:00
const Note = require('../../src/becca/entities/note.js');
const Branch = require('../../src/becca/entities/branch.js');
const Attribute = require('../../src/becca/entities/attribute.js');
const becca = require('../../src/becca/becca.js');
2020-05-25 06:25:47 +08:00
const randtoken = require('rand-token').generator({source: 'crypto'});
/** @returns {Note} */
2020-05-25 06:25:47 +08:00
function findNoteByTitle(searchResults, title) {
return searchResults
2021-04-17 05:00:08 +08:00
.map(sr => becca.notes[sr.noteId])
2020-05-25 06:25:47 +08:00
.find(note => note.title === title);
}
class NoteBuilder {
constructor(note) {
this.note = note;
}
label(name, value = '', isInheritable = false) {
2021-05-19 02:56:49 +08:00
new Attribute({
2020-05-25 06:25:47 +08:00
attributeId: id(),
noteId: this.note.noteId,
type: 'label',
isInheritable,
name,
value
});
return this;
}
relation(name, targetNote) {
2021-05-19 02:56:49 +08:00
new Attribute({
2020-05-25 06:25:47 +08:00
attributeId: id(),
noteId: this.note.noteId,
type: 'relation',
name,
value: targetNote.noteId
});
return this;
}
child(childNoteBuilder, prefix = "") {
2021-04-17 05:00:08 +08:00
new Branch(becca, {
2020-05-25 06:25:47 +08:00
branchId: id(),
noteId: childNoteBuilder.note.noteId,
parentNoteId: this.note.noteId,
2020-12-15 06:59:05 +08:00
prefix,
notePosition: 10
2020-05-25 06:25:47 +08:00
});
return this;
}
}
function id() {
return randtoken.generate(10);
}
function note(title, extraParams = {}) {
const row = Object.assign({
noteId: id(),
title: title,
type: 'text',
mime: 'text/html'
}, extraParams);
2021-05-03 03:34:57 +08:00
const note = new Note(row);
2020-05-25 06:25:47 +08:00
return new NoteBuilder(note);
}
module.exports = {
NoteBuilder,
findNoteByTitle,
note
};