Skip to content

Commit

Permalink
🐛 (functions): Fix copyHyperLinkToClipboard to handle Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
vict0rsch committed Nov 28, 2023
1 parent eae864e commit 4be9205
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
11 changes: 5 additions & 6 deletions src/popup/js/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ const handleCopyMarkdownLink = async (e) => {
const text = prefs.checkPreferPdf ? "PDF" : "Abstract";
const paper = global.state.papers[id];
const md = makeMdLink(paper, prefs);
copyAndConfirmMemoryItem(id, md, `Markdown ${text} link copied!`);
await copyAndConfirmMemoryItem(id, md, `Markdown ${text} link copied!`);
};

const handleCopyBibtex = (e) => {
const handleCopyBibtex = async (e) => {
const id = eventId(e);
const bibtex = global.state.papers[id].bibtex;
let bibobj = bibtexToObject(bibtex);
Expand All @@ -80,7 +80,7 @@ const handleCopyBibtex = (e) => {
if (!bibobj.hasOwnProperty("pdf") && global.state.papers[id].source !== "website") {
bibobj.pdf = paperToPDF(global.state.papers[id]);
}
copyAndConfirmMemoryItem(id, bibtexToString(bibobj), "Bibtex copied!");
await copyAndConfirmMemoryItem(id, bibtexToString(bibobj), "Bibtex copied!");
};

const handleCopyPDFLink = async (e) => {
Expand All @@ -89,16 +89,15 @@ const handleCopyPDFLink = async (e) => {
const paper = global.state.papers[id];
const link = prefs.checkPreferPdf ? paperToPDF(paper) : paperToAbs(paper);
const text = prefs.checkPreferPdf ? "PDF" : "Abstract";
copyAndConfirmMemoryItem(id, link, `${text} link copied!`);
await copyAndConfirmMemoryItem(id, link, `${text} link copied!`);
};

const handleCopyHyperLink = async (e) => {
const id = eventId(e);
const prefs = global.state.prefs;
const paper = global.state.papers[id];
const link = prefs.checkPreferPdf ? paperToPDF(paper) : paperToAbs(paper);
const text = prefs.checkPreferPdf ? "PDF" : "Abstract";
copyAndConfirmMemoryItem(id, link, `Hyperlink copied!`, false, paper.title);
await copyAndConfirmMemoryItem(id, link, `Hyperlink copied!`, false, paper.title);
};

const handleAddItemToFavorites = (e) => {
Expand Down
5 changes: 2 additions & 3 deletions src/popup/js/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,17 @@ const showConfirmDeleteModal = (id) => {
* @param {string} feedbackText Text to display as feedback
* @param {boolean} isPopup If the action took place in the main popup or in the memory
*/
const copyAndConfirmMemoryItem = (
const copyAndConfirmMemoryItem = async (
id,
textToCopy,
feedbackText,
isPopup,
hyperLinkTitle
) => {
console.log("hyperLinkTitle: ", hyperLinkTitle);
if (!hyperLinkTitle) {
copyTextToClipboard(textToCopy);
} else {
copyHyperLinkToClipboard(textToCopy, hyperLinkTitle);
await copyHyperLinkToClipboard(textToCopy, hyperLinkTitle);
}
const element = isPopup
? findEl(`popup-feedback-copied`)
Expand Down
21 changes: 13 additions & 8 deletions src/popup/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,28 +406,33 @@ const popupMain = async (url, is, manualTrigger = false, tab = null) => {
// global.close && global.close();
}
});
addListener(`popup-memory-item-copy-link--${id}`, "click", () => {
addListener(`popup-memory-item-copy-link--${id}`, "click", async () => {
const link = prefs.checkPreferPdf ? paperToPDF(paper) : paperToAbs(paper);
const text = prefs.checkPreferPdf ? "PDF" : "Abstract";
copyAndConfirmMemoryItem(id, link, `${text} link copied!`, true);
await copyAndConfirmMemoryItem(id, link, `${text} link copied!`, true);
});
addListener(`popup-memory-item-copy-hyperlink--${id}`, "click", () => {
addListener(`popup-memory-item-copy-hyperlink--${id}`, "click", async () => {
const link = prefs.checkPreferPdf ? paperToPDF(paper) : paperToAbs(paper);
const text = prefs.checkPreferPdf ? "PDF" : "Abstract";
copyAndConfirmMemoryItem(
await copyAndConfirmMemoryItem(
id,
link,
`${text} hyperlink copied!`,
true,
paper.title
);
});
addListener(`popup-memory-item-md--${id}`, "click", () => {
addListener(`popup-memory-item-md--${id}`, "click", async () => {
const md = makeMdLink(paper, prefs);
const text = prefs.checkPreferPdf ? "PDF" : "Abstract";
copyAndConfirmMemoryItem(id, md, `Markdown link to ${text} copied!`, true);
await copyAndConfirmMemoryItem(
id,
md,
`Markdown link to ${text} copied!`,
true
);
});
addListener(`popup-memory-item-bibtex--${id}`, "click", () => {
addListener(`popup-memory-item-bibtex--${id}`, "click", async () => {
let bibtex = global.state.papers[id].bibtex;
let bibobj = bibtexToObject(bibtex);
if (!bibobj.hasOwnProperty("url")) {
Expand All @@ -437,7 +442,7 @@ const popupMain = async (url, is, manualTrigger = false, tab = null) => {
bibobj.pdf = paperToPDF(global.state.papers[id]);
}
bibtex = bibtexToString(bibobj);
copyAndConfirmMemoryItem(id, bibtex, "Bibtex citation copied!", true);
await copyAndConfirmMemoryItem(id, bibtex, "Bibtex citation copied!", true);
});
addListener(`popup-memory-item-openLocal--${id}`, "click", async () => {
const file = (await findLocalFile(paper)) || global.state.files[paper.id];
Expand Down
6 changes: 3 additions & 3 deletions src/popup/min/popup.min.js

Large diffs are not rendered by default.

43 changes: 28 additions & 15 deletions src/shared/js/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,29 +269,42 @@ const copyTextToClipboard = (text) => {
);
};

/** Paste richly formatted text.
*
* @param {string} rich - the text formatted as HTML
* @param {string} plain - a plain text fallback
*/
async function pasteRich(rich, plain) {
if (typeof ClipboardItem !== "undefined") {
// Shiny new Clipboard API, not fully supported in Firefox.
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API#browser_compatibility
const html = new Blob([rich], { type: "text/html" });
const text = new Blob([plain], { type: "text/plain" });
const data = new ClipboardItem({ "text/html": html, "text/plain": text });
await navigator.clipboard.write([data]);
} else {
// Fallback using the deprecated `document.execCommand`.
// https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#browser_compatibility
const cb = (e) => {
e.clipboardData.setData("text/html", rich);
e.clipboardData.setData("text/plain", plain);
e.preventDefault();
};
document.addEventListener("copy", cb);
document.execCommand("copy");
document.removeEventListener("copy", cb);
}
}

/** Copy a hyperlink to the clipboard using the Clipboard API,
* if available, or fallback to the fallbackCopyTextToClipboard function
* @param {string} url The url to copy to the clipboard
* @param {string} title The title of the url to copy to the clipboard
* @returns {void}
* */
copyHyperLinkToClipboard = (url, title) => {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(url);
return;
}
const linkHtml = `<a href="${url}">${title}</a>`;
const type = "text/html";
const blob = new Blob([linkHtml], { type });
// Copy the blob to the clipboard
navigator.clipboard
.write([new ClipboardItem({ [type]: blob })])

// Optional but recommended. Check the result of the async operation
.then(
() => log("Async: Copying to clipboard was successful!"),
(err) => console.error("Async: Could not copy text: ", err)
);
pasteRich(linkHtml, `${title} ${url}`);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/min/utils.min.js

Large diffs are not rendered by default.

0 comments on commit 4be9205

Please sign in to comment.