Files
open-webui/src/lib/components/chat/Messages/ContentRenderer.svelte
T
Classic298 571e4e3b52 fix: Enter or Escape while typing with an IME saves, submits or closes too early (#35295)
When typing Japanese, Chinese or Korean with an input method (IME), Enter, Space and the arrow keys pick conversion candidates and Escape drops them. Many inputs treated those keys as their own: Escape stopped a running response, threw away a message, cell, rename or comment draft, cleared a search or closed the dialog, drawer, menu or model selector, and Enter or Space saved, submitted, searched, picked a model or added the text while it was still half-typed. Inputs, dialogs and menus that act on these keys now leave keys that belong to the IME alone, the same way chat rename already does.

Fixes #35294
2026-11-30 18:26:49 +04:00

399 lines
11 KiB
Svelte

<script>
import {
commentSurface,
contentHash,
commentSelectionDraft,
selectedComment
} from '$lib/components/comments/anchors';
import { config, user, mobile, settings, showEmbeds } from '$lib/stores';
import { onDestroy, onMount, tick, getContext, setContext } from 'svelte';
import { writable } from 'svelte/store';
const i18n = getContext('i18n');
import Markdown from './Markdown.svelte';
import StructuredOutputRenderer from './StructuredOutputRenderer.svelte';
import { getOutputText } from './structuredOutput';
import FloatingButtons from '../ContentRenderer/FloatingButtons.svelte';
import { createMessagesList, replaceOutsideCode } from '$lib/utils';
import { getCitations } from '$lib/utils/citations';
/**
* Extracts all top-level <details>...</details> blocks from content,
* handling nested <details> via depth tracking.
* Returns { detailsContent, plainContent }.
*/
const extractDetailsBlocks = (text) => {
const blocks = [];
let remaining = text;
let result = '';
const openTag = '<details';
const closeTag = '</details>';
while (true) {
const start = remaining.indexOf(openTag);
if (start === -1) {
result += remaining;
break;
}
result += remaining.slice(0, start);
// Find matching closing tag with depth tracking
let depth = 1;
let idx = start + openTag.length;
while (depth > 0 && idx < remaining.length) {
if (remaining.startsWith(openTag, idx)) {
depth++;
} else if (remaining.startsWith(closeTag, idx)) {
depth--;
}
if (depth > 0) idx++;
}
if (depth === 0) {
const end = idx + closeTag.length;
blocks.push(remaining.slice(start, end));
remaining = remaining.slice(end);
} else {
// Unmatched opening tag, treat as plain text
result += remaining.slice(start);
remaining = '';
break;
}
}
return {
detailsContent: blocks.join('\n'),
plainContent: result.trim()
};
};
export let id;
export let chatId = '';
export let content;
/** @type {import('./structuredOutput').OutputItem[]} */
export let output = [];
export let history;
export let messageId;
export let selectedModels = [];
export let done = true;
export let model = null;
export let sources = null;
export let save = false;
export let preview = false;
export let compactPreview = false;
export let floatingButtons = true;
export let editCodeBlock = true;
export let topPadding = false;
export let allowEmbeds = false;
export let allowToolApproval = false;
export let onSave = (e) => {};
export let onSourceClick = (e) => {};
export let onTaskClick = (e) => {};
export let onToolCallResolved = (e) => {};
export let onSetInputText = (text) => {};
/** Opens the chat's artifacts; null where messages render outside a chat. */
/** @type {((code?: string) => void) | null} */
export let onOpenArtifacts = null;
/** @type {HTMLDivElement | undefined} */
let contentContainerElement;
/** @type {{ closeHandler: () => void } | undefined} */
let floatingButtonsElement;
/** @type {Range | null} */
let floatingButtonsRange = null;
let selectedText = '';
let usingFloatingButtons = false;
let sourceIds = [];
/** @type {import('svelte/store').Writable<any[]>} */
const citations = writable([]);
setContext('citations', citations);
$: getSourceIds(sources);
const getSourceIds = (sources) => {
const showCitations = model?.info?.meta?.capabilities?.citations ?? true;
const sourceCitations = getCitations(sources);
citations.set(showCitations ? sourceCitations : []);
// Titles by citation number; numbers are shared across the chat, so there can be gaps.
const result = [];
for (const citation of sourceCitations) {
result[citation.number - 1] = showCitations ? citation.title : 'N/A';
}
sourceIds = result;
};
/** @param {string} messageContent */
const formatMessageContent = (messageContent) =>
model?.info?.meta?.capabilities?.citations == false
? replaceOutsideCode(messageContent, (segment) =>
segment.replace(/\s*(\[(?:\d+(?:#[^,\]\s]+)?(?:,\s*\d+(?:#[^,\]\s]+)?)*)\])+/g, '')
)
: messageContent;
const initialContent = content;
const initialOutputText = getOutputText(output);
// The final code fence can render after the response is marked done.
let hasStreamed = !done;
$: if (!done) hasStreamed = true;
const seenArtifactIds = new Set();
/** @type {ReturnType<typeof selectedComment>} */
let commentSelection = null;
const hasClosingCodeFence = (raw = '') => /(?:^|\n)```[ \t]*$/.test(raw.trimEnd());
const markdownUpdateHandler = /** @type {any} */ (
async (
/** @type {{ lang?: string; raw?: string; text?: string }} */ token,
codeBlockId = ''
) => {
const { lang = '', raw = '', text: code = '' } = token;
const normalizedLang = lang.toLowerCase();
const isArtifact =
['html', 'svg'].includes(normalizedLang) ||
(normalizedLang === 'xml' && code.toLowerCase().includes('<svg'));
const artifactId = codeBlockId || `${normalizedLang}:${raw}`;
if (!isArtifact || !hasClosingCodeFence(raw) || seenArtifactIds.has(artifactId)) return;
// Restored blocks are seen too, so continuing a response cannot reopen them.
seenArtifactIds.add(artifactId);
if (
onOpenArtifacts &&
hasStreamed &&
(content !== initialContent || getOutputText(output) !== initialOutputText) &&
($settings?.detectArtifacts ?? true) &&
!compactPreview &&
!$mobile
) {
await tick();
onOpenArtifacts();
}
}
);
const previewHandler = /** @type {any} */ (
async (/** @type {string} */ value) => {
showEmbeds.set(false);
onOpenArtifacts?.(value);
}
);
const updateSelection = () => {
if (usingFloatingButtons) return;
const selection = window.getSelection();
const range = selection?.rangeCount ? selection.getRangeAt(0) : null;
if (
!selection ||
!range ||
!selection.toString().trim() ||
!contentContainerElement?.contains(range.startContainer) ||
!contentContainerElement.contains(range.endContainer)
) {
closeFloatingButtons();
return;
}
floatingButtonsElement?.closeHandler();
selectedText = selection.toString();
floatingButtonsRange = range.cloneRange();
commentSelection = $config?.features?.enable_comments && $user ? selectedComment() : null;
};
/** @param {Event} event */
const toolbarInteractionHandler = (event) => {
if (!(event.target instanceof Node)) return;
const toolbar = document.getElementById(`floating-buttons-${id}`);
// Keep the snapshot while a touch focuses an action or its question input.
usingFloatingButtons = !!toolbar?.contains(event.target);
if (!usingFloatingButtons && !contentContainerElement?.contains(event.target)) {
closeFloatingButtons();
}
};
const closeFloatingButtons = () => {
commentSelection = null;
floatingButtonsRange = null;
selectedText = '';
if (floatingButtonsElement) {
// check if closeHandler is defined
if (typeof floatingButtonsElement?.closeHandler === 'function') {
// call the closeHandler function
floatingButtonsElement?.closeHandler();
}
}
};
const keydownHandler = (e) => {
if (e.isComposing || e.keyCode === 229) return;
if (e.key === 'Escape') {
closeFloatingButtons();
}
};
// Reactive listener attachment: re-attaches when floatingButtons
// transitions from false → true (e.g. when message.done flips).
let listenersAttached = false;
function attachListeners() {
if (!listenersAttached && contentContainerElement) {
document.addEventListener('selectionchange', updateSelection);
document.addEventListener('pointerdown', toolbarInteractionHandler, true);
document.addEventListener('focusin', toolbarInteractionHandler, true);
document.addEventListener('keydown', keydownHandler);
listenersAttached = true;
}
}
function detachListeners() {
if (listenersAttached) {
document.removeEventListener('selectionchange', updateSelection);
document.removeEventListener('pointerdown', toolbarInteractionHandler, true);
document.removeEventListener('focusin', toolbarInteractionHandler, true);
document.removeEventListener('keydown', keydownHandler);
listenersAttached = false;
}
}
$: if (floatingButtons && contentContainerElement) {
attachListeners();
} else {
detachListeners();
}
onDestroy(() => {
detachListeners();
});
</script>
<div bind:this={contentContainerElement}>
{#if output?.length}
<StructuredOutputRenderer
commentToolbarId={floatingButtons ? `floating-buttons-${id}` : undefined}
{id}
{chatId}
{messageId}
{output}
{model}
{save}
{preview}
{compactPreview}
{done}
{allowEmbeds}
{allowToolApproval}
{editCodeBlock}
{topPadding}
{sourceIds}
renderMarkdown={$settings?.renderMarkdownInAssistantMessages ?? true}
{formatMessageContent}
{onSourceClick}
{onTaskClick}
{onToolCallResolved}
{onSave}
onUpdate={markdownUpdateHandler}
onPreview={previewHandler}
/>
{:else if $settings?.renderMarkdownInAssistantMessages ?? true}
<div
class="markdown-prose"
use:commentSurface={{
resource_type: 'chat',
resource_id: chatId,
target_type: 'chat_message',
target_id: messageId,
surface_id: 'content',
content_hash: contentHash(content ?? ''),
enabled: done && !compactPreview && !!chatId,
toolbar_id: floatingButtons ? `floating-buttons-${id}` : undefined
}}
>
<Markdown
{id}
{chatId}
{messageId}
content={formatMessageContent(content)}
{model}
{save}
{preview}
{compactPreview}
{done}
{allowEmbeds}
{editCodeBlock}
{topPadding}
{sourceIds}
{onSourceClick}
{onTaskClick}
{onToolCallResolved}
{onSave}
onUpdate={markdownUpdateHandler}
onPreview={previewHandler}
/>
</div>
{:else}
{@const extracted = extractDetailsBlocks(content)}
{#if extracted.detailsContent}
<!-- Render structural blocks (tool calls, reasoning, etc.) through Markdown -->
<div class="markdown-prose">
<Markdown
{id}
{chatId}
{messageId}
content={extracted.detailsContent}
{save}
{preview}
{compactPreview}
{done}
{allowEmbeds}
{onToolCallResolved}
/>
</div>
{/if}
{#if extracted.plainContent}
<div
class="whitespace-pre-wrap text-[0.90625rem]"
use:commentSurface={{
resource_type: 'chat',
resource_id: chatId,
target_type: 'chat_message',
target_id: messageId,
surface_id: 'content',
content_hash: contentHash(content ?? ''),
enabled: done && !compactPreview && !!chatId,
toolbar_id: floatingButtons ? `floating-buttons-${id}` : undefined
}}
>
{extracted.plainContent}
</div>
{/if}
{/if}
</div>
{#if floatingButtons}
<FloatingButtons
bind:this={floatingButtonsElement}
range={floatingButtonsRange}
{selectedText}
{id}
actions={$settings?.floatingActionButtons ?? []}
onComment={commentSelection
? () => {
if (!commentSelection) return;
commentSelectionDraft.set(commentSelection);
window.getSelection()?.removeAllRanges();
closeFloatingButtons();
}
: null}
onSetInputText={(text) => {
onSetInputText(text);
closeFloatingButtons();
}}
/>
{/if}