Skip to content

Commit

Permalink
Fixed django#1276 -- Added copy buttons to console and shell commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
nvishnya committed Dec 6, 2023
1 parent 923dd69 commit d2e3566
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions djangoproject/scss/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3624,6 +3624,10 @@ ul.corporate-members li {
}

.code-block-caption,
.highlight-console,
.c-content-win,
.highlight-shell,
.highlight-doscon,
.snippet {
.btn-clipboard {
float: right;
Expand Down
7 changes: 6 additions & 1 deletion djangoproject/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ define(function() {
mods.push('mod/messages');
}

if (hasClass('code-block-caption') || hasClass('snippet')) {
if (hasClass('code-block-caption') ||
hasClass('snippet') ||
hasClass('highlight-console') ||
hasClass('c-content-win') ||
hasClass('highlight-shell') ||
hasClass('highlight-doscon')) {
mods.push('mod/clippify');
}

Expand Down
21 changes: 15 additions & 6 deletions djangoproject/static/js/mod/clippify.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
define(['jquery', 'clipboard'], function($, Clipboard) {
$('.code-block-caption').each(function() {
var header = $(this);
var wrapper = header.parent();
var code = $('.highlight', wrapper);
function addCopyButton(btnParent, code){
var btn = $('<span class="btn-clipboard" title="Copy this code">');
btn.append('<i class="icon icon-clipboard">');
btn.data('clipboard-text', $.trim(code.text()));
header.append(btn);
btn.data('clipboard-text', $.trim(code));
btnParent.prepend(btn);
};
$('.code-block-caption').each(function(){
var header = $(this);
var wrapper = header.parent();
var codeBlock = $('pre', wrapper);
addCopyButton(header, codeBlock.text())
});
$('.highlight-console, .c-content-win, .highlight-shell, .highlight-doscon').each(function(){
var codeBlock = $('pre', this);
var clone = $(codeBlock).clone();
$('.gp, .go', clone).remove();
addCopyButton(codeBlock, clone.text());
});
// For Django 2.0 docs and older.
$('.snippet').each(function() {
Expand Down

0 comments on commit d2e3566

Please sign in to comment.