mirror of
https://github.com/zadam/trilium.git
synced 2025-02-25 07:25:32 +08:00
script API changes for task management #140
This commit is contained in:
parent
ce5c385c15
commit
3424406ff1
4 changed files with 63 additions and 12 deletions
|
@ -138,28 +138,39 @@ class Note extends Entity {
|
||||||
return label ? label.value : null;
|
return label ? label.value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async toggleLabel(enabled, name, value = "") {
|
||||||
|
if (enabled) {
|
||||||
|
await this.setLabel(name, value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await this.removeLabel(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async setLabel(name, value = "") {
|
async setLabel(name, value = "") {
|
||||||
let label = await this.getLabel(name);
|
const attributes = await this.getOwnedAttributes();
|
||||||
|
let label = attributes.find(attr => attr.type === 'label' && attr.value === value);
|
||||||
|
|
||||||
if (!label) {
|
if (!label) {
|
||||||
label = new Attribute({
|
label = new Attribute({
|
||||||
noteId: this.noteId,
|
noteId: this.noteId,
|
||||||
type: 'label',
|
type: 'label',
|
||||||
name: name
|
name: name,
|
||||||
|
value: value
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
label.value = value;
|
|
||||||
|
|
||||||
await label.save();
|
await label.save();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async removeLabel(name) {
|
async removeLabel(name, value = "") {
|
||||||
const label = await this.getLabel(name);
|
const attributes = await this.getOwnedAttributes();
|
||||||
|
|
||||||
if (label) {
|
for (const attribute of attributes) {
|
||||||
label.isDeleted = true;
|
if (attribute.type === 'label' && (!value || value === attribute.value)) {
|
||||||
await label.save();
|
attribute.isDeleted = true;
|
||||||
|
await attribute.save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,15 @@ async function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function toggleNoteInParent(present, noteId, parentNoteId, prefix) {
|
||||||
|
if (present) {
|
||||||
|
await ensureNoteIsPresentInParent(noteId, parentNoteId, prefix);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await ensureNoteIsAbsentFromParent(noteId, parentNoteId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function cloneNoteAfter(noteId, afterBranchId) {
|
async function cloneNoteAfter(noteId, afterBranchId) {
|
||||||
const afterNote = await treeService.getBranch(afterBranchId);
|
const afterNote = await treeService.getBranch(afterBranchId);
|
||||||
|
|
||||||
|
@ -79,5 +88,6 @@ module.exports = {
|
||||||
cloneNoteToParent,
|
cloneNoteToParent,
|
||||||
ensureNoteIsPresentInParent,
|
ensureNoteIsPresentInParent,
|
||||||
ensureNoteIsAbsentFromParent,
|
ensureNoteIsAbsentFromParent,
|
||||||
|
toggleNoteInParent,
|
||||||
cloneNoteAfter
|
cloneNoteAfter
|
||||||
};
|
};
|
|
@ -59,6 +59,8 @@ function ScriptApi(startNote, currentNote, originEntity) {
|
||||||
this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent;
|
this.ensureNoteIsPresentInParent = cloningService.ensureNoteIsPresentInParent;
|
||||||
this.ensureNoteIsAbsentFromParent = cloningService.ensureNoteIsAbsentFromParent;
|
this.ensureNoteIsAbsentFromParent = cloningService.ensureNoteIsAbsentFromParent;
|
||||||
|
|
||||||
|
this.toggleNoteInParent = cloningService.toggleNoteInParent;
|
||||||
|
|
||||||
this.createNote = noteService.createNote;
|
this.createNote = noteService.createNote;
|
||||||
|
|
||||||
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
|
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);
|
||||||
|
@ -68,6 +70,8 @@ function ScriptApi(startNote, currentNote, originEntity) {
|
||||||
|
|
||||||
this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
|
this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
|
||||||
|
|
||||||
|
this.setNoteToParent = treeService.setNoteToParent;
|
||||||
|
|
||||||
this.transactional = sql.transactional;
|
this.transactional = sql.transactional;
|
||||||
|
|
||||||
this.refreshTree = () => messagingService.sendMessageToAllClients({ type: 'refresh-tree' });
|
this.refreshTree = () => messagingService.sendMessageToAllClients({ type: 'refresh-tree' });
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const sql = require('./sql');
|
const sql = require('./sql');
|
||||||
|
const repository = require('./repository');
|
||||||
|
const Branch = require('../entities/branch');
|
||||||
const syncTableService = require('./sync_table');
|
const syncTableService = require('./sync_table');
|
||||||
const protectedSessionService = require('./protected_session');
|
const protectedSessionService = require('./protected_session');
|
||||||
|
|
||||||
|
@ -99,8 +101,32 @@ async function sortNotesAlphabetically(parentNoteId) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setNoteToParent(noteId, prefix, parentNoteId) {
|
||||||
|
// case where there might be more such branches is ignored. It's expected there should be just one
|
||||||
|
const branch = await repository.getEntity("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
|
||||||
|
|
||||||
|
if (branch) {
|
||||||
|
if (!parentNoteId) {
|
||||||
|
branch.isDeleted = true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
branch.parentNoteId = parentNoteId;
|
||||||
|
}
|
||||||
|
|
||||||
|
await branch.save();
|
||||||
|
}
|
||||||
|
else if (parentNoteId) {
|
||||||
|
await new Branch({
|
||||||
|
noteId: noteId,
|
||||||
|
parentNoteId: parentNoteId,
|
||||||
|
prefix: prefix
|
||||||
|
}).save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validateParentChild,
|
validateParentChild,
|
||||||
getBranch,
|
getBranch,
|
||||||
sortNotesAlphabetically
|
sortNotesAlphabetically,
|
||||||
|
setNoteToParent
|
||||||
};
|
};
|
Loading…
Reference in a new issue