Skip to content

Commit

Permalink
bouton copier (refs #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
slafayIGN committed Apr 30, 2024
1 parent 6466d57 commit b16bbc2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions _includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<script src="/js/mermaid.initialize.js"></script>
{% endif %}

<script src="/js/copy.js" defer></script>
<title>{{ title or metadata.title }}</title>
</head>
<body id="top">
Expand Down
2 changes: 0 additions & 2 deletions content/fr/tutoriels/alimentation-diffusion-simple/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ POST /datastores/{datastore}/uploads

??? Requête et réponse

Requête : `POST /datastores/{datastore}/uploads`

**Corps de requête JSON**

```json
Expand Down
11 changes: 11 additions & 0 deletions public/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,14 @@ div.fr-callout__text p {
.text-center {
text-align: center;
}

/* Copy button on every code blocks */
pre[class*="language"] {
position: relative;
}

.code-copy-btn {
position: absolute;
right: .5em;
top: .5em;
}
21 changes: 21 additions & 0 deletions public/js/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Ajoute un bouton pour copier les exemples de code
*/
(() => {
const code = document.querySelectorAll('pre[class*="language"]');

const buttonClasses = ['fr-btn', 'code-copy-btn'];

code.forEach((pre) => {
const button = document.createElement('button');
button.title = 'Copier';
button.classList.add(...buttonClasses);
button.innerHTML = 'Copier';
button.addEventListener('click', () => {
navigator.clipboard.writeText(pre.firstElementChild.textContent);
button.innerHTML = 'Copié !';
setTimeout(() => {button.innerHTML = 'Copier';}, 1500);
});
pre.appendChild(button);
});
})();

0 comments on commit b16bbc2

Please sign in to comment.