Skip to content

Commit b9ec5a9

Browse files
author
Philipp
committed
'Copy to clipboard' functionality added
1 parent fb297d7 commit b9ec5a9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

philsweb.jquery-codemirror.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@
7070

7171
}
7272

73+
function copyToClipboard(value) {
74+
var $temp = $("<textarea>");
75+
$('body').append($temp.css({opacity: 0, zIndex: -9999}));
76+
77+
$temp.val(value).select();
78+
document.execCommand("copy");
79+
$temp.remove();
80+
}
81+
7382
return this.each(function () {
7483

7584
var basicConfigs = $.extend({}, defaults, options);
@@ -83,7 +92,9 @@
8392
var buttons = $('<div>');
8493
var copyToClipboardButton = $('<div>Copy to clipboard</div>');
8594
$(this).prepend(buttonsWrapper.addClass("codemirror-buttons-wrapper").append(
86-
buttons.addClass("codemirror-buttons").append(copyToClipboardButton.addClass("codemirror-button"))
95+
buttons.addClass("codemirror-buttons").append(
96+
copyToClipboardButton.addClass("codemirror-button").addClass("copy-to-clipboard")
97+
)
8798
));
8899

89100
buttonsWrapper.css('pointer-events', 'none');
@@ -96,6 +107,10 @@
96107
$(this).mouseleave(function () {
97108
$(this).children('.codemirror-buttons-wrapper').first().hide();
98109
});
110+
111+
$(this).find('.copy-to-clipboard').click(function () {
112+
copyToClipboard($(this).parent().parent().parent().codemirror().getValue());
113+
});
99114
});
100115
};
101116

philsweb.jquery-codemirror.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)