From 53c0f991fe17e2f61adce6bdb48fe4289b8008f2 Mon Sep 17 00:00:00 2001 From: Karsten Kosmala Date: Thu, 18 Sep 2025 09:01:09 +0200 Subject: [PATCH] feat: add copy-to-clipboard functionality for code elements --- css/area_content.less | 75 +++++++++++++++++++++++ js/copy-code.js | 137 ++++++++++++++++++++++++++++++++++++++++++ script.js | 2 +- 3 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 js/copy-code.js diff --git a/css/area_content.less b/css/area_content.less index 0390921..ce4d525 100755 --- a/css/area_content.less +++ b/css/area_content.less @@ -61,3 +61,78 @@ line-height: @line-height-default; } } + +/* Code copy functionality styles */ +.code-wrapper { + position: relative; + display: inline-block; + width: 100%; + + &.has-copy-btn { + .code, + pre { + padding-right: 3rem; // Make room for copy button + } + } +} + +.code-copy-btn { + position: absolute; + top: 1.5rem; + right: 0.5rem; + padding: 0.25rem 0.5rem; + background-color: @ini_background; + border: 1px solid @ini_border; + border-radius: @fix_border-radius; + cursor: pointer; + opacity: 0.7; + transition: opacity @transition, background-color @transition; + color: @ini_text; + line-height: 1; + + &:hover { + opacity: 1; + background-color: @ini_background_alt; + } + + &:active { + transform: translateY(1px); + } + + svg { + display: block; + width: 16px; + height: 16px; + } + + &.copy-success { + background-color: #d4edda; + border-color: #c3e6cb; + color: #155724; + } + + &.copy-error { + background-color: #f8d7da; + border-color: #f5c6cb; + color: #721c24; + } +} + +/* Ensure code blocks have proper positioning context */ +.code, +pre.code, +pre > code { + position: relative; +} + +/* Mobile adjustments */ +@media @screen_max-xs { + .code-copy-btn { + padding: 0.2rem 0.4rem; + + svg { + width: 14px; + height: 14px; + } + } +} diff --git a/js/copy-code.js b/js/copy-code.js new file mode 100644 index 0000000..b6077de --- /dev/null +++ b/js/copy-code.js @@ -0,0 +1,137 @@ +/** + * Adds copy-to-clipboard functionality for code blocks + * + * @author Karsten Kosmala + */ +(function($) { + 'use strict'; + + /** + * Create and insert copy button for a code element + */ + var createCopyButton = function($codeElement) { + var $button = $('