gitea源码

repo-findfile.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {svg} from '../svg.ts';
  2. import {toggleElem} from '../utils/dom.ts';
  3. import {pathEscapeSegments} from '../utils/url.ts';
  4. import {GET} from '../modules/fetch.ts';
  5. const threshold = 50;
  6. let files: Array<string> = [];
  7. let repoFindFileInput: HTMLInputElement;
  8. let repoFindFileTableBody: HTMLElement;
  9. let repoFindFileNoResult: HTMLElement;
  10. // return the case-insensitive sub-match result as an array: [unmatched, matched, unmatched, matched, ...]
  11. // res[even] is unmatched, res[odd] is matched, see unit tests for examples
  12. // argument subLower must be a lower-cased string.
  13. export function strSubMatch(full: string, subLower: string) {
  14. const res = [''];
  15. let i = 0, j = 0;
  16. const fullLower = full.toLowerCase();
  17. while (i < subLower.length && j < fullLower.length) {
  18. if (subLower[i] === fullLower[j]) {
  19. if (res.length % 2 !== 0) res.push('');
  20. res[res.length - 1] += full[j];
  21. j++;
  22. i++;
  23. } else {
  24. if (res.length % 2 === 0) res.push('');
  25. res[res.length - 1] += full[j];
  26. j++;
  27. }
  28. }
  29. if (i !== subLower.length) {
  30. // if the sub string doesn't match the full, only return the full as unmatched.
  31. return [full];
  32. }
  33. if (j < full.length) {
  34. // append remaining chars from full to result as unmatched
  35. if (res.length % 2 === 0) res.push('');
  36. res[res.length - 1] += full.substring(j);
  37. }
  38. return res;
  39. }
  40. export function calcMatchedWeight(matchResult: Array<any>) {
  41. let weight = 0;
  42. for (let i = 0; i < matchResult.length; i++) {
  43. if (i % 2 === 1) { // matches are on odd indices, see strSubMatch
  44. // use a function f(x+x) > f(x) + f(x) to make the longer matched string has higher weight.
  45. weight += matchResult[i].length * matchResult[i].length;
  46. }
  47. }
  48. return weight;
  49. }
  50. export function filterRepoFilesWeighted(files: Array<string>, filter: string) {
  51. let filterResult = [];
  52. if (filter) {
  53. const filterLower = filter.toLowerCase();
  54. // TODO: for large repo, this loop could be slow, maybe there could be one more limit:
  55. // ... && filterResult.length < threshold * 20, wait for more feedbacks
  56. for (const file of files) {
  57. const res = strSubMatch(file, filterLower);
  58. if (res.length > 1) { // length==1 means unmatched, >1 means having matched sub strings
  59. filterResult.push({matchResult: res, matchWeight: calcMatchedWeight(res)});
  60. }
  61. }
  62. filterResult.sort((a, b) => b.matchWeight - a.matchWeight);
  63. filterResult = filterResult.slice(0, threshold);
  64. } else {
  65. for (let i = 0; i < files.length && i < threshold; i++) {
  66. filterResult.push({matchResult: [files[i]], matchWeight: 0});
  67. }
  68. }
  69. return filterResult;
  70. }
  71. function filterRepoFiles(filter: string) {
  72. const treeLink = repoFindFileInput.getAttribute('data-url-tree-link');
  73. repoFindFileTableBody.innerHTML = '';
  74. const filterResult = filterRepoFilesWeighted(files, filter);
  75. toggleElem(repoFindFileNoResult, !filterResult.length);
  76. for (const r of filterResult) {
  77. const row = document.createElement('tr');
  78. const cell = document.createElement('td');
  79. const a = document.createElement('a');
  80. a.setAttribute('href', `${treeLink}/${pathEscapeSegments(r.matchResult.join(''))}`);
  81. a.innerHTML = svg('octicon-file', 16, 'tw-mr-2');
  82. row.append(cell);
  83. cell.append(a);
  84. for (const [index, part] of r.matchResult.entries()) {
  85. const span = document.createElement('span');
  86. // safely escape by using textContent
  87. span.textContent = part;
  88. span.title = span.textContent;
  89. // if the target file path is "abc/xyz", to search "bx", then the matchResult is ['a', 'b', 'c/', 'x', 'yz']
  90. // the matchResult[odd] is matched and highlighted to red.
  91. if (index % 2 === 1) span.classList.add('ui', 'text', 'red');
  92. a.append(span);
  93. }
  94. repoFindFileTableBody.append(row);
  95. }
  96. }
  97. async function loadRepoFiles() {
  98. const response = await GET(repoFindFileInput.getAttribute('data-url-data-link'));
  99. files = await response.json();
  100. filterRepoFiles(repoFindFileInput.value);
  101. }
  102. export function initFindFileInRepo() {
  103. repoFindFileInput = document.querySelector('#repo-file-find-input');
  104. if (!repoFindFileInput) return;
  105. repoFindFileTableBody = document.querySelector('#repo-find-file-table tbody');
  106. repoFindFileNoResult = document.querySelector('#repo-find-file-no-result');
  107. repoFindFileInput.addEventListener('input', () => filterRepoFiles(repoFindFileInput.value));
  108. loadRepoFiles();
  109. }