Skip to content

Commit 5b424bb

Browse files
author
Jesse Haigh
committed
code copying functionality on code listings
1 parent deca402 commit 5b424bb

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/components/ContentNode/CodeListing.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
>{{ fileName }}
2323
</Filename>
2424
<div class="container-general">
25+
<button class="copy-button" @click="copyToClipboard">Copy</button>
2526
<!-- Do not add newlines in <pre>, as they'll appear in the rendered HTML. -->
2627
<pre><CodeBlock><template
2728
v-for="(line, index) in syntaxHighlightedLines"
@@ -122,6 +123,13 @@ export default {
122123
line === '' ? '\n' : line
123124
));
124125
},
126+
copyToClipboard() {
127+
const lines = this.content;
128+
const text = lines.join('\n');
129+
navigator.clipboard.writeText(text)
130+
.then(() => console.log('Copied!'))
131+
.catch(err => console.error('Failed to copy: ', err));
132+
},
125133
},
126134
};
127135
</script>

0 commit comments

Comments
 (0)