|
1 | | -import {matchEmoji, matchMention, matchIssueOrPullRequest} from '../../utils/match.ts'; |
| 1 | +import {matchEmoji, matchMention, matchIssue} from '../../utils/match.ts'; |
2 | 2 | import {emojiString} from '../emoji.ts'; |
| 3 | +import {svg} from '../../svg.ts'; |
| 4 | + |
| 5 | +type Issue = {state: 'open' | 'closed'; pull_request: {draft: boolean; merged: boolean} | null}; |
| 6 | +function getIssueIcon(issue: Issue) { |
| 7 | + if (issue.pull_request !== null) { |
| 8 | + if (issue.state === 'open') { |
| 9 | + if (issue.pull_request.draft === true) { |
| 10 | + return 'octicon-git-pull-request-draft'; // WIP PR |
| 11 | + } |
| 12 | + return 'octicon-git-pull-request'; // Open PR |
| 13 | + } else if (issue.pull_request.merged === true) { |
| 14 | + return 'octicon-git-merge'; // Merged PR |
| 15 | + } |
| 16 | + return 'octicon-git-pull-request'; // Closed PR |
| 17 | + } else if (issue.state === 'open') { |
| 18 | + return 'octicon-issue-opened'; // Open Issue |
| 19 | + } |
| 20 | + return 'octicon-issue-closed'; // Closed Issue |
| 21 | +} |
| 22 | + |
| 23 | +function getIssueColor(issue: Issue) { |
| 24 | + if (issue.pull_request !== null) { |
| 25 | + if (issue.pull_request.draft === true) { |
| 26 | + return 'grey'; // WIP PR |
| 27 | + } else if (issue.pull_request.merged === true) { |
| 28 | + return 'purple'; // Merged PR |
| 29 | + } |
| 30 | + } |
| 31 | + if (issue.state === 'open') { |
| 32 | + return 'green'; // Open Issue |
| 33 | + } |
| 34 | + return 'red'; // Closed Issue |
| 35 | +} |
3 | 36 |
|
4 | 37 | export function initTextExpander(expander) { |
5 | | - expander?.addEventListener('text-expander-change', async ({detail: {key, provide, text}}) => { |
| 38 | + expander?.addEventListener('text-expander-change', ({detail: {key, provide, text}}) => { |
6 | 39 | if (key === ':') { |
7 | 40 | const matches = matchEmoji(text); |
8 | 41 | if (!matches.length) return provide({matched: false}); |
@@ -50,29 +83,37 @@ export function initTextExpander(expander) { |
50 | 83 |
|
51 | 84 | provide({matched: true, fragment: ul}); |
52 | 85 | } else if (key === '#') { |
53 | | - const url = window.location.href; |
54 | | - const matches = matchIssueOrPullRequest(url, text); |
55 | | - if (!matches.length) return provide({matched: false}); |
| 86 | + provide(new Promise(async (resolve) => { |
| 87 | + const url = window.location.href; |
| 88 | + const matches = await matchIssue(url, text); |
| 89 | + if (!matches.length) return resolve({matched: false}); |
56 | 90 |
|
57 | | - const ul = document.createElement('ul'); |
58 | | - ul.classList.add('suggestions'); |
59 | | - for (const {value, name, type} of matches) { |
60 | | - const li = document.createElement('li'); |
61 | | - li.setAttribute('role', 'option'); |
62 | | - li.setAttribute('data-value', `${key}${value}`); |
| 91 | + const ul = document.createElement('ul'); |
| 92 | + ul.classList.add('suggestions'); |
| 93 | + for (const {value, name, issue} of matches) { |
| 94 | + const li = document.createElement('li'); |
| 95 | + li.classList.add('tw-flex', 'tw-gap-2'); |
| 96 | + li.setAttribute('role', 'option'); |
| 97 | + li.setAttribute('data-value', `${key}${value}`); |
63 | 98 |
|
64 | | - const icon = document.createElement('span'); |
65 | | - icon.classList.add('icon', type === 'issue' ? 'issue' : 'pull-request'); |
66 | | - li.append(icon); |
| 99 | + const icon = document.createElement('div'); |
| 100 | + icon.innerHTML = svg(getIssueIcon(issue), 16, ['text', getIssueColor(issue)].join(' ')).trim(); |
| 101 | + li.append(icon.firstChild); |
67 | 102 |
|
68 | | - const nameSpan = document.createElement('span'); |
69 | | - nameSpan.textContent = name; |
70 | | - li.append(nameSpan); |
| 103 | + const id = document.createElement('span'); |
| 104 | + id.classList.add('id'); |
| 105 | + id.textContent = value; |
| 106 | + li.append(id); |
71 | 107 |
|
72 | | - ul.append(li); |
73 | | - } |
| 108 | + const nameSpan = document.createElement('span'); |
| 109 | + nameSpan.textContent = name; |
| 110 | + li.append(nameSpan); |
74 | 111 |
|
75 | | - provide({matched: true, fragment: ul}); |
| 112 | + ul.append(li); |
| 113 | + } |
| 114 | + |
| 115 | + resolve({matched: true, fragment: ul}); |
| 116 | + })); |
76 | 117 | } |
77 | 118 | }); |
78 | 119 | expander?.addEventListener('text-expander-value', ({detail}) => { |
|
0 commit comments