2024-10-29 05:20:49 -04:00
|
|
|
import {matchEmoji, matchMention, matchIssue} from '../../utils/match.ts';
|
2024-07-07 11:32:30 -04:00
|
|
|
import {emojiString} from '../emoji.ts';
|
2024-10-29 05:20:49 -04:00
|
|
|
import {svg} from '../../svg.ts';
|
2024-10-30 16:06:36 -04:00
|
|
|
import {parseIssueHref, parseIssueNewHref} from '../../utils.ts';
|
2024-10-29 05:20:49 -04:00
|
|
|
import {createElementFromAttrs, createElementFromHTML} from '../../utils/dom.ts';
|
|
|
|
import {getIssueColor, getIssueIcon} from '../issue.ts';
|
|
|
|
import {debounce} from 'perfect-debounce';
|
|
|
|
|
|
|
|
const debouncedSuggestIssues = debounce((key: string, text: string) => new Promise<{matched:boolean; fragment?: HTMLElement}>(async (resolve) => {
|
2024-10-30 16:06:36 -04:00
|
|
|
let issuePathInfo = parseIssueHref(window.location.href);
|
|
|
|
if (!issuePathInfo.ownerName) issuePathInfo = parseIssueNewHref(window.location.href);
|
|
|
|
if (!issuePathInfo.ownerName) return resolve({matched: false});
|
|
|
|
|
|
|
|
const matches = await matchIssue(issuePathInfo.ownerName, issuePathInfo.repoName, issuePathInfo.indexString, text);
|
2024-10-29 05:20:49 -04:00
|
|
|
if (!matches.length) return resolve({matched: false});
|
|
|
|
|
2024-10-30 16:06:36 -04:00
|
|
|
const ul = createElementFromAttrs('ul', {class: 'suggestions'});
|
2024-10-29 05:20:49 -04:00
|
|
|
for (const issue of matches) {
|
2024-10-30 16:06:36 -04:00
|
|
|
const li = createElementFromAttrs(
|
|
|
|
'li', {role: 'option', class: 'tw-flex tw-gap-2', 'data-value': `${key}${issue.number}`},
|
|
|
|
createElementFromHTML(svg(getIssueIcon(issue), 16, ['text', getIssueColor(issue)])),
|
|
|
|
createElementFromAttrs('span', null, `#${issue.number}`),
|
|
|
|
createElementFromAttrs('span', null, issue.title),
|
|
|
|
);
|
2024-10-29 05:20:49 -04:00
|
|
|
ul.append(li);
|
|
|
|
}
|
|
|
|
resolve({matched: true, fragment: ul});
|
|
|
|
}), 100);
|
2023-05-08 18:22:52 -04:00
|
|
|
|
|
|
|
export function initTextExpander(expander) {
|
|
|
|
expander?.addEventListener('text-expander-change', ({detail: {key, provide, text}}) => {
|
|
|
|
if (key === ':') {
|
|
|
|
const matches = matchEmoji(text);
|
|
|
|
if (!matches.length) return provide({matched: false});
|
|
|
|
|
|
|
|
const ul = document.createElement('ul');
|
|
|
|
ul.classList.add('suggestions');
|
|
|
|
for (const name of matches) {
|
|
|
|
const emoji = emojiString(name);
|
|
|
|
const li = document.createElement('li');
|
|
|
|
li.setAttribute('role', 'option');
|
|
|
|
li.setAttribute('data-value', emoji);
|
|
|
|
li.textContent = `${emoji} ${name}`;
|
|
|
|
ul.append(li);
|
|
|
|
}
|
|
|
|
|
|
|
|
provide({matched: true, fragment: ul});
|
|
|
|
} else if (key === '@') {
|
|
|
|
const matches = matchMention(text);
|
|
|
|
if (!matches.length) return provide({matched: false});
|
|
|
|
|
|
|
|
const ul = document.createElement('ul');
|
|
|
|
ul.classList.add('suggestions');
|
|
|
|
for (const {value, name, fullname, avatar} of matches) {
|
|
|
|
const li = document.createElement('li');
|
|
|
|
li.setAttribute('role', 'option');
|
|
|
|
li.setAttribute('data-value', `${key}${value}`);
|
|
|
|
|
|
|
|
const img = document.createElement('img');
|
|
|
|
img.src = avatar;
|
|
|
|
li.append(img);
|
|
|
|
|
|
|
|
const nameSpan = document.createElement('span');
|
|
|
|
nameSpan.textContent = name;
|
|
|
|
li.append(nameSpan);
|
|
|
|
|
|
|
|
if (fullname && fullname.toLowerCase() !== name) {
|
|
|
|
const fullnameSpan = document.createElement('span');
|
|
|
|
fullnameSpan.classList.add('fullname');
|
|
|
|
fullnameSpan.textContent = fullname;
|
|
|
|
li.append(fullnameSpan);
|
|
|
|
}
|
|
|
|
|
|
|
|
ul.append(li);
|
|
|
|
}
|
|
|
|
|
|
|
|
provide({matched: true, fragment: ul});
|
2024-10-29 05:20:49 -04:00
|
|
|
} else if (key === '#') {
|
|
|
|
provide(debouncedSuggestIssues(key, text));
|
2023-05-08 18:22:52 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
expander?.addEventListener('text-expander-value', ({detail}) => {
|
|
|
|
if (detail?.item) {
|
2024-10-29 05:20:49 -04:00
|
|
|
// add a space after @mentions and #issue as it's likely the user wants one
|
|
|
|
const suffix = ['@', '#'].includes(detail.key) ? ' ' : '';
|
2023-06-18 04:38:47 -04:00
|
|
|
detail.value = `${detail.item.getAttribute('data-value')}${suffix}`;
|
2023-05-08 18:22:52 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|