Skip to content

Commit e667bfa

Browse files
committed
Merge branch 'feature/copy-code' of https://github.com/andreihoncharuk/thingsboard.github.io into andreihoncharuk-feature/copy-code
2 parents 3b551c6 + 766f348 commit e667bfa

File tree

2 files changed

+90
-35
lines changed

2 files changed

+90
-35
lines changed

_includes/docwithnav.html

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,56 @@ <h4>On this page</h4>
141141
</style>
142142
<script>
143143
jqueryDefer(function () {
144-
$( document ).ready(function() {
145-
var allCodeBlocksElements = $( ".copy-code" );
146-
allCodeBlocksElements.each(function(i) {
144+
$(document).on('selectionchange',function() {
145+
// $(document).mouseup(function () {
146+
$('.clipboard-btn').removeClass('noChars');
147+
var selectedChars = getSelectedText();
148+
// $('#numberChars').html(selectedChars.length);
149+
if (selectedChars == 0) {
150+
$('.clipboard-btn').addClass('noChars');
151+
}
152+
});
153+
});
154+
function getSelectedText() {
155+
var text;
156+
if (window.getSelection) {
157+
text = window.getSelection().toString();
158+
} else if (document.getSelection) {
159+
text = document.getSelection();
160+
} else if (document.selection) {
161+
text = document.selection.createRange().text;
162+
}
163+
return text;
164+
}
165+
</script>
166+
<script>
167+
jqueryDefer(function () {
168+
$(document).ready(function () {
169+
var allCodeBlocksElements = $(".copy-code");
170+
allCodeBlocksElements.each(function (i) {
147171
var codeBlock = $(this);
148172
if (!codeBlock.hasClass('highlighter-rouge')) {
149173
codeBlock = codeBlock.find('.highlighter-rouge');
150174
}
151-
codeBlock.each(function() {
175+
codeBlock.each(function () {
152176
var block = codeBlock.find('pre.highlight > code .rouge-code');
153177
var currentId = "codeblock" + (i + 1);
154178
block.attr('id', currentId);
155179
var clipButton = $('<button class="clipboard-btn" data-clipboard-target="#' + currentId + '"><p>Copy to clipboard</p><div><img src="/images/copy-code-icon.svg" alt="Copy to clipboard"></div></button>');
156180
$(this).append(clipButton);
157-
clipButton.on('mouseleave', clearTooltip);
158-
clipButton.on('blur', clearTooltip);
181+
var troyan = codeBlock.find('pre.highlight');
182+
var Tooltip = $('<div class="customTooltip"><div class="tooltipText">Copied!</div></div>');
183+
troyan.append(Tooltip);
184+
troyan.addClass('clipboard-btn');
185+
troyan.attr('data-clipboard-target', "#" + currentId);
186+
troyan.on('mouseleave', clearTooltip);
187+
troyan.on('blur', clearTooltip);
159188
});
160189
});
161-
var clipboard = new Clipboard('.clipboard-btn');
162-
clipboard.on('success', function(e) {
190+
191+
var clipboard = new Clipboard('.noChars');
192+
clipboard.on('success', function (e) {
193+
$('.clipboard-btn').removeClass('noChars');
163194
e.clearSelection();
164195
showTooltip(e.trigger, 'Copied!');
165196
});
@@ -192,13 +223,13 @@ <h4>On this page</h4>
192223

193224
function clearTooltip(e) {
194225
var el = $(e.currentTarget);
195-
el.removeClass('tooltipped tooltipped-s');
226+
el.removeClass('showTool');
196227
el.attr('aria-label', null);
197228
}
198229

199230
function showTooltip(elem, msg) {
200231
var el = $(elem);
201-
el.addClass('tooltipped tooltipped-s');
232+
el.addClass('showTool');
202233
el.attr('aria-label', msg);
203234
}
204235
});

_sass/_base.sass

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,19 +1088,57 @@ section
10881088
transition: 0ms
10891089

10901090
.copy-code
1091+
.highlight
1092+
background: none!important
1093+
.customTooltip
1094+
display: none
1095+
position: absolute
1096+
width: calc(100% - 2px)
1097+
height: calc(100% - 2px)
1098+
top: 1px
1099+
left: 1px
1100+
border-radius: 4px
1101+
background: rgba(255, 255, 255, 0.55)
1102+
.tooltipText
1103+
bottom: calc(50% - 18px)
1104+
left: calc(50% - 54px)
1105+
position: absolute
1106+
text-align: center
1107+
margin-left: auto
1108+
margin-right: auto
1109+
border-radius: 4px
1110+
color: white
1111+
background: #212529
1112+
padding: 8px 22px
1113+
&.showTool .customTooltip
1114+
display: block
1115+
.rouge-code pre
1116+
padding-right: 32px
10911117
position: relative
10921118
button.clipboard-btn
1093-
width: 100%!important
1094-
height: 100%!important
1095-
top: 0!important
1096-
right: 0!important
1097-
background: none!important
1119+
pointer-events: none
1120+
outline: none
1121+
position: absolute
1122+
img
1123+
width: 18px
1124+
height: 18px
1125+
filter: invert(51%) sepia(6%) saturate(172%) hue-rotate(177deg) brightness(94%) contrast(92%)
1126+
width: 206px
1127+
height: 46px
1128+
top: 0
1129+
right: 0
1130+
background: none
1131+
-webkit-user-select: none
1132+
-moz-user-select: none
1133+
-ms-user-select: none
1134+
user-select: none
10981135
p
10991136
padding: 10px
11001137
top: 1px
11011138
transition: 0.2s
11021139
color: #2a7dec
1103-
background: #fff
1140+
background: rgba(255,255,255,0.85)
1141+
backdrop-filter: blur(4px)
11041142
opacity: 0
11051143
font-weight: 500
11061144
right: 32px
@@ -1125,26 +1163,12 @@ section
11251163
opacity: 1
11261164
img
11271165
filter: invert(49%) sepia(97%) saturate(3730%) hue-rotate(200deg) brightness(95%) contrast(95%)
1128-
1129-
button.clipboard-btn
1130-
outline: none
1131-
position: absolute
1132-
top: 3px
1133-
right: 8px
1134-
width: 38px
1135-
height: 38px
1136-
background: #fff
1137-
padding: 10px
1138-
line-height: 18px
1139-
1140-
&:hover, &:active
1166+
&:active
1167+
button.clipboard-btn
1168+
p
1169+
opacity: 0
11411170
img
1142-
filter: invert(49%) sepia(97%) saturate(3730%) hue-rotate(200deg) brightness(95%) contrast(95%)
1143-
1144-
img
1145-
width: 18px
1146-
height: 18px
1147-
filter: invert(51%) sepia(6%) saturate(172%) hue-rotate(177deg) brightness(94%) contrast(92%)
1171+
filter: invert(51%) sepia(6%) saturate(172%) hue-rotate(177deg) brightness(94%) contrast(92%)
11481172

11491173
#searchBox
11501174
display: block

0 commit comments

Comments
 (0)