Skip to content

Commit

Permalink
refactor: inline paste implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Feb 2, 2022
1 parent 52da1fc commit bf734dc
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/clipboard/paste.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {dispatchUIEvent} from '../event'
import {Config, Instance} from '../setup'
import {
createDataTransfer,
Expand All @@ -15,7 +14,7 @@ export async function paste(
const doc = this[Config].document
const target = doc.activeElement ?? /* istanbul ignore next */ doc.body

const data: DataTransfer =
const dataTransfer: DataTransfer =
(typeof clipboardData === 'string'
? getClipboardDataFromString(clipboardData)
: clipboardData) ??
Expand All @@ -25,25 +24,17 @@ export async function paste(
)
}))

return pasteImpl(this[Config], target, data)
}

function pasteImpl(
config: Config,
target: Element,
clipboardData: DataTransfer,
) {
dispatchUIEvent(config, target, 'paste', {
clipboardData,
this.dispatchUIEvent(target, 'paste', {
clipboardData: dataTransfer,
})

if (isEditable(target)) {
const data = clipboardData
const textData = dataTransfer
.getData('text')
.substr(0, getSpaceUntilMaxLength(target))

if (data) {
prepareInput(config, data, target, 'insertFromPaste')?.commit()
if (textData) {
prepareInput(this[Config], textData, target, 'insertFromPaste')?.commit()
}
}
}
Expand Down

0 comments on commit bf734dc

Please sign in to comment.