Skip to content

Commit dc56b51

Browse files
committed
feat: show root folders when empty query is entered inside the folder location dialog
1 parent a1efcb4 commit dc56b51

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/LiveDevelopment/LivePreviewEdit.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,36 @@ define(function (require, exports, module) {
808808
// these folders are generally very large, and we don't scan them otherwise it might freeze the UI
809809
const EXCLUDED_FOLDERS = ['node_modules', 'bower_components', '.git', '.npm', '.yarn'];
810810

811+
/**
812+
* this function scans all the root directories
813+
* root directories means those directories that are directly inside the project folder
814+
* we need this to show when the query is empty
815+
*
816+
* @param {Directory} directory - project root directory
817+
* @param {Array<string>} folderList - array to store discovered root folder paths
818+
* @return {Promise} Resolves when root scan is complete
819+
*/
820+
function _scanRootDirectoriesOnly(directory, folderList) {
821+
return new Promise((resolve) => {
822+
directory.getContents((err, contents) => {
823+
if (err) {
824+
resolve();
825+
return;
826+
}
827+
828+
const directories = contents.filter(entry => entry.isDirectory);
829+
830+
directories.forEach(dir => {
831+
// ignore all the excluded folders
832+
if (EXCLUDED_FOLDERS.includes(dir.name)) { return; }
833+
// add root folder name with trailing slash
834+
folderList.push(dir.name + '/');
835+
});
836+
resolve();
837+
});
838+
});
839+
}
840+
811841
/**
812842
* this function scans all the directories recursively
813843
* and then add the relative paths of the directories to the folderList array
@@ -902,15 +932,20 @@ define(function (require, exports, module) {
902932
*
903933
* @param {string} query - The search query from the input field
904934
* @param {Array<string>} folderList - List of all available folder paths
935+
* @param {Array<string>} rootFolders - list of root-level folder paths
905936
* @param {StringMatch.StringMatcher} stringMatcher - StringMatcher instance for fuzzy matching
906937
* @param {JQuery} $suggestions - jQuery element for the suggestions container
907938
* @param {JQuery} $input - jQuery element for the input field
908939
*/
909-
function _updateFolderSuggestions(query, folderList, stringMatcher, $suggestions, $input) {
940+
function _updateFolderSuggestions(query, folderList, rootFolders, stringMatcher, $suggestions, $input) {
910941
if (!query || query.trim() === '') {
942+
// when input is empty we show the root folders
943+
_renderFolderSuggestions(rootFolders.slice(0, 15), $suggestions, $input);
911944
return;
912945
}
913946

947+
if (!stringMatcher) { return; }
948+
914949
// filter folders using fuzzy matching
915950
const matches = folderList
916951
.map(folder => {
@@ -1034,16 +1069,19 @@ define(function (require, exports, module) {
10341069
const $rememberCheckbox = $dlg.find("#remember-folder-checkbox");
10351070

10361071
let folderList = [];
1072+
let rootFolders = [];
10371073
let stringMatcher = null;
10381074

1039-
// Scan project directories and setup event handlers
1040-
_scanDirectories(projectRoot, '', folderList).then(() => {
1075+
_scanRootDirectoriesOnly(projectRoot, rootFolders).then(() => {
10411076
stringMatcher = new StringMatch.StringMatcher({ segmentedSearch: true });
1077+
_renderFolderSuggestions(rootFolders.slice(0, 15), $suggestions, $input);
10421078
});
10431079

1080+
_scanDirectories(projectRoot, '', folderList);
1081+
10441082
// input event handler
10451083
$input.on('input', function() {
1046-
_updateFolderSuggestions($input.val(), folderList, stringMatcher, $suggestions, $input);
1084+
_updateFolderSuggestions($input.val(), folderList, rootFolders, stringMatcher, $suggestions, $input);
10471085
});
10481086
_registerFolderDialogInputHandlers($input, $suggestions, $dlg);
10491087
// focus the input box

0 commit comments

Comments
 (0)