mirror of
https://github.com/HuLaSpark/HuLa.git
synced 2026-09-24 23:11:59 +08:00
fix(worker): 🐛 修复ws在重连后清空token导致无法对应获取消息问题
优化右键粘贴图片问题(需要tauri 兼容剪贴板识别更多类型)
This commit is contained in:
File diff suppressed because one or more lines are too long
+71
-18
@@ -159,35 +159,88 @@ export const useMsgInput = (messageInputDom: Ref) => {
|
||||
icon: 'intersection',
|
||||
click: async () => {
|
||||
try {
|
||||
// 先尝试读取图片
|
||||
let imageProcessed = false
|
||||
|
||||
// 使用Tauri的readImage API获取剪贴板图片
|
||||
const clipboardImage = await readImage().catch(() => null)
|
||||
if (clipboardImage) {
|
||||
try {
|
||||
// 获取图片的宽度和高度
|
||||
const { width, height } = await clipboardImage.size()
|
||||
|
||||
// 获取图片的 RGBA 数据
|
||||
const imageData = await clipboardImage.rgba()
|
||||
const blob = new Blob([imageData], { type: 'image/png' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
console.log(url)
|
||||
|
||||
messageInputDom.value.focus()
|
||||
nextTick(() => {
|
||||
imgPaste(blob, messageInputDom.value)
|
||||
})
|
||||
return
|
||||
// 创建Canvas来正确转换RGBA数据为PNG格式
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
const ctx = canvas.getContext('2d')
|
||||
|
||||
if (!ctx) {
|
||||
console.log('无法创建Canvas上下文')
|
||||
return
|
||||
}
|
||||
|
||||
// 创建ImageData对象
|
||||
const canvasImageData = ctx.createImageData(width, height)
|
||||
|
||||
// 将RGBA数据复制到ImageData中
|
||||
let uint8Array: Uint8Array
|
||||
if (imageData.buffer instanceof ArrayBuffer) {
|
||||
uint8Array = new Uint8Array(imageData.buffer, imageData.byteOffset, imageData.byteLength)
|
||||
} else {
|
||||
// 如果不是ArrayBuffer,创建新的Uint8Array
|
||||
uint8Array = new Uint8Array(imageData)
|
||||
}
|
||||
|
||||
// 复制数据到canvas ImageData
|
||||
canvasImageData.data.set(uint8Array)
|
||||
|
||||
// 将ImageData绘制到canvas
|
||||
ctx.putImageData(canvasImageData, 0, 0)
|
||||
|
||||
// 将canvas转换为Blob
|
||||
canvas.toBlob(
|
||||
(blob) => {
|
||||
if (!blob) {
|
||||
console.error('Canvas转换为Blob失败')
|
||||
return
|
||||
}
|
||||
|
||||
// 创建File对象,使用缓存机制
|
||||
const file = new File([blob], `image-${Date.now()}.png`, { type: 'image/png' })
|
||||
|
||||
messageInputDom.value.focus()
|
||||
nextTick(() => {
|
||||
// 使用File对象触发缓存机制
|
||||
imgPaste(file, messageInputDom.value)
|
||||
})
|
||||
},
|
||||
'image/png',
|
||||
1
|
||||
)
|
||||
|
||||
imageProcessed = true
|
||||
} catch (error) {
|
||||
console.error('处理图片数据失败:', error)
|
||||
console.error('Tauri处理图片数据失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有图片,尝试读取文本
|
||||
const content = await readText().catch(() => null)
|
||||
if (content) {
|
||||
messageInputDom.value.focus()
|
||||
nextTick(() => {
|
||||
insertNode(MsgEnum.TEXT, content, {} as HTMLElement)
|
||||
triggerInputEvent(messageInputDom.value)
|
||||
})
|
||||
return
|
||||
if (!imageProcessed) {
|
||||
const content = await readText().catch(() => null)
|
||||
if (content) {
|
||||
messageInputDom.value.focus()
|
||||
nextTick(() => {
|
||||
insertNode(MsgEnum.TEXT, content, {} as HTMLElement)
|
||||
triggerInputEvent(messageInputDom.value)
|
||||
})
|
||||
return
|
||||
} else {
|
||||
// 当既没有图片也没有文本时,显示提示信息
|
||||
alert('无法获取当前剪贴板中对于的类型的内容,请使用 ctrl/command + v')
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('粘贴失败:', error)
|
||||
|
||||
@@ -142,7 +142,6 @@ const onConnectClose = () => {
|
||||
console.log('📡 WebSocket 连接断开')
|
||||
updateConnectionState(ConnectionState.DISCONNECTED)
|
||||
onCloseHandler()
|
||||
token = null
|
||||
postMsg({ type: WorkerMsgEnum.CLOSE })
|
||||
}
|
||||
// ws 连接成功
|
||||
@@ -227,6 +226,7 @@ self.onmessage = (e: MessageEvent<string>) => {
|
||||
reconnectCount = value.reconnectCount + 1
|
||||
// 如果没有超过最大重连次数才继续重连
|
||||
if (reconnectCount < reconnectCountMax) {
|
||||
console.log('重连中,当前clientId:', clientId, '当前token状态:', token ? '存在' : '不存在')
|
||||
initConnection()
|
||||
lockReconnect = false
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user