mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 21:29:13 +00:00
Render action buttons for new Khoj chat responses in Obsidian
- Dedupe the code to add action buttons to chat messages - Update the renderIncrementalMessage function to also add the action buttons to newly generated chat messages by Khoj
This commit is contained in:
@@ -302,20 +302,7 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
|
|
||||||
// Add a copy button to each chat message, if it doesn't already exist
|
// Add a copy button to each chat message, if it doesn't already exist
|
||||||
if (willReplace === true) {
|
if (willReplace === true) {
|
||||||
let copyButton = this.contentEl.createEl('button');
|
this.renderActionButtons(message, chat_message_body_text_el);
|
||||||
copyButton.classList.add("copy-button");
|
|
||||||
copyButton.title = "Copy Message to Clipboard";
|
|
||||||
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 = this.contentEl.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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chat_message_body_text_el;
|
return chat_message_body_text_el;
|
||||||
@@ -395,22 +382,9 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
MarkdownRenderer.renderMarkdown(message, chat_message_body_text_el, '', null);
|
MarkdownRenderer.renderMarkdown(message, chat_message_body_text_el, '', null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a copy button to each chat message
|
// Add action buttons to each chat message element
|
||||||
if (willReplace === true) {
|
if (willReplace === true) {
|
||||||
let copyButton = chatMessageEl.createEl('button');
|
this.renderActionButtons(message, chat_message_body_text_el);
|
||||||
copyButton.classList.add("copy-button");
|
|
||||||
copyButton.title = "Copy Message to Clipboard";
|
|
||||||
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 Current 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
|
// Remove user-select: none property to make text selectable
|
||||||
@@ -450,10 +424,29 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
htmlElement.innerHTML = "";
|
htmlElement.innerHTML = "";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
await MarkdownRenderer.renderMarkdown(this.result, htmlElement, '', null);
|
await MarkdownRenderer.renderMarkdown(this.result, htmlElement, '', null);
|
||||||
|
// Render action buttons for the message
|
||||||
|
this.renderActionButtons(this.result, htmlElement);
|
||||||
// Scroll to bottom of modal, till the send message input box
|
// Scroll to bottom of modal, till the send message input box
|
||||||
this.scrollChatToBottom();
|
this.scrollChatToBottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderActionButtons(message: string, chat_message_body_text_el: HTMLElement) {
|
||||||
|
let copyButton = this.contentEl.createEl('button');
|
||||||
|
copyButton.classList.add("copy-button");
|
||||||
|
copyButton.title = "Copy Message to Clipboard";
|
||||||
|
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 = this.contentEl.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);
|
||||||
|
}
|
||||||
|
|
||||||
formatDate(date: Date): string {
|
formatDate(date: Date): string {
|
||||||
// Format date in HH:MM, DD MMM YYYY format
|
// Format date in HH:MM, DD MMM YYYY format
|
||||||
let time_string = date.toLocaleTimeString('en-IN', { hour: '2-digit', minute: '2-digit', hour12: false });
|
let time_string = date.toLocaleTimeString('en-IN', { hour: '2-digit', minute: '2-digit', hour12: false });
|
||||||
|
|||||||
Reference in New Issue
Block a user