gitea源码

common-issue-list.ts 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {isElemVisible, onInputDebounce, submitEventSubmitter, toggleElem} from '../utils/dom.ts';
  2. import {GET} from '../modules/fetch.ts';
  3. const {appSubUrl} = window.config;
  4. const reIssueIndex = /^(\d+)$/; // eg: "123"
  5. const reIssueSharpIndex = /^#(\d+)$/; // eg: "#123"
  6. const reIssueOwnerRepoIndex = /^([-.\w]+)\/([-.\w]+)#(\d+)$/; // eg: "{owner}/{repo}#{index}"
  7. // if the searchText can be parsed to an "issue goto link", return the link, otherwise return empty string
  8. export function parseIssueListQuickGotoLink(repoLink: string, searchText: string) {
  9. searchText = searchText.trim();
  10. let targetUrl = '';
  11. if (repoLink) {
  12. // try to parse it in current repo
  13. if (reIssueIndex.test(searchText)) {
  14. targetUrl = `${repoLink}/issues/${searchText}`;
  15. } else if (reIssueSharpIndex.test(searchText)) {
  16. targetUrl = `${repoLink}/issues/${searchText.substring(1)}`;
  17. }
  18. } else {
  19. // try to parse it for a global search (eg: "owner/repo#123")
  20. const [_, owner, repo, index] = reIssueOwnerRepoIndex.exec(searchText) || [];
  21. if (owner) {
  22. targetUrl = `${appSubUrl}/${owner}/${repo}/issues/${index}`;
  23. }
  24. }
  25. return targetUrl;
  26. }
  27. export function initCommonIssueListQuickGoto() {
  28. const goto = document.querySelector<HTMLElement>('#issue-list-quick-goto');
  29. if (!goto) return;
  30. const form = goto.closest('form');
  31. const input = form.querySelector<HTMLInputElement>('input[name=q]');
  32. const repoLink = goto.getAttribute('data-repo-link');
  33. form.addEventListener('submit', (e) => {
  34. // if there is no goto button, or the form is submitted by non-quick-goto elements, submit the form directly
  35. let doQuickGoto = isElemVisible(goto);
  36. const submitter = submitEventSubmitter(e);
  37. if (submitter !== form && submitter !== input && submitter !== goto) doQuickGoto = false;
  38. if (!doQuickGoto) return;
  39. // if there is a goto button, use its link
  40. e.preventDefault();
  41. window.location.href = goto.getAttribute('data-issue-goto-link');
  42. });
  43. const onInput = async () => {
  44. const searchText = input.value;
  45. // try to check whether the parsed goto link is valid
  46. let targetUrl = parseIssueListQuickGotoLink(repoLink, searchText);
  47. if (targetUrl) {
  48. const res = await GET(`${targetUrl}/info`); // backend: GetIssueInfo, it only checks whether the issue exists by status code
  49. if (res.status !== 200) targetUrl = '';
  50. }
  51. // if the input value has changed, then ignore the result
  52. if (input.value !== searchText) return;
  53. toggleElem(goto, Boolean(targetUrl));
  54. goto.setAttribute('data-issue-goto-link', targetUrl);
  55. };
  56. input.addEventListener('input', onInputDebounce(onInput));
  57. onInput();
  58. }