feat: add shared utilities for chat message handling and mermaid rendering
- Introduced new utility functions in `chatMessageShared.ts` for handling incomplete image placeholders, formatting titles, building markdown, and copying text to clipboard. - Added `mermaidShared.ts` to manage Mermaid initialization and rendering, including functions for creating code renderers and binding fullscreen events. - Updated `AgentStreamDisplay.vue` and `botmsg.vue` components to utilize the new utilities, improving code organization and reusability. - Removed redundant Mermaid initialization and image handling logic from components, streamlining the codebase. These changes enhance the functionality and maintainability of chat message rendering and Mermaid diagram integration.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
const STREAMING_IMAGE_PLACEHOLDER = '<span class="streaming-image-loading"><span class="streaming-image-loading__skeleton"></span></span>';
|
||||
|
||||
export const replaceIncompleteImageWithPlaceholder = (content: string): string => {
|
||||
if (!content) return '';
|
||||
|
||||
const lastImgStart = content.lastIndexOf('![');
|
||||
if (lastImgStart < 0) return content;
|
||||
|
||||
const tail = content.slice(lastImgStart);
|
||||
const hasImageOpen = tail.startsWith('![');
|
||||
const hasBracketClose = tail.includes(']');
|
||||
const hasParenOpen = tail.includes('(');
|
||||
const hasParenClose = tail.includes(')');
|
||||
if (!hasImageOpen) return content;
|
||||
|
||||
// Incomplete image syntax at stream tail, e.g. ) {
|
||||
return content.slice(0, lastImgStart) + STREAMING_IMAGE_PLACEHOLDER;
|
||||
}
|
||||
|
||||
return content;
|
||||
};
|
||||
|
||||
export const formatManualTitle = (question?: string): string => {
|
||||
if (!question) {
|
||||
return '会话摘录';
|
||||
}
|
||||
const condensed = question.replace(/\s+/g, ' ').trim();
|
||||
if (!condensed) {
|
||||
return '会话摘录';
|
||||
}
|
||||
return condensed.length > 40 ? `${condensed.slice(0, 40)}...` : condensed;
|
||||
};
|
||||
|
||||
export const buildManualMarkdown = (_question: string, answer: string): string => {
|
||||
const safeAnswer = answer?.trim() || '(无回答内容)';
|
||||
return `${safeAnswer}`;
|
||||
};
|
||||
|
||||
export const copyTextToClipboard = async (content: string): Promise<void> => {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
await navigator.clipboard.writeText(content);
|
||||
return;
|
||||
}
|
||||
|
||||
const textArea = document.createElement('textarea');
|
||||
textArea.value = content;
|
||||
textArea.style.position = 'fixed';
|
||||
textArea.style.opacity = '0';
|
||||
document.body.appendChild(textArea);
|
||||
textArea.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
};
|
||||
@@ -0,0 +1,108 @@
|
||||
import mermaid from 'mermaid';
|
||||
|
||||
let mermaidInitialized = false;
|
||||
|
||||
const MERMAID_CONFIG = {
|
||||
startOnLoad: false,
|
||||
theme: 'default',
|
||||
securityLevel: 'strict',
|
||||
fontFamily: 'PingFang SC, Microsoft YaHei, sans-serif',
|
||||
flowchart: {
|
||||
useMaxWidth: true,
|
||||
htmlLabels: true,
|
||||
curve: 'basis',
|
||||
},
|
||||
sequence: {
|
||||
useMaxWidth: true,
|
||||
diagramMarginX: 8,
|
||||
diagramMarginY: 8,
|
||||
actorMargin: 50,
|
||||
width: 150,
|
||||
height: 65,
|
||||
},
|
||||
gantt: {
|
||||
useMaxWidth: true,
|
||||
leftPadding: 75,
|
||||
gridLineStartPadding: 35,
|
||||
barHeight: 20,
|
||||
barGap: 4,
|
||||
topPadding: 50,
|
||||
},
|
||||
};
|
||||
|
||||
export const ensureMermaidInitialized = () => {
|
||||
if (mermaidInitialized) return;
|
||||
mermaid.initialize(MERMAID_CONFIG as any);
|
||||
mermaidInitialized = true;
|
||||
};
|
||||
|
||||
export const createMermaidCodeRenderer = (idPrefix: string) => {
|
||||
let mermaidCount = 0;
|
||||
|
||||
return (code: string, infostring?: string) => {
|
||||
const lang = (infostring || '').trim();
|
||||
if (lang === 'mermaid') {
|
||||
const id = `${idPrefix}-${++mermaidCount}`;
|
||||
return `<div class="mermaid" id="${id}">${code}</div>`;
|
||||
}
|
||||
|
||||
const displayLang = lang || 'Code';
|
||||
const escapedCode = code
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>');
|
||||
return `<pre><code class="language-${displayLang}">${escapedCode}</code></pre>`;
|
||||
};
|
||||
};
|
||||
|
||||
export const renderMermaidInContainer = async (
|
||||
rootElement: HTMLElement | null | undefined,
|
||||
renderedMermaidIds: Set<string>,
|
||||
) => {
|
||||
if (!rootElement) return 0;
|
||||
|
||||
const mermaidElements = rootElement.querySelectorAll<HTMLElement>('.mermaid');
|
||||
const unrenderedElements: HTMLElement[] = [];
|
||||
|
||||
mermaidElements.forEach((el) => {
|
||||
const id = el.id || `mermaid-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
if (!el.id) {
|
||||
el.id = id;
|
||||
}
|
||||
if (!renderedMermaidIds.has(el.id) && !el.querySelector('svg')) {
|
||||
renderedMermaidIds.add(el.id);
|
||||
unrenderedElements.push(el);
|
||||
}
|
||||
});
|
||||
|
||||
if (unrenderedElements.length === 0) return 0;
|
||||
|
||||
await mermaid.run({ nodes: unrenderedElements });
|
||||
return unrenderedElements.length;
|
||||
};
|
||||
|
||||
export const bindMermaidFullscreenEvents = (
|
||||
rootElement: HTMLElement | null | undefined,
|
||||
onOpenFullscreen: (svgOuterHTML: string) => void,
|
||||
) => {
|
||||
if (!rootElement) return;
|
||||
|
||||
const mermaidDivs = rootElement.querySelectorAll<HTMLElement>('.mermaid');
|
||||
mermaidDivs.forEach((div) => {
|
||||
div.style.cursor = 'pointer';
|
||||
const oldHandler = (div as any).__mermaidClickHandler as EventListener | undefined;
|
||||
if (oldHandler) {
|
||||
div.removeEventListener('click', oldHandler);
|
||||
}
|
||||
const handler: EventListener = (e: Event) => {
|
||||
e.stopPropagation();
|
||||
const target = e.currentTarget as HTMLElement;
|
||||
const svg = target.querySelector('svg');
|
||||
if (svg) {
|
||||
onOpenFullscreen(svg.outerHTML);
|
||||
}
|
||||
};
|
||||
(div as any).__mermaidClickHandler = handler;
|
||||
div.addEventListener('click', handler);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user