@@ -3,38 +3,40 @@ import {emojiString} from '../emoji.ts';
33import { svg } from '../../svg.ts' ;
44import { parseIssueHref } from '../../utils.ts' ;
55import { createElementFromAttrs , createElementFromHTML } from '../../utils/dom.ts' ;
6-
7- type Issue = { id : number ; title : string ; state : 'open' | 'closed' ; pull_request ?: { draft : boolean ; merged : boolean } } ;
8- function getIssueIcon ( issue : Issue ) {
9- if ( issue . pull_request ) {
10- if ( issue . state === 'open' ) {
11- if ( issue . pull_request . draft === true ) {
12- return 'octicon-git-pull-request-draft' ; // WIP PR
13- }
14- return 'octicon-git-pull-request' ; // Open PR
15- } else if ( issue . pull_request . merged === true ) {
16- return 'octicon-git-merge' ; // Merged PR
17- }
18- return 'octicon-git-pull-request' ; // Closed PR
19- } else if ( issue . state === 'open' ) {
20- return 'octicon-issue-opened' ; // Open Issue
6+ import { getIssueColor , getIssueIcon } from '../issue.ts' ;
7+ import { debounce } from 'perfect-debounce' ;
8+
9+ const debouncedSuggestIssues = debounce ( ( key : string , text : string ) => new Promise < { matched :boolean ; fragment ?: HTMLElement } > ( async ( resolve ) => {
10+ const { owner, repo, index} = parseIssueHref ( window . location . href ) ;
11+ const matches = await matchIssue ( owner , repo , index , text ) ;
12+ if ( ! matches . length ) return resolve ( { matched : false } ) ;
13+
14+ const ul = document . createElement ( 'ul' ) ;
15+ ul . classList . add ( 'suggestions' ) ;
16+ for ( const issue of matches ) {
17+ const li = createElementFromAttrs ( 'li' , {
18+ role : 'option' ,
19+ 'data-value' : `${ key } ${ issue . id } ` ,
20+ } ) ;
21+ li . classList . add ( 'tw-flex' , 'tw-gap-2' ) ;
22+
23+ const icon = svg ( getIssueIcon ( issue ) , 16 , [ 'text' , getIssueColor ( issue ) ] . join ( ' ' ) ) ;
24+ li . append ( createElementFromHTML ( icon ) ) ;
25+
26+ const id = document . createElement ( 'span' ) ;
27+ id . classList . add ( 'id' ) ;
28+ id . textContent = issue . id . toString ( ) ;
29+ li . append ( id ) ;
30+
31+ const nameSpan = document . createElement ( 'span' ) ;
32+ nameSpan . textContent = issue . title ;
33+ li . append ( nameSpan ) ;
34+
35+ ul . append ( li ) ;
2136 }
22- return 'octicon-issue-closed' ; // Closed Issue
23- }
2437
25- function getIssueColor ( issue : Issue ) {
26- if ( issue . pull_request ) {
27- if ( issue . pull_request . draft === true ) {
28- return 'grey' ; // WIP PR
29- } else if ( issue . pull_request . merged === true ) {
30- return 'purple' ; // Merged PR
31- }
32- }
33- if ( issue . state === 'open' ) {
34- return 'green' ; // Open Issue
35- }
36- return 'red' ; // Closed Issue
37- }
38+ resolve ( { matched : true , fragment : ul } ) ;
39+ } ) , 100 ) ;
3840
3941export function initTextExpander ( expander ) {
4042 expander ?. addEventListener ( 'text-expander-change' , ( { detail : { key, provide, text} } ) => {
@@ -85,37 +87,7 @@ export function initTextExpander(expander) {
8587
8688 provide ( { matched : true , fragment : ul } ) ;
8789 } else if ( key === '#' ) {
88- provide ( new Promise ( async ( resolve ) => {
89- const { owner, repo, index} = parseIssueHref ( window . location . href ) ;
90- const matches = await matchIssue ( owner , repo , index , text ) ;
91- if ( ! matches . length ) return resolve ( { matched : false } ) ;
92-
93- const ul = document . createElement ( 'ul' ) ;
94- ul . classList . add ( 'suggestions' ) ;
95- for ( const issue of matches ) {
96- const li = createElementFromAttrs ( 'li' , {
97- role : 'option' ,
98- 'data-value' : `${ key } ${ issue . id } ` ,
99- } ) ;
100- li . classList . add ( 'tw-flex' , 'tw-gap-2' ) ;
101-
102- const icon = svg ( getIssueIcon ( issue ) , 16 , [ 'text' , getIssueColor ( issue ) ] . join ( ' ' ) ) ;
103- li . append ( createElementFromHTML ( icon ) ) ;
104-
105- const id = document . createElement ( 'span' ) ;
106- id . classList . add ( 'id' ) ;
107- id . textContent = issue . id . toString ( ) ;
108- li . append ( id ) ;
109-
110- const nameSpan = document . createElement ( 'span' ) ;
111- nameSpan . textContent = issue . title ;
112- li . append ( nameSpan ) ;
113-
114- ul . append ( li ) ;
115- }
116-
117- resolve ( { matched : true , fragment : ul } ) ;
118- } ) ) ;
90+ provide ( debouncedSuggestIssues ( key , text ) ) ;
11991 }
12092 } ) ;
12193 expander ?. addEventListener ( 'text-expander-value' , ( { detail} ) => {
0 commit comments