@@ -64,7 +64,13 @@ class TextExpander {
64
64
this . input . removeEventListener ( 'blur' , this . onblur )
65
65
}
66
66
67
- activate ( match : Match , menu : HTMLElement ) {
67
+ dismissMenu ( ) {
68
+ if ( this . deactivate ( ) ) {
69
+ this . lookBackIndex = this . input . selectionEnd || this . lookBackIndex
70
+ }
71
+ }
72
+
73
+ private activate ( match : Match , menu : HTMLElement ) {
68
74
if ( this . input !== document . activeElement ) return
69
75
70
76
this . deactivate ( )
@@ -86,19 +92,22 @@ class TextExpander {
86
92
this . combobox . navigate ( 1 )
87
93
}
88
94
89
- deactivate ( ) {
95
+ private deactivate ( ) {
90
96
const menu = this . menu
91
- if ( ! menu || ! this . combobox ) return
97
+ if ( ! menu || ! this . combobox ) return false
92
98
this . menu = null
93
99
94
100
menu . removeEventListener ( 'combobox-commit' , this . oncommit )
95
101
menu . removeEventListener ( 'mousedown' , this . onmousedown )
102
+
96
103
this . combobox . destroy ( )
97
104
this . combobox = null
98
105
menu . remove ( )
106
+
107
+ return true
99
108
}
100
109
101
- onCommit ( { target} : Event ) {
110
+ private onCommit ( { target} : Event ) {
102
111
const item = target
103
112
if ( ! ( item instanceof HTMLElement ) ) return
104
113
if ( ! this . combobox ) return
@@ -118,17 +127,17 @@ class TextExpander {
118
127
119
128
this . input . value = beginning + value + remaining
120
129
130
+ const cursor = beginning . length + value . length
131
+
121
132
this . deactivate ( )
122
133
this . input . focus ( )
123
134
124
- const cursor = beginning . length + value . length
125
135
this . input . selectionStart = cursor
126
136
this . input . selectionEnd = cursor
127
-
128
137
this . lookBackIndex = cursor
129
138
}
130
139
131
- onBlur ( ) {
140
+ private onBlur ( ) {
132
141
if ( this . interactingWithList ) {
133
142
this . interactingWithList = false
134
143
return
@@ -137,7 +146,7 @@ class TextExpander {
137
146
this . deactivate ( )
138
147
}
139
148
140
- onPaste ( ) {
149
+ private onPaste ( ) {
141
150
this . justPasted = true
142
151
}
143
152
@@ -193,19 +202,20 @@ class TextExpander {
193
202
return fragments [ 0 ]
194
203
}
195
204
196
- onMousedown ( ) {
205
+ private onMousedown ( ) {
197
206
this . interactingWithList = true
198
207
}
199
208
200
- onKeydown ( event : KeyboardEvent ) {
201
- if ( event . key === 'Escape' && ( this . menu || this . combobox ) ) {
202
- this . deactivate ( )
203
- event . stopImmediatePropagation ( )
204
- event . preventDefault ( )
209
+ private onKeydown ( event : KeyboardEvent ) {
210
+ if ( event . key === 'Escape' ) {
211
+ if ( this . deactivate ( ) ) {
212
+ this . lookBackIndex = this . input . selectionEnd || this . lookBackIndex
213
+ event . stopImmediatePropagation ( )
214
+ event . preventDefault ( )
215
+ }
205
216
}
206
217
}
207
218
}
208
-
209
219
export default class TextExpanderElement extends HTMLElement {
210
220
get keys ( ) : Key [ ] {
211
221
const keysAttr = this . getAttribute ( 'keys' )
@@ -226,9 +236,15 @@ export default class TextExpanderElement extends HTMLElement {
226
236
}
227
237
228
238
disconnectedCallback ( ) {
229
- const state = states . get ( this )
239
+ const state : TextExpander = states . get ( this )
230
240
if ( ! state ) return
231
241
state . destroy ( )
232
242
states . delete ( this )
233
243
}
244
+
245
+ dismiss ( ) {
246
+ const state : TextExpander = states . get ( this )
247
+ if ( ! state ) return
248
+ state . dismissMenu ( )
249
+ }
234
250
}
0 commit comments