Skip to content

Commit

Permalink
Merge pull request #22896 from colemanw/api4ExplorerCopy
Browse files Browse the repository at this point in the history
APIv4 Explorer - Add "copy" button to quicly copy code to clipboard
  • Loading branch information
demeritcowboy authored Mar 6, 2022
2 parents f534a70 + d0f217d commit 549c624
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 16 additions & 2 deletions ang/api4Explorer/Explorer.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,14 @@ <h4 ng-bind-html="helpContent.description"></h4>
</p>
</div>
<div ng-repeat="style in code[selectedTab.code]">
<button class="btn btn-xs btn-default pull-right" ng-click="$ctrl.copyCode('api4-code-' + selectedTab.code + '-' + style.name)">
<i class="crm-i fa-clipboard"></i>
{{:: ts('Copy') }}
</button>
<label>{{:: style.label }}</label>
<div><pre class="prettyprint" ng-bind-html="style.code"></pre></div>
<div>
<pre class="prettyprint" id="api4-code-{{ selectedTab.code + '-' + style.name }}" ng-bind-html="style.code"></pre>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -261,7 +267,15 @@ <h4 ng-bind-html="helpContent.description"></h4>
</div>
<div class="panel-body">
<div ng-show="selectedTab.result === 'result'">
<pre class="prettyprint" ng-repeat="code in result" ng-bind-html="code"></pre>
<div ng-repeat="code in result">
<div class="clearfix">
<button ng-if="$index" class="btn btn-xs btn-default pull-right" ng-click="$ctrl.copyCode('api4-result-' + $index)">
<i class="crm-i fa-clipboard"></i>
{{:: ts('Copy') }}
</button>
</div>
<pre class="prettyprint" id="api4-result-{{ $index }}" ng-bind-html="code"></pre>
</div>
</div>
<div ng-if="::perm.accessDebugOutput" ng-show="selectedTab.result === 'debug'">
<pre ng-if="debug" class="prettyprint" ng-bind-html="debug"></pre>
Expand Down
12 changes: 11 additions & 1 deletion ang/api4Explorer/Explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@
default: 'json'
});

// Copy text to the clipboard
this.copyCode = function(domId) {
var node = document.getElementById(domId);
var range = document.createRange();
range.selectNode(node);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
};

function ucfirst(str) {
return str[0].toUpperCase() + str.slice(1);
}
Expand Down Expand Up @@ -976,7 +986,7 @@
break;

case 'php':
$scope.result.push(prettyPrintOne((_.isArray(response.values) ? '(' + response.values.length + ') ' : '') + _.escape(phpFormat(response.values, 2, 2)), 'php', 1));
$scope.result.push(prettyPrintOne('return ' + _.escape(phpFormat(response.values, 2, 2)) + ';', 'php', 1));
break;
}
};
Expand Down

0 comments on commit 549c624

Please sign in to comment.