@@ -2,22 +2,30 @@ import Combobox from '@github/combobox-nav'
2
2
import query from './query'
3
3
import { InputRange } from 'dom-input-range'
4
4
5
- type Match = {
5
+ export type TextExpanderMatch = {
6
6
text : string
7
7
key : string
8
8
position : number
9
9
}
10
10
11
- type Result = {
12
- fragment : HTMLElement
11
+ export type TextExpanderResult = {
12
+ fragment ? : HTMLElement
13
13
matched : boolean
14
14
}
15
15
16
- type Key = {
16
+ export type TextExpanderKey = {
17
17
key : string
18
18
multiWord : boolean
19
19
}
20
20
21
+ export type TextExpanderChangeEvent = Event & {
22
+ detail ?: {
23
+ key : string
24
+ text : string
25
+ provide : ( result : Promise < TextExpanderResult > | TextExpanderResult ) => void
26
+ }
27
+ }
28
+
21
29
const states = new WeakMap ( )
22
30
23
31
class TextExpander {
@@ -31,7 +39,7 @@ class TextExpander {
31
39
onblur : ( event : Event ) => void
32
40
onmousedown : ( event : Event ) => void
33
41
combobox : Combobox | null
34
- match : Match | null
42
+ match : TextExpanderMatch | null
35
43
justPasted : boolean
36
44
lookBackIndex : number
37
45
interactingWithList : boolean
@@ -70,7 +78,7 @@ class TextExpander {
70
78
}
71
79
}
72
80
73
- private activate ( match : Match , menu : HTMLElement ) {
81
+ private activate ( match : TextExpanderMatch , menu : HTMLElement ) {
74
82
if ( this . input !== document . activeElement && this . input !== document . activeElement ?. shadowRoot ?. activeElement ) {
75
83
return
76
84
}
@@ -218,7 +226,7 @@ class TextExpander {
218
226
}
219
227
}
220
228
221
- findMatch ( ) : Match | void {
229
+ findMatch ( ) : TextExpanderMatch | void {
222
230
const cursor = this . input . selectionEnd || 0
223
231
const text = this . input . value
224
232
if ( cursor <= this . lookBackIndex ) {
@@ -236,12 +244,14 @@ class TextExpander {
236
244
}
237
245
}
238
246
239
- async notifyProviders ( match : Match ) : Promise < HTMLElement | void > {
240
- const providers : Array < Promise < Result > | Result > = [ ]
241
- const provide = ( result : Promise < Result > | Result ) => providers . push ( result )
242
- const canceled = ! this . expander . dispatchEvent (
243
- new CustomEvent ( 'text-expander-change' , { cancelable : true , detail : { provide, text : match . text , key : match . key } } )
244
- )
247
+ async notifyProviders ( match : TextExpanderMatch ) : Promise < HTMLElement | void > {
248
+ const providers : Array < Promise < TextExpanderResult > | TextExpanderResult > = [ ]
249
+ const provide = ( result : Promise < TextExpanderResult > | TextExpanderResult ) => providers . push ( result )
250
+ const changeEvent = new CustomEvent ( 'text-expander-change' , {
251
+ cancelable : true ,
252
+ detail : { provide, text : match . text , key : match . key }
253
+ } ) as TextExpanderChangeEvent
254
+ const canceled = ! this . expander . dispatchEvent ( changeEvent )
245
255
if ( canceled ) return
246
256
247
257
const all = await Promise . all ( providers )
@@ -265,7 +275,7 @@ class TextExpander {
265
275
}
266
276
}
267
277
export default class TextExpanderElement extends HTMLElement {
268
- get keys ( ) : Key [ ] {
278
+ get keys ( ) : TextExpanderKey [ ] {
269
279
const keysAttr = this . getAttribute ( 'keys' )
270
280
const keys = keysAttr ? keysAttr . split ( ' ' ) : [ ]
271
281
0 commit comments