-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JS-hook for WebShare-API/clipboard-copy
QQ: seems like I can only use buttons to do click events, how might I go about emitting click events from icons?
- Loading branch information
Showing
4 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Externalised hooks object to be used by app.js. | ||
*/ | ||
import { getSharer } from "./web_share.js"; | ||
|
||
let Hooks = {}; | ||
|
||
Hooks.ShareQuoteButton = { | ||
mounted() { | ||
let callback = () => console.log("share quote!"); | ||
if ("share" in navigator) { // uses webshare api: | ||
callback = () => { | ||
const shareTitle = this.el.getAttribute("data-share-title"); | ||
const shareUrl = window.location.href; | ||
const sharer = getSharer("url", shareTitle, shareUrl); | ||
if (!sharer) { | ||
return; | ||
} | ||
|
||
window.shareUrl = sharer; | ||
window.shareUrl(shareUrl); | ||
}; | ||
} else if ("clipboard" in navigator) { | ||
callback = () => { | ||
const verse = JSON.parse(this.el.getAttribute("data-verse")); | ||
navigator.clipboard.writeText(verse.text); | ||
}; | ||
} | ||
|
||
this.el.addEventListener("click", callback); | ||
}, | ||
}; | ||
|
||
export default Hooks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* This file is for helpers to interface with the WebShare API. | ||
* | ||
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/Web_Share_API | ||
*/ | ||
|
||
const sharers = { | ||
"file": shareFile, // to assign to window.shareContent | ||
"url": shareUrl, // to assign to window.shareUrl | ||
}; | ||
|
||
/** | ||
* Returns a function that can be used to set various webshare api attributes. | ||
*/ | ||
export function getSharer(sharingType, ...args) { | ||
if (!checkBrowserSupport()) { | ||
console.warning(`This browser doesn't support shares like that, please copy the url instead.`); | ||
return null; | ||
} | ||
if (!(sharingType in sharers)) { | ||
console.warning(`$Sharing Type of {sharingType} is not supported yet.`); | ||
return null; | ||
} | ||
|
||
return () => sharers[sharingType](...args); | ||
} | ||
|
||
function shareFile(title, url) { | ||
if (navigator.share) { | ||
navigator.share({ | ||
title: title, | ||
url: url, | ||
}) | ||
.then(() => console.log("Successful share")) | ||
.catch((error) => console.log("Error sharing", error)); | ||
} else { | ||
console.info(`Your system doesn't support sharing files.`); | ||
} | ||
} | ||
|
||
function shareUrl(url) { | ||
if (navigator.share) { | ||
navigator.share({ | ||
url: url, | ||
}) | ||
.then(() => console.log("Successful share")) | ||
.catch((error) => console.log("Error sharing", error)); | ||
} else { | ||
console.info(`Your system doesn't support sharing urls.`); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the browser supports the navigator.share function, false otherwise. | ||
* */ | ||
function checkBrowserSupport() { | ||
// TODO: implement this | ||
return true | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters