From 818e43dd1b3147373dd2bdcfb36931338a1f9566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Fri, 29 May 2026 04:39:44 +0300 Subject: [PATCH] add copy code button --- .gitignore | 1 + app/lib/babycode.py | 2 +- app/templates/threads/thread.html | 2 +- data/static/js/bits/ui.js | 25 ++++++++++++++++++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 03e49c6..b78425b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ data/static/avatars/* !data/static/avatars/default.webp data/static/badges/user data/_cached +data/static/js/vnd/*.source.* config/secrets.prod.env config/pyrom_config.toml diff --git a/app/lib/babycode.py b/app/lib/babycode.py index 2f208c0..dea0228 100644 --- a/app/lib/babycode.py +++ b/app/lib/babycode.py @@ -358,7 +358,7 @@ def tag_code(children, attr): code = input_code button = f'' - block = f'
{language}{button}
{code}
' + block = f'
{language}{button}
{code}
' return block diff --git a/app/templates/threads/thread.html b/app/templates/threads/thread.html index d094694..9fb556f 100644 --- a/app/templates/threads/thread.html +++ b/app/templates/threads/thread.html @@ -83,7 +83,7 @@ {%- if is_logged_in() and get_active_user().can_post_to_thread_or_topic(thread) -%} -
+

Reply to "{{thread.title}}"

{{- babycode_editor_component() -}} diff --git a/data/static/js/bits/ui.js b/data/static/js/bits/ui.js index 70665be..cc701de 100644 --- a/data/static/js/bits/ui.js +++ b/data/static/js/bits/ui.js @@ -112,6 +112,11 @@ export function babycodeEditorCharCount(evOrPayload, sender, el) { } } +export function clearThreadDraft(_, __, ___) { + const threadId = getThreadId(); + localStorage.removeItem(`thread-${threadId}`); +} + export function babycodeEditorCharCountInit(_, __, el) { if (el === undefined) { // no editors on page return; @@ -119,7 +124,7 @@ export function babycodeEditorCharCountInit(_, __, el) { const threadId = getThreadId(); if (threadId !== -1) { - el.value = localStorage.getItem(`thread-${threadId}`); + el.value = localStorage.getItem(`thread-${threadId}`) || ''; } b.send({ sender: el }, 'babycodeEditorCharCount'); } @@ -192,3 +197,21 @@ export function babycodeEditorQuote(ev, sender, el) { b.send({ sender: el }, 'babycodeEditorCharCount'); el.focus(); } + +export async function copyCode(ev, sender, el) { + if (!el.contains(sender)) { + return; + } + + const originalText = sender.textContent; + const doneText = 'Copied!'; + const code = el.dataset.code; + + try { + await navigator.clipboard.writeText(code); + sender.textContent = doneText; + setTimeout(() => { sender.textContent = originalText }, 2000); + } catch (error) { + console.log(error); + } +}