From cd01c71926c21bde336707d94b9bf22cd2a8d7ca Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 21 Nov 2024 20:49:46 +0800 Subject: [PATCH] addDelegatedEventListener --- web_src/js/features/repo-diff.ts | 4 ++-- web_src/js/features/repo-issue.ts | 4 ++-- web_src/js/utils/dom.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/web_src/js/features/repo-diff.ts b/web_src/js/features/repo-diff.ts index 1b8d023c0f..0d489665a2 100644 --- a/web_src/js/features/repo-diff.ts +++ b/web_src/js/features/repo-diff.ts @@ -13,7 +13,7 @@ import { hideElem, showElem, animateOnce, - addElemsEventListener, + addDelegatedEventListener, createElementFromHTML, } from '../utils/dom.ts'; import {POST, GET} from '../modules/fetch.ts'; @@ -37,7 +37,7 @@ function initRepoDiffFileViewToggle() { } function initRepoDiffConversationForm() { - addElemsEventListener(document, 'submit', '.conversation-holder form', async (form, e) => { + addDelegatedEventListener(document, 'submit', '.conversation-holder form', async (form, e) => { e.preventDefault(); const textArea = form.querySelector('textarea'); if (!validateTextareaNonEmpty(textArea)) return; diff --git a/web_src/js/features/repo-issue.ts b/web_src/js/features/repo-issue.ts index ca87add32a..9cc478712b 100644 --- a/web_src/js/features/repo-issue.ts +++ b/web_src/js/features/repo-issue.ts @@ -1,7 +1,7 @@ import $ from 'jquery'; import {htmlEscape} from 'escape-goat'; import {createTippy, showTemporaryTooltip} from '../modules/tippy.ts'; -import {addElemsEventListener, createElementFromHTML, hideElem, showElem, toggleElem} from '../utils/dom.ts'; +import {addDelegatedEventListener, createElementFromHTML, hideElem, showElem, toggleElem} from '../utils/dom.ts'; import {setFileFolding} from './file-fold.ts'; import {ComboMarkdownEditor, getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts'; import {parseIssuePageInfo, toAbsoluteUrl} from '../utils.ts'; @@ -443,7 +443,7 @@ export function initRepoPullRequestReview() { }); } - addElemsEventListener(document, 'click', '.add-code-comment', async (el, e) => { + addDelegatedEventListener(document, 'click', '.add-code-comment', async (el, e) => { e.preventDefault(); const isSplit = el.closest('.code-diff')?.classList.contains('code-diff-split'); diff --git a/web_src/js/utils/dom.ts b/web_src/js/utils/dom.ts index 674c497596..8bb9b8defc 100644 --- a/web_src/js/utils/dom.ts +++ b/web_src/js/utils/dom.ts @@ -307,11 +307,11 @@ export function createElementFromHTML(htmlString: string): T { if (htmlString.startsWith(', ...children: (Node|string)[]): HTMLElement { @@ -348,7 +348,7 @@ export function querySingleVisibleElem(parent: Element, s return candidates.length ? candidates[0] as T : null; } -export function addElemsEventListener(parent: Node, type: string, selector: string, listener: (elem: T, e: Event) => void | Promise, options?: boolean | AddEventListenerOptions) { +export function addDelegatedEventListener(parent: Node, type: string, selector: string, listener: (elem: T, e: Event) => void | Promise, options?: boolean | AddEventListenerOptions) { parent.addEventListener(type, (e: Event) => { const elem = (e.target as HTMLElement).closest(selector); if (!elem) return;