import type { WordpressBlogApi } from '#/api/blog'; import { computed, defineComponent, ref, watch } from 'vue'; import { useRoute, useRouter } from 'vue-router'; import { ArrowLeft } from '@vben/icons'; import { Alert, Button, Space, Spin, Tag } from 'antdv-next'; import { buildKtBlogPreviewUrl, getArticleDetail } from '#/api/blog'; import './index.scss'; type PreviewState = 'error' | 'loading' | 'ready'; const AAlert = Alert as any; const AButton = Button as any; const ASpace = Space as any; const ASpin = Spin as any; const ATag = Tag as any; const articleStatusOptions = [ { color: 'success', label: '已发布', value: 'publish' }, { color: 'default', label: '草稿', value: 'draft' }, { color: 'warning', label: '待审核', value: 'pending' }, { color: 'processing', label: '私有', value: 'private' }, ] as const; export default defineComponent({ name: 'BlogArticlePreview', setup() { const route = useRoute(); const router = useRouter(); const article = ref(null); const errorMessage = ref(''); const state = ref('loading'); const routeArticleId = computed(() => normalizeRouteParam(route.params.articleId), ); const previewTitle = computed( () => getArticleTitle(article.value) || '文章预览', ); const iframeUrl = computed(() => article.value ? buildKtBlogPreviewUrl(article.value, routeArticleId.value) : '', ); const iframeHost = computed(() => getPreviewHost(iframeUrl.value)); const statusMeta = computed(() => getStatusMeta(state.value)); const articleStatusMeta = computed(() => getArticleStatusMeta(article.value?.status), ); const primaryStatusMeta = computed(() => article.value ? articleStatusMeta.value : statusMeta.value, ); watch( routeArticleId, () => { void loadArticlePreview(); }, { immediate: true }, ); function goBack() { void router.push({ name: 'BlogArticle' }); } function openPreviewInNewWindow() { if (!iframeUrl.value) { return; } window.open(iframeUrl.value, '_blank', 'noopener,noreferrer'); } async function loadArticlePreview() { const articleId = routeArticleId.value; article.value = null; errorMessage.value = ''; if (!articleId) { state.value = 'error'; errorMessage.value = '缺少文章 ID'; return; } state.value = 'loading'; try { article.value = await getArticleDetail(articleId); state.value = 'ready'; } catch (error) { state.value = 'error'; errorMessage.value = error instanceof Error ? error.message : '文章预览加载失败'; } } const renderFloatingCard = () => { return (
{previewTitle.value} {primaryStatusMeta.value.label}
文章预览 运行态:{statusMeta.value.label} {routeArticleId.value ? ( ID:{routeArticleId.value} ) : null} {iframeHost.value ? Host:{iframeHost.value} : null}
返回 刷新 新窗口
); }; const renderBody = () => { if (state.value === 'ready' && iframeUrl.value) { return (