Skip to content

Commit

Permalink
fix(clipboard): await DataTransferItem.getAsString() callback (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Jan 15, 2025
1 parent 1ed8b15 commit 7b11b0e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/utils/dataTransfer/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export async function writeDataTransferToClipboard(
const items = []
for (let i = 0; i < clipboardData.items.length; i++) {
const dtItem = clipboardData.items[i]
const blob = getBlobFromDataTransferItem(window, dtItem)
const blob = await getBlobFromDataTransferItem(window, dtItem)
items.push(createClipboardItem(window, blob))
}

Expand Down
9 changes: 3 additions & 6 deletions src/utils/dataTransfer/DataTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,14 @@ export function createDataTransfer(
return dt
}

export function getBlobFromDataTransferItem(
export async function getBlobFromDataTransferItem(
window: Window & typeof globalThis,
item: DataTransferItem,
) {
if (item.kind === 'file') {
return item.getAsFile() as File
}
// TODO: await callback
let data: string = ''
item.getAsString(s => {
data = s
return new window.Blob([await new Promise(r => item.getAsString(r))], {
type: item.type,
})
return new window.Blob([data], {type: item.type})
}
26 changes: 15 additions & 11 deletions tests/utils/dataTransfer/DataTransfer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { waitFor } from '@testing-library/dom'
import {waitFor} from '@testing-library/dom'
import {createDataTransfer, getBlobFromDataTransferItem} from '#src/utils'

describe('create DataTransfer', () => {
Expand Down Expand Up @@ -45,12 +45,14 @@ describe('create DataTransfer', () => {
const dt = createDataTransfer(window, [f0, f1])
dt.setData('text/html', 'foo')

expect(dt.types).toEqual(expect.arrayContaining(
expect(dt.types).toEqual(
expect.arrayContaining(
// TODO: Fix DataTransferStub
typeof window.DataTransfer === 'undefined'
? ['Files', 'text/html']
: ['text/html']
))
? ['Files', 'text/html']
: ['text/html'],
),
)
expect(dt.files.length).toBe(2)
})

Expand Down Expand Up @@ -108,16 +110,18 @@ test('get Blob from DataTransfer', async () => {
dt.items.add('foo', 'text/plain')
dt.items.add(new File(['bar'], 'bar.txt', {type: 'text/plain'}))

expect(getBlobFromDataTransferItem(window, dt.items[0])).toHaveProperty(
expect(await getBlobFromDataTransferItem(window, dt.items[0])).toHaveProperty(
'type',
'text/plain',
)
expect(getBlobFromDataTransferItem(window, dt.items[0])).not.toBeInstanceOf(
File,
)
expect(getBlobFromDataTransferItem(window, dt.items[1])).toHaveProperty(
expect(
await getBlobFromDataTransferItem(window, dt.items[0]),
).not.toBeInstanceOf(File)
expect(await getBlobFromDataTransferItem(window, dt.items[1])).toHaveProperty(
'type',
'text/plain',
)
expect(getBlobFromDataTransferItem(window, dt.items[1])).toBeInstanceOf(File)
expect(await getBlobFromDataTransferItem(window, dt.items[1])).toBeInstanceOf(
File,
)
})

0 comments on commit 7b11b0e

Please sign in to comment.