1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-10-10 19:32:02 +00:00
forgejo/web_src/js/features/comp/TextExpander.js

106 lines
3.5 KiB
JavaScript
Raw Normal View History

2025-06-06 01:25:07 +03:00
import {matchEmoji, matchMention, matchIssue} from '../../utils/match.js';
import {emojiString} from '../emoji.js';
2025-06-07 19:21:15 +03:00
import {getIssueIcon, getIssueColor, isIssueSuggestionsLoaded, fetchIssueSuggestions} from '../issue.js';
import {svg} from '../../svg.js';
2025-06-06 01:25:07 +03:00
import {createElementFromHTML} from '../../utils/dom.js';
2025-06-07 19:21:15 +03:00
import {parseIssueHref} from '../../utils.js';
2025-06-06 01:25:07 +03:00
async function issueSuggestions(text) {
const key = '#';
2025-06-07 19:17:00 +03:00
const issuePathInfo = parseIssueHref(window.location.href);
const matches = matchIssue(text, Number(issuePathInfo.index));
if (!matches.length) return {matched: false};
2025-06-06 01:25:07 +03:00
const ul = document.createElement('ul');
ul.classList.add('suggestions');
for (const issue of matches) {
const li = document.createElement('li');
li.setAttribute('role', 'option');
li.setAttribute('data-value', `${key}${issue.number}`);
2025-06-07 19:21:15 +03:00
li.classList.add('tw-flex', 'tw-gap-2');
2025-06-06 01:25:07 +03:00
const icon = svg(getIssueIcon(issue), 16, ['text', getIssueColor(issue)].join(' '));
li.append(createElementFromHTML(icon));
const id = document.createElement('span');
id.textContent = issue.number.toString();
2025-06-06 01:25:07 +03:00
li.append(id);
const nameSpan = document.createElement('span');
nameSpan.textContent = issue.title;
li.append(nameSpan);
ul.append(li);
}
return {matched: true, fragment: ul};
}
export function initTextExpander(expander) {
if (!expander) return;
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});
2025-06-06 01:25:07 +03:00
} else if (key === '#') {
if (!isIssueSuggestionsLoaded()) {
provide(fetchIssueSuggestions().then(() => issueSuggestions(text)));
} else {
provide(issueSuggestions(text));
}
}
});
expander?.addEventListener('text-expander-value', ({detail}) => {
if (detail?.item) {
2025-06-06 01:25:07 +03:00
// add a space after @mentions and #issue as it's likely the user wants one
const suffix = ['@', '#'].includes(detail.key) ? ' ' : '';
detail.value = `${detail.item.getAttribute('data-value')}${suffix}`;
}
});
}