From 0dbaa68fc0a975b909ce55a42dcc41b577d40244 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Sat, 24 Feb 2024 11:41:16 -0700 Subject: [PATCH] added copy and paste support for tags input --- .../bootstrap-tagsinput.js | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/wwwroot/lib/bootstrap-tagsinput/bootstrap-tagsinput.js b/wwwroot/lib/bootstrap-tagsinput/bootstrap-tagsinput.js index 8940ca9..23a9ebc 100644 --- a/wwwroot/lib/bootstrap-tagsinput/bootstrap-tagsinput.js +++ b/wwwroot/lib/bootstrap-tagsinput/bootstrap-tagsinput.js @@ -299,8 +299,7 @@ if (self.$element.attr('disabled')) { self.$input.attr('disabled', 'disabled'); return; - } - + } switch (event.which) { // BACKSPACE case 8: @@ -339,7 +338,31 @@ $nextTag.after($inputWrapper); $input.focus(); } - break; + break; + //COPY EVENT + case 67: + if (event.ctrlKey) { + event.preventDefault(); + navigator.clipboard.writeText(self.itemsArray.join(" ")); + } + break; + //PASTE EVENT + case 86: + if (event.ctrlKey) { + setTimeout(function () { + var pastedString = $input.val(); + //clear pasted string. + $input.val(''); + //process input one by one. + if (pastedString.length > 0) { + var tagsToAdd = pastedString.split(" "); + tagsToAdd.forEach(x => { + self.add(x); + }) + } + }, 250); + } + break; default: // ignore } @@ -358,17 +381,13 @@ maxLengthReached = self.options.maxChars && text.length >= self.options.maxChars; if (self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)) { //check if confirm keys are in input and then replace them. + event.preventDefault(); text = text.replace(String.fromCharCode(event.which), "") // Only attempt to add a tag if there is data in the field if (text.length !== 0) { self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text); $input.val(''); } - - // If the field is empty, let the event triggered fire as usual - if (self.options.cancelConfirmKeysOnEmpty === false) { - event.preventDefault(); - } } var textLength = $input.val().length, wordSpace = Math.ceil(textLength / 5), @@ -388,17 +407,13 @@ maxLengthReached = self.options.maxChars && text.length >= self.options.maxChars; if (self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)) { //check if confirm keys are in input and then replace them. - text = text.replace(String.fromCharCode(event.which), "") + event.preventDefault(); + text = text.replace(String.fromCharCode(event.which), ""); // Only attempt to add a tag if there is data in the field if (text.length !== 0) { self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text); $input.val(''); } - - // If the field is empty, let the event triggered fire as usual - if (self.options.cancelConfirmKeysOnEmpty === false) { - event.preventDefault(); - } } // Reset internal input's size