Skip to content

Commit

Permalink
feat(web-share-api): allow newly shared entries to be copied or sent …
Browse files Browse the repository at this point in the history
…to other app via Web Share API additionally handle redirect for unsupported browsers in JSx

update(entry dataset attribute): change from entry to status to check whether entry is shared or not
  • Loading branch information
jeankhawand committed Jul 30, 2023
1 parent 0261c0b commit 798b2a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
3 changes: 2 additions & 1 deletion template/templates/views/entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h1 dir="auto">
<li>
<a href="{{ route "sharedEntry" "shareCode" .entry.ShareCode }}"
title="{{ t "entry.shared_entry.title" }}"
data-share-entry="share"
data-share-status="shared"
target="_blank">{{ icon "share" }}<span class="icon-label">{{ t "entry.shared_entry.label" }}</span></a>
</li>
<li>
Expand All @@ -65,6 +65,7 @@ <h1 dir="auto">
<li>
<a href="{{ route "shareEntry" "entryID" .entry.ID }}"
title="{{ t "entry.share.title" }}"
data-share-status="share"
target="_blank">{{ icon "share" }}<span class="icon-label">{{ t "entry.share.label" }}</span></a>
</li>
{{ end }}
Expand Down
53 changes: 35 additions & 18 deletions ui/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,24 +640,41 @@ function handlePlayerProgressionSave(playerElement) {
}

/**
* handle share using browser native Web Share API
* handle new share entires and already shared entries
*/

function handleShare() {
let link = document.querySelector('a[data-share-entry]');
let link = document.querySelector('a[data-share-status]');
let title = document.querySelector("body > main > section > header > h1 > a");
if (!navigator.canShare) {
console.error("Your browser doesn't support the Web Share API.");
window.location = link.href;
return;
}
try {
const shareData = {
title: title,
url: link.href,
};
navigator.share(shareData);
} catch (err) {
console.error(err);
}
}
if(link.dataset.shareStatus === "shared"){
checkShareAPI(title, link.href);
}
if(link.dataset.shareStatus === "share"){
let request = new RequestBuilder(link.href);
request.withCallback((r) => {
checkShareAPI(title, r.url);
});
request.withHttpMethod("GET");
request.execute();
}
}

/**
* wrapper for Web Share API
*/
function checkShareAPI(title, url){
if (!navigator.canShare) {
console.error("Your browser doesn't support the Web Share API.");
window.location = url;
return;
}
try {
navigator.share({
title: title,
url: url
});
window.location.reload();
} catch (err) {
console.error(err);
window.location.reload();
}
}
2 changes: 1 addition & 1 deletion ui/static/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ document.addEventListener("DOMContentLoaded", function () {
onClick("a[data-toggle-bookmark]", (event) => handleBookmark(event.target));
onClick("a[data-fetch-content-entry]", () => handleFetchOriginalContent());
onClick("a[data-action=search]", (event) => setFocusToSearchInput(event));
onClick("a[data-share-entry]", () => handleShare());
onClick("a[data-share-status]", () => handleShare());
onClick("a[data-action=markPageAsRead]", (event) => handleConfirmationMessage(event.target, () => markPageAsRead()));
onClick("a[data-toggle-status]", (event) => handleEntryStatus("next", event.target));

Expand Down

0 comments on commit 798b2a8

Please sign in to comment.