Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrt committed Dec 11, 2020
1 parent ef5b037 commit dea3e30
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
91 changes: 88 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,92 @@
import joplin from 'api';
import { MenuItemLocation, ToolbarButtonLocation } from 'api/types';


function escapeTitleText(text: string) {
return text.replace(/(\[|\])/g, '\\$1');
}

joplin.plugins.register({
onStart: async function() {
console.info('Test plugin started!');
},
onStart: async function () {
await joplin.commands.register({
name: 'goToItemViaText',
label: 'Go to note via highlighted text',
iconName: 'fas fa-anchor',
execute: async () => {
//Get selected text
const selectedText = (await joplin.commands.execute('selectedText') as string);

try {
let first = selectedText.substr(0,1);
if(first == "#"){
let tagId =''
let page = 1
let tags
console.info(tags);
let has_more = true
while(has_more){
tags = await joplin.data.get(['tags'], {page:page});
tags.items.forEach(element => {
console.log(element)
if (element.title==selectedText.substr(1)){
console.log('open '+element.id)
tagId = element.id
}
});
if(tags.has_more) {page=page+1} else {has_more=false}

}


await joplin.commands.execute('openTag',tagId);


}
else if(first =="@"){
let folderId =''
let page = 1
let folders

let has_more = true
while(has_more){
folders = await joplin.data.get(['folders'], {page:page});
folders.items.forEach(element => {
console.log(element)
if (element.title==selectedText.substr(1)){
console.log('open '+element.id)
folderId = element.id
}
});
if(folders.has_more) {page=page+1} else {has_more=false}

}

console.info(folders);
await joplin.commands.execute('openFolder',folderId);


}
else{
await joplin.commands.execute('openNote',selectedText);
}

}
catch(err)
{
console.log(err)
let dialog = await joplin.views.dialogs.create('wrongText')
await joplin.views.dialogs.setHtml(dialog, "Seems like text is not an exact id,tag or folder<br>Or other error occured");
await joplin.views.dialogs.setButtons(dialog,[{id:"ok"}]);
await joplin.views.dialogs.open(dialog);
}


}
})
await joplin.views.menuItems.create('goToViaTextMenuItem', 'goToItemViaText', MenuItemLocation.EditorContextMenu);
await joplin.views.toolbarButtons.create('Go to item via text', 'goToItemViaText',ToolbarButtonLocation.EditorToolbar);

}

});

10 changes: 5 additions & 5 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"manifest_version": 1,
"id": "com.ambrth.GoToItemViaText",
"app_min_version": "1.4",
"id": "goToItemViaTextPlugin",
"app_min_version": "1.4.19",
"version": "1.0.0",
"name": "Go To Item Via Text",
"description": "Goes to item (#tag, @notebook, noteId) highlighted in text editor and goes to it.",
"name": "Go to via text",
"description": "Goes to tag, notebook or note via highlighted text",
"author": "a",
"homepage_url": ""
"homepage_url": "a"
}

0 comments on commit dea3e30

Please sign in to comment.