From 1a8346e367dd294698042720a3a542ef161d30ab Mon Sep 17 00:00:00 2001 From: thesayfulla Date: Sat, 11 Jan 2025 18:13:24 +0500 Subject: [PATCH 1/4] added clipboard copy button --- debug_toolbar/panels/templates/panel.py | 7 ++++ .../static/debug_toolbar/js/copy_context.js | 39 +++++++++++++++++++ .../debug_toolbar/panels/templates.html | 3 ++ 3 files changed, 49 insertions(+) create mode 100644 debug_toolbar/static/debug_toolbar/js/copy_context.js diff --git a/debug_toolbar/panels/templates/panel.py b/debug_toolbar/panels/templates/panel.py index e9a5b4e83..e3006b8ce 100644 --- a/debug_toolbar/panels/templates/panel.py +++ b/debug_toolbar/panels/templates/panel.py @@ -5,6 +5,7 @@ from django import http from django.core import signing +from django.templatetags.static import static from django.db.models.query import QuerySet, RawQuerySet from django.template import RequestContext, Template from django.test.signals import template_rendered @@ -225,3 +226,9 @@ def generate_stats(self, request, response): "context_processors": context_processors, } ) + + @property + def scripts(self): + scripts = super().scripts + scripts.append(static("debug_toolbar/js/copy_context.js")) + return scripts diff --git a/debug_toolbar/static/debug_toolbar/js/copy_context.js b/debug_toolbar/static/debug_toolbar/js/copy_context.js new file mode 100644 index 000000000..bb6da3918 --- /dev/null +++ b/debug_toolbar/static/debug_toolbar/js/copy_context.js @@ -0,0 +1,39 @@ +document.addEventListener("click", (event) => { + if (event.target.classList.contains("copy-context")) { + const context = event.target.getAttribute("data-context"); + + if (context) { + try { + const decodedContext = decodeUnicode(context); + navigator.clipboard + .writeText(decodedContext) + .then(() => { + const originalText = event.target.textContent; + event.target.textContent = "Copied!"; + + setTimeout(() => { + event.target.textContent = originalText; + }, 2000); + }) + .catch((error) => { + console.error("Failed to copy context:", error); + }); + } catch (error) { + console.error("Error decoding context:", error); + } + } + } +}); + +/** + * Decodes escaped Unicode characters in a string. + * E.g., converts "\\u0027" to "'". + * + * @param {string} text - The text containing escaped Unicode characters. + * @returns {string} - Decoded text. + */ +function decodeUnicode(text) { + return text.replace(/\\u[\dA-Fa-f]{4}/g, (match) => { + return String.fromCharCode(parseInt(match.replace("\\u", ""), 16)); + }); +} diff --git a/debug_toolbar/templates/debug_toolbar/panels/templates.html b/debug_toolbar/templates/debug_toolbar/panels/templates.html index 121c086a8..6747fbe73 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/templates.html +++ b/debug_toolbar/templates/debug_toolbar/panels/templates.html @@ -20,6 +20,9 @@

{% blocktrans count templates|length as template_count %}Template{% plural %
{% trans "Toggle context" %} + {{ template.context }}
From e88996925396c4bf6ceeda62d09f15db3c9749e6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Jan 2025 13:21:54 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- debug_toolbar/panels/templates/panel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/panels/templates/panel.py b/debug_toolbar/panels/templates/panel.py index e3006b8ce..fc955baa7 100644 --- a/debug_toolbar/panels/templates/panel.py +++ b/debug_toolbar/panels/templates/panel.py @@ -5,9 +5,9 @@ from django import http from django.core import signing -from django.templatetags.static import static from django.db.models.query import QuerySet, RawQuerySet from django.template import RequestContext, Template +from django.templatetags.static import static from django.test.signals import template_rendered from django.test.utils import instrumented_test_render from django.urls import path From 21e65d78c5a14ca8c727acdef2d39a2b008761e0 Mon Sep 17 00:00:00 2001 From: thesayfulla Date: Wed, 5 Feb 2025 16:17:08 +0500 Subject: [PATCH 3/4] Update copy button to support list --- .../static/debug_toolbar/js/copy_context.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/debug_toolbar/static/debug_toolbar/js/copy_context.js b/debug_toolbar/static/debug_toolbar/js/copy_context.js index bb6da3918..8f49ebeb4 100644 --- a/debug_toolbar/static/debug_toolbar/js/copy_context.js +++ b/debug_toolbar/static/debug_toolbar/js/copy_context.js @@ -5,8 +5,10 @@ document.addEventListener("click", (event) => { if (context) { try { const decodedContext = decodeUnicode(context); + const listContext = makeList(decodedContext); + navigator.clipboard - .writeText(decodedContext) + .writeText(listContext) .then(() => { const originalText = event.target.textContent; event.target.textContent = "Copied!"; @@ -19,7 +21,7 @@ document.addEventListener("click", (event) => { console.error("Failed to copy context:", error); }); } catch (error) { - console.error("Error decoding context:", error); + console.error("Error processing context:", error); } } } @@ -27,7 +29,6 @@ document.addEventListener("click", (event) => { /** * Decodes escaped Unicode characters in a string. - * E.g., converts "\\u0027" to "'". * * @param {string} text - The text containing escaped Unicode characters. * @returns {string} - Decoded text. @@ -37,3 +38,13 @@ function decodeUnicode(text) { return String.fromCharCode(parseInt(match.replace("\\u", ""), 16)); }); } + +/** + * Wraps multiple JSON objects into a list format. + * + * @param {string} text - The raw text containing multiple JSON objects. + * @returns {string} - Properly formatted JSON array string. + */ +function makeList(text) { + return `[${text.replace(/\n(?=\{)/g, ',')}]`; +} From 32443e1f786c054e4f9c07bb76dc1a730aa2b93c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Mar 2025 10:58:52 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- debug_toolbar/static/debug_toolbar/js/copy_context.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/static/debug_toolbar/js/copy_context.js b/debug_toolbar/static/debug_toolbar/js/copy_context.js index 8f49ebeb4..1452dff92 100644 --- a/debug_toolbar/static/debug_toolbar/js/copy_context.js +++ b/debug_toolbar/static/debug_toolbar/js/copy_context.js @@ -35,7 +35,9 @@ document.addEventListener("click", (event) => { */ function decodeUnicode(text) { return text.replace(/\\u[\dA-Fa-f]{4}/g, (match) => { - return String.fromCharCode(parseInt(match.replace("\\u", ""), 16)); + return String.fromCharCode( + Number.parseInt(match.replace("\\u", ""), 16) + ); }); } @@ -46,5 +48,5 @@ function decodeUnicode(text) { * @returns {string} - Properly formatted JSON array string. */ function makeList(text) { - return `[${text.replace(/\n(?=\{)/g, ',')}]`; + return `[${text.replace(/\n(?=\{)/g, ",")}]`; }