diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index e1c0349f00..8894d01e29 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -173,6 +173,7 @@ import Sidebar from '../icons/Sidebar.svelte'; import Image from '../common/Image.svelte'; import XMark from '../icons/XMark.svelte'; + import ChevronDown from '../icons/ChevronDown.svelte'; import EmbeddedChatHistoryDropdown from './EmbeddedChatHistoryDropdown.svelte'; import ChatMenu from '$lib/components/layout/Navbar/Menu.svelte'; import ShareChatModal from './ShareChatModal.svelte'; @@ -239,6 +240,11 @@ export let onBeforeNoteSubmit: (() => Promise) | null = null; export let onInsertToNote: ((content: string) => void) | null = null; export let onCloseEmbedded: (() => void) | null = null; + // Collapsed embedded chats render only the composer; the host expands them on demand. + export let embeddedCollapsed = false; + export let onCollapseEmbedded: (() => void) | null = null; + // Pads the header and messages when the host draws the composer flush to its frame. + export let embeddedInset = false; export let onNewEmbeddedChat: (() => void | Promise) | null = null; export let onCreateEmbeddedChat: | ((chat: { @@ -6170,9 +6176,11 @@ {/if} {:else} - {#if embedded && !tabbed} + {#if embedded && !tabbed && !embeddedCollapsed}
+ {#if onCollapseEmbedded} + + {/if} {#if onCloseEmbedded}
- {:else if suggestedPrompts.length > 0} + {:else if suggestedPrompts.length > 0 && !embeddedCollapsed}
@@ -6509,6 +6532,7 @@ {contextUsage} {contextCompactionEnabled} {embedded} + autofocus={!embeddedCollapsed} compactHandler={handleManualCompact} statusHandler={handleStatusCommand} forkHandler={handleForkChat} diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 09600dee61..f21efee371 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -179,6 +179,7 @@ export let contextUsage = null; export let contextCompactionEnabled = false; export let embedded = false; + export let autofocus = true; export let callActive = false; export let callEnabled = true; @@ -1903,7 +1904,7 @@ loaded = true; window.setTimeout(() => { - if (!$mobile && !$voiceCall) { + if (autofocus && !$mobile && !$voiceCall) { const chatInput = document.getElementById(inputId); chatInput?.focus(); } @@ -2426,6 +2427,7 @@ json={true} richText={$settings?.richTextInput ?? true} messageInput={true} + {autofocus} followUpSuggestion={!generating && history?.messages?.[history?.currentId]?.role === 'assistant' && history?.messages?.[history?.currentId]?.done diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index 8f2165b710..f54e9aed54 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/src/lib/components/common/RichTextInput.svelte @@ -421,6 +421,7 @@ } export let messageInput = false; + export let autofocus = true; export let shiftEnter = false; export let largeTextAsFile = false; export let insertPromptAsRichText = false; @@ -1106,7 +1107,7 @@ ], content: provider ? undefined : content, editable, - autofocus: messageInput && !$mobile && !$voiceCall, + autofocus: autofocus && messageInput && !$mobile && !$voiceCall, onTransaction: ({ transaction }) => { if (!editor) return; diff --git a/src/lib/components/notes/NoteEditor.svelte b/src/lib/components/notes/NoteEditor.svelte index d437203bb8..969aaf2f6f 100644 --- a/src/lib/components/notes/NoteEditor.svelte +++ b/src/lib/components/notes/NoteEditor.svelte @@ -27,7 +27,8 @@ import CommentsPanel, { type CommentsRequest } from '$lib/components/comments/CommentsPanel.svelte'; - import XMark from '$lib/components/icons/XMark.svelte'; + import Dropdown from '$lib/components/common/Dropdown.svelte'; + import ChevronUp from '$lib/components/icons/ChevronUp.svelte'; import { getContext, onDestroy, onMount, tick } from 'svelte'; import { v4 as uuidv4 } from 'uuid'; import fileSaver from 'file-saver'; @@ -75,7 +76,6 @@ deleteNoteById, createNoteChatById, getNoteById, - getNoteChatById, getNoteChatsById, updateNoteById, updateNotePages, @@ -104,7 +104,6 @@ import ArrowUturnLeft from '../icons/ArrowUturnLeft.svelte'; import ArrowUturnRight from '../icons/ArrowUturnRight.svelte'; import Sidebar from '../icons/Sidebar.svelte'; - import ChatBubbleOval from '../icons/ChatBubbleOval.svelte'; export let id: null | string = null; let noteRoot: HTMLDivElement; @@ -352,7 +351,6 @@ let recording = false; let displayMediaRecord = false; - let showNoteChat = false; $: commentsEnabled = !!($config?.features?.enable_comments && $user && note?.id); let currentNoteComments: CommentsRequest | null = null; let linkedComment = ''; @@ -364,16 +362,6 @@ } $: if (currentNoteComments && (!commentsEnabled || currentNoteComments.resource_id !== note?.id)) currentNoteComments = null; - onMount(() => { - const close = (event: KeyboardEvent) => { - if (event.key === 'Escape' && currentNoteComments) closeNotePanel(); - }; - document.addEventListener('keydown', close); - return () => { - document.removeEventListener('keydown', close); - }; - }); - let noteCommentCount = 0; // Editable notes comment from the formatting toolbar instead of the floating selection button. $: selectionCommentsEnabled = commentsEnabled && versionIdx === null && !!note?.write_access; const commentOnSelection = async () => { @@ -391,15 +379,17 @@ commentSelectionDraft.set(selected); window.getSelection()?.removeAllRanges(); }; - $: if (currentNoteComments) showNoteChat = true; - const closeNotePanel = async () => { - currentNoteComments = null; - showNoteChat = false; - - await tick(); - if (document.activeElement === document.body) noteChatButtonElement?.focus(); - }; let noteChatId = null; + let noteChatExpanded = false; + // Opening a note lands in its body; the docked chat composer no longer takes focus. + let focusedNoteId = ''; + const focusOpenedNote = (instance) => { + if (!instance || instance.isDestroyed || !note?.id || note.id === focusedNoteId) return; + focusedNoteId = note.id; + if (note.write_access && !$mobile && document.activeElement === document.body) + instance.commands.focus('start', { scrollIntoView: false }); + }; + let noteChatNoteId = ''; let noteChatLoading = false; let noteChats = []; let noteChatDraftKey = ''; @@ -424,7 +414,6 @@ let titleInputFocused = false; let titleGenerating = false; let titleInputElement: HTMLInputElement; - let noteChatButtonElement: HTMLButtonElement; let pagesToggleElement: HTMLButtonElement; let dragged = false; @@ -839,53 +828,35 @@ ${content} input.click(); }; - const openNoteChat = async () => { - console.info('[note-chat] open requested', { - noteId: note?.id, - alreadyLoading: noteChatLoading, - currentChatId: noteChatId, - sidebarOpen: showNoteChat - }); - if (!note?.id) return; - currentNoteComments = null; - showNoteChat = true; - if (noteChatLoading || noteChatId || noteChatDraftKey) return; - + // The docked composer is always mounted, so list existing chats without creating one; + // an empty note gets a draft whose backing chat is created on the first message. + const loadNoteChat = async (noteId: string) => { + noteChatId = null; + noteChats = []; + noteChatDraftKey = ''; + noteChatExpanded = false; noteChatLoading = true; - const chat = await getNoteChatById(localStorage.token, note.id).catch((error) => { - console.error('[note-chat] open failed', { noteId: note?.id, error }); - toast.error(`${error}`); - return null; + const chats = await getNoteChatsById(localStorage.token, noteId).catch((error) => { + console.error('[note-chat] history failed', { noteId, error }); + return []; }); - const chats = chat - ? await getNoteChatsById(localStorage.token, note.id).catch((error) => { - console.error('[note-chat] history failed', { noteId: note?.id, error }); - return null; - }) - : null; + if (noteId !== noteChatNoteId) return; noteChatLoading = false; - - if (chat?.id) { - console.info('[note-chat] open resolved', { - noteId: note.id, - chatId: chat.id, - title: chat.title, - hasChatPayload: !!chat.chat - }); - noteChatId = chat.id; - noteChats = chats ?? [chat]; - showNoteChat = true; - } else { - console.warn('[note-chat] open returned no chat id', { noteId: note.id, chat }); - } + noteChats = chats ?? []; + if (noteChats.length > 0) noteChatId = noteChats[0].id; + else noteChatDraftKey = `${Date.now()}`; }; + $: if (note?.id && note.id !== noteChatNoteId) { + noteChatNoteId = note.id; + loadNoteChat(note.id); + } const createNoteChat = async () => { if (!note?.id || noteChatLoading) return; noteChatId = null; noteChatDraftKey = `${Date.now()}`; - showNoteChat = true; + noteChatExpanded = true; }; const createNoteChatOnFirstMessage = async () => { @@ -907,7 +878,6 @@ ${content} if (chat?.id) { noteChats = chats ?? [chat, ...noteChats.filter((item) => item.id !== chat.id)]; - showNoteChat = true; } return chat; @@ -936,16 +906,8 @@ ${content} })) ?? []; if (noteChatId === chatId) { - let nextChat = chats[0]; - if (!nextChat) { - nextChat = await getNoteChatById(localStorage.token, note.id).catch((error) => { - console.error('[note-chat] recreate failed after delete', { noteId: note?.id, error }); - toast.error(`${error}`); - return null; - }); - chats = nextChat ? [nextChat] : []; - } - noteChatId = nextChat?.id ?? null; + noteChatId = chats[0]?.id ?? null; + if (!noteChatId) noteChatDraftKey = `${Date.now()}`; } noteChats = chats; @@ -1293,23 +1255,35 @@ ${content} {/if} {/if} - - - - - (currentNoteComments = request)} - resource={{ resource_type: 'note', resource_id: note.id }} - /> + + (currentNoteComments = currentNoteComments ? null : request)} + resource={{ resource_type: 'note', resource_id: note.id }} + /> +
+ {#if currentNoteComments} + {#key `${$user.id}:${note.id}:${currentNoteComments.comment_id ?? ''}`} + (currentNoteComments = null)} + /> + {/key} + {/if} +
+ + {/if} {#if note?.write_access} - {$i18n.t( 'A page changed elsewhere. Your previous text is available to download.' )} - +
{/if}
@@ -1597,92 +1576,179 @@ ${content} onDownload={(pageId, format) => downloadHandler(format, pageId)} /> -
+
- (currentNoteComments = { - resource_type: 'note', - resource_id: note.id, - ...target - }) - }} + class="min-w-0 flex-1 h-full overflow-auto px-3 pb-32 relative flex flex-col" + id="note-content-container" + bind:this={noteContent} + role="tabpanel" + aria-label={activePage?.id === 'main' && activePage.inherit_note_title !== false + ? note.title || $i18n.t('Untitled') + : activePage?.title} > - {#each [activePage] as renderedPage (`${note.id}:${renderedPage.id}:${renderedPage.generation}:${autoFormat}`)} - (editor = event.detail)} - id={`note-${note.id}-${renderedPage.id}`} - className="input-prose-sm ps-4 pe-0.5 pt-2 flex-1 min-h-[12rem]" - json={true} - bind:value={renderedPage.content.json} - html={editorHtml} - documentId={activeDocumentId} - bind:cursorTarget - collaboration={true} - socket={$socket} - user={$user} - dragHandle={true} - link={true} - image={true} - attachments={true} - placeholder={$i18n.t('Write something...')} - editable={versionIdx === null && note?.write_access} - onComment={selectionCommentsEnabled ? commentOnSelection : null} - onSelectionUpdate={({ editor }) => { - const { from, to } = editor.state.selection; - const selectedText = editor.state.doc.textBetween(from, to, ' '); +
+ (currentNoteComments = { + resource_type: 'note', + resource_id: note.id, + ...target + }) + }} + > + {#each [activePage] as renderedPage (`${note.id}:${renderedPage.id}:${renderedPage.generation}:${autoFormat}`)} + { + editor = event.detail; + focusOpenedNote(event.detail); + }} + id={`note-${note.id}-${renderedPage.id}`} + className="input-prose-sm ps-4 pe-0.5 pt-2 flex-1 min-h-[12rem]" + json={true} + bind:value={renderedPage.content.json} + html={editorHtml} + documentId={activeDocumentId} + bind:cursorTarget + collaboration={true} + socket={$socket} + user={$user} + dragHandle={true} + link={true} + image={true} + attachments={true} + placeholder={$i18n.t('Write something...')} + editable={versionIdx === null && note?.write_access} + onComment={selectionCommentsEnabled ? commentOnSelection : null} + onSelectionUpdate={({ editor }) => { + const { from, to } = editor.state.selection; + const selectedText = editor.state.doc.textBetween(from, to, ' '); - if (selectedText.length === 0) { - selectedContent = null; - } else { - selectedContent = { - text: selectedText, - from: from, - to: to - }; - } - }} - onChange={(content) => { - renderedPage.content.json = content.json; - renderedPage.content.html = content.html; - renderedPage.content.md = content.md; - }} - fileHandler={true} - onFileDrop={(currentEditor, files, pos) => - inputFilesHandler(files, currentEditor, pos)} - onFilePaste={() => {}} - on:paste={(event) => { - const e = event.detail.event || event; - const pasted = Array.from(e.clipboardData?.files ?? []); - if (pasted.length) { - e.preventDefault(); - inputFilesHandler(pasted); - } - }} - /> - {/each} + if (selectedText.length === 0) { + selectedContent = null; + } else { + selectedContent = { + text: selectedText, + from: from, + to: to + }; + } + }} + onChange={(content) => { + renderedPage.content.json = content.json; + renderedPage.content.html = content.html; + renderedPage.content.md = content.md; + }} + fileHandler={true} + onFileDrop={(currentEditor, files, pos) => + inputFilesHandler(files, currentEditor, pos)} + onFilePaste={() => {}} + on:paste={(event) => { + const e = event.detail.event || event; + const pasted = Array.from(e.clipboardData?.files ?? []); + if (pasted.length) { + e.preventDefault(); + inputFilesHandler(pasted); + } + }} + /> + {/each} +
+ {#if note && !noteChatLoading && (noteChatId || noteChatDraftKey)} + +
+ {#if !noteChatExpanded} +
+ {/if} +
+ +
+ {#if !noteChatExpanded} + + {/if} +
+ {#key note.id} + (noteChatExpanded = false)} + onBeforeNoteSubmit={async () => { + noteChatExpanded = true; + await flushActivePage(); + }} + onInsertToNote={note?.write_access ? insertHandler : null} + onNewEmbeddedChat={createNoteChat} + onCreateEmbeddedChat={createNoteChatOnFirstMessage} + onSelectEmbeddedChat={(chatId) => { + if (!chatId || chatId === noteChatId) return; + noteChatId = chatId; + noteChatDraftKey = ''; + }} + onDeleteEmbeddedChat={deleteNoteChat} + onEmbeddedChatTitle={(chatId, title) => { + noteChats = noteChats.map((chat) => + chat.id === chatId + ? { + ...chat, + title, + chat: { + ...(chat.chat ?? {}), + title + } + } + : chat + ); + }} + /> + {/key} +
+
+
+
+ {/if}
@@ -1715,90 +1781,4 @@ ${content}
{/if}
- -
-
- {#each commentsEnabled ? ['Chat', 'Comments'] : ['Chat'] as tab} - - {/each} -
- -
- {#if currentNoteComments} -
- {#key `${$user.id}:${note.id}:${currentNoteComments.comment_id ?? ''}`} - (noteCommentCount = count)} - /> - {/key} -
- {/if} -
- {#if noteChatLoading} -
- -
- {:else if note && (noteChatId || noteChatDraftKey)} - { - if (!chatId || chatId === noteChatId) return; - noteChatId = chatId; - noteChatDraftKey = ''; - }} - onDeleteEmbeddedChat={deleteNoteChat} - onEmbeddedChatTitle={(chatId, title) => { - noteChats = noteChats.map((chat) => - chat.id === chatId - ? { - ...chat, - title, - chat: { - ...(chat.chat ?? {}), - title - } - } - : chat - ); - }} - /> - {/if} -
-
diff --git a/src/lib/components/notes/NotesPage.svelte b/src/lib/components/notes/NotesPage.svelte index 0d6b7afa37..d72f08e433 100644 --- a/src/lib/components/notes/NotesPage.svelte +++ b/src/lib/components/notes/NotesPage.svelte @@ -54,7 +54,7 @@ >
{#if loaded} -
+
{:else}