Skip to content

Commit 0227bc8

Browse files
Add Copy Button and Fix Selection All Shortcuts in Modal (#376)
* Add 'Click to Copy response' * Refactor to work with Ctrl+A * Update pages/getting-started/understand-odata-in-6-steps.html Co-authored-by: John Gathogo <[email protected]>
1 parent 7d035e6 commit 0227bc8

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

pages/getting-started/understand-odata-in-6-steps.html

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ <h4 class="modal-title" id="step-collection-modal-label">Response</h4>
247247
{% endhighlight %}
248248
</p></div>
249249
<div class="modal-footer">
250+
<button type="button" class="btn btn-secondary btn-copy-code" onclick="copyCode()">Copy</button>
250251
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
252+
<div id="feedback" aria-live="assertive" role="alert"></div>
251253
</div>
252254
</p></div>
253255
</p></div>
@@ -296,7 +298,9 @@ <h4 class="modal-title" id="step-individual-modal-label">Response</h4>
296298
{% endhighlight %}
297299
</p></div>
298300
<div class="modal-footer">
301+
<button type="button" class="btn btn-secondary btn-copy-code" aria-live="assertive" onclick="copyCode()">Copy</button>
299302
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
303+
<div id="feedback" aria-live="assertive" role="alert"></div>
300304
</div>
301305
</p></div>
302306
</p></div>
@@ -336,7 +340,9 @@ <h4 class="modal-title" id="step-query-modal-label">Response</h4>
336340
{% endhighlight %}
337341
</p></div>
338342
<div class="modal-footer">
343+
<button type="button" class="btn btn-secondary btn-copy-code" aria-live="assertive" onclick="copyCode()">Copy</button>
339344
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
345+
<div id="feedback" aria-live="assertive" role="alert"></div>
340346
</div>
341347
</p></div>
342348
</p></div>
@@ -359,7 +365,9 @@ <h4 class="modal-title" id="step-post-modal-label">Response</h4>
359365
{% endhighlight %}
360366
</p></div>
361367
<div class="modal-footer">
368+
<button type="button" class="btn btn-secondary btn-copy-code" aria-live="assertive" onclick="copyCode()">Copy</button>
362369
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
370+
<div id="feedback" aria-live="assertive" role="alert"></div>
363371
</div>
364372
</p></div>
365373
</p></div>
@@ -378,7 +386,9 @@ <h4 class="modal-title" id="step-relationship-modal-label">Response</h4>
378386
{% endhighlight %}
379387
</p></div>
380388
<div class="modal-footer">
389+
<button type="button" class="btn btn-secondary btn-copy-code" aria-live="assertive" onclick="copyCode()">Copy</button>
381390
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
391+
<div id="feedback" aria-live="assertive" role="alert"></div>
382392
</div>
383393
</p></div>
384394
</p></div>
@@ -453,7 +463,9 @@ <h4 class="modal-title" id="step-function-modal-label">Response</h4>
453463
{% endhighlight %}
454464
</p></div>
455465
<div class="modal-footer">
466+
<button type="button" class="btn btn-secondary btn-copy-code" aria-live="assertive" onclick="copyCode()">Copy</button>
456467
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
468+
<div id="feedback" aria-live="assertive" role="alert"></div>
457469
</div>
458470
</p></div>
459471
</p></div>
@@ -1360,7 +1372,7 @@ <h4>Contribute to "Understanding OData in 6 steps"</h4>
13601372
</p></div>
13611373
<script>
13621374
$(document).ready(function() {
1363-
$('.modal').on('shown.bs.modal', function () {
1375+
$('.modal').on('shown.bs.modal', function () {
13641376
$('.close').focus();
13651377
trapFocus(this)
13661378
$(this).find(".modal-body").attr("tabindex", -1);
@@ -1421,4 +1433,60 @@ <h4>Contribute to "Understanding OData in 6 steps"</h4>
14211433

14221434
});
14231435
}
1436+
1437+
// Select all text in a modal when Ctrl+A is pressed
1438+
$(document).keydown(function(e) {
1439+
// If the 'Ctrl + a' or 'Ctrl + Shift + a' for Narrator key are pressed simultaneously.
1440+
// If the condition is met, it prevents the default action and selects the contents of any visible modal's body.
1441+
// Check if the modal is open
1442+
if ($('.modal:visible .modal-body').length && e.ctrlKey && (e.key === 'a' || e.key === 'A')) {
1443+
e.preventDefault();
1444+
var modalBody = $('.modal:visible .modal-body');
1445+
modalBody.focus();
1446+
1447+
// Create a range to select the content within the modal body
1448+
var range = document.createRange();
1449+
range.selectNodeContents(modalBody[0]);
1450+
1451+
// Get the current selection and remove any existing ranges
1452+
var selection = window.getSelection();
1453+
selection.removeAllRanges();
1454+
1455+
// Add the new range to the selection
1456+
selection.addRange(range);
1457+
}
1458+
});
1459+
1460+
// Copy code to clipboard
1461+
function copyCode() {
1462+
// Loop through each modal
1463+
document.querySelectorAll('.modal').forEach(function(modal) {
1464+
// Check if the modal is currently open
1465+
if (modal.style.display === 'block') {
1466+
var body = modal.querySelector('.modal-body');
1467+
1468+
var range = document.createRange();
1469+
range.selectNodeContents(body);
1470+
// Get the current selection and remove any existing ranges
1471+
var selection = window.getSelection();
1472+
selection.removeAllRanges();
1473+
// Add the new range to the selection
1474+
selection.addRange(range);
1475+
1476+
document.execCommand("copy");
1477+
1478+
// Provide feedback to the user
1479+
1480+
$('#feedback').text('Payload copied to the clipboard.').show();
1481+
1482+
// Hide the feedback after 3 seconds
1483+
setTimeout(function() {
1484+
$('#feedback').fadeOut('slow');
1485+
}, 3000);
1486+
1487+
// focus on the modal body to show the user that the content has been copied
1488+
body.focus();
1489+
}
1490+
});
1491+
}
14241492
</script>

public/css/site.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ a.carousel-control.left:focus, a.carousel-control.right:focus {
6565
background-color: #d23714;
6666
border-color: #d23714;
6767
}
68+
.btn-secondary {
69+
padding: 10px 20px;
70+
color: #ffffff;
71+
background-color: #6c757d;
72+
border-color: #6c757d;
73+
cursor: pointer;
74+
}
75+
.btn-secondary:hover {
76+
background-color: #272b2e;
77+
}
6878
.label-success{
6979
background-color: #277a33;
7080
}
@@ -478,4 +488,10 @@ td {
478488

479489
.other-protocols-item {
480490
background-color: #f2f2f2;
491+
}
492+
493+
#feedback {
494+
margin: 10px;
495+
font-weight: bold;
496+
color: green;
481497
}

0 commit comments

Comments
 (0)