Skip to content

Commit 7329c72

Browse files
committed
feat: optimize paragraph list handling with pagination and local state management
--bug=1057685 --user=刘瑞斌 【知识库】上传一个txt文档,智能分段,页面会崩溃 https://www.tapd.cn/62980211/s/1727688
1 parent d26872a commit 7329c72

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

ui/src/views/knowledge/component/ParagraphList.vue

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,18 @@
4848
</template>
4949
<script setup lang="ts">
5050
import { cloneDeep } from 'lodash'
51-
import { ref, computed } from 'vue'
51+
import { ref, computed, watchEffect } from 'vue'
5252
import EditParagraphDialog from './EditParagraphDialog.vue'
5353
import { MsgConfirm } from '@/utils/message'
5454
import { t } from '@/locales'
55+
5556
const page_size = ref<number>(30)
5657
const current_page = ref<number>(1)
5758
const currentCIndex = ref<number>(0)
5859
const EditParagraphDialogRef = ref()
5960
const emit = defineEmits(['update:modelValue'])
6061
const loading = ref<boolean>(false)
61-
const editHandle = (item: any, cIndex: number) => {
62-
currentCIndex.value = cIndex
63-
EditParagraphDialogRef.value.open(item)
64-
}
62+
const localParagraphList = ref<any[]>([])
6563
6664
const props = defineProps({
6765
modelValue: {
@@ -72,14 +70,32 @@ const props = defineProps({
7270
knowledgeId: String
7371
})
7472
73+
// 监听分页变化,只加载需要的数据
74+
watchEffect(() => {
75+
const start = 0;
76+
const end = page_size.value * current_page.value;
77+
// 只获取所需数量的数据,而不是每次都对整个数组进行切片
78+
if (end <= props.modelValue.length) {
79+
localParagraphList.value = props.modelValue.slice(start, end);
80+
}
81+
})
82+
7583
const paragraph_list = computed(() => {
76-
return props.modelValue.slice(0, page_size.value * (current_page.value - 1) + page_size.value)
84+
return localParagraphList.value;
7785
})
7886
7987
const next = () => {
80-
loading.value = true
81-
current_page.value += 1
82-
loading.value = false
88+
loading.value = true;
89+
setTimeout(() => {
90+
current_page.value += 1;
91+
loading.value = false;
92+
}, 100); // 添加小延迟让UI有时间更新
93+
}
94+
95+
const editHandle = (item: any, cIndex: number) => {
96+
// 计算实际索引,考虑分页
97+
currentCIndex.value = cIndex + (page_size.value * (current_page.value - 1));
98+
EditParagraphDialogRef.value.open(item)
8399
}
84100
85101
const updateContent = (data: any) => {
@@ -95,9 +111,18 @@ const updateContent = (data: any) => {
95111
}
96112
new_value[currentCIndex.value] = cloneDeep(data)
97113
emit('update:modelValue', new_value)
114+
115+
// 更新本地列表
116+
const localIndex = currentCIndex.value - (page_size.value * (current_page.value - 1));
117+
if (localIndex >= 0 && localIndex < localParagraphList.value.length) {
118+
localParagraphList.value[localIndex] = cloneDeep(data);
119+
}
98120
}
99121
100122
const deleteHandle = (item: any, cIndex: number) => {
123+
// 计算实际索引,考虑分页
124+
const actualIndex = cIndex + (page_size.value * (current_page.value - 1));
125+
101126
MsgConfirm(
102127
`${t('views.paragraph.delete.confirmTitle')}${item.title || '-'} ?`,
103128
t('views.paragraph.delete.confirmMessage'),
@@ -108,8 +133,18 @@ const deleteHandle = (item: any, cIndex: number) => {
108133
)
109134
.then(() => {
110135
const new_value = [...props.modelValue]
111-
new_value.splice(cIndex, 1)
136+
new_value.splice(actualIndex, 1)
112137
emit('update:modelValue', new_value)
138+
139+
// 更新本地列表
140+
localParagraphList.value.splice(cIndex, 1);
141+
// 如果当前页删除完了,从总数据中再取一条添加到末尾
142+
if (props.modelValue.length > localParagraphList.value.length * current_page.value) {
143+
const nextItem = props.modelValue[localParagraphList.value.length * current_page.value];
144+
if (nextItem) {
145+
localParagraphList.value.push(nextItem);
146+
}
147+
}
113148
})
114149
.catch(() => {})
115150
}

0 commit comments

Comments
 (0)