gitea源码

repo-diff.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {initRepoIssueContentHistory} from './repo-issue-content.ts';
  2. import {initDiffFileTree} from './repo-diff-filetree.ts';
  3. import {initDiffCommitSelect} from './repo-diff-commitselect.ts';
  4. import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.ts';
  5. import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.ts';
  6. import {initImageDiff} from './imagediff.ts';
  7. import {showErrorToast} from '../modules/toast.ts';
  8. import {submitEventSubmitter, queryElemSiblings, hideElem, showElem, animateOnce, addDelegatedEventListener, createElementFromHTML, queryElems} from '../utils/dom.ts';
  9. import {POST, GET} from '../modules/fetch.ts';
  10. import {createTippy} from '../modules/tippy.ts';
  11. import {invertFileFolding} from './file-fold.ts';
  12. import {parseDom} from '../utils.ts';
  13. import {registerGlobalSelectorFunc} from '../modules/observer.ts';
  14. const {i18n} = window.config;
  15. function initRepoDiffFileBox(el: HTMLElement) {
  16. // switch between "rendered" and "source", for image and CSV files
  17. queryElems(el, '.file-view-toggle', (btn) => btn.addEventListener('click', () => {
  18. queryElemSiblings(btn, '.file-view-toggle', (el) => el.classList.remove('active'));
  19. btn.classList.add('active');
  20. const target = document.querySelector(btn.getAttribute('data-toggle-selector'));
  21. if (!target) throw new Error('Target element not found');
  22. hideElem(queryElemSiblings(target));
  23. showElem(target);
  24. }));
  25. }
  26. function initRepoDiffConversationForm() {
  27. // FIXME: there could be various different form in a conversation-holder (for example: reply form, edit form).
  28. // This listener is for "reply form" only, it should clearly distinguish different forms in the future.
  29. addDelegatedEventListener<HTMLFormElement, SubmitEvent>(document, 'submit', '.conversation-holder form', async (form, e) => {
  30. e.preventDefault();
  31. const textArea = form.querySelector<HTMLTextAreaElement>('textarea');
  32. if (!validateTextareaNonEmpty(textArea)) return;
  33. if (form.classList.contains('is-loading')) return;
  34. try {
  35. form.classList.add('is-loading');
  36. const formData = new FormData(form);
  37. // if the form is submitted by a button, append the button's name and value to the form data
  38. const submitter = submitEventSubmitter(e);
  39. const isSubmittedByButton = (submitter?.nodeName === 'BUTTON') || (submitter?.nodeName === 'INPUT' && submitter.type === 'submit');
  40. if (isSubmittedByButton && submitter.name) {
  41. formData.append(submitter.name, submitter.value);
  42. }
  43. // on the diff page, the form is inside a "tr" and need to get the line-type ahead
  44. // but on the conversation page, there is no parent "tr"
  45. const trLineType = form.closest('tr')?.getAttribute('data-line-type');
  46. const response = await POST(form.getAttribute('action'), {data: formData});
  47. const newConversationHolder = createElementFromHTML(await response.text());
  48. const path = newConversationHolder.getAttribute('data-path');
  49. const side = newConversationHolder.getAttribute('data-side');
  50. const idx = newConversationHolder.getAttribute('data-idx');
  51. form.closest('.conversation-holder').replaceWith(newConversationHolder);
  52. form = null; // prevent further usage of the form because it should have been replaced
  53. if (trLineType) {
  54. // if there is a line-type for the "tr", it means the form is on the diff page
  55. // then hide the "add-code-comment" [+] button for current code line by adding "tw-invisible" because the conversation has been added
  56. let selector;
  57. if (trLineType === 'same') {
  58. selector = `[data-path="${path}"] .add-code-comment[data-idx="${idx}"]`;
  59. } else {
  60. selector = `[data-path="${path}"] .add-code-comment[data-side="${side}"][data-idx="${idx}"]`;
  61. }
  62. for (const el of document.querySelectorAll(selector)) {
  63. el.classList.add('tw-invisible');
  64. }
  65. }
  66. // the default behavior is to add a pending review, so if no submitter, it also means "pending_review"
  67. if (!submitter || submitter?.matches('button[name="pending_review"]')) {
  68. const reviewBox = document.querySelector('#review-box');
  69. const counter = reviewBox?.querySelector('.review-comments-counter');
  70. if (!counter) return;
  71. const num = parseInt(counter.getAttribute('data-pending-comment-number')) + 1 || 1;
  72. counter.setAttribute('data-pending-comment-number', String(num));
  73. counter.textContent = String(num);
  74. animateOnce(reviewBox, 'pulse-1p5-200');
  75. }
  76. } catch (error) {
  77. console.error('Error:', error);
  78. showErrorToast(i18n.network_error);
  79. } finally {
  80. form?.classList.remove('is-loading');
  81. }
  82. });
  83. addDelegatedEventListener(document, 'click', '.resolve-conversation', async (el, e) => {
  84. e.preventDefault();
  85. const comment_id = el.getAttribute('data-comment-id');
  86. const origin = el.getAttribute('data-origin');
  87. const action = el.getAttribute('data-action');
  88. const url = el.getAttribute('data-update-url');
  89. try {
  90. const response = await POST(url, {data: new URLSearchParams({origin, action, comment_id})});
  91. const data = await response.text();
  92. const elConversationHolder = el.closest('.conversation-holder');
  93. if (elConversationHolder) {
  94. const elNewConversation = createElementFromHTML(data);
  95. elConversationHolder.replaceWith(elNewConversation);
  96. } else {
  97. window.location.reload();
  98. }
  99. } catch (error) {
  100. console.error('Error:', error);
  101. }
  102. });
  103. }
  104. function initRepoDiffConversationNav() {
  105. // Previous/Next code review conversation
  106. addDelegatedEventListener(document, 'click', '.previous-conversation, .next-conversation', (el, e) => {
  107. e.preventDefault();
  108. const isPrevious = el.matches('.previous-conversation');
  109. const elCurConversation = el.closest('.comment-code-cloud');
  110. const elAllConversations = document.querySelectorAll('.comment-code-cloud:not(.tw-hidden)');
  111. const index = Array.from(elAllConversations).indexOf(elCurConversation);
  112. const previousIndex = index > 0 ? index - 1 : elAllConversations.length - 1;
  113. const nextIndex = index < elAllConversations.length - 1 ? index + 1 : 0;
  114. const navIndex = isPrevious ? previousIndex : nextIndex;
  115. const elNavConversation = elAllConversations[navIndex];
  116. const anchor = elNavConversation.querySelector('.comment').id;
  117. window.location.href = `#${anchor}`;
  118. });
  119. }
  120. function initDiffHeaderPopup() {
  121. for (const btn of document.querySelectorAll('.diff-header-popup-btn:not([data-header-popup-initialized])')) {
  122. btn.setAttribute('data-header-popup-initialized', '');
  123. const popup = btn.nextElementSibling;
  124. if (!popup?.matches('.tippy-target')) throw new Error('Popup element not found');
  125. createTippy(btn, {
  126. content: popup,
  127. theme: 'menu',
  128. placement: 'bottom-end',
  129. trigger: 'click',
  130. interactive: true,
  131. hideOnClick: true,
  132. });
  133. }
  134. }
  135. // Will be called when the show more (files) button has been pressed
  136. function onShowMoreFiles() {
  137. // TODO: replace these calls with the "observer.ts" methods
  138. initRepoIssueContentHistory();
  139. initViewedCheckboxListenerFor();
  140. countAndUpdateViewedFiles();
  141. initImageDiff();
  142. initDiffHeaderPopup();
  143. }
  144. async function loadMoreFiles(btn: Element): Promise<boolean> {
  145. if (btn.classList.contains('disabled')) {
  146. return false;
  147. }
  148. btn.classList.add('disabled');
  149. const url = btn.getAttribute('data-href');
  150. try {
  151. const response = await GET(url);
  152. const resp = await response.text();
  153. const respDoc = parseDom(resp, 'text/html');
  154. const respFileBoxes = respDoc.querySelector('#diff-file-boxes');
  155. // the response is a full HTML page, we need to extract the relevant contents:
  156. // * append the newly loaded file list items to the existing list
  157. document.querySelector('#diff-incomplete').replaceWith(...Array.from(respFileBoxes.children));
  158. onShowMoreFiles();
  159. return true;
  160. } catch (error) {
  161. console.error('Error:', error);
  162. showErrorToast('An error occurred while loading more files.');
  163. } finally {
  164. btn.classList.remove('disabled');
  165. }
  166. return false;
  167. }
  168. function initRepoDiffShowMore() {
  169. addDelegatedEventListener(document, 'click', 'a#diff-show-more-files', (el, e) => {
  170. e.preventDefault();
  171. loadMoreFiles(el);
  172. });
  173. addDelegatedEventListener(document, 'click', 'a.diff-load-button', async (el, e) => {
  174. e.preventDefault();
  175. if (el.classList.contains('disabled')) return;
  176. el.classList.add('disabled');
  177. const url = el.getAttribute('data-href');
  178. try {
  179. const response = await GET(url);
  180. const resp = await response.text();
  181. const respDoc = parseDom(resp, 'text/html');
  182. const respFileBody = respDoc.querySelector('#diff-file-boxes .diff-file-body .file-body');
  183. const respFileBodyChildren = Array.from(respFileBody.children); // respFileBody.children will be empty after replaceWith
  184. el.parentElement.replaceWith(...respFileBodyChildren);
  185. for (const el of respFileBodyChildren) window.htmx.process(el);
  186. // FIXME: calling onShowMoreFiles is not quite right here.
  187. // But since onShowMoreFiles mixes "init diff box" and "init diff body" together,
  188. // so it still needs to call it to make the "ImageDiff" and something similar work.
  189. onShowMoreFiles();
  190. } catch (error) {
  191. console.error('Error:', error);
  192. } finally {
  193. el.classList.remove('disabled');
  194. }
  195. });
  196. }
  197. async function loadUntilFound() {
  198. const hashTargetSelector = window.location.hash;
  199. if (!hashTargetSelector.startsWith('#diff-') && !hashTargetSelector.startsWith('#issuecomment-')) {
  200. return;
  201. }
  202. while (true) {
  203. // use getElementById to avoid querySelector throws an error when the hash is invalid
  204. // eslint-disable-next-line unicorn/prefer-query-selector
  205. const targetElement = document.getElementById(hashTargetSelector.substring(1));
  206. if (targetElement) {
  207. targetElement.scrollIntoView();
  208. return;
  209. }
  210. // the button will be refreshed after each "load more", so query it every time
  211. const showMoreButton = document.querySelector('#diff-show-more-files');
  212. if (!showMoreButton) {
  213. return; // nothing more to load
  214. }
  215. // Load more files, await ensures we don't block progress
  216. const ok = await loadMoreFiles(showMoreButton);
  217. if (!ok) return; // failed to load more files
  218. }
  219. }
  220. function initRepoDiffHashChangeListener() {
  221. window.addEventListener('hashchange', loadUntilFound);
  222. loadUntilFound();
  223. }
  224. export function initRepoDiffView() {
  225. initRepoDiffConversationForm(); // such form appears on the "conversation" page and "diff" page
  226. if (!document.querySelector('#diff-file-boxes')) return;
  227. initRepoDiffConversationNav(); // "previous" and "next" buttons only appear on "diff" page
  228. initDiffFileTree();
  229. initDiffCommitSelect();
  230. initRepoDiffShowMore();
  231. initDiffHeaderPopup();
  232. initViewedCheckboxListenerFor();
  233. initExpandAndCollapseFilesButton();
  234. initRepoDiffHashChangeListener();
  235. registerGlobalSelectorFunc('#diff-file-boxes .diff-file-box', initRepoDiffFileBox);
  236. addDelegatedEventListener(document, 'click', '.fold-file', (el) => {
  237. invertFileFolding(el.closest('.file-content'), el);
  238. });
  239. }