Skip to content

Commit

Permalink
Improve and export types (#72)
Browse files Browse the repository at this point in the history
* Improve types

* swap order to match

* make fragment optional

---------

Co-authored-by: Josh Black <joshblack@github.com>
  • Loading branch information
silverwind and joshblack authored Jan 29, 2025
1 parent c158e69 commit 4a8fe03
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/text-expander-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ import Combobox from '@github/combobox-nav'
import query from './query'
import {InputRange} from 'dom-input-range'

type Match = {
export type TextExpanderMatch = {
text: string
key: string
position: number
}

type Result = {
fragment: HTMLElement
export type TextExpanderResult = {
fragment?: HTMLElement
matched: boolean
}

type Key = {
export type TextExpanderKey = {
key: string
multiWord: boolean
}

export type TextExpanderChangeEvent = Event & {
detail?: {
key: string
text: string
provide: (result: Promise<TextExpanderResult> | TextExpanderResult) => void
}
}

const states = new WeakMap()

class TextExpander {
Expand All @@ -31,7 +39,7 @@ class TextExpander {
onblur: (event: Event) => void
onmousedown: (event: Event) => void
combobox: Combobox | null
match: Match | null
match: TextExpanderMatch | null
justPasted: boolean
lookBackIndex: number
interactingWithList: boolean
Expand Down Expand Up @@ -70,7 +78,7 @@ class TextExpander {
}
}

private activate(match: Match, menu: HTMLElement) {
private activate(match: TextExpanderMatch, menu: HTMLElement) {
if (this.input !== document.activeElement && this.input !== document.activeElement?.shadowRoot?.activeElement) {
return
}
Expand Down Expand Up @@ -218,7 +226,7 @@ class TextExpander {
}
}

findMatch(): Match | void {
findMatch(): TextExpanderMatch | void {
const cursor = this.input.selectionEnd || 0
const text = this.input.value
if (cursor <= this.lookBackIndex) {
Expand All @@ -236,12 +244,14 @@ class TextExpander {
}
}

async notifyProviders(match: Match): Promise<HTMLElement | void> {
const providers: Array<Promise<Result> | Result> = []
const provide = (result: Promise<Result> | Result) => providers.push(result)
const canceled = !this.expander.dispatchEvent(
new CustomEvent('text-expander-change', {cancelable: true, detail: {provide, text: match.text, key: match.key}})
)
async notifyProviders(match: TextExpanderMatch): Promise<HTMLElement | void> {
const providers: Array<Promise<TextExpanderResult> | TextExpanderResult> = []
const provide = (result: Promise<TextExpanderResult> | TextExpanderResult) => providers.push(result)
const changeEvent = new CustomEvent('text-expander-change', {
cancelable: true,
detail: {provide, text: match.text, key: match.key}
}) as TextExpanderChangeEvent
const canceled = !this.expander.dispatchEvent(changeEvent)
if (canceled) return

const all = await Promise.all(providers)
Expand All @@ -265,7 +275,7 @@ class TextExpander {
}
}
export default class TextExpanderElement extends HTMLElement {
get keys(): Key[] {
get keys(): TextExpanderKey[] {
const keysAttr = this.getAttribute('keys')
const keys = keysAttr ? keysAttr.split(' ') : []

Expand Down

0 comments on commit 4a8fe03

Please sign in to comment.