-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8fe217
commit 8727a2d
Showing
14 changed files
with
517 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {fireEvent} from '@testing-library/dom' | ||
import type {UserEvent} from '../setup' | ||
import {copySelection, writeDataTransferToClipboard} from '../utils' | ||
|
||
export interface copyOptions { | ||
document?: Document | ||
writeToClipboard?: boolean | ||
} | ||
|
||
export function copy( | ||
this: UserEvent, | ||
options: Omit<copyOptions, 'writeToClipboard'> & {writeToClipboard: true}, | ||
): Promise<DataTransfer> | ||
export function copy( | ||
this: UserEvent, | ||
options?: Omit<copyOptions, 'writeToClipboard'> & { | ||
writeToClipboard?: boolean | ||
}, | ||
): DataTransfer | ||
export function copy(this: UserEvent, options?: copyOptions) { | ||
const doc = options?.document ?? document | ||
const target = doc.activeElement ?? /* istanbul ignore next */ doc.body | ||
|
||
const clipboardData = copySelection(target) | ||
|
||
if (clipboardData.items.length === 0) { | ||
return | ||
} | ||
|
||
fireEvent.copy(target, { | ||
clipboardData, | ||
}) | ||
|
||
return options?.writeToClipboard | ||
? writeDataTransferToClipboard(doc, clipboardData).then(() => clipboardData) | ||
: clipboardData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {fireEvent} from '@testing-library/dom' | ||
import type {UserEvent} from '../setup' | ||
import { | ||
copySelection, | ||
isEditable, | ||
prepareInput, | ||
writeDataTransferToClipboard, | ||
} from '../utils' | ||
|
||
export interface cutOptions { | ||
document?: Document | ||
writeToClipboard?: boolean | ||
} | ||
|
||
export function cut( | ||
this: UserEvent, | ||
options: Omit<cutOptions, 'writeToClipboard'> & {writeToClipboard: true}, | ||
): Promise<DataTransfer> | ||
export function cut( | ||
this: UserEvent, | ||
options?: Omit<cutOptions, 'writeToClipboard'> & {writeToClipboard?: boolean}, | ||
): DataTransfer | ||
export function cut(this: UserEvent, options?: cutOptions) { | ||
const doc = options?.document ?? document | ||
const target = doc.activeElement ?? /* istanbul ignore next */ doc.body | ||
|
||
const clipboardData = copySelection(target) | ||
|
||
if (clipboardData.items.length === 0) { | ||
return | ||
} | ||
|
||
fireEvent.cut(target, { | ||
clipboardData, | ||
}) | ||
|
||
if (isEditable(target)) { | ||
prepareInput('', target, 'deleteByCut')?.commit() | ||
} | ||
|
||
return options?.writeToClipboard | ||
? writeDataTransferToClipboard(doc, clipboardData).then(() => clipboardData) | ||
: clipboardData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './copy' | ||
export * from './cut' | ||
export * from './paste' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {getUISelection, getUIValue} from '../../document' | ||
import {createDataTransfer} from '../dataTransfer/DataTransfer' | ||
import {EditableInputType} from '../edit/isEditable' | ||
import {hasOwnSelection} from './selection' | ||
|
||
export function copySelection(target: Element) { | ||
const data: Record<string, string> = hasOwnSelection(target) | ||
? {'text/plain': readSelectedValueFromInput(target)} | ||
: // TODO: We could implement text/html copying of DOM nodes here | ||
{'text/plain': String(target.ownerDocument.getSelection())} | ||
|
||
const dt = createDataTransfer() | ||
for (const type in data) { | ||
if (data[type]) { | ||
dt.setData(type, data[type]) | ||
} | ||
} | ||
|
||
return dt | ||
} | ||
|
||
function readSelectedValueFromInput( | ||
target: (HTMLInputElement & {type: EditableInputType}) | HTMLTextAreaElement, | ||
) { | ||
const sel = getUISelection(target) | ||
const val = getUIValue(target) | ||
|
||
return val.substring(sel.startOffset, sel.endOffset) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.