Skip to content

Commit d3ce4b4

Browse files
committed
fix: prevent focus change when tab key is pressed inside the template text input
1 parent 9bb9930 commit d3ce4b4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/extensionsIntegrated/CustomSnippets/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ define(function (require, exports, module) {
138138
$abbrInput.on("input", Helper.toggleSaveButtonDisability);
139139
$templateInput.on("input", Helper.toggleSaveButtonDisability);
140140

141+
$templateInput.on("keydown", function (e) {
142+
Helper.handleTextareaTabKey(e, this);
143+
});
144+
141145
$fileExtnInput.on("input", function () {
142146
Helper.handleFileExtensionInput($(this));
143147
});
@@ -151,6 +155,10 @@ define(function (require, exports, module) {
151155
$editAbbrInput.on("input", Helper.toggleEditSaveButtonDisability);
152156
$editTemplateInput.on("input", Helper.toggleEditSaveButtonDisability);
153157

158+
$editTemplateInput.on("keydown", function (e) {
159+
Helper.handleTextareaTabKey(e, this);
160+
});
161+
154162
$editFileExtnInput.on("input", function () {
155163
Helper.handleFileExtensionInput($(this));
156164
});

src/extensionsIntegrated/CustomSnippets/src/helper.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,28 @@ define(function (require, exports, module) {
338338
}
339339
}
340340

341+
/**
342+
* this function is responsible to handle tab key press in textarea to insert tab character instead of moving focus
343+
*
344+
* @param {Event} e - The keydown event
345+
* @param {HTMLElement} textarea - The textarea element
346+
*/
347+
function handleTextareaTabKey(e, textarea) {
348+
// check if the key that is pressed is a tab key
349+
if (e.keyCode === 9 || e.which === 9) {
350+
e.preventDefault(); // to prevent focus change
351+
352+
const start = textarea.selectionStart;
353+
const end = textarea.selectionEnd;
354+
const value = textarea.value;
355+
356+
// to insert the tab character
357+
textarea.value = value.substring(0, start) + "\t" + value.substring(end);
358+
textarea.selectionStart = textarea.selectionEnd = start + 1;
359+
$(textarea).trigger('input');
360+
}
361+
}
362+
341363
exports.toggleSaveButtonDisability = toggleSaveButtonDisability;
342364
exports.createHintItem = createHintItem;
343365
exports.clearAllInputFields = clearAllInputFields;
@@ -355,4 +377,5 @@ define(function (require, exports, module) {
355377
exports.getEditSnippetData = getEditSnippetData;
356378
exports.toggleEditSaveButtonDisability = toggleEditSaveButtonDisability;
357379
exports.clearEditInputFields = clearEditInputFields;
380+
exports.handleTextareaTabKey = handleTextareaTabKey;
358381
});

0 commit comments

Comments
 (0)