Skip to content

Commit

Permalink
button is a child of the anchor element and only appears on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
tejonaco committed May 29, 2024
1 parent 53ee429 commit 4684581
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/copyButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { clipboardIcon, checkIcon } from ".";

export const setCopyButtons = (links: HTMLAnchorElement[]) => {
links.forEach((link) => {
if (link.nextElementSibling?.classList.contains(`${PL.id}-button`)) return;
if (link.querySelector(`.${PL.id}-button`) !== null) return

const href = link.getAttribute("href");

Expand All @@ -18,7 +18,8 @@ export const setCopyButtons = (links: HTMLAnchorElement[]) => {
// add icon
button.innerHTML = clipboardIcon;

button.addEventListener("click", async () => {
button.addEventListener("click", async e => {
e.preventDefault()
try {
await parent.navigator.clipboard.writeText(href);
button.innerHTML = checkIcon;
Expand All @@ -29,7 +30,7 @@ export const setCopyButtons = (links: HTMLAnchorElement[]) => {
});

// add button
link.insertAdjacentElement("afterend", button);
link.appendChild(button);
});
};

Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logseq as PL } from "../package.json";
import { getExternalLinks } from "./utils";
import { initLinksObserver, runLinksObserver } from "./observer";
import { removeCopyButtons, setCopyButtons } from "./copyButtons";
import { globals } from './global'

export const clipboardIcon = `
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
Expand All @@ -24,7 +25,12 @@ const main = () => {
vertical-align: middle;
cursor: pointer;
display: inline-flex;
margin-left: 0.25rem;
margin-left: 0.3rem;
width: 1.3em;
margin-bottom: 0.15rem;
}
${globals.extLinksSelector}:not(:hover) > .logseq-copy-url-button {
display: none;
}
`,
});
Expand Down

0 comments on commit 4684581

Please sign in to comment.