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

Update copy plugin #2580

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion packages/core-web/src/styles/markbind.css
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,12 @@ button.function-btn {
color: darkgray;
cursor: pointer;
display: none;
height: 30px;
margin: 0.17rem;
padding: 0.35rem;
position: absolute;
right: 0;
width: 30px;
text-align: center;
white-space: nowrap;
}
Expand Down Expand Up @@ -404,4 +406,4 @@ h6 {
left: 0;
width: 100%;
height: 100%;
}
}
17 changes: 17 additions & 0 deletions packages/core/src/plugins/codeBlockCopyButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ const COPY_ICON = `
</svg>
`;

const TICK_ICON = `
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
viewBox="2 2 22 22"
>
<path d="M 19.28125 5.28125 L 9 15.5625 L 4.71875 11.28125 L 3.28125 12.71875 L 8.28125 17.71875
L 9 18.40625 L 9.71875 17.71875 L 20.71875 6.71875 Z"></path>
</svg>
`;

function getButtonHTML() {
const html = `<button onclick="copyCodeBlock(this)" class="function-btn d-print-none">
<div class="function-btn-body">
Expand All @@ -35,6 +47,7 @@ const copyCodeBlockScript = `<script>
function copyCodeBlock(element) {
const pre = element.parentElement.parentElement;
const codeElement = pre.querySelector('code');
const copyButton = element;

// create dummy text element to select() the text field
const textElement = document.createElement('textarea');
Expand All @@ -44,6 +57,10 @@ const copyCodeBlockScript = `<script>

document.execCommand('copy');
document.body.removeChild(textElement);
copyButton.innerHTML = \`${TICK_ICON}\`;
setTimeout(() => {
copyButton.innerHTML = \`${COPY_ICON}\`;
}, 3000);
}
</script>`;

Expand Down
Loading