fix: Web Share Target text field merge (#1572)

Co-authored-by: userquin <userquin@gmail.com>
This commit is contained in:
Horváth Bálint 2023-02-03 15:20:32 +01:00 committed by GitHub
parent 6eedaa98bc
commit faa96c7705
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 12 deletions

View file

@ -32,21 +32,30 @@ async function handleSharedTarget(event: FetchEvent) {
}
async function sendShareTargetMessage(client: Client, data: FormData) {
const sharedData: { text?: string; files?: File[] } = {}
const sharedData: { textParts: string[]; files: File[] } = {
textParts: [],
files: [],
}
// We collect the text data shared with us
const title = data.get('title')
if (title !== null)
sharedData.textParts.push(title.toString())
const text = data.get('text')
if (text !== null)
sharedData.text = text.toString()
sharedData.textParts.push(text.toString())
const files: File[] = []
const link = data.get('link')
if (link !== null)
sharedData.textParts.push(link.toString())
// We collect the files shared with us
for (const [name, file] of data.entries()) {
if (name === 'files' && file instanceof File)
files.push(file)
sharedData.files.push(file)
}
if (files.length !== 0)
sharedData.files = files
client.postMessage({ data: sharedData, action: 'compose-with-shared-data' })
}