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

fix: use another method for suggestions

This commit is contained in:
Maxim Slipenko 2025-06-07 18:21:41 +03:00
parent 3d4372c8bf
commit 6e2c72cd70
9 changed files with 122 additions and 102 deletions

View file

@ -1,30 +1,29 @@
import {matchEmoji, matchMention, matchIssue} from '../../utils/match.js';
import {emojiString} from '../emoji.js';
import {getIssueIcon, getIssueColor} from '../issue.js'
import {parseIssueHref} from '../../utils.js'
import {getIssueIcon, getIssueColor,isIssueSuggestionsLoaded, fetchIssueSuggestions} from '../issue.js'
import {svg} from '../../svg.js'
import {createElementFromHTML} from '../../utils/dom.js';
import {debounce} from 'perfect-debounce';
import { GET } from '../../modules/fetch.js';
const debouncedSuggestIssues = debounce((key, text) => new Promise(
async (resolve, reject) => {
const {owner, repo, index} = parseIssueHref(window.location.href);
const matches = await matchIssue(owner, repo, index, text);
if (!matches.length) return resolve({matched: false});
async function issueSuggestions(text) {
const key = '#';
const matches = matchIssue(text);
if (!matches.length) return {matched: false};
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.id}`);
li.setAttribute('data-value', `${key}${issue.number}`);
li.classList.add('tw-flex', 'tw-gap-2')
const icon = svg(getIssueIcon(issue), 16, ['text', getIssueColor(issue)].join(' '));
li.append(createElementFromHTML(icon));
const id = document.createElement('span');
id.textContent = issue.id.toString();
id.textContent = issue.number.toString();
li.append(id);
const nameSpan = document.createElement('span');
@ -34,10 +33,14 @@ const debouncedSuggestIssues = debounce((key, text) => new Promise(
ul.append(li);
}
resolve({matched: true, fragment: ul});
}), 100)
return {matched: true, fragment: ul};
}
export function initTextExpander(expander) {
if (!expander) return;
const textarea = expander.querySelector('textarea');
expander?.addEventListener('text-expander-change', ({detail: {key, provide, text}}) => {
if (key === ':') {
const matches = matchEmoji(text);
@ -86,7 +89,11 @@ export function initTextExpander(expander) {
provide({matched: true, fragment: ul});
} else if (key === '#') {
provide(debouncedSuggestIssues(key, text));
if (!isIssueSuggestionsLoaded()) {
provide(fetchIssueSuggestions().then(() => issueSuggestions(text)));
} else {
provide(issueSuggestions(text));
}
}
});
expander?.addEventListener('text-expander-value', ({detail}) => {

View file

@ -1,3 +1,6 @@
import { GET } from '../modules/fetch.js';
import {parseIssueHref, parseRepoOwnerPathInfo} from '../utils.js'
export function getIssueIcon(issue) {
if (issue.pull_request) {
if (issue.state === 'open') {
@ -15,16 +18,37 @@ export function getIssueIcon(issue) {
return 'octicon-issue-closed'; // Closed Issue
}
export function getIssueColor(issue) {
if (issue.pull_request) {
if (issue.pull_request.draft === true) {
return 'grey'; // WIP PR
} else if (issue.pull_request.merged === true) {
return 'purple'; // Merged PR
}
export function getIssueColor(issue) {
if (issue.pull_request) {
if (issue.pull_request.draft === true) {
return 'grey'; // WIP PR
} else if (issue.pull_request.merged === true) {
return 'purple'; // Merged PR
}
if (issue.state === 'open') {
return 'green'; // Open Issue
}
return 'red'; // Closed Issue
}
}
if (issue.state === 'open') {
return 'green'; // Open Issue
}
return 'red'; // Closed Issue
}
export function isIssueSuggestionsLoaded() {
return !!window.config.issueValues
}
async function fetchIssueSuggestions() {
const issuePathInfo = parseIssueHref(window.location.href);
if (!issuePathInfo.ownerName) {
const repoOwnerPathInfo = parseRepoOwnerPathInfo(window.location.pathname);
issuePathInfo.ownerName = repoOwnerPathInfo.ownerName;
issuePathInfo.repoName = repoOwnerPathInfo.repoName;
// then no issuePathInfo.indexString here, it is only used to exclude the current issue when "matchIssue"
}
if (!issuePathInfo.ownerName) {
throw new Error('unexpected');
}
const res = await GET(`${window.config.appSubUrl}/${issuePathInfo.ownerName}/${issuePathInfo.repoName}/issues/suggestions`);
const issues = await res.json();
window.config.issueValues = issues;
}