📚 The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
fix: code block copy behavior & improved design. Closes #642
Smaug67394624adefe95c
diff --git a/frontend/app/components/Node/Document/ContentCompiled.vue b/frontend/app/components/Node/Document/ContentCompiled.vueindex 2d747685..09a339ab 100644--- a/frontend/app/components/Node/Document/ContentCompiled.vue+++ b/frontend/app/components/Node/Document/ContentCompiled.vue@@ -5,7 +5,7 @@:class="['markdown-preview', `${theme}-theme`, `document-content`, { numbering: documentTitlesNumbering }]":style="{ fontSize: documentFontSize + 'px', fontFamily: documentFontFamily, lineHeight: documentLineHeight }"@change="handleCheckboxChange"- @click="handleInternalLinkClick"+ @click="handleClick"v-html="node?.content_compiled"/></template>@@ -54,6 +54,36 @@ const updateMarkdownCheckbox = (markdown: string, indexToTarget: number, shouldC});};+// Handle the "Copy code" button click event+const handleCodeCopyClick = async (e: MouseEvent) => {+ const btn = (e.target as HTMLElement)?.closest('.code-copy-btn') as HTMLButtonElement | null;+ if (!btn || btn.classList.contains('copied')) return;++ const wrapper = btn.closest('.code-block-wrapper');+ const codeEl = wrapper?.querySelector('pre code');+ if (!codeEl) return;++ const codeToCopy = codeEl.textContent || '';++ let success = false;++ if (navigator.clipboard && window.isSecureContext) {+ try {+ await navigator.clipboard.writeText(codeToCopy);+ success = true;+ } catch (err) {+ console.warn('Cannot copy text to clipboard', err);+ }+ }++ if (success) {+ btn.classList.add('copied');+ setTimeout(() => {+ btn.classList.remove('copied');+ }, 1500);+ }+};+// Navigate internal document links (#<id>) through the router, without a full page reloadconst handleInternalLinkClick = (event: MouseEvent) => {if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;@@ -65,6 +95,11 @@ const handleInternalLinkClick = (event: MouseEvent) => {router.push(`/dashboard/docs/${id}`);};+const handleClick = (event: MouseEvent) => {+ handleCodeCopyClick(event);+ handleInternalLinkClick(event);+};+const handleCheckboxChange = (event: Event) => {const target = event.target as HTMLInputElement;if (!target || target.type !== 'checkbox' || !target.hasAttribute('data-checkbox-index')) return;diff --git a/frontend/app/helpers/markdown/code-block.ts b/frontend/app/helpers/markdown/code-block.tsindex b231e019..54b57833 100644--- a/frontend/app/helpers/markdown/code-block.ts+++ b/frontend/app/helpers/markdown/code-block.ts@@ -1,47 +1,25 @@import type { MarkdownIt } from 'markdown-it';/**- * Markdown-it plugin to add a "copy to clipboard" button to code blocks.- * @param md - The MarkdownIt instance+ * This plugin adds a copy button to code blocks in the rendered markdown.*/++const ICON_COPY = `<svg class="copy-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>`;+const ICON_CHECK = `<svg class="check-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg>`;+export const copyCodePlugin = (md: MarkdownIt) => {- // Sauvegarde du renderer par défaut des blocs <pre><code>const defaultFence = md.renderer.rules.fence;md.renderer.rules.fence = function (tokens, idx, options, env, self) {- const token = tokens[idx];- const rawCode = token?.content || '';-- // HTML original rendu par markdown-itconst original = defaultFence ? defaultFence(tokens, idx, options, env, self) : self.renderToken(tokens, idx, options);- // On enveloppe dans un container pour pouvoir placer le boutonreturn `<div class="code-block-wrapper">- <button class="code-copy-btn" data-code="${md.utils.escapeHtml(rawCode)}" aria-label="Copy code">- ${COPY_ICON}+ <button type="button" class="code-copy-btn" aria-label="Copy code" title="Copy code">+ <span class="btn-icon">${ICON_COPY}</span>+ <span class="btn-icon-success">${ICON_CHECK}</span></button>${original}-</div>- `;+</div>`;};};--// Icon svg "copy"-const COPY_ICON = `<svg xmlns="http://www.w3.org/2000/svg" height="18px" viewBox="0 -960 960 960" width="18px"><path d="M160-80q-33 0-56.5-23.5T80-160v-480q0-33 23.5-56.5T160-720h80v-80q0-33 23.5-56.5T320-880h480q33 0 56.5 23.5T880-800v480q0 33-23.5 56.5T800-240h-80v80q0 33-23.5 56.5T640-80H160Zm160-240h480v-480H320v480Z"/></svg>`;--// Registered at module scope, so it must be guarded for SSR: the markdown pipeline-// is imported on server-rendered pages (e.g. the public document reader) where-// `document` does not exist.-if (import.meta.client) {- document.addEventListener('click', e => {- const btn = (e.target as HTMLElement)?.closest('.code-copy-btn') as HTMLButtonElement | null;- if (!btn) return;-- const code = btn.dataset.code ?? '';- navigator.clipboard.writeText(code);-- btn.classList.add('copied');- setTimeout(() => btn.classList.remove('copied'), 800);- });-}diff --git a/frontend/app/styles/features/markdown.scss b/frontend/app/styles/features/markdown.scssindex 23facc76..ace57ccb 100644--- a/frontend/app/styles/features/markdown.scss+++ b/frontend/app/styles/features/markdown.scss@@ -166,35 +166,70 @@}// *************** CODE BLOCKS **************** //-// Copy Button Styles.code-block-wrapper {position: relative;+ margin: 1em 0;}.code-copy-btn {position: absolute;- top: 22px;- right: 14px;- display: flex;- justify-content: center;+ top: 20px;+ right: 8px;+ z-index: 5;+ display: inline-flex;align-items: center;- padding: 4px;- opacity: 0;+ justify-content: center;+ width: 32px;+ height: 32px;+ padding: 6px;+ border-radius: 6px;+ border: 1px solid var(--grey-dark);+ background: rgba(30, 30, 30, 0.7);+ color: #a0a0a0;+ backdrop-filter: blur(4px);cursor: pointer;- transition: opacity $transition-fast ease, background-color $transition-fast ease, border-color $transition-fast ease;- fill: #e3e4e6;+ transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);}-.code-block-wrapper:hover .code-copy-btn {- opacity: 1;+.code-copy-btn:hover {+ background: rgba(45, 45, 45, 0.9);+ color: #fff;+ border-color: var(--grey);}-.code-copy-btn:hover {- background: #b0aeb91a;+.code-copy-btn:active {+ transform: scale(0.92);+}++.code-copy-btn .btn-icon,+.code-copy-btn .btn-icon-success {+ display: flex;+ align-items: center;+ justify-content: center;+ transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1),+ opacity 0.2s ease;+}++.code-copy-btn .btn-icon-success {+ position: absolute;+ opacity: 0;+ transform: scale(0.5) rotate(-45deg);+ color: var(--green);}.code-copy-btn.copied {- fill: #2ac27a;+ border-color: var(--green-border);+ background: var(--green-bg);+}++.code-copy-btn.copied .btn-icon {+ opacity: 0;+ transform: scale(0.5) rotate(45deg);+}++.code-copy-btn.copied .btn-icon-success {+ opacity: 1;+ transform: scale(1) rotate(0deg);}// *************** CARDS BLOCKS **************** //