AI_Painting_V2.0/src/stores/display.js

84 lines
2.3 KiB
JavaScript

const DisplayStoreSetup = () => {
const Sender_variant = ref('updown')
const scrollerRef = ref(null)
const tempList = ref([])
const isSubGerenate = ref(false)
const addGeneratingItem = (item) => {
const newItem = {
id: item.taskId || crypto.randomUUID(),
status: 'generate',
text: item.text || '生成中...',
name: item.name || '生成中...',
type: item.type || 'image',
time: item.time || new Date().toLocaleString(),
files: [],
...item
}
tempList.value.push(newItem)
return newItem
}
const updateItemToSuccess = (taskId, fileUrl) => {
const index = tempList.value.findIndex(item => item.id === taskId)
if (index !== -1) {
tempList.value[index].status = 'success'
tempList.value[index].files = [fileUrl]
}
}
const initHistoryList = (historyList) => {
tempList.value = historyList
}
const scrollToBottom = async () => {
console.log('store - 滚动到底部')
const refValue = scrollerRef.value
if (!refValue) {
console.log('store - scrollerRef 不存在')
return
}
try {
if (typeof refValue.scrollToBottom === 'function') {
console.log('store - 使用新组件 scrollToBottom')
await nextTick()
refValue.scrollToBottom()
return
}
const scrollerEl = refValue.$el
if (scrollerEl) {
const viewport = scrollerEl.querySelector('.vue-recycle-scroller__viewport')
if (viewport) {
console.log('store - 原生滚动, scrollHeight:', viewport.scrollHeight)
viewport.scrollTop = viewport.scrollHeight
}
}
if (typeof refValue.scrollToItem === 'function' && tempList.value && tempList.value.length > 0) {
console.log('store - scrollToItem, index:', tempList.value.length - 1)
await nextTick()
refValue.scrollToItem(tempList.value.length - 1)
}
} catch (error) {
console.error('store - 滚动出错:', error)
}
}
return {
Sender_variant,
scrollerRef,
tempList,
isSubGerenate,
addGeneratingItem,
updateItemToSuccess,
initHistoryList,
scrollToBottom
}
}
// eslint-disable-next-line no-undef
export const useDisplayStore = defineStore('display', DisplayStoreSetup)