mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-16 11:20:12 -04:00
Make copy to clipboard work with prefixes
The prefix was being included in the selection copied.
This commit is contained in:
@@ -27,11 +27,27 @@
|
||||
`
|
||||
};
|
||||
|
||||
this.getRangeFromElement = function (keyElement) {
|
||||
const range = document.createRange();
|
||||
let prefixIndex = -1;
|
||||
|
||||
Array.from(keyElement.childNodes).forEach((el, idx) => {
|
||||
if ((el.nodeType === 1) && el.classList.contains('govuk-visually-hidden')) {
|
||||
prefixIndex = idx;
|
||||
}
|
||||
});
|
||||
|
||||
range.selectNodeContents(keyElement);
|
||||
if (prefixIndex !== -1) { range.setStart(keyElement, prefixIndex + 1); }
|
||||
|
||||
return range;
|
||||
};
|
||||
|
||||
this.copyKey = function(keyElement, callback) {
|
||||
var selection = window.getSelection ? window.getSelection() : document.selection,
|
||||
range = document.createRange();
|
||||
range = this.getRangeFromElement(keyElement);
|
||||
|
||||
selection.removeAllRanges();
|
||||
range.selectNodeContents(keyElement);
|
||||
selection.addRange(range);
|
||||
document.execCommand('copy');
|
||||
selection.removeAllRanges();
|
||||
|
||||
Reference in New Issue
Block a user