-
Notifications
You must be signed in to change notification settings - Fork 239
/
copySelection.ts
30 lines (25 loc) 路 982 Bytes
/
copySelection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {getUISelection, getUIValue} from '../../document'
import {createDataTransfer} from '../dataTransfer/DataTransfer'
import {EditableInputType} from '../edit/isEditable'
import {getWindow} from '../misc/getWindow'
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(getWindow(target))
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)
}