Files
FileCodeBoxFronted/src/utils/clipboard-paste.ts
T
2026-06-03 02:01:57 +08:00

41 lines
888 B
TypeScript

export const getClipboardFile = (items: DataTransferItemList): File | null => {
for (let index = 0; index < items.length; index++) {
const item = items[index]
if (item.kind !== 'file') {
continue
}
const file = item.getAsFile()
if (file) {
return file
}
}
return null
}
export type TextInsertionInput = {
text: string
insertText: string
selectionStart: number
selectionEnd: number
}
export type TextInsertionResult = {
value: string
cursor: number
}
export const insertTextAtSelection = ({
text,
insertText,
selectionStart,
selectionEnd
}: TextInsertionInput): TextInsertionResult => {
const beforeSelection = text.substring(0, selectionStart)
const afterSelection = text.substring(selectionEnd)
return {
value: beforeSelection + insertText + afterSelection,
cursor: selectionStart + insertText.length
}
}