All files / src actions.ts

0% Statements 0/47
0% Branches 0/15
0% Functions 0/7
0% Lines 0/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                                                 
import joplin from "api";
import { NewNote, NOTE_ID_PLACEHOLDER } from "./parser";
import { getSelectedFolder } from "./utils/folders";
import { applyTagToNote, getAnyTagWithTitle } from "./utils/tags";
import { ApplyTagsWhileInsertingSetting, FocusTitleAfterCreateSetting } from "./settings";
 
export enum TemplateAction {
    NewNote = "newNote",
    NewTodo = "newTodo",
    InsertText = "insertText"
}
 
 
const performInsertTextAction = async (template: NewNote) => {
    // When inserting into an existing note, resolve {{ note_id }} to the
    // currently selected note's ID, since there is no "new note" being created.
    const currentNote = await joplin.workspace.selectedNote();
    const body = template.body.includes(NOTE_ID_PLACEHOLDER)
        ? template.body.split(NOTE_ID_PLACEHOLDER).join(currentNote.id)
        : template.body;
 
    await joplin.commands.execute("insertText", body);
 
    const applyTags = await ApplyTagsWhileInsertingSetting.get()
    Iif (applyTags) {
        for (const tag of template.tags) {
            const tagId = (await getAnyTagWithTitle(tag)).id;
            await applyTagToNote(tagId, currentNote.id);
        }
    }
}
 
const performNewNoteAction = async (template: NewNote, isTodo: 0 | 1) => {
    const folderId = template.folder ? template.folder : await getSelectedFolder();
    const notePayload = { body: template.body, parent_id: folderId, title: template.title, is_todo: isTodo };
 
    Iif (isTodo && template.todo_due) {
        notePayload["todo_due"] = template.todo_due;
    }
 
    const note = await joplin.data.post(["notes"], null, notePayload);
 
    // Post-process: replace the note_id placeholder with the actual note ID
    Iif (template.body.includes(NOTE_ID_PLACEHOLDER)) {
        const updatedBody = template.body.split(NOTE_ID_PLACEHOLDER).join(note.id);
        await joplin.data.put(["notes", note.id], null, { body: updatedBody });
    }
 
    await joplin.commands.execute("openNote", note.id);
 
    // Focus title bar if the user has enabled the setting
    const focusTitle = await FocusTitleAfterCreateSetting.get();
    Iif (focusTitle) {
        try {
            await joplin.commands.execute("focusElement", "noteTitle");
        } catch {
            // focusElement may not be available in all Joplin versions — fail silently
        }
    }
 
    for (const tag of template.tags) {
        const tagId = (await getAnyTagWithTitle(tag)).id;
        await applyTagToNote(tagId, note.id);
    }
}
 
 
export const performAction = async (action: TemplateAction, template: NewNote): Promise<void> => {
    switch (action) {
        case TemplateAction.InsertText:
            await performInsertTextAction(template);
            break;
        case TemplateAction.NewNote:
            await performNewNoteAction(template, 0);
            break;
        case TemplateAction.NewTodo:
            await performNewNoteAction(template, 1);
            break;
    }
}