Skip to content

Commit e61fff2

Browse files
committed
fix: pasted content is exceeding the max char limit
1 parent 29d2093 commit e61fff2

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/extensionsIntegrated/CustomSnippets/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ define(function (require, exports, module) {
144144
$abbrInput.on("keydown", function (e) {
145145
Helper.validateAbbrInput(e, this);
146146
});
147+
$abbrInput.on("paste", function (e) {
148+
Helper.handleAbbrPaste(e, $(this));
149+
});
147150

148151
$templateInput.on("keydown", function (e) {
149152
Helper.handleTextareaTabKey(e, this);
@@ -165,6 +168,9 @@ define(function (require, exports, module) {
165168
$editAbbrInput.on("keydown", function (e) {
166169
Helper.validateAbbrInput(e, this);
167170
});
171+
$editAbbrInput.on("paste", function (e) {
172+
Helper.handleAbbrPaste(e, $(this));
173+
});
168174

169175
$editTemplateInput.on("keydown", function (e) {
170176
Helper.handleTextareaTabKey(e, this);

src/extensionsIntegrated/CustomSnippets/src/helper.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,90 @@ define(function (require, exports, module) {
506506
}
507507
}
508508

509+
/**
510+
* Handles abbreviation paste event with validation
511+
* @param {Event} e - The paste event
512+
* @param {jQuery} $input - The input element
513+
*/
514+
function handleAbbrPaste(e, $input) {
515+
e.preventDefault();
516+
517+
const clipboardData = (e.originalEvent || e).clipboardData.getData("text");
518+
519+
// Remove spaces and limit to 30 characters
520+
let sanitized = clipboardData.replace(/\s/g, ""); // Remove all spaces
521+
let wasTruncated = false;
522+
let hadSpaces = clipboardData !== sanitized;
523+
524+
if (sanitized.length > 30) {
525+
sanitized = sanitized.substring(0, 30);
526+
wasTruncated = true;
527+
}
528+
529+
// Insert sanitized value at current cursor position
530+
const input = $input[0];
531+
const start = input.selectionStart;
532+
const end = input.selectionEnd;
533+
const currentValue = input.value;
534+
535+
// Check if the final result would exceed 30 characters
536+
const beforeCursor = currentValue.substring(0, start);
537+
const afterCursor = currentValue.substring(end);
538+
const finalValue = beforeCursor + sanitized + afterCursor;
539+
540+
if (finalValue.length > 30) {
541+
// Trim the sanitized content to fit within the limit
542+
const availableSpace = 30 - (beforeCursor.length + afterCursor.length);
543+
if (availableSpace > 0) {
544+
sanitized = sanitized.substring(0, availableSpace);
545+
wasTruncated = true;
546+
} else {
547+
sanitized = ""; // No space available
548+
wasTruncated = true;
549+
}
550+
}
551+
552+
// Insert the final sanitized value
553+
input.value = beforeCursor + sanitized + afterCursor;
554+
555+
// Move the cursor to the end of the inserted text
556+
const newPos = start + sanitized.length;
557+
input.setSelectionRange(newPos, newPos);
558+
559+
// Show appropriate error message
560+
if (wasTruncated || hadSpaces) {
561+
const isEditForm = $input.attr("id") === 'edit-abbr-box';
562+
const inputId = isEditForm ? 'edit-abbr-box' : 'abbr-box';
563+
const wrapperId = isEditForm ? 'edit-abbr-box-wrapper' : 'abbr-box-wrapper';
564+
565+
// Prioritize length error over space error if both occurred
566+
if (wasTruncated) {
567+
const errorId = isEditForm ? 'edit-abbreviation-paste-length-error' : 'abbreviation-paste-length-error';
568+
UIHelper.showError(
569+
inputId,
570+
wrapperId,
571+
"Abbreviation cannot be more than 30 characters.",
572+
errorId
573+
);
574+
} else if (hadSpaces) {
575+
const errorId = isEditForm ? 'edit-abbreviation-paste-space-error' : 'abbreviation-paste-space-error';
576+
UIHelper.showError(
577+
inputId,
578+
wrapperId,
579+
"Space is not accepted as a valid abbreviation character.",
580+
errorId
581+
);
582+
}
583+
}
584+
585+
// Determine which save button to toggle based on input field
586+
if ($input.attr("id") === "edit-abbr-box") {
587+
toggleEditSaveButtonDisability();
588+
} else {
589+
toggleSaveButtonDisability();
590+
}
591+
}
592+
509593
exports.toggleSaveButtonDisability = toggleSaveButtonDisability;
510594
exports.createHintItem = createHintItem;
511595
exports.clearAllInputFields = clearAllInputFields;
@@ -528,4 +612,5 @@ define(function (require, exports, module) {
528612
exports.clearEditInputFields = clearEditInputFields;
529613
exports.handleTextareaTabKey = handleTextareaTabKey;
530614
exports.validateAbbrInput = validateAbbrInput;
615+
exports.handleAbbrPaste = handleAbbrPaste;
531616
});

0 commit comments

Comments
 (0)