Applies to :
PowerDocs for Linux
PowerDocs for Windows
Class `SelectionContext`
The SelectionContext object is supplied as the argument to the execute(selection) callback when an editor selection action is triggered. It represents the currently selected span of text in the active document and provides methods to read and modify that text with full integration into the application's Undo/Redo command history.
Properties
| Property | Type | Description |
|---|---|---|
text |
string |
The plain text currently selected by the user in the editor. |
Methods
replaceText(newText)
Replaces the currently selected text range with the specified string. The change is tracked in the editor's undo stack, allowing the user to press Ctrl+Z to undo.
Parameters
newText(string): The replacement text.
insertText(textToInsert)
Inserts the given string at the current selection location. Equivalent to replacing the selected range with textToInsert.
Parameters
textToInsert(string): The text to insert.
Examples
1. Markdown Quoting
export function activate(context) {
context.editor.registerSelectionAction({
id: 'quote-selection',
title: 'Quote Text',
icon: 'TextQuote',
execute: (selection) => {
const lines = selection.text.split('\n');
const quoted = lines.map(line => `> ${line}`).join('\n');
selection.replaceText(quoted);
}
});
}
2. Markdown Table Generator from CSV Selection
export function activate(context) {
context.editor.registerSelectionAction({
id: 'csv-to-table',
title: 'Convert CSV to Table',
icon: 'Table',
execute: (selection) => {
const lines = selection.text.trim().split('\n');
if (lines.length === 0) return;
const tableRows = lines.map(line => {
const cells = line.split(',').map(c => c.trim());
return `| ${cells.join(' | ')} |`;
});
const colCount = lines[0].split(',').length;
const separator = `| ${new Array(colCount).fill('---').join(' | ')} |`;
tableRows.splice(1, 0, separator);
selection.replaceText(tableRows.join('\n') + '\n');
}
});
}