gitea源码

ReactionSelector.ts 1.4KB

1234567891011121314151617181920212223242526272829303132
  1. import {POST} from '../../modules/fetch.ts';
  2. import type {DOMEvent} from '../../utils/dom.ts';
  3. import {registerGlobalEventFunc} from '../../modules/observer.ts';
  4. export function initCompReactionSelector() {
  5. registerGlobalEventFunc('click', 'onCommentReactionButtonClick', async (target: HTMLElement, e: DOMEvent<MouseEvent>) => {
  6. // there are 2 places for the "reaction" buttons, one is the top-right reaction menu, one is the bottom of the comment
  7. e.preventDefault();
  8. if (target.classList.contains('disabled')) return;
  9. const actionUrl = target.closest('[data-action-url]').getAttribute('data-action-url');
  10. const reactionContent = target.getAttribute('data-reaction-content');
  11. const commentContainer = target.closest('.comment-container');
  12. const bottomReactions = commentContainer.querySelector('.bottom-reactions'); // may not exist if there is no reaction
  13. const bottomReactionBtn = bottomReactions?.querySelector(`a[data-reaction-content="${CSS.escape(reactionContent)}"]`);
  14. const hasReacted = bottomReactionBtn?.getAttribute('data-has-reacted') === 'true';
  15. const res = await POST(`${actionUrl}/${hasReacted ? 'unreact' : 'react'}`, {
  16. data: new URLSearchParams({content: reactionContent}),
  17. });
  18. const data = await res.json();
  19. bottomReactions?.remove();
  20. if (data.html) {
  21. commentContainer.insertAdjacentHTML('beforeend', data.html);
  22. }
  23. });
  24. }