Add paste post

This commit is contained in:
Kwaroran
2025-01-31 23:04:01 +09:00
parent 43ebf73b2b
commit 3139eac739
2 changed files with 49 additions and 7 deletions

View File

@@ -28,6 +28,7 @@
import { getInlayAsset } from 'src/ts/process/files/inlays'; import { getInlayAsset } from 'src/ts/process/files/inlays';
import PlaygroundMenu from '../Playground/PlaygroundMenu.svelte'; import PlaygroundMenu from '../Playground/PlaygroundMenu.svelte';
import { ConnectionOpenStore } from 'src/ts/sync/multiuser'; import { ConnectionOpenStore } from 'src/ts/sync/multiuser';
import { preventDefault } from 'svelte/legacy';
let messageInput:string = $state('') let messageInput:string = $state('')
let messageInputTranslate:string = $state('') let messageInputTranslate:string = $state('')
@@ -462,6 +463,43 @@
e.preventDefault() e.preventDefault()
} }
}} }}
onpaste={(e) => {
const items = e.clipboardData?.items
if(!items){
return
}
let canceled = false
for(const item of items){
if(item.kind === 'file' && item.type.startsWith('image')){
if(!canceled){
e.preventDefault()
canceled = true
}
const file = item.getAsFile()
if(file){
const reader = new FileReader()
reader.onload = async (e) => {
const buf = e.target?.result as ArrayBuffer
const uint8 = new Uint8Array(buf)
const res = await postChatFile({
name: file.name,
data: uint8
})
if(res?.type === 'asset'){
fileInput.push(res.data)
updateInputSizeAll()
}
if(res?.type === 'text'){
messageInput += `{{file::${res.name}::${res.data}}}`
updateInputSizeAll()
}
}
reader.readAsArrayBuffer(file)
}
}
}
}}
oninput={()=>{updateInputSizeAll();updateInputTransateMessage(false)}} oninput={()=>{updateInputSizeAll();updateInputTransateMessage(false)}}
style:height={inputHeight} style:height={inputHeight}
></textarea> ></textarea>

View File

@@ -194,8 +194,11 @@ type postFileResultText = {
type: 'text', type: 'text',
name: string name: string
} }
export async function postChatFile(query:string):Promise<postFileResult>{ export async function postChatFile(query:string|{
const file = await selectSingleFile([ name:string,
data:Uint8Array<ArrayBufferLike>
}):Promise<postFileResult>{
const file = typeof(query) === 'string' ? (await selectSingleFile([
//image format //image format
'jpg', 'jpg',
'jpeg', 'jpeg',
@@ -220,12 +223,13 @@ export async function postChatFile(query:string):Promise<postFileResult>{
'po', 'po',
// 'pdf', // 'pdf',
'txt' 'txt'
]) ])) : query
if(!file){ if(!file){
return null return null
} }
const xquery = typeof(query) === 'string' ? query : ''
const extention = file.name.split('.').at(-1) const extention = file.name.split('.').at(-1)
console.log(extention) console.log(extention)
@@ -233,7 +237,7 @@ export async function postChatFile(query:string):Promise<postFileResult>{
case 'po':{ case 'po':{
await sendPofile({ await sendPofile({
file: BufferToText(file.data), file: BufferToText(file.data),
query: query query: xquery
}) })
return { return {
type: 'void' type: 'void'
@@ -244,7 +248,7 @@ export async function postChatFile(query:string):Promise<postFileResult>{
type: 'text', type: 'text',
data: await sendPDFFile({ data: await sendPDFFile({
file: BufferToText(file.data), file: BufferToText(file.data),
query: query query: xquery
}), }),
name: file.name name: file.name
} }
@@ -254,7 +258,7 @@ export async function postChatFile(query:string):Promise<postFileResult>{
type: 'text', type: 'text',
data: await sendXMLFile({ data: await sendXMLFile({
file: BufferToText(file.data), file: BufferToText(file.data),
query: query query: xquery
}), }),
name: file.name name: file.name
} }
@@ -293,7 +297,7 @@ export async function postChatFile(query:string):Promise<postFileResult>{
type: 'text', type: 'text',
data: await sendTxtFile({ data: await sendTxtFile({
file: BufferToText(file.data), file: BufferToText(file.data),
query: query query: xquery
}), }),
name: file.name name: file.name
} }