-
Notifications
You must be signed in to change notification settings - Fork 240
/
selection.ts
146 lines (125 loc) Β· 3.35 KB
/
selection.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {getUIValue} from '.'
import {prepareInterceptor} from './interceptor'
const UISelection = Symbol('Displayed selection in UI')
interface Value extends Number {
[UISelection]?: typeof UISelection
}
export interface UISelectionRange {
startOffset: number
endOffset: number
}
export interface UISelection {
anchorOffset: number
focusOffset: number
}
declare global {
interface Element {
[UISelection]?: UISelection
}
}
export function prepareSelectionInterceptor(
element: HTMLInputElement | HTMLTextAreaElement,
) {
prepareInterceptor(
element,
'setSelectionRange',
function interceptorImpl(
this: HTMLInputElement | HTMLTextAreaElement,
start: number | Value | null,
end: number | null,
direction: 'forward' | 'backward' | 'none' = 'none',
) {
const isUI = start && typeof start === 'object' && start[UISelection]
if (!isUI) {
this[UISelection] = undefined
}
return {
realArgs: [Number(start), end, direction] as [
number,
number,
typeof direction,
],
}
},
)
prepareInterceptor(
element,
'selectionStart',
function interceptorImpl(this, v) {
this[UISelection] = undefined
return {realArgs: v}
},
)
prepareInterceptor(
element,
'selectionEnd',
function interceptorImpl(this, v) {
this[UISelection] = undefined
return {realArgs: v}
},
)
prepareInterceptor(
element,
'select',
function interceptorImpl(this: HTMLInputElement | HTMLTextAreaElement) {
this[UISelection] = {
anchorOffset: 0,
focusOffset: getUIValue(element).length,
}
return {realArgs: [] as []}
},
)
}
export function setUISelection(
element: HTMLInputElement | HTMLTextAreaElement,
{
focusOffset: focusOffsetParam,
anchorOffset: anchorOffsetParam = focusOffsetParam,
}: {
anchorOffset?: number
focusOffset: number
},
mode: 'replace' | 'modify' = 'replace',
) {
const valueLength = getUIValue(element).length
const sanitizeOffset = (o: number) => Math.max(0, Math.min(valueLength, o))
const anchorOffset =
mode === 'replace' || element[UISelection] === undefined
? sanitizeOffset(anchorOffsetParam)
: (element[UISelection] as UISelection).anchorOffset
const focusOffset = sanitizeOffset(focusOffsetParam)
const startOffset = Math.min(anchorOffset, focusOffset)
const endOffset = Math.max(anchorOffset, focusOffset)
element[UISelection] = {
anchorOffset,
focusOffset,
}
if (
element.selectionStart === startOffset &&
element.selectionEnd === endOffset
) {
return
}
// eslint-disable-next-line no-new-wrappers
const startObj = new Number(startOffset)
;(startObj as Value)[UISelection] = UISelection
try {
element.setSelectionRange(startObj as number, endOffset)
} catch {
// DOMException for invalid state is expected when calling this
// on an element without support for setSelectionRange
}
}
export function getUISelection(
element: HTMLInputElement | HTMLTextAreaElement,
) {
const sel = element[UISelection] ?? {
anchorOffset: element.selectionStart ?? 0,
focusOffset: element.selectionEnd ?? 0,
}
return {
...sel,
startOffset: Math.min(sel.anchorOffset, sel.focusOffset),
endOffset: Math.max(sel.anchorOffset, sel.focusOffset),
}
}