Skip to content

Commit 7b11b0e

Browse files
authored
fix(clipboard): await DataTransferItem.getAsString() callback (#1251)
1 parent 1ed8b15 commit 7b11b0e

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

src/utils/dataTransfer/Clipboard.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export async function writeDataTransferToClipboard(
192192
const items = []
193193
for (let i = 0; i < clipboardData.items.length; i++) {
194194
const dtItem = clipboardData.items[i]
195-
const blob = getBlobFromDataTransferItem(window, dtItem)
195+
const blob = await getBlobFromDataTransferItem(window, dtItem)
196196
items.push(createClipboardItem(window, blob))
197197
}
198198

src/utils/dataTransfer/DataTransfer.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,14 @@ export function createDataTransfer(
150150
return dt
151151
}
152152

153-
export function getBlobFromDataTransferItem(
153+
export async function getBlobFromDataTransferItem(
154154
window: Window & typeof globalThis,
155155
item: DataTransferItem,
156156
) {
157157
if (item.kind === 'file') {
158158
return item.getAsFile() as File
159159
}
160-
// TODO: await callback
161-
let data: string = ''
162-
item.getAsString(s => {
163-
data = s
160+
return new window.Blob([await new Promise(r => item.getAsString(r))], {
161+
type: item.type,
164162
})
165-
return new window.Blob([data], {type: item.type})
166163
}

tests/utils/dataTransfer/DataTransfer.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { waitFor } from '@testing-library/dom'
1+
import {waitFor} from '@testing-library/dom'
22
import {createDataTransfer, getBlobFromDataTransferItem} from '#src/utils'
33

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

48-
expect(dt.types).toEqual(expect.arrayContaining(
48+
expect(dt.types).toEqual(
49+
expect.arrayContaining(
4950
// TODO: Fix DataTransferStub
5051
typeof window.DataTransfer === 'undefined'
51-
? ['Files', 'text/html']
52-
: ['text/html']
53-
))
52+
? ['Files', 'text/html']
53+
: ['text/html'],
54+
),
55+
)
5456
expect(dt.files.length).toBe(2)
5557
})
5658

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

111-
expect(getBlobFromDataTransferItem(window, dt.items[0])).toHaveProperty(
113+
expect(await getBlobFromDataTransferItem(window, dt.items[0])).toHaveProperty(
112114
'type',
113115
'text/plain',
114116
)
115-
expect(getBlobFromDataTransferItem(window, dt.items[0])).not.toBeInstanceOf(
116-
File,
117-
)
118-
expect(getBlobFromDataTransferItem(window, dt.items[1])).toHaveProperty(
117+
expect(
118+
await getBlobFromDataTransferItem(window, dt.items[0]),
119+
).not.toBeInstanceOf(File)
120+
expect(await getBlobFromDataTransferItem(window, dt.items[1])).toHaveProperty(
119121
'type',
120122
'text/plain',
121123
)
122-
expect(getBlobFromDataTransferItem(window, dt.items[1])).toBeInstanceOf(File)
124+
expect(await getBlobFromDataTransferItem(window, dt.items[1])).toBeInstanceOf(
125+
File,
126+
)
123127
})

0 commit comments

Comments
 (0)