feat(audio): add audio file preview support and enhance UI components
- Integrated audio file preview functionality in document content and preview components. - Updated localization files to include audio-related messages. - Enhanced the UI to display an audio player for supported audio formats. - Implemented audio loading state and error handling for unsupported audio playback.
This commit is contained in:
@@ -6,7 +6,7 @@ import hljs from "highlight.js";
|
||||
import "highlight.js/styles/github.css";
|
||||
import mermaid from "mermaid";
|
||||
import { onMounted, ref, nextTick, onUnmounted, watch, computed } from "vue";
|
||||
import { downKnowledgeDetails, deleteGeneratedQuestion, getChunkByIdOnly } from "@/api/knowledge-base/index";
|
||||
import { downKnowledgeDetails, deleteGeneratedQuestion, getChunkByIdOnly, previewKnowledgeFile } from "@/api/knowledge-base/index";
|
||||
import { MessagePlugin, DialogPlugin } from "tdesign-vue-next";
|
||||
import { sanitizeHTML, safeMarkdownToHTML, createSafeImage, isValidImageURL, hydrateProtectedFileImages } from '@/utils/security';
|
||||
import { openMermaidFullscreen } from '@/utils/mermaidViewer';
|
||||
@@ -167,6 +167,9 @@ onUnmounted(() => {
|
||||
if (doc) {
|
||||
doc.removeEventListener('scroll', handleDetailsScroll);
|
||||
}
|
||||
if (audioBlobUrl.value) {
|
||||
URL.revokeObjectURL(audioBlobUrl.value);
|
||||
}
|
||||
})
|
||||
const checkImage = (url) => {
|
||||
return new Promise((resolve) => {
|
||||
@@ -257,18 +260,29 @@ const previewSupportedTypes = new Set([
|
||||
'py', 'java', 'go', 'cpp', 'c', 'h', 'sh', 'yaml', 'yml',
|
||||
'ini', 'conf', 'log', 'sql', 'rs', 'rb', 'php', 'swift', 'kt',
|
||||
'scala', 'r', 'lua', 'pl', 'toml',
|
||||
'mp3', 'wav', 'm4a', 'flac', 'ogg',
|
||||
]);
|
||||
|
||||
const canPreview = (): boolean => {
|
||||
if (props.details?.type !== 'file') return false;
|
||||
const ft = props.details?.file_type?.toLowerCase();
|
||||
return !!ft && previewSupportedTypes.has(ft);
|
||||
if (!ft) return false;
|
||||
if (audioExtensions.has(ft)) return false; // 音频不走预览tab,播放器已内嵌
|
||||
return previewSupportedTypes.has(ft);
|
||||
};
|
||||
|
||||
// 当文档详情加载完成时,file 类型自动切换到「预览」
|
||||
// 当文档详情加载完成时,file 类型自动切换到「预览」;音频类型使用 merged + 播放器
|
||||
watch(() => props.details?.id, (newId) => {
|
||||
// 清理旧音频
|
||||
if (audioBlobUrl.value) {
|
||||
URL.revokeObjectURL(audioBlobUrl.value);
|
||||
audioBlobUrl.value = '';
|
||||
}
|
||||
if (!newId) return;
|
||||
if (props.details?.type === 'file' && canPreview()) {
|
||||
if (isAudioFile(props.details?.file_type)) {
|
||||
viewMode.value = 'merged'; // 音频默认全文视图,播放器已内嵌
|
||||
loadAudioPreview();
|
||||
} else if (props.details?.type === 'file' && canPreview()) {
|
||||
viewMode.value = 'preview';
|
||||
} else {
|
||||
viewMode.value = 'merged';
|
||||
@@ -285,6 +299,28 @@ const isMarkdownFile = (fileType?: string): boolean => {
|
||||
const markdownTypes = ['md', 'markdown'];
|
||||
return markdownTypes.includes(fileType.toLowerCase());
|
||||
};
|
||||
|
||||
// 音频文件判断与播放器状态
|
||||
const audioExtensions = new Set(['mp3', 'wav', 'm4a', 'flac', 'ogg']);
|
||||
const isAudioFile = (fileType?: string): boolean => {
|
||||
if (!fileType) return false;
|
||||
return audioExtensions.has(fileType.toLowerCase());
|
||||
};
|
||||
const audioBlobUrl = ref('');
|
||||
const audioLoading = ref(false);
|
||||
|
||||
const loadAudioPreview = async () => {
|
||||
if (!props.details?.id || audioBlobUrl.value) return;
|
||||
audioLoading.value = true;
|
||||
try {
|
||||
const blob = await previewKnowledgeFile(props.details.id);
|
||||
audioBlobUrl.value = URL.createObjectURL(blob);
|
||||
} catch (err) {
|
||||
console.error('Audio preview load failed:', err);
|
||||
} finally {
|
||||
audioLoading.value = false;
|
||||
}
|
||||
};
|
||||
const runMarkdownPostRenderPipeline = async () => {
|
||||
await nextTick();
|
||||
const renderRoot = mdContentWrap.value as ParentNode;
|
||||
@@ -807,7 +843,18 @@ const handleDetailsScroll = () => {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- 音频播放器(音频文件时固定显示在内容区顶部) -->
|
||||
<div v-if="isAudioFile(details.file_type)" class="audio-player-section">
|
||||
<div v-if="audioLoading" class="audio-loading">
|
||||
<t-loading size="small" />
|
||||
<span>{{ $t('preview.audioLoading') }}</span>
|
||||
</div>
|
||||
<audio v-else-if="audioBlobUrl" controls class="audio-player" :src="audioBlobUrl">
|
||||
{{ $t('preview.audioNotSupported') }}
|
||||
</audio>
|
||||
</div>
|
||||
|
||||
<!-- 合并视图 -->
|
||||
<div v-if="viewMode === 'merged'">
|
||||
<div v-if="!mergedContent" class="no_content">{{ $t('common.noData') }}</div>
|
||||
@@ -1330,6 +1377,29 @@ const handleDetailsScroll = () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 音频播放器样式
|
||||
.audio-player-section {
|
||||
margin-bottom: 16px;
|
||||
padding: 12px 16px;
|
||||
background: var(--td-bg-color-container-hover);
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--td-component-border);
|
||||
|
||||
.audio-player {
|
||||
width: 100%;
|
||||
height: 40px;
|
||||
}
|
||||
|
||||
.audio-loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
color: var(--td-text-color-placeholder);
|
||||
font-size: 13px;
|
||||
padding: 4px 0;
|
||||
}
|
||||
}
|
||||
|
||||
.md-content {
|
||||
word-break: break-word;
|
||||
line-height: 1.6;
|
||||
|
||||
@@ -21,7 +21,7 @@ const props = defineProps<{
|
||||
|
||||
const loading = ref(false);
|
||||
const error = ref('');
|
||||
const previewType = ref<'pdf' | 'docx' | 'image' | 'excel' | 'text' | 'markdown' | 'pptx' | 'unsupported'>('unsupported');
|
||||
const previewType = ref<'pdf' | 'docx' | 'image' | 'excel' | 'text' | 'markdown' | 'pptx' | 'audio' | 'unsupported'>('unsupported');
|
||||
const blobUrl = ref('');
|
||||
const textContent = ref('');
|
||||
const highlightedCode = ref('');
|
||||
@@ -55,6 +55,7 @@ const fileTypeMap: Record<string, typeof previewType.value> = {};
|
||||
['txt', 'json', 'xml', 'html', 'css', 'js', 'ts', 'py', 'java', 'go',
|
||||
'cpp', 'c', 'h', 'sh', 'yaml', 'yml', 'ini', 'conf', 'log', 'sql', 'rs', 'rb', 'php',
|
||||
'swift', 'kt', 'scala', 'r', 'lua', 'pl', 'toml'].forEach(t => fileTypeMap[t] = 'text');
|
||||
['mp3', 'wav', 'm4a', 'flac', 'ogg'].forEach(t => fileTypeMap[t] = 'audio');
|
||||
|
||||
const mimeTypeMap: Record<string, string> = {
|
||||
pdf: 'application/pdf',
|
||||
@@ -73,6 +74,8 @@ const mimeTypeMap: Record<string, string> = {
|
||||
html: 'text/html', css: 'text/css',
|
||||
js: 'text/javascript', ts: 'text/typescript',
|
||||
py: 'text/x-python', java: 'text/x-java', go: 'text/x-go',
|
||||
mp3: 'audio/mpeg', wav: 'audio/wav', m4a: 'audio/mp4',
|
||||
flac: 'audio/flac', ogg: 'audio/ogg',
|
||||
};
|
||||
|
||||
function getMimeType(ft: string): string {
|
||||
@@ -282,6 +285,10 @@ async function loadPreview() {
|
||||
pptxData.value = await blob.arrayBuffer();
|
||||
break;
|
||||
}
|
||||
case 'audio': {
|
||||
blobUrl.value = URL.createObjectURL(blob);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error('Document preview failed:', err);
|
||||
@@ -399,6 +406,17 @@ onUnmounted(() => {
|
||||
<div v-else-if="previewType === 'text' && highlightedCode" class="preview-text">
|
||||
<pre class="code-preview"><code class="hljs" v-html="highlightedCode"></code></pre>
|
||||
</div>
|
||||
|
||||
<!-- Audio -->
|
||||
<div v-else-if="previewType === 'audio' && blobUrl" class="preview-audio">
|
||||
<div class="audio-wrapper">
|
||||
<t-icon name="sound" size="48px" />
|
||||
<p class="audio-filename">{{ fileName }}</p>
|
||||
<audio controls :src="blobUrl" class="audio-element">
|
||||
{{ $t('preview.audioNotSupported') }}
|
||||
</audio>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -633,6 +651,22 @@ onUnmounted(() => {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Audio ──
|
||||
.preview-audio {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
padding: 40px 20px;
|
||||
.audio-wrapper {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
color: @text-secondary;
|
||||
.audio-filename { font-size: 14px; color: @text-primary; margin: 0; }
|
||||
.audio-element { width: 100%; max-width: 480px; }
|
||||
}
|
||||
}
|
||||
|
||||
// ── Deep styles (v-html / third-party components) ──
|
||||
|
||||
// Shared table mixin for v-html content
|
||||
|
||||
@@ -3011,6 +3011,8 @@ export default {
|
||||
unsupportedHint: 'Please download and open with a local application',
|
||||
fullscreen: 'Fullscreen',
|
||||
exitFullscreen: 'Exit Fullscreen',
|
||||
audioLoading: 'Loading audio…',
|
||||
audioNotSupported: 'Your browser does not support audio playback',
|
||||
},
|
||||
knowledgeSearch: {
|
||||
title: 'Search',
|
||||
|
||||
@@ -3065,6 +3065,8 @@ export default {
|
||||
unsupportedHint: '파일을 다운로드하여 로컬 앱으로 열어주세요',
|
||||
fullscreen: '전체 화면',
|
||||
exitFullscreen: '전체 화면 종료',
|
||||
audioLoading: '오디오 로딩 중…',
|
||||
audioNotSupported: '브라우저가 오디오 재생을 지원하지 않습니다',
|
||||
},
|
||||
knowledgeSearch: {
|
||||
title: '검색',
|
||||
|
||||
@@ -2682,6 +2682,8 @@ export default {
|
||||
unsupportedHint: 'Скачайте файл и откройте локально',
|
||||
fullscreen: 'Полноэкранный режим',
|
||||
exitFullscreen: 'Выйти из полноэкранного режима',
|
||||
audioLoading: 'Загрузка аудио…',
|
||||
audioNotSupported: 'Ваш браузер не поддерживает воспроизведение аудио',
|
||||
},
|
||||
knowledgeSearch: {
|
||||
title: 'Поиск',
|
||||
|
||||
@@ -3009,6 +3009,8 @@ export default {
|
||||
unsupportedHint: "请下载文件后使用本地应用查看",
|
||||
fullscreen: "全屏预览",
|
||||
exitFullscreen: "退出全屏",
|
||||
audioLoading: "加载音频中…",
|
||||
audioNotSupported: "您的浏览器不支持音频播放",
|
||||
},
|
||||
knowledgeSearch: {
|
||||
title: "搜索",
|
||||
|
||||
@@ -237,6 +237,7 @@
|
||||
:selected-model-id="formData.asrConfig.modelId"
|
||||
:all-models="allModels"
|
||||
@update:selected-model-id="(val: string) => { if (formData) formData.asrConfig.modelId = val }"
|
||||
@add-model="handleAddASRModel"
|
||||
:placeholder="$t('knowledgeEditor.asr.modelPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
@@ -557,6 +558,10 @@ const handleAddVLLMModel = () => {
|
||||
uiStore.openSettings('models', 'vllm')
|
||||
}
|
||||
|
||||
const handleAddASRModel = () => {
|
||||
uiStore.openSettings('models', 'asr')
|
||||
}
|
||||
|
||||
const handleStorageProviderUpdate = (value: string) => {
|
||||
if (formData.value) {
|
||||
formData.value.storageProvider = value || 'local'
|
||||
|
||||
Reference in New Issue
Block a user