From e23a98f2d65607d8aa6c7b409a513f8fdf4acdde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Guimar=C3=A3es?= Date: Thu, 17 Aug 2017 04:17:00 -0300 Subject: [PATCH] Create feature copy query to clipboard (#352) * Bugfix getSqlWithParams ReplacementParamsQuery * backward compatibility * Copy query to clipboard feature * remove space --- .../Resources/widgets/sqlqueries/widget.css | 6 ++++ .../Resources/widgets/sqlqueries/widget.js | 35 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/DebugBar/Resources/widgets/sqlqueries/widget.css b/src/DebugBar/Resources/widgets/sqlqueries/widget.css index b5fce268..04fd0afb 100644 --- a/src/DebugBar/Resources/widgets/sqlqueries/widget.css +++ b/src/DebugBar/Resources/widgets/sqlqueries/widget.css @@ -15,6 +15,7 @@ div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-database, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-duration, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-memory, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-row-count, +div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-copy-clipboard, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-stmt-id { float: right; margin-left: 8px; @@ -24,6 +25,7 @@ div.phpdebugbar-widgets-sqlqueries div.phpdebugbar-widgets-status span.phpdebugb div.phpdebugbar-widgets-sqlqueries div.phpdebugbar-widgets-status span.phpdebugbar-widgets-duration, div.phpdebugbar-widgets-sqlqueries div.phpdebugbar-widgets-status span.phpdebugbar-widgets-memory, div.phpdebugbar-widgets-sqlqueries div.phpdebugbar-widgets-status span.phpdebugbar-widgets-row-count, +div.phpdebugbar-widgets-sqlqueries div.phpdebugbar-widgets-status span.phpdebugbar-widgets-copy-clipboard, div.phpdebugbar-widgets-sqlqueries div.phpdebugbar-widgets-status span.phpdebugbar-widgets-stmt-id { color: #555; } @@ -31,6 +33,7 @@ div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-database:before, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-duration:before, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-memory:before, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-row-count:before, +div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-copy-clipboard:before, div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-stmt-id:before { font-family: PhpDebugbarFontAwesome; margin-right: 4px; @@ -51,6 +54,9 @@ div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-row-count:before { div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-stmt-id:before { content: "\f08d"; } +div.phpdebugbar-widgets-sqlqueries span.phpdebugbar-widgets-copy-clipboard:before { + content: "\f0c5"; +} div.phpdebugbar-widgets-sqlqueries table.phpdebugbar-widgets-params { display: none; width: 70%; diff --git a/src/DebugBar/Resources/widgets/sqlqueries/widget.js b/src/DebugBar/Resources/widgets/sqlqueries/widget.js index 74a81f9c..514915c7 100644 --- a/src/DebugBar/Resources/widgets/sqlqueries/widget.js +++ b/src/DebugBar/Resources/widgets/sqlqueries/widget.js @@ -24,7 +24,32 @@ this.set('exclude', excludedLabels); }, - + onCopyToClipboard: function (el) { + var code = $(el).parent('li').find('code').get(0); + var copy = function () { + try { + document.execCommand('copy'); + alert('Query copied to the clipboard'); + } catch (err) { + console.log('Oops, unable to copy'); + } + }; + var select = function (node) { + if (document.selection) { + var range = document.body.createTextRange(); + range.moveToElementText(node); + range.select(); + } else if (window.getSelection) { + var range = document.createRange(); + range.selectNodeContents(node); + window.getSelection().removeAllRanges(); + window.getSelection().addRange(range); + } + copy(); + window.getSelection().removeAllRanges(); + }; + select(code); + }, render: function() { this.$status = $('
').addClass(csscls('status')).appendTo(this.$el); @@ -67,6 +92,14 @@ li.addClass(csscls('error')); li.append($('').addClass(csscls('error')).text("[" + stmt.error_code + "] " + stmt.error_message)); } + $('') + .addClass(csscls('copy-clipboard')) + .css('cursor', 'pointer') + .on('click', function (event) { + self.onCopyToClipboard(this); + event.stopPropagation(); + }) + .appendTo(li); if (stmt.params && !$.isEmptyObject(stmt.params)) { var table = $('
Params
').addClass(csscls('params')).appendTo(li); for (var key in stmt.params) {