Skip to content

Commit 4a8fe03

Browse files
Improve and export types (#72)
* Improve types * swap order to match * make fragment optional --------- Co-authored-by: Josh Black <[email protected]>
1 parent c158e69 commit 4a8fe03

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/text-expander-element.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ import Combobox from '@github/combobox-nav'
22
import query from './query'
33
import {InputRange} from 'dom-input-range'
44

5-
type Match = {
5+
export type TextExpanderMatch = {
66
text: string
77
key: string
88
position: number
99
}
1010

11-
type Result = {
12-
fragment: HTMLElement
11+
export type TextExpanderResult = {
12+
fragment?: HTMLElement
1313
matched: boolean
1414
}
1515

16-
type Key = {
16+
export type TextExpanderKey = {
1717
key: string
1818
multiWord: boolean
1919
}
2020

21+
export type TextExpanderChangeEvent = Event & {
22+
detail?: {
23+
key: string
24+
text: string
25+
provide: (result: Promise<TextExpanderResult> | TextExpanderResult) => void
26+
}
27+
}
28+
2129
const states = new WeakMap()
2230

2331
class TextExpander {
@@ -31,7 +39,7 @@ class TextExpander {
3139
onblur: (event: Event) => void
3240
onmousedown: (event: Event) => void
3341
combobox: Combobox | null
34-
match: Match | null
42+
match: TextExpanderMatch | null
3543
justPasted: boolean
3644
lookBackIndex: number
3745
interactingWithList: boolean
@@ -70,7 +78,7 @@ class TextExpander {
7078
}
7179
}
7280

73-
private activate(match: Match, menu: HTMLElement) {
81+
private activate(match: TextExpanderMatch, menu: HTMLElement) {
7482
if (this.input !== document.activeElement && this.input !== document.activeElement?.shadowRoot?.activeElement) {
7583
return
7684
}
@@ -218,7 +226,7 @@ class TextExpander {
218226
}
219227
}
220228

221-
findMatch(): Match | void {
229+
findMatch(): TextExpanderMatch | void {
222230
const cursor = this.input.selectionEnd || 0
223231
const text = this.input.value
224232
if (cursor <= this.lookBackIndex) {
@@ -236,12 +244,14 @@ class TextExpander {
236244
}
237245
}
238246

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)
245255
if (canceled) return
246256

247257
const all = await Promise.all(providers)
@@ -265,7 +275,7 @@ class TextExpander {
265275
}
266276
}
267277
export default class TextExpanderElement extends HTMLElement {
268-
get keys(): Key[] {
278+
get keys(): TextExpanderKey[] {
269279
const keysAttr = this.getAttribute('keys')
270280
const keys = keysAttr ? keysAttr.split(' ') : []
271281

0 commit comments

Comments
 (0)