From b16bbc28274a2c40ba8a710f45272a463492c890 Mon Sep 17 00:00:00 2001 From: Sylvain Lafay Date: Tue, 30 Apr 2024 08:51:16 +0200 Subject: [PATCH] bouton copier (refs #27) --- _includes/layouts/base.njk | 1 + .../alimentation-diffusion-simple/index.md | 2 -- public/css/index.css | 11 ++++++++++ public/js/copy.js | 21 +++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 public/js/copy.js diff --git a/_includes/layouts/base.njk b/_includes/layouts/base.njk index 2fbd1cb..a296eeb 100644 --- a/_includes/layouts/base.njk +++ b/_includes/layouts/base.njk @@ -32,6 +32,7 @@ {% endif %} + {{ title or metadata.title }} diff --git a/content/fr/tutoriels/alimentation-diffusion-simple/index.md b/content/fr/tutoriels/alimentation-diffusion-simple/index.md index f497395..58349d8 100644 --- a/content/fr/tutoriels/alimentation-diffusion-simple/index.md +++ b/content/fr/tutoriels/alimentation-diffusion-simple/index.md @@ -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 diff --git a/public/css/index.css b/public/css/index.css index 78761e1..464569a 100644 --- a/public/css/index.css +++ b/public/css/index.css @@ -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; +} diff --git a/public/js/copy.js b/public/js/copy.js new file mode 100644 index 0000000..1dd80b5 --- /dev/null +++ b/public/js/copy.js @@ -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); + }); + })(); \ No newline at end of file