From 5c3be3e74f2431c5def2de5b8449a7ada261e046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sat, 2 Aug 2025 17:16:40 -0700 Subject: [PATCH] refactor(js): combine `handleShare()` and `triggerWebShare()` functions --- internal/ui/static/js/app.js | 42 ++++++++++++++---------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/internal/ui/static/js/app.js b/internal/ui/static/js/app.js index e53fdbf8..525f1785 100644 --- a/internal/ui/static/js/app.js +++ b/internal/ui/static/js/app.js @@ -323,35 +323,25 @@ function goToListItem(offset) { * If the share status is "share", it will send an Ajax request to fetch the share URL and then trigger the Web Share API. * If the Web Share API is not supported, it will redirect to the entry URL. */ -async function handleShare() { +async function handleEntryShareAction() { const link = document.querySelector(':is(a, button)[data-share-status]'); if (link.dataset.shareStatus === "shared") { const title = document.querySelector(".entry-header > h1 > a"); - await triggerWebShare(title, link.href); - } -} + const url = link.href; -/** - * Trigger the Web Share API to share the entry. - * - * If the Web Share API is not supported, it will redirect to the entry URL. - * - * @param {Element} title - The title element of the entry. - * @param {string} url - The URL of the entry to share. - */ -async function triggerWebShare(title, url) { - if (!navigator.canShare) { - console.error("Your browser doesn't support the Web Share API."); - window.location = url; - return; - } - try { - await navigator.share({ - title: title ? title.textContent : url, - url: url - }); - } catch (err) { - console.error(err); + if (!navigator.canShare) { + console.error("Your browser doesn't support the Web Share API."); + window.location = url; + return; + } + try { + await navigator.share({ + title: title ? title.textContent : url, + url: url + }); + } catch (err) { + console.error(err); + } } } @@ -1189,7 +1179,7 @@ function initializeClickHandlers() { onClick(":is(a, button)[data-toggle-bookmark]", (event) => handleBookmark(event.target)); onClick(":is(a, button)[data-toggle-status]", (event) => handleEntryStatus("next", event.target)); onClick(":is(a, button)[data-fetch-content-entry]", handleFetchOriginalContent); - onClick(":is(a, button)[data-share-status]", handleShare); + onClick(":is(a, button)[data-share-status]", handleEntryShareAction); // Page actions with confirmation onClick(":is(a, button)[data-action=markPageAsRead]", (event) =>