From fa53d48de3c80370666b6cc474b15b8fcb8a2079 Mon Sep 17 00:00:00 2001 From: mgt Date: Sat, 28 Sep 2024 21:06:23 +0800 Subject: [PATCH] refactor: remove chained then/finally call --- frontend/lib/main.ts | 130 ++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 63 deletions(-) diff --git a/frontend/lib/main.ts b/frontend/lib/main.ts index f9947ee2..6d2f60f4 100644 --- a/frontend/lib/main.ts +++ b/frontend/lib/main.ts @@ -305,7 +305,7 @@ const _openCommentsPanel = async () => { }); } - _renderComments(comments); + await _renderComments(comments); let selectedCommentsGroup = document.querySelector( `#review-comments-panel .comments_group[data-original-document-start="${selectedOffset?.dataset.originalDocumentStart}"][data-original-document-end="${selectedOffset?.dataset.originalDocumentEnd}"]`, ) as HTMLElement; @@ -503,7 +503,7 @@ const _fetchComments = async (force: boolean = false) => { return comments; }; -const _renderComments = (comments: Comment[]) => { +const _renderComments = async (comments: Comment[]) => { const commentsEl = commentsPanel.querySelector( ".panel_main", )! as HTMLDivElement; @@ -767,7 +767,7 @@ const _renderComments = (comments: Comment[]) => { } for (const actions of container.querySelectorAll(".comment_actions_item")) { - actions.addEventListener("click", () => { + actions.addEventListener("click", async () => { const target = actions as HTMLButtonElement; const textarea = container.querySelector( @@ -818,9 +818,8 @@ const _renderComments = (comments: Comment[]) => { const permalink = new URL(window.location.href); permalink.hash = `#comment-${id}`; - navigator.clipboard.writeText(permalink.toString()).then(() => { - notification.textContent = "已复制评论链接地址"; - }); + await navigator.clipboard.writeText(permalink.toString()); + notification.textContent = "已复制评论链接地址"; break; } case "login": { @@ -854,43 +853,45 @@ const _renderComments = (comments: Comment[]) => { notification.textContent = ""; - _submitComment({ - offsets: [ - parseInt(selectedOffset!.dataset.originalDocumentStart!), - parseInt(selectedOffset!.dataset.originalDocumentEnd!), - ], - content: textarea.value, - }) - .then(() => { - textarea.value = ""; - notification.textContent = ""; - }) - .catch(_handleError) - .finally(() => { - _openCommentsPanel().then(() => { - const newNotification = commentsPanel.querySelector( - "[data-review-selected] .comment_actions_notification", - ); - const newTextArea = commentsPanel.querySelector( - "[data-review-selected] .comment_actions_panel textarea", - ) as HTMLTextAreaElement; - if (newNotification) { - newNotification.textContent = notification.textContent; - } - if (newTextArea) { - newTextArea.value = textarea.value; - } - }); + try { + await _submitComment({ + offsets: [ + parseInt(selectedOffset!.dataset.originalDocumentStart!), + parseInt(selectedOffset!.dataset.originalDocumentEnd!), + ], + content: textarea.value, }); - _openCommentsPanel().then(() => { - const newSubmitButton = commentsPanel.querySelector( - "[data-review-selected] button[data-action='submit']", - ) as HTMLButtonElement; - if (newSubmitButton) { - newSubmitButton.disabled = true; + textarea.value = ""; + notification.textContent = ""; + } catch (error) { + _handleError(error); + } finally { + await _openCommentsPanel(); + + const newNotification = commentsPanel.querySelector( + "[data-review-selected] .comment_actions_notification" + ); + const newTextArea = commentsPanel.querySelector( + "[data-review-selected] .comment_actions_panel textarea" + ) as HTMLTextAreaElement; + + if (newNotification) { + newNotification.textContent = notification.textContent; } - }); + if (newTextArea) { + newTextArea.value = textarea.value; + } + } + + await _openCommentsPanel(); + const newSubmitButton = commentsPanel.querySelector( + "[data-review-selected] button[data-action='submit']" + ) as HTMLButtonElement; + if (newSubmitButton) { + newSubmitButton.disabled = true; + } + break; } case "modify": { @@ -943,30 +944,33 @@ const _renderComments = (comments: Comment[]) => { const id = container.dataset.modifingId; if (id == undefined) return; - _modifyComment({ id: parseInt(id), comment: textarea.value }) - .then(() => { - textarea.value = ""; - notification.textContent = ""; - }) - .catch(_handleError) - .finally(() => { - _openCommentsPanel().then(() => { - const newNotification = commentsPanel.querySelector( - "[data-review-selected] .comment_actions_notification", - ); - const newTextArea = commentsPanel.querySelector( - "[data-review-selected] .comment_actions_panel textarea", - ) as HTMLTextAreaElement; - if (newNotification) { - newNotification.textContent = notification.textContent; - } - if (newTextArea) { - newTextArea.value = textarea.value; - } - }); - }); + try { + await _modifyComment({ id: parseInt(id), comment: textarea.value }); + + textarea.value = ""; + notification.textContent = ""; + } catch (error) { + _handleError(error); + } finally { + await _openCommentsPanel(); + + const newNotification = commentsPanel.querySelector( + "[data-review-selected] .comment_actions_notification" + ); + const newTextArea = commentsPanel.querySelector( + "[data-review-selected] .comment_actions_panel textarea" + ) as HTMLTextAreaElement; + + if (newNotification) { + newNotification.textContent = notification.textContent; + } + if (newTextArea) { + newTextArea.value = textarea.value; + } + } + - _openCommentsPanel(); + await _openCommentsPanel(); // ?? break; } case "delete": {