11<script setup lang="ts">
2- import { ref , watch } from ' vue' ;
2+ import { onMounted , ref , watch , computed } from ' vue' ;
33import { renderMarkdown } from ' @/utils/markdown' ;
44import type { WriterMessage } from ' @/utils/response'
5+ import { ScrollArea } from ' @/components/ui/scroll-area'
6+ import { getWriterSeque } from ' @/apis/commonApi' ;
57
68interface ContentSection {
79 id: number ;
810 content: string ;
911 renderedContent: string ;
12+ sub_title? : string ;
1013}
1114
1215const props = defineProps <{
1316 messages: WriterMessage []
1417}>()
1518
19+ const writerSequence = ref <string []>([]);
20+
21+ onMounted (async () => {
22+ const res = await getWriterSeque ();
23+ writerSequence .value = Array .isArray (res .data ) ? res .data : [];
24+ });
25+
1626const sections = ref <ContentSection []>([]);
1727let nextId = 0 ;
1828
1929// 添加新的内容段落
20- const appendContent = async (content : string ) => {
30+ const appendContent = async (content : string , sub_title ? : string ) => {
2131 const renderedContent = await renderMarkdown (content );
2232 sections .value .push ({
2333 id: nextId ++ ,
2434 content ,
25- renderedContent
35+ renderedContent ,
36+ sub_title
2637 });
2738};
2839
40+ // 根据 writerSequence 排序内容
41+ const sortedSections = computed (() => {
42+ if (! writerSequence .value .length ) return sections .value ;
43+
44+ return [... sections .value ].sort ((a , b ) => {
45+ const aIndex = a .sub_title ? writerSequence .value .indexOf (a .sub_title ) : Infinity ;
46+ const bIndex = b .sub_title ? writerSequence .value .indexOf (b .sub_title ) : Infinity ;
47+
48+ if (aIndex === Infinity && bIndex === Infinity ) return 0 ;
49+ if (aIndex === Infinity ) return 1 ;
50+ if (bIndex === Infinity ) return - 1 ;
51+
52+ return aIndex - bIndex ;
53+ });
54+ });
55+
2956// 监听消息变化
3057watch (() => props .messages , async (messages ) => {
3158 // 清空现有内容
@@ -35,27 +62,25 @@ watch(() => props.messages, async (messages) => {
3562 // 按顺序添加每个消息的内容
3663 for (const msg of messages ) {
3764 if (msg .content ) {
38- await appendContent (msg .content );
65+ await appendContent (msg .content , msg . sub_title );
3966 }
4067 }
4168}, { immediate: true });
4269 </script >
4370
4471<template >
45- <div class =" h-full overflow-hidden" >
46- <div class =" h-full overflow-y-auto p-6" >
47- <div class =" max-w-4xl mx-auto space-y-6" >
48- <TransitionGroup name =" section" tag =" div" class =" space-y-6" >
49- <div v-for =" section in sections" :key =" section.id"
50- class =" bg-white rounded-lg shadow-lg overflow-hidden transform transition-all duration-500" >
51- <div class =" p-6" >
52- <div class =" prose prose-slate max-w-none" v-html =" section.renderedContent" ></div >
53- </div >
72+ <ScrollArea class =" h-full overflow-y-auto p-6" >
73+ <div class =" max-w-4xl mx-auto space-y-6" >
74+ <TransitionGroup name =" section" tag =" div" class =" space-y-6" >
75+ <div v-for =" section in sortedSections" :key =" section.id"
76+ class =" bg-white rounded-lg shadow-lg overflow-hidden transform transition-all duration-500" >
77+ <div class =" p-6" >
78+ <div class =" prose prose-slate max-w-none" v-html =" section.renderedContent" ></div >
5479 </div >
55- </TransitionGroup >
56- </div >
80+ </div >
81+ </TransitionGroup >
5782 </div >
58- </div >
83+ </ScrollArea >
5984</template >
6085
6186<style >
0 commit comments