2025-10-26 09:15:11 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { tick } from "svelte";
|
|
|
|
|
import { setIcon } from "obsidian";
|
2025-10-29 19:35:19 +00:00
|
|
|
import type { UserInputService } from "Services/UserInputService";
|
|
|
|
|
import type { ISearchState, SearchStateStore } from "Stores/SearchStateStore";
|
|
|
|
|
import { Resolve } from "Services/DependencyService";
|
|
|
|
|
import { Services } from "Services/Services";
|
|
|
|
|
import { SearchTrigger } from "Enums/SearchTrigger";
|
|
|
|
|
import ChatSearchResults from "./ChatSearchResults.svelte";
|
|
|
|
|
import type { Writable } from "svelte/store";
|
2025-10-29 19:46:20 +00:00
|
|
|
import type { InputService } from "Services/InputService";
|
2025-10-26 09:15:11 +00:00
|
|
|
|
|
|
|
|
export let hasNoApiKey: boolean;
|
|
|
|
|
export let isSubmitting: boolean;
|
|
|
|
|
export let editModeActive: boolean;
|
2025-10-29 19:35:19 +00:00
|
|
|
export let onsubmit: (userRequest: string, formattedRequest: string) => void;
|
2025-10-26 09:15:11 +00:00
|
|
|
export let ontoggleeditmode: () => void;
|
|
|
|
|
export let onstop: () => void;
|
|
|
|
|
|
2025-10-29 19:46:20 +00:00
|
|
|
const inputService: InputService = Resolve<InputService>(Services.InputService);
|
2025-10-29 19:35:19 +00:00
|
|
|
const userInputService: UserInputService = Resolve<UserInputService>(Services.UserInputService);
|
|
|
|
|
const searchStateStore: SearchStateStore = Resolve<SearchStateStore>(Services.SearchStateStore);
|
|
|
|
|
|
|
|
|
|
const searchState: Writable<ISearchState> = searchStateStore.searchState;
|
|
|
|
|
|
2025-10-26 09:15:11 +00:00
|
|
|
let textareaElement: HTMLDivElement;
|
|
|
|
|
let submitButton: HTMLButtonElement;
|
|
|
|
|
let editModeButton: HTMLButtonElement;
|
|
|
|
|
let userRequest = "";
|
|
|
|
|
|
|
|
|
|
export function focusInput() {
|
|
|
|
|
tick().then(() => {
|
|
|
|
|
textareaElement?.focus();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:35:19 +00:00
|
|
|
$: if (submitButton) {
|
|
|
|
|
setIcon(submitButton, isSubmitting ? "square" : "send-horizontal");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$: if (editModeButton) {
|
|
|
|
|
setIcon(editModeButton, editModeActive ? "pencil" : "pencil-off");
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 09:15:11 +00:00
|
|
|
function handleStop() {
|
|
|
|
|
onstop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
|
if (userRequest.trim() === "" || isSubmitting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentRequest = userRequest;
|
|
|
|
|
textareaElement.textContent = "";
|
|
|
|
|
userRequest = "";
|
|
|
|
|
|
2025-10-29 19:35:19 +00:00
|
|
|
onsubmit(currentRequest, "");
|
2025-10-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toggleEditMode() {
|
|
|
|
|
ontoggleeditmode();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:35:19 +00:00
|
|
|
async function handleKeydown(e: KeyboardEvent) {
|
|
|
|
|
if ($searchState.active) {
|
|
|
|
|
await continueSearch(e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key === "Enter") {
|
2025-10-26 09:15:11 +00:00
|
|
|
if (e.shiftKey) {
|
|
|
|
|
return;
|
2025-10-29 19:35:19 +00:00
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleSubmit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SearchTrigger.isSearchTrigger(e.key)) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2025-10-29 19:46:20 +00:00
|
|
|
const position = inputService.getCursorPosition(textareaElement)
|
2025-10-29 19:35:19 +00:00
|
|
|
const trigger = SearchTrigger.fromInput(e.key);
|
|
|
|
|
|
|
|
|
|
searchStateStore.initializeSearch(trigger, position);
|
|
|
|
|
|
|
|
|
|
textareaElement.textContent = textareaElement.textContent + e.key;
|
2025-10-29 19:46:20 +00:00
|
|
|
inputService.setCursorPosition(textareaElement, position + 1);
|
2025-10-29 19:35:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function continueSearch(e: KeyboardEvent) {
|
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
|
searchStateStore.resetSearch();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
// need to choose selected search query (or do nothing if there isn't one)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key === "Backspace" || e.key === "Delete") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key.startsWith("Arrow")) {
|
|
|
|
|
if (e.key === "ArrowUp") {
|
2025-10-26 09:15:11 +00:00
|
|
|
e.preventDefault();
|
2025-10-29 19:35:19 +00:00
|
|
|
searchStateStore.setSelectedResultToPrevious();
|
2025-10-26 09:15:11 +00:00
|
|
|
}
|
2025-10-29 19:35:19 +00:00
|
|
|
if (e.key === "ArrowDown") {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
searchStateStore.setSelectedResultToNext()
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only append printable characters to the query
|
2025-10-29 19:46:20 +00:00
|
|
|
if (inputService.isPrintableKey(e.key, e.ctrlKey, e.metaKey)) {
|
2025-10-29 19:35:19 +00:00
|
|
|
searchStateStore.appendToQuery(e.key);
|
|
|
|
|
userInputService.performSearch();
|
2025-10-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleInput() {
|
|
|
|
|
if (textareaElement) {
|
|
|
|
|
userRequest = textareaElement.textContent || "";
|
2025-10-29 19:35:19 +00:00
|
|
|
|
|
|
|
|
if (textareaElement.innerHTML !== textareaElement.textContent) {
|
|
|
|
|
// HTML detected - sanitize by replacing with plain text
|
|
|
|
|
const plainText = textareaElement.textContent || "";
|
2025-10-29 19:46:20 +00:00
|
|
|
const cursorPos = inputService.getCursorPosition(textareaElement);
|
2025-10-29 19:35:19 +00:00
|
|
|
|
|
|
|
|
textareaElement.textContent = plainText;
|
|
|
|
|
|
|
|
|
|
// Restore cursor position after sanitization
|
2025-10-29 19:46:20 +00:00
|
|
|
inputService.setCursorPosition(textareaElement, cursorPos);
|
2025-10-29 19:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If in search mode, synchronize the query with actual text content
|
|
|
|
|
if ($searchState.active && $searchState.position !== null) {
|
|
|
|
|
const fullText = textareaElement.textContent || "";
|
|
|
|
|
const triggerPos = $searchState.position;
|
|
|
|
|
|
|
|
|
|
// Extract the query portion (everything after the trigger)
|
|
|
|
|
const actualQuery = fullText.substring(triggerPos + 1);
|
|
|
|
|
|
|
|
|
|
// Only update if the query has changed
|
|
|
|
|
if (actualQuery !== $searchState.query) {
|
|
|
|
|
searchStateStore.setQuery(actualQuery);
|
|
|
|
|
userInputService.performSearch();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 09:15:11 +00:00
|
|
|
if (userRequest.trim() === "") {
|
|
|
|
|
textareaElement.textContent = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:35:19 +00:00
|
|
|
function handlePaste(e: ClipboardEvent) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2025-10-29 19:46:20 +00:00
|
|
|
const plainText = inputService.getPlainTextFromClipboard(e.clipboardData);
|
2025-10-29 19:35:19 +00:00
|
|
|
|
|
|
|
|
if (!plainText) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:46:20 +00:00
|
|
|
inputService.insertTextAtCursor(plainText);
|
2025-10-29 19:35:19 +00:00
|
|
|
handleInput();
|
2025-10-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:35:19 +00:00
|
|
|
function handleCopy(e: ClipboardEvent) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
const selection = window.getSelection();
|
|
|
|
|
|
|
|
|
|
if (!selection) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectedText = selection.toString();
|
|
|
|
|
e.clipboardData?.setData('text/plain', selectedText);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCursorPositionChange() {
|
|
|
|
|
if (!$searchState.active || $searchState.position === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:46:20 +00:00
|
|
|
const currentPosition = inputService.getCursorPosition(textareaElement);
|
2025-10-29 19:35:19 +00:00
|
|
|
|
2025-10-29 19:46:20 +00:00
|
|
|
if (!inputService.isInSearchZone(currentPosition, $searchState.position)) {
|
2025-10-29 19:35:19 +00:00
|
|
|
searchStateStore.resetSearch();
|
|
|
|
|
}
|
2025-10-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div id="input-container" class:edit-mode={editModeActive}>
|
2025-10-29 19:35:19 +00:00
|
|
|
<div id="input-search-results-container">
|
|
|
|
|
<ChatSearchResults searchState={$searchState}/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-10-26 09:15:11 +00:00
|
|
|
<div
|
|
|
|
|
id="input-field"
|
|
|
|
|
class:error={hasNoApiKey}
|
|
|
|
|
class:edit-mode={editModeActive && !hasNoApiKey}
|
|
|
|
|
bind:this={textareaElement}
|
|
|
|
|
contenteditable="true"
|
|
|
|
|
on:keydown={handleKeydown}
|
|
|
|
|
on:input={handleInput}
|
2025-10-29 19:35:19 +00:00
|
|
|
on:paste={handlePaste}
|
|
|
|
|
on:copy={handleCopy}
|
|
|
|
|
on:click={handleCursorPositionChange}
|
|
|
|
|
on:keyup={handleCursorPositionChange}
|
2025-10-26 09:15:11 +00:00
|
|
|
data-placeholder="Type a message..."
|
|
|
|
|
role="textbox"
|
|
|
|
|
aria-multiline="true"
|
|
|
|
|
tabindex="0">
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
id="edit-mode-button"
|
|
|
|
|
class:edit-mode={editModeActive}
|
|
|
|
|
bind:this={editModeButton}
|
|
|
|
|
on:click={() => { toggleEditMode() }}
|
|
|
|
|
disabled={isSubmitting}
|
|
|
|
|
aria-label={editModeActive ? "Turn off Agent Mode" : "Turn on Agent Mode"}>
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
id="submit-button"
|
|
|
|
|
class:edit-mode={editModeActive}
|
|
|
|
|
bind:this={submitButton}
|
|
|
|
|
on:click={() => { isSubmitting ? handleStop() : handleSubmit() }}
|
|
|
|
|
disabled={!isSubmitting && userRequest.trim() === ""}
|
|
|
|
|
aria-label={isSubmitting ? "Cancel" : "Send Message"}>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
#input-container {
|
|
|
|
|
grid-row: 2;
|
|
|
|
|
grid-column: 1;
|
|
|
|
|
display: grid;
|
2025-10-29 19:35:19 +00:00
|
|
|
grid-template-rows: auto var(--size-4-3) 1fr var(--size-4-3);
|
2025-10-26 09:15:11 +00:00
|
|
|
grid-template-columns: var(--size-4-3) 1fr var(--size-4-2) auto var(--size-4-2) auto var(--size-4-3);
|
|
|
|
|
border-radius: var(--modal-radius);
|
|
|
|
|
background-color: var(--background-primary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-container.edit-mode {
|
|
|
|
|
border-color: var(--alt-interactive-accent);
|
|
|
|
|
transition: border-color 0.5s ease-out;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-29 19:35:19 +00:00
|
|
|
#input-search-results-container {
|
|
|
|
|
grid-row: 1;
|
|
|
|
|
grid-column: 2 / 8;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 09:15:11 +00:00
|
|
|
#input-field {
|
2025-10-29 19:35:19 +00:00
|
|
|
grid-row: 3;
|
2025-10-26 09:15:11 +00:00
|
|
|
grid-column: 2;
|
|
|
|
|
height: 100%;
|
|
|
|
|
max-height: 30vh;
|
|
|
|
|
border-radius: var(--input-radius);
|
|
|
|
|
font-weight: var(--input-font-weight);
|
|
|
|
|
border-width: var(--input-border-width);
|
|
|
|
|
border-style: solid;
|
|
|
|
|
border-color: var(--background-modifier-border);
|
|
|
|
|
padding: var(--size-2-2) var(--size-2-3);
|
|
|
|
|
background-color: var(--background-primary);
|
|
|
|
|
font-family: var(--font-interface-theme);
|
|
|
|
|
resize: none;
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
overflow-x: hidden;
|
|
|
|
|
scroll-behavior: smooth;
|
|
|
|
|
color: var(--font-interface-theme);
|
|
|
|
|
transition: border-color 0.5s ease-out;
|
|
|
|
|
word-wrap: break-word;
|
|
|
|
|
white-space: pre-wrap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-field:focus {
|
|
|
|
|
border-color: var(--color-accent);
|
|
|
|
|
box-shadow: 0px 0px 4px 1px var(--color-accent);
|
|
|
|
|
transition: border-color 0.5s ease-out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-field.edit-mode:focus {
|
|
|
|
|
border-color: var(--alt-interactive-accent);
|
|
|
|
|
box-shadow: 0px 0px 3px 1px var(--alt-interactive-accent);
|
|
|
|
|
transition: border-color 0.5s ease-out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-field.error,
|
|
|
|
|
#input-field.error:focus {
|
|
|
|
|
border-color: var(--color-red);
|
|
|
|
|
box-shadow: 0px 0px 4px 1px var(--color-red);
|
|
|
|
|
transition: border-color 0.5s ease-out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-field::-webkit-scrollbar {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-field:empty::before {
|
|
|
|
|
content: attr(data-placeholder);
|
|
|
|
|
color: var(--text-muted);
|
|
|
|
|
opacity: 0.75;
|
|
|
|
|
pointer-events: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#input-field[contenteditable]:focus {
|
|
|
|
|
outline: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#edit-mode-button {
|
2025-10-29 19:35:19 +00:00
|
|
|
grid-row: 3;
|
2025-10-26 09:15:11 +00:00
|
|
|
grid-column: 4;
|
|
|
|
|
border-radius: var(--button-radius);
|
|
|
|
|
align-self: end;
|
|
|
|
|
transition-duration: 0.5s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#submit-button {
|
2025-10-29 19:35:19 +00:00
|
|
|
grid-row: 3;
|
2025-10-26 09:15:11 +00:00
|
|
|
grid-column: 6;
|
|
|
|
|
border-radius: var(--button-radius);
|
|
|
|
|
padding-left: var(--size-4-5);
|
|
|
|
|
padding-right: var(--size-4-5);
|
|
|
|
|
align-self: end;
|
|
|
|
|
transition-duration: 0.5s;
|
|
|
|
|
background-color: var(--interactive-accent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#submit-button:not(:disabled):hover {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
background-color: var(--interactive-accent-hover);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#submit-button.edit-mode {
|
|
|
|
|
background-color: var(--alt-interactive-accent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#submit-button.edit-mode:not(:disabled):hover {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
background-color: var(--alt-interactive-accent-hover);
|
|
|
|
|
}
|
|
|
|
|
</style>
|