Skip to content

Commit cd01c71

Browse files
committed
addDelegatedEventListener
1 parent 3b4b391 commit cd01c71

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

web_src/js/features/repo-diff.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
hideElem,
1414
showElem,
1515
animateOnce,
16-
addElemsEventListener,
16+
addDelegatedEventListener,
1717
createElementFromHTML,
1818
} from '../utils/dom.ts';
1919
import {POST, GET} from '../modules/fetch.ts';
@@ -37,7 +37,7 @@ function initRepoDiffFileViewToggle() {
3737
}
3838

3939
function initRepoDiffConversationForm() {
40-
addElemsEventListener<HTMLFormElement>(document, 'submit', '.conversation-holder form', async (form, e) => {
40+
addDelegatedEventListener<HTMLFormElement>(document, 'submit', '.conversation-holder form', async (form, e) => {
4141
e.preventDefault();
4242
const textArea = form.querySelector<HTMLTextAreaElement>('textarea');
4343
if (!validateTextareaNonEmpty(textArea)) return;

web_src/js/features/repo-issue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $ from 'jquery';
22
import {htmlEscape} from 'escape-goat';
33
import {createTippy, showTemporaryTooltip} from '../modules/tippy.ts';
4-
import {addElemsEventListener, createElementFromHTML, hideElem, showElem, toggleElem} from '../utils/dom.ts';
4+
import {addDelegatedEventListener, createElementFromHTML, hideElem, showElem, toggleElem} from '../utils/dom.ts';
55
import {setFileFolding} from './file-fold.ts';
66
import {ComboMarkdownEditor, getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
77
import {parseIssuePageInfo, toAbsoluteUrl} from '../utils.ts';
@@ -443,7 +443,7 @@ export function initRepoPullRequestReview() {
443443
});
444444
}
445445

446-
addElemsEventListener(document, 'click', '.add-code-comment', async (el, e) => {
446+
addDelegatedEventListener(document, 'click', '.add-code-comment', async (el, e) => {
447447
e.preventDefault();
448448

449449
const isSplit = el.closest('.code-diff')?.classList.contains('code-diff-split');

web_src/js/utils/dom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ export function createElementFromHTML<T = HTMLElement>(htmlString: string): T {
307307
if (htmlString.startsWith('<tr')) {
308308
const container = document.createElement('table');
309309
container.innerHTML = htmlString;
310-
return container.querySelector('tr') as HTMLElement;
310+
return container.querySelector('tr') as T;
311311
}
312312
const div = document.createElement('div');
313313
div.innerHTML = htmlString;
314-
return div.firstChild as HTMLElement;
314+
return div.firstChild as T;
315315
}
316316

317317
export function createElementFromAttrs(tagName: string, attrs: Record<string, any>, ...children: (Node|string)[]): HTMLElement {
@@ -348,7 +348,7 @@ export function querySingleVisibleElem<T extends HTMLElement>(parent: Element, s
348348
return candidates.length ? candidates[0] as T : null;
349349
}
350350

351-
export function addElemsEventListener<T extends HTMLElement>(parent: Node, type: string, selector: string, listener: (elem: T, e: Event) => void | Promise<any>, options?: boolean | AddEventListenerOptions) {
351+
export function addDelegatedEventListener<T extends HTMLElement>(parent: Node, type: string, selector: string, listener: (elem: T, e: Event) => void | Promise<any>, options?: boolean | AddEventListenerOptions) {
352352
parent.addEventListener(type, (e: Event) => {
353353
const elem = (e.target as HTMLElement).closest(selector);
354354
if (!elem) return;

0 commit comments

Comments
 (0)