Skip to content

Commit 2d646d3

Browse files
committed
feat: add macro selection helper
1 parent f1e2a9f commit 2d646d3

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

docs/docs/Choices/MacroChoice.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ module.exports = async (params) => {
188188
};
189189
```
190190

191+
### Getting the current selection
192+
193+
Use the utility helper to read the active editor selection. It returns an empty
194+
string when nothing is selected or no editor is active.
195+
196+
```javascript
197+
module.exports = async (params) => {
198+
const selection = params.quickAddApi.utility.getSelection();
199+
if (selection) {
200+
params.variables.selectedText = selection;
201+
}
202+
};
203+
```
204+
191205
### Accessing Other Plugins
192206

193207
Scripts can interact with other Obsidian plugins:

docs/docs/QuickAddAPI.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,18 @@ Sets the clipboard contents.
426426
await quickAddApi.utility.setClipboard("Hello, World!");
427427
```
428428
429+
### `getSelection(): string`
430+
Gets the currently selected text in the active editor. Returns an empty string if
431+
there is no active editor or no selection.
432+
433+
**Example:**
434+
```javascript
435+
const selection = quickAddApi.utility.getSelection();
436+
if (selection) {
437+
console.log("Selected:", selection);
438+
}
439+
```
440+
429441
Combined example:
430442
```javascript
431443
// Transform clipboard contents

src/quickAddApi.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ export class QuickAddApi {
461461
setClipboard: async (text: string) => {
462462
return await navigator.clipboard.writeText(text);
463463
},
464+
getSelection: () => {
465+
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
466+
467+
if (!activeView) {
468+
return "";
469+
}
470+
471+
return activeView.editor.getSelection() ?? "";
472+
},
464473
getSelectedText: () => {
465474
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
466475

0 commit comments

Comments
 (0)