Skip to content

Commit a920dd7

Browse files
committed
fix: add char limit to description too
1 parent 4ca17c8 commit a920dd7

File tree

2 files changed

+144
-30
lines changed

2 files changed

+144
-30
lines changed

src/extensionsIntegrated/CustomSnippets/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ define(function (require, exports, module) {
112112
const $filterInput = $("#filter-snippets-input");
113113

114114
const $editAbbrInput = $("#edit-abbr-box");
115+
const $editDescInput = $("#edit-desc-box");
115116
const $editTemplateInput = $("#edit-template-text-box");
116117
const $editFileExtnInput = $("#edit-file-extn-box");
117118
const $saveEditSnippetBtn = $("#save-edit-snippet-btn");
@@ -148,6 +149,13 @@ define(function (require, exports, module) {
148149
Helper.handleAbbrPaste(e, $(this));
149150
});
150151

152+
$descInput.on("keydown", function (e) {
153+
Helper.validateDescInput(e, this);
154+
});
155+
$descInput.on("paste", function (e) {
156+
Helper.handleDescPaste(e, $(this));
157+
});
158+
151159
$templateInput.on("keydown", function (e) {
152160
Helper.handleTextareaTabKey(e, this);
153161
});
@@ -172,6 +180,13 @@ define(function (require, exports, module) {
172180
Helper.handleAbbrPaste(e, $(this));
173181
});
174182

183+
$editDescInput.on("keydown", function (e) {
184+
Helper.validateDescInput(e, this);
185+
});
186+
$editDescInput.on("paste", function (e) {
187+
Helper.handleDescPaste(e, $(this));
188+
});
189+
175190
$editTemplateInput.on("keydown", function (e) {
176191
Helper.handleTextareaTabKey(e, this);
177192
});

src/extensionsIntegrated/CustomSnippets/src/helper.js

Lines changed: 129 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@ define(function (require, exports, module) {
33
const Global = require("./global");
44
const UIHelper = require("./UIHelper");
55

6+
// list of all the navigation and function keys that are allowed inside the input fields
7+
const ALLOWED_NAVIGATION_KEYS = [
8+
"Backspace",
9+
"Delete",
10+
"Tab",
11+
"Escape",
12+
"Enter",
13+
"ArrowLeft",
14+
"ArrowRight",
15+
"ArrowUp",
16+
"ArrowDown",
17+
"Home",
18+
"End",
19+
"PageUp",
20+
"PageDown",
21+
"F1",
22+
"F2",
23+
"F3",
24+
"F4",
25+
"F5",
26+
"F6",
27+
"F7",
28+
"F8",
29+
"F9",
30+
"F10",
31+
"F11",
32+
"F12"
33+
];
34+
635
/**
736
* map the language IDs to their file extensions for snippet matching
837
* this is needed because we expect the user to enter file extensions and not the file type inside the input field
@@ -470,36 +499,8 @@ define(function (require, exports, module) {
470499
}
471500

472501
// Allow navigation and function keys
473-
const allowedKeys = [
474-
"Backspace",
475-
"Delete",
476-
"Tab",
477-
"Escape",
478-
"Enter",
479-
"ArrowLeft",
480-
"ArrowRight",
481-
"ArrowUp",
482-
"ArrowDown",
483-
"Home",
484-
"End",
485-
"PageUp",
486-
"PageDown",
487-
"F1",
488-
"F2",
489-
"F3",
490-
"F4",
491-
"F5",
492-
"F6",
493-
"F7",
494-
"F8",
495-
"F9",
496-
"F10",
497-
"F11",
498-
"F12"
499-
];
500-
501-
if (allowedKeys.includes(e.key)) {
502-
return; // Allow these keys to work normally
502+
if (ALLOWED_NAVIGATION_KEYS.includes(e.key)) {
503+
return;
503504
}
504505

505506
// Prevent space character
@@ -534,6 +535,35 @@ define(function (require, exports, module) {
534535
}
535536
}
536537

538+
function validateDescInput(e, descBox) {
539+
// Allow keyboard shortcuts and navigation keys
540+
if (e.ctrlKey || e.metaKey || e.altKey) {
541+
return;
542+
}
543+
544+
// Allow navigation and function keys
545+
if (ALLOWED_NAVIGATION_KEYS.includes(e.key)) {
546+
return;
547+
}
548+
549+
// Check for character limit (70 characters) - only for printable characters (spaces allowed)
550+
if (
551+
descBox.value.length >= 70 &&
552+
e.key.length === 1 &&
553+
e.key.match(/[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\ ]/)
554+
) {
555+
e.preventDefault();
556+
557+
// Determine if this is the edit form or new form
558+
const isEditForm = descBox.id === "edit-desc-box";
559+
const inputId = isEditForm ? "edit-desc-box" : "desc-box";
560+
const wrapperId = isEditForm ? "edit-desc-box-wrapper" : "desc-box-wrapper";
561+
const errorId = isEditForm ? "edit-description-length-error" : "description-length-error";
562+
563+
UIHelper.showError(inputId, wrapperId, "Description cannot be more than 70 characters.", errorId);
564+
}
565+
}
566+
537567
/**
538568
* Handles abbreviation paste event with validation
539569
* @param {Event} e - The paste event
@@ -613,6 +643,73 @@ define(function (require, exports, module) {
613643
}
614644
}
615645

646+
/**
647+
* Handles description paste event with validation
648+
* @param {Event} e - The paste event
649+
* @param {jQuery} $input - The input element
650+
*/
651+
function handleDescPaste(e, $input) {
652+
e.preventDefault();
653+
654+
const clipboardData = (e.originalEvent || e).clipboardData.getData("text");
655+
656+
// Keep spaces but limit to 70 characters
657+
let sanitized = clipboardData;
658+
let wasTruncated = false;
659+
660+
if (sanitized.length > 70) {
661+
sanitized = sanitized.substring(0, 70);
662+
wasTruncated = true;
663+
}
664+
665+
// Insert sanitized value at current cursor position
666+
const input = $input[0];
667+
const start = input.selectionStart;
668+
const end = input.selectionEnd;
669+
const currentValue = input.value;
670+
671+
// Check if the final result would exceed 70 characters
672+
const beforeCursor = currentValue.substring(0, start);
673+
const afterCursor = currentValue.substring(end);
674+
const finalValue = beforeCursor + sanitized + afterCursor;
675+
676+
if (finalValue.length > 70) {
677+
// Trim the sanitized content to fit within the limit
678+
const availableSpace = 70 - (beforeCursor.length + afterCursor.length);
679+
if (availableSpace > 0) {
680+
sanitized = sanitized.substring(0, availableSpace);
681+
wasTruncated = true;
682+
} else {
683+
sanitized = ""; // No space available
684+
wasTruncated = true;
685+
}
686+
}
687+
688+
// Insert the final sanitized value
689+
input.value = beforeCursor + sanitized + afterCursor;
690+
691+
// Move the cursor to the end of the inserted text
692+
const newPos = start + sanitized.length;
693+
input.setSelectionRange(newPos, newPos);
694+
695+
// Show error message if content was truncated
696+
if (wasTruncated) {
697+
const isEditForm = $input.attr("id") === "edit-desc-box";
698+
const inputId = isEditForm ? "edit-desc-box" : "desc-box";
699+
const wrapperId = isEditForm ? "edit-desc-box-wrapper" : "desc-box-wrapper";
700+
const errorId = isEditForm ? "edit-description-paste-length-error" : "description-paste-length-error";
701+
702+
UIHelper.showError(inputId, wrapperId, "Description cannot be more than 70 characters.", errorId);
703+
}
704+
705+
// Determine which save button to toggle based on input field
706+
if ($input.attr("id") === "edit-desc-box") {
707+
toggleEditSaveButtonDisability();
708+
} else {
709+
toggleSaveButtonDisability();
710+
}
711+
}
712+
616713
exports.toggleSaveButtonDisability = toggleSaveButtonDisability;
617714
exports.createHintItem = createHintItem;
618715
exports.clearAllInputFields = clearAllInputFields;
@@ -635,5 +732,7 @@ define(function (require, exports, module) {
635732
exports.clearEditInputFields = clearEditInputFields;
636733
exports.handleTextareaTabKey = handleTextareaTabKey;
637734
exports.validateAbbrInput = validateAbbrInput;
735+
exports.validateDescInput = validateDescInput;
638736
exports.handleAbbrPaste = handleAbbrPaste;
737+
exports.handleDescPaste = handleDescPaste;
639738
});

0 commit comments

Comments
 (0)