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);
|
||||
};
|
||||
Reference in New Issue
Block a user