Skip to content

Commit 3a66ab4

Browse files
authored
✨ Add copy clipboard (#126)
1 parent 42348ec commit 3a66ab4

File tree

7 files changed

+75
-8
lines changed

7 files changed

+75
-8
lines changed

public/css/app.css

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mix-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"/js/app.js": "/js/app.js?id=857207f48168eb5cc9d90ca0a9e71d34",
3-
"/css/app.css": "/css/app.css?id=eb36bd34f363220666642203eb9c55cb"
2+
"/js/app.js": "/js/app.js?id=771d5d47a2ec2c3ddfd6ca0852da949e",
3+
"/css/app.css": "/css/app.css?id=3542b9735c706c26665a33113b79c212"
44
}

resources/css/torchlight.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ pre {
1414
colors extend edge to edge.
1515
*/
1616
pre code.torchlight {
17-
@apply block py-4 min-w-max;
17+
@apply block py-6 min-w-max;
1818
}
1919

2020
/*
2121
Horizontal line padding.
2222
*/
2323
pre code.torchlight .line {
24-
@apply px-4;
24+
@apply px-5;
2525
}
2626

2727
/*
@@ -85,3 +85,7 @@ pre code.torchlight .summary-caret {
8585
.torchlight details:not([open]) .summary-hide-when-open {
8686
display: initial;
8787
}
88+
89+
.code-block .copyBtn {
90+
@apply absolute right-2 top-2 outline-none;
91+
}

resources/js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '@helpers/scrollspy'
1111
import './elements'
1212
import './utils/editor'
1313
import './utils/filepond'
14+
import './utils/clipboard'
1415

1516
registerHeader()
1617

resources/js/utils/clipboard.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copy to Clipboard.
2+
let codeBlocks = document.querySelectorAll('#content pre')
3+
4+
codeBlocks.forEach((element, key) => {
5+
let wrapper = document.createElement('div')
6+
wrapper.classList.add('relative', 'code-block')
7+
8+
element.parentNode.insertBefore(wrapper, element)
9+
wrapper.appendChild(element)
10+
11+
let codeElement = element.querySelector('code')
12+
codeElement.id = `clipText-${key}`
13+
14+
// Copy to clipboard button.
15+
const copyToClipboardContainer = document.createElement('div')
16+
17+
copyToClipboardContainer.innerHTML = `
18+
<div x-data="{
19+
copyNotification: false,
20+
copyToClipboard() {
21+
this.copyNotification = true
22+
const clipboardItem = new ClipboardItem({
23+
'text/plain': new Blob([\`${codeElement.innerText}\`], { type: 'text/plain' })
24+
})
25+
navigator.clipboard.write([clipboardItem]).then(() => {
26+
console.log('Copied to clipboard')
27+
})
28+
let that = this
29+
setTimeout(function() {
30+
that.copyNotification = false
31+
}, 3000)
32+
}
33+
}" class="relative z-20 flex items-center">
34+
<div x-show="copyNotification" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 translate-x-2" x-transition:enter-end="opacity-100 translate-x-0" x-transition:leave="transition ease-in duration-300" x-transition:leave-start="opacity-100 translate-x-0" x-transition:leave-end="opacity-0 translate-x-2" class="absolute left-0" x-cloak>
35+
<div class="px-3 h-7 -ml-1.5 items-center flex text-xs bg-primary-500 border-r border-primary-500 -translate-x-full text-white rounded">
36+
<span>Copié!</span>
37+
<div class="absolute right-0 inline-block h-full -mt-px overflow-hidden translate-x-3 -translate-y-2 top-1/2">
38+
<div class="w-3 h-3 origin-top-left transform rotate-45 bg-primary-500 border border-transparent"></div>
39+
</div>
40+
</div>
41+
</div>
42+
<button @click="copyToClipboard();" class="flex items-center justify-center h-8 text-xs bg-gray-700 rounded-md cursor-pointer w-9 hover:bg-gray-900/50 active:bg-gray-600 focus:bg-gray-700 focus:outline-none text-slate-300 hover:text-white group">
43+
<svg x-show="copyNotification" class="w-4 h-4 text-primary-500 stroke-current" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" x-cloak>
44+
<path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" />
45+
</svg>
46+
<svg x-show="!copyNotification" class="w-4 h-4 stroke-current" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
47+
<g fill="none" stroke="none">
48+
<path d="M7.75 7.757V6.75a3 3 0 0 1 3-3h6.5a3 3 0 0 1 3 3v6.5a3 3 0 0 1-3 3h-.992" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
49+
<path d="M3.75 10.75a3 3 0 0 1 3-3h6.5a3 3 0 0 1 3 3v6.5a3 3 0 0 1-3 3h-6.5a3 3 0 0 1-3-3v-6.5z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
50+
</g>
51+
</svg>
52+
</button>
53+
</div>
54+
`
55+
56+
copyToClipboardContainer.setAttribute('aria-label', 'Copy to Clipboard')
57+
copyToClipboardContainer.setAttribute('title', 'Copy to Clipboard')
58+
copyToClipboardContainer.classList.add('copyBtn');
59+
60+
wrapper.append(copyToClipboardContainer)
61+
})

resources/views/articles/show.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
<x-markdown-content
144144
id="content"
145145
class="mt-8 prose prose-lg prose-green text-skin-base mx-auto overflow-x-hidden md:prose-xl lg:max-w-none"
146-
:content="$article->body" />
146+
:content="$article->body"
147+
/>
147148

148149
<div class="mt-6 pt-5 border-t border-skin-base sm:hidden">
149150
<div class="space-y-4">

0 commit comments

Comments
 (0)