From bd4931e70b52ba566d44a4b72f207309f9451fe0 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 7 May 2024 11:11:21 -0700 Subject: [PATCH] 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 --- src/interface/obsidian/src/chat_view.ts | 10 +++++++++- src/interface/obsidian/src/utils.ts | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/interface/obsidian/src/chat_view.ts b/src/interface/obsidian/src/chat_view.ts index 14ca1aff..9549969e 100644 --- a/src/interface/obsidian/src/chat_view.ts +++ b/src/interface/obsidian/src/chat_view.ts @@ -1,7 +1,7 @@ import { MarkdownRenderer, WorkspaceLeaf, request, requestUrl, setIcon } from 'obsidian'; import { KhojSetting } from 'src/settings'; import { KhojPaneView } from 'src/pane_view'; -import { KhojView, createCopyParentText } from 'src/utils'; +import { KhojView, createCopyParentText, pasteTextAtCursor } from 'src/utils'; export interface ChatJsonResult { image?: string; @@ -244,6 +244,14 @@ export class KhojChatView extends KhojPaneView { setIcon(copyButton, "copy-plus"); copyButton.addEventListener('click', createCopyParentText(message)); chat_message_body_text_el.append(copyButton); + + // Add button to paste into current buffer + let pasteToFile = chatMessageEl.createEl('button'); + pasteToFile.classList.add("copy-button"); + pasteToFile.title = "Paste Message to File"; + setIcon(pasteToFile, "clipboard-paste"); + pasteToFile.addEventListener('click', (event) => { pasteTextAtCursor(createCopyParentText(message, 'clipboard-paste')(event)); }); + chat_message_body_text_el.append(pasteToFile); } // Remove user-select: none property to make text selectable diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index b0160c48..53df076f 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -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); + } +}