Getting the currently selected text when creating an Obsidian plugin…
import { MarkdownView } from 'obsidian';
// ...
// Access to the editor component...
const editor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
const selectedText = editor.getSelection(); // Get selected text
You can also update selected text…
editor.replaceSelection("New Text Goes Here");