Skip to content

Commit 95fbbdf

Browse files
committed
Feat: Copy to Clipboard Buttons to Code Blocks
1 parent 3c6c26e commit 95fbbdf

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

src/App.js

+57-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class App extends Component {
5858
this.handleCopyNote = this.handleCopyNote.bind(this);
5959
this.handleCopyEvent = this.handleCopyEvent.bind(this);
6060
this.handleSortNotes = this.handleSortNotes.bind(this);
61-
this.updateCodeSyntaxHighlighting();
61+
this.updateCodeSyntaxHighlighting;
62+
this.addCopyButtons;
63+
this.handleCopyCodeButtonClick;
6264
}
6365

6466
async componentDidMount() {
@@ -71,11 +73,13 @@ class App extends Component {
7173
});
7274
document.getElementById(getnotes[0].noteid).click();
7375
}
74-
// this.updateCodeSyntaxHighlighting();
76+
this.updateCodeSyntaxHighlighting();
77+
this.handleCopyCodeButtonClick();
7578
}
7679

7780
componentDidUpdate() {
78-
// this.updateCodeSyntaxHighlighting();
81+
this.updateCodeSyntaxHighlighting();
82+
this.handleCopyCodeButtonClick();
7983
}
8084

8185
updateCodeSyntaxHighlighting = () => {
@@ -84,6 +88,56 @@ class App extends Component {
8488
});
8589
};
8690

91+
handleCopyCodeButtonClick = () => {
92+
if (navigator && navigator.clipboard) {
93+
this.addCopyButtons(navigator.clipboard);
94+
} else {
95+
var script = document.createElement("script");
96+
script.src =
97+
"https://cdnjs.cloudflare.com/ajax/libs/clipboard-polyfill/2.7.0/clipboard-polyfill.promise.js";
98+
script.integrity = "sha256-waClS2re9NUbXRsryKoof+F9qc1gjjIhc2eT7ZbIv94=";
99+
script.crossOrigin = "anonymous";
100+
script.onload = function () {
101+
this.addCopyButtons(clipboard);
102+
};
103+
document.body.appendChild(script);
104+
}
105+
};
106+
107+
addCopyButtons = (clipboard) => {
108+
document.querySelectorAll("pre code").forEach(function (codeBlock) {
109+
var button = document.createElement("button");
110+
button.className = "copy-code-button";
111+
button.type = "button";
112+
button.innerText = "Copy";
113+
114+
button.addEventListener("click", function () {
115+
clipboard.writeText(codeBlock.innerText).then(
116+
function () {
117+
/* Chrome doesn't seem to blur automatically,
118+
leaving the button in a focused state. */
119+
button.blur();
120+
button.innerText = "Copied!";
121+
setTimeout(function () {
122+
button.innerText = "Copy";
123+
}, 2000);
124+
},
125+
function (error) {
126+
button.innerText = "Error";
127+
}
128+
);
129+
});
130+
131+
var pre = codeBlock.parentNode;
132+
if (pre.parentNode.classList.contains("highlight")) {
133+
var highlight = pre.parentNode;
134+
highlight.parentNode.insertBefore(button, highlight);
135+
} else {
136+
pre.parentNode.insertBefore(button, pre);
137+
}
138+
});
139+
};
140+
87141
// Indexed DB class
88142
async handleIndexedDB(cmd = "", note = "") {
89143
const db = await openDB("notesdb", 1, {

src/styles.css

+41
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,44 @@ button[disabled]:hover {
459459
.highlight {
460460
background-color: yellow;
461461
}
462+
463+
/* Copy Code block button */
464+
465+
.copy-code-button {
466+
color: #272822;
467+
background-color: #fff;
468+
border-color: #272822;
469+
border: 1px solid rgba(27, 31, 35, 0.2);
470+
border-radius: 3px 3px 0px 0px;
471+
472+
/* right-align */
473+
display: block;
474+
margin-left: auto;
475+
margin-right: 0;
476+
477+
margin-bottom: -2px;
478+
padding: 3px 8px;
479+
font-size: 0.8em;
480+
}
481+
482+
.copy-code-button:hover {
483+
cursor: pointer;
484+
background-color: #f2f2f2;
485+
}
486+
487+
.copy-code-button:focus {
488+
/* Avoid an ugly focus outline on click in Chrome,
489+
but darken the button for accessibility.
490+
See https://stackoverflow.com/a/25298082/1481479 */
491+
background-color: #e6e6e6;
492+
outline: 0;
493+
}
494+
495+
.copy-code-button:active {
496+
background-color: #d9d9d9;
497+
}
498+
499+
.highlight pre {
500+
/* Avoid pushing up the copy buttons. */
501+
margin: 0;
502+
}

0 commit comments

Comments
 (0)