Files
frontend/src/composables/template/useTemplatePreviewPage.js
T
2026-08-11 15:03:02 +08:00

134 lines
4.3 KiB
JavaScript

import { computed, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { createDocument as createDocumentApi, getTemplate } from '@/services/api';
import { useApiAction } from '@/composables/actions/useApiAction';
import { DEFAULT_DOCUMENT_TYPE, normalizeDocumentType } from '@/utils/documentType';
import { formatDate as _formatDate } from '@/utils/formatDate';
export function useTemplatePreviewPage({ t, route, router }) {
const { runWithLoading } = useApiAction({ t });
const loading = ref(false);
const template = ref(null);
const showCreateDialog = ref(false);
const creatingLoading = ref(false);
const templateId = computed(() => route.params.templateId || route.params.id);
const formatDate = (value) => _formatDate(value, { fallback: t('common.unknown') });
const scopeLabel = computed(() => {
const scope = template.value?.scope;
if (scope === 'system') return t('templates.public');
if (scope === 'knowledge_base') return t('templates.scopeKb');
if (scope === 'private') return t('templates.private');
return '';
});
const scopeTagType = computed(() => {
const scope = template.value?.scope;
if (scope === 'system') return 'success';
if (scope === 'knowledge_base') return 'warning';
return 'info';
});
const previewContent = computed(() => String(template.value?.content || ''));
const previewDocumentType = computed(() => template.value?.type || DEFAULT_DOCUMENT_TYPE);
const fetchTemplate = async () => {
if (!templateId.value) return;
await runWithLoading(
loading,
() => getTemplate(templateId.value, { record_recent_log: true }),
{
context: 'fetchTemplate',
errorMessage: t('common.requestFailed'),
onSuccess: (data) => {
template.value = data.template;
}
}
);
};
const goBack = () => {
router.push('/templates');
};
const openCreateDocumentDialog = () => {
showCreateDialog.value = true;
};
const cancelCreateDocument = () => {
showCreateDialog.value = false;
};
const createDocument = async (payload) => {
const title = payload?.title?.trim() || t('document.untitledDefaultTitle');
const containerType = payload?.container_type === 'knowledge_base' ? 'knowledge_base' : 'own';
const selectedKBExternalID =
payload?.knowledge_base_external_id || template.value?.knowledge_base_external_id || '';
if (containerType === 'knowledge_base' && !selectedKBExternalID) {
ElMessage.error(t('knowledgeBase.selectKnowledgeBase'));
return;
}
await runWithLoading(
creatingLoading,
async () => {
const requestBody = {
title,
type: normalizeDocumentType(payload?.type || template.value?.type || DEFAULT_DOCUMENT_TYPE),
container_type: containerType,
is_public: typeof payload?.is_public === 'boolean' ? payload.is_public : false
};
if (containerType === 'knowledge_base') {
requestBody.knowledge_base_external_id = selectedKBExternalID;
}
if (payload?.parent_external_id) {
requestBody.parent_external_id = payload.parent_external_id;
}
if (payload?.template) {
requestBody.template_id = payload.template;
}
const response = await createDocumentApi(requestBody);
return { response, containerType, selectedKBExternalID };
},
{
context: 'createDocumentFromTemplate',
errorMessage: t('knowledgeBase.createDocumentError'),
onSuccess: ({ response, containerType, selectedKBExternalID }) => {
showCreateDialog.value = false;
if (response?.external_id) {
if (containerType === 'knowledge_base') {
router.push(`/knowledge-base/${selectedKBExternalID}/document/${response.external_id}`);
} else {
router.push(`/mydocument/${response.external_id}`);
}
}
}
}
);
};
const init = async () => {
await fetchTemplate();
};
return {
loading,
template,
showCreateDialog,
creatingLoading,
previewContent,
previewDocumentType,
scopeLabel,
scopeTagType,
formatDate,
goBack,
openCreateDocumentDialog,
cancelCreateDocument,
createDocument,
init
};
}