I've built several custom UI components in my userstyle.css
and I'm now taking a journey into plugin development so I can automatically insert/surround selected text with my custom HTML and CSS class definitions to avoid the process of manually typing out my desired HTML.
I only want this to work in the markdown editor, not in the RTE editor.
Looking at the API reference editor.execCommand
is what I'm looking for. But I can't for the life of me find a list of all commands that can be passed to this. Just skimming other plugins on GitHub I found the following:
// Some commands here: https://github.com/laurent22/joplin/blob/2014fbf480fc57f5b87f2c900baf486631332cc1/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v5/CodeMirror.tsx#L172
// More commands here: https://github.com/laurent22/joplin/blob/2014fbf480fc57f5b87f2c900baf486631332cc1/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/v6/useEditorCommands.ts#L31
await joplin.commands.execute('textSelectAll'); // Selects all text
await joplin.commands.execute("selectedText"); // Returns currently selected text
await joplin.commands.execute("replaceSelection", "replacement_text"); // Replaces current selection with text
await joplin.commands.execute('editor.setText', 'something'); // Not sure what this command is?
await joplin.commands.execute('editor.focus'); // Also not sure what this command is?
Based on what I've found, I think I can accomplish what I need with this - but I'm not sure if this is the best way to do it:
import joplin from 'api';
import { MenuItemLocation, ToolbarButtonLocation } from 'api/types';
joplin.plugins.register({
onStart: async function() {
await joplin.commands.register({
name: 'CreateButton',
label: 'Creates a button.',
iconName: 'fas fa-drum',
execute: async () => {
const tag_start = "<span class=\"fm-enhanced-link theme-blue-filled-1 br-6px\">"
const tag_end = "</span>"
var selectedText = await joplin.commands.execute("selectedText");
var newButtonDef = tag_start + selectedText + tag_end
await joplin.commands.execute("replaceSelection", newButtonDef);
},
});
await joplin.views.toolbarButtons.create('CreateHTMLButton', 'CreateButton', ToolbarButtonLocation.EditorToolbar);
},
});
Am I on the right track?
And can someone please provide a reference page that actually lists all commands that can be passed to joplin.commands.execute
?