Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add copy button for markdown code blocks #2799

Merged
merged 26 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b3ec6a2
feat: add copy button
chayandatta Feb 1, 2024
afd0ee8
removed consoles
chayandatta Feb 2, 2024
be1d90f
chore: move css files
chayandatta Feb 2, 2024
4820697
chore: added EOL
chayandatta Feb 2, 2024
eed6f67
Merge branch 'main' into cd/copy-button
chayandatta Feb 2, 2024
b33b4e3
chore: change variable to const
chayandatta Feb 8, 2024
33e9251
Merge branch 'main' into cd/copy-button
chayandatta Feb 8, 2024
6d63ed9
chore: modularize the generateCopyCodeButton
chayandatta Feb 8, 2024
0f217bc
Merge branch 'cd/copy-button' of github.com:chayandatta/Pluto.jl into…
chayandatta Feb 8, 2024
15ca905
chore: move generateCopyCodeButton inside RawHTMLContainer
chayandatta Feb 8, 2024
492c8f8
chore: lint
chayandatta Feb 8, 2024
d39a198
chore: changes
chayandatta Feb 8, 2024
e210a37
Merge branch 'main' into cd/copy-button
chayandatta Feb 8, 2024
f9dc130
chore: refactor generateCopyCodeButton
chayandatta Feb 9, 2024
4d09d33
chore: changes
chayandatta Feb 9, 2024
b651aa5
chore: changes
chayandatta Feb 9, 2024
5e6225b
Merge branch 'main' into cd/copy-button
chayandatta Feb 20, 2024
4d89b83
Merge branch 'main' into cd/copy-button
chayandatta Feb 29, 2024
fdd1342
Merge branch 'main' into cd/copy-button
chayandatta Mar 13, 2024
7f58e89
few fixes
chayandatta Mar 13, 2024
6bf4dc5
more changes
chayandatta Mar 13, 2024
c65336e
styling changes
chayandatta Mar 13, 2024
dc80390
run vs code formatter
fonsp Apr 11, 2024
62411a9
remove unused styles
fonsp Apr 11, 2024
69fbeb8
prepend instead of append
fonsp Apr 11, 2024
c9c4f61
make code a bit more compact
fonsp Apr 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions frontend/components/CellOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,18 @@ export let RawHTMLContainer = ({ body, className = "", persist_js_state = false,
} catch (err) {
console.warn("Highlighting failed", err)
}

// Find code blocks and add a copy button:
try {
if (container.firstElementChild?.matches("div.markdown")) {
container.querySelectorAll("pre > code").forEach((code_element) => {
const pre = code_element.parentElement
generateCopyCodeButton(pre)
})
}
} catch (err) {
console.warn("Adding markdown code copy button failed", err)
}
} finally {
js_init_set?.delete(container)
}
Expand Down Expand Up @@ -683,3 +695,44 @@ export let highlight = (code_element, language) => {
}
}
}

/**
* Generates a copy button for Markdown code blocks and copies them into the clipboard.
* @param {HTMLElement | null} pre - The <pre> element containing the code block.
*/
export const generateCopyCodeButton = (pre) => {
if (!pre) {
console.error('Error: pre is null.');
return;
}

const copyToClipboard = () => {
const range = document.createRange()
range.selectNode(pre)
const txt = range.startContainer.textContent || ''
navigator.clipboard
.writeText(txt)
.then(() => {
console.log("Code copied to clipboard:\n" + txt)
})
.catch((error) => {
console.error("Error copying code to clipboard:", error)
})
}
chayandatta marked this conversation as resolved.
Show resolved Hide resolved

// create copy button
const copyCodeButton = document.createElement("button")
copyCodeButton.className = "markdown-code-block-copy-code-button"
copyCodeButton.addEventListener("click", copyToClipboard)

// Create copy button image
const copyImg = document.createElement("img")
copyImg.src = "https://unpkg.com/[email protected]/dist/svg/copy-outline.svg"
chayandatta marked this conversation as resolved.
Show resolved Hide resolved
copyImg.alt = "Copy to Clipboard"

// Append image to button
copyCodeButton.appendChild(copyImg)

// Append copy button to the code block element
pre.appendChild(copyCodeButton)
}
19 changes: 19 additions & 0 deletions frontend/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -3610,3 +3610,22 @@ pluto-cell.hooked_up pluto-output {
justify-content: flex-end;
gap: 0.5em;
}

/* Styles for mark down copy Button*/
.markdown-code-block-copy-code-button {
position: absolute;
top: 10px;
right: 20px;
cursor: pointer;
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
padding: 0;
}

.markdown-code-block-copy-code-button > img {
width: 14px;
height: 14px;
}
Loading