Skip to content

Commit

Permalink
refactored toggle into function and created newHtmlNote function
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeraxa committed Sep 6, 2022
1 parent 8e7b983 commit 5dbb923
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,48 @@ joplin.plugins.register({
label: "Toggle the markup language type of the current note",
execute: async () => {
toggleMarkupLanguage();
},
},
})

async function toggleMarkupLanguage(){

//Get current note and its ID
const curNote = await joplin.workspace.selectedNote();
const noteId = await curNote.id;

//console.info(noteId);

//const note = await joplin.data.get(['notes', noteId], { fields: ['id', 'title', 'markup_language']});

//console.info(await curNote.markup_language);


//check the markup_language type (1=markdown, 2=html)
let customMarkup: number;

if (curNote.markup_language == 1) {
customMarkup = 2;
} else if (curNote.markup_language == 2) {
await joplin.commands.register({
name: "newNoteHtml",
label: "New HTML note",
execute: async () => {
newHtmlNote();
toggleMarkupLanguage();
},
})
},
});

async function toggleMarkupLanguage(){

//Get current note and its ID
const curNote = await joplin.workspace.selectedNote();
const noteId = await curNote.id;

//check the markup_language type (1=markdown, 2=html)
let customMarkup: number;

if (curNote.markup_language == 1) {
customMarkup = 2;
} else if (curNote.markup_language == 2) {
customMarkup = 1;
};

//update markup_language
await joplin.data.put(['notes', noteId], null, { markup_language: customMarkup});
//update markup_language
await joplin.data.put(['notes', noteId], null, { markup_language: customMarkup });
};


}},
});
async function newHtmlNote(){

//Get current selected folder id
const curFolder = await joplin.workspace.selectedFolder();
const folderId = await curFolder.id;

//create new note and use its returned Id to focus it and move the cursor to the title line
const newNote = await joplin.data.post(['notes'], null, { title: "", parent_id: folderId, markup_language: 2 });

await joplin.commands.execute('openNote', newNote.id);
await joplin.commands.execute('focusElement', 'noteTitle');
};

0 comments on commit 5dbb923

Please sign in to comment.