Add ability to paste chat messages directly into current file

It'll replace any highlighted text with the chat message or if not
text is highlighted, it'll insert the chat message at the last cursor
position in the active file
This commit is contained in:
Debanjum Singh Solanky
2024-05-07 11:11:21 -07:00
parent 032ad3b521
commit bd4931e70b
2 changed files with 24 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
import { FileSystemAdapter, Notice, Vault, Modal, TFile, request, setIcon } from 'obsidian';
import { FileSystemAdapter, Notice, Vault, Modal, TFile, request, setIcon, Editor } from 'obsidian';
import { KhojSetting, UserInfo } from 'src/settings'
export function getVaultAbsolutePath(vault: Vault): string {
@@ -333,3 +333,17 @@ export function createCopyParentText(message: string, originalButton: string = '
return copyParentText(event, message, originalButton);
}
}
export function pasteTextAtCursor(text: string | undefined) {
// Get the current active file's editor
const editor: Editor = this.app.workspace.getActiveFileView()?.editor
if (!editor || !text) return;
const cursor = editor.getCursor();
// If there is a selection, replace it with the text
if (editor?.getSelection()) {
editor.replaceSelection(text);
// If there is no selection, insert the text at the cursor position
} else if (cursor) {
editor.replaceRange(text, cursor);
}
}