File tree Expand file tree Collapse file tree 3 files changed +35
-0
lines changed
Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Original file line number Diff line number Diff 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
193207Scripts can interact with other Obsidian plugins:
Original file line number Diff line number Diff line change @@ -426,6 +426,18 @@ Sets the clipboard contents.
426426await 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+
429441Combined example:
430442` ` ` javascript
431443// Transform clipboard contents
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments