Skip to content

Improve and export types #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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