Skip to content

Commit 5a43f23

Browse files
author
Raunak Raj
committed
Fix search bar bugs, enhance search functionality, and resolve duplication issue
- Fixed bugs related to the search bar on the plugin page. - The search bar will no longer close accidentally when clicking elsewhere on the screen. - Enhanced search functionality to allow searching across all available plugins from "all" section of the plugin page. - Resolved duplication issue related to the feature added in #989.
1 parent 4b528fe commit 5a43f23

5 files changed

Lines changed: 846 additions & 761 deletions

File tree

src/components/searchbar/index.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import './style.scss';
2-
import Ref from 'html-tag-js/ref';
3-
import actionStack from 'lib/actionStack';
1+
import "./style.scss";
2+
import Ref from "html-tag-js/ref";
3+
import actionStack from "lib/actionStack";
44

55
/**
66
* Create and activate search bar
@@ -14,15 +14,22 @@ function searchBar($list, setHide, onhideCb, searchFunction) {
1414
let timeout = null;
1515
const $searchInput = new Ref();
1616
/**@type {HTMLDivElement} */
17-
const $container = <div id="search-bar">
18-
<input ref={$searchInput} type="search" placeholder={strings.search} enterKeyHint="go" />
17+
const $container = (
18+
<div id="search-bar">
19+
<input
20+
ref={$searchInput}
21+
type="search"
22+
placeholder={strings.search}
23+
enterKeyHint="go"
24+
/>
1925
<span className="icon clearclose" onclick={hide}></span>
20-
</div>;
26+
</div>
27+
);
2128

2229
/**@type {HTMLElement[]} */
2330
const children = [...$list.children];
2431

25-
if (typeof setHide === 'function') {
32+
if (typeof setHide === "function") {
2633
hideOnBlur = false;
2734
setHide(hide);
2835
}
@@ -36,18 +43,18 @@ function searchBar($list, setHide, onhideCb, searchFunction) {
3643
};
3744

3845
actionStack.push({
39-
id: 'searchbar',
46+
id: "searchbar",
4047
action: hide,
4148
});
4249

4350
function hide() {
44-
actionStack.remove('searchbar');
51+
actionStack.remove("searchbar");
4552

4653
if (!$list.parentElement) return;
47-
if (typeof onhideCb === 'function') onhideCb();
54+
if (typeof onhideCb === "function") onhideCb();
4855

4956
$list.content = children;
50-
$container.classList.add('hide');
57+
$container.classList.add("hide");
5158
setTimeout(() => {
5259
$container.remove();
5360
}, 300);
@@ -61,23 +68,38 @@ function searchBar($list, setHide, onhideCb, searchFunction) {
6168
/**
6269
* @this {HTMLInputElement}
6370
*/
64-
function searchNow() {
71+
async function searchNow() {
6572
const val = $searchInput.value.toLowerCase();
66-
const result = searchFunction ? searchFunction(val) : filterList(val);
73+
let result;
6774

68-
$list.textContent = '';
75+
if (searchFunction) {
76+
result = searchFunction(val);
77+
78+
if (result instanceof Promise) {
79+
try {
80+
result = await result;
81+
} catch (error) {
82+
console.error("Search function failed:", error);
83+
result = [];
84+
}
85+
}
86+
} else {
87+
result = filterList(val);
88+
}
89+
90+
$list.textContent = "";
6991
$list.append(...result);
7092
}
7193

7294
/**
7395
* Search list items
74-
* @param {string} val
75-
* @returns
96+
* @param {string} val
97+
* @returns
7698
*/
7799
function filterList(val) {
78100
return children.filter((child) => {
79101
const text = child.textContent.toLowerCase();
80-
return text.match(val, 'i');
102+
return text.match(val, "i");
81103
});
82104
}
83105
}

src/lib/openFolder.js

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function openFolder(_path, opts = {}) {
7171

7272
const $root = collapsableList(title, "folder", {
7373
tail: <Tail target={() => $root.$title} />,
74-
allCaps: true,
75-
ontoggle: () => expandList($root),
74+
allCaps: true,
75+
ontoggle: () => expandList($root),
7676
});
7777
const $text = $root.$title.get(":scope>span.text");
7878

@@ -85,10 +85,10 @@ function openFolder(_path, opts = {}) {
8585
$root.$title.dataset.name = title;
8686

8787
$root.$ul.onclick =
88-
$root.$ul.oncontextmenu =
89-
$root.$title.onclick =
90-
$root.$title.oncontextmenu =
91-
handleItems;
88+
$root.$ul.oncontextmenu =
89+
$root.$title.onclick =
90+
$root.$title.oncontextmenu =
91+
handleItems;
9292

9393
recents.addFolder(_path, opts);
9494
sidebarApps.get("files").append($root);
@@ -182,24 +182,24 @@ async function expandList($list) {
182182
startLoading();
183183
const entries = await fsOperation(url).lsDir();
184184
helpers
185-
.sortDir(entries, {
186-
sortByName: true,
187-
showHiddenFiles: true,
188-
})
189-
.map((entry) => {
190-
const name = entry.name || Path.basename(entry.url);
191-
if (entry.isDirectory) {
192-
const $list = createFolderTile(name, entry.url);
193-
$ul.appendChild($list);
194-
195-
if (listState[entry.url]) {
196-
$list.expand();
197-
}
198-
} else {
199-
const $item = createFileTile(name, entry.url);
200-
$ul.append($item);
185+
.sortDir(entries, {
186+
sortByName: true,
187+
showHiddenFiles: true,
188+
})
189+
.map((entry) => {
190+
const name = entry.name || Path.basename(entry.url);
191+
if (entry.isDirectory) {
192+
const $list = createFolderTile(name, entry.url);
193+
$ul.appendChild($list);
194+
195+
if (listState[entry.url]) {
196+
$list.expand();
201197
}
202-
});
198+
} else {
199+
const $item = createFileTile(name, entry.url);
200+
$ul.append($item);
201+
}
202+
});
203203
} catch (err) {
204204
$list.collapse();
205205
helpers.error(err);
@@ -388,7 +388,7 @@ function execOperation(type, action, url, $target, name) {
388388
$target.dataset.name = newName;
389389
if (helpers.isFile(type)) {
390390
$target.querySelector(":scope>span").className =
391-
helpers.getIconForFile(newName);
391+
helpers.getIconForFile(newName);
392392
let file = editorManager.getFile(url, "uri");
393393
if (file) {
394394
file.uri = newUrl;
@@ -406,9 +406,9 @@ function execOperation(type, action, url, $target, name) {
406406

407407
async function createNew() {
408408
const msg =
409-
action === "new file"
410-
? strings["enter file name"]
411-
: strings["enter folder name"];
409+
action === "new file"
410+
? strings["enter file name"]
411+
: strings["enter folder name"];
412412

413413
let newName = await prompt(msg, "", "text", {
414414
match: constants.FILE_NAME_REGEX,
@@ -425,11 +425,12 @@ function execOperation(type, action, url, $target, name) {
425425
} else {
426426
newUrl = await helpers.createFileStructure(url, newName, false);
427427
}
428+
if (!newUrl) return;
428429
newName = Url.basename(newUrl.uri);
429430
if ($target.unclasped) {
430431
if (newUrl.type == "file") {
431432
appendTile($target, createFileTile(newName, newUrl.uri));
432-
} else {
433+
} else if (newUrl.type == "folder") {
433434
appendList($target, createFolderTile(newName, newUrl.uri));
434435
}
435436
}
@@ -635,7 +636,7 @@ function appendList($target, $list) {
635636
function createFolderTile(name, url) {
636637
const $list = collapsableList(name, "folder", {
637638
tail: <Tail target={() => $list.$title} />,
638-
ontoggle: () => expandList($list),
639+
ontoggle: () => expandList($list),
639640
});
640641
const { $title } = $list;
641642
$title.dataset.url = url;
@@ -654,8 +655,8 @@ function createFolderTile(name, url) {
654655
function createFileTile(name, url) {
655656
const $tile = tile({
656657
lead: <span className={helpers.getIconForFile(name)}></span>,
657-
text: name,
658-
tail: <Tail target={() => $tile} />,
658+
text: name,
659+
tail: <Tail target={() => $tile} />,
659660
});
660661
$tile.dataset.url = url;
661662
$tile.dataset.name = name;
@@ -673,16 +674,16 @@ function createFileTile(name, url) {
673674
function Tail({ target }) {
674675
return (
675676
<span
676-
className="icon more_vert"
677-
attr-action="close"
678-
onclick={(e) => {
679-
e.stopPropagation();
680-
e.preventDefault();
681-
handleItems({
682-
target: target(),
683-
type: "contextmenu",
684-
});
685-
}}
677+
className="icon more_vert"
678+
attr-action="close"
679+
onclick={(e) => {
680+
e.stopPropagation();
681+
e.preventDefault();
682+
handleItems({
683+
target: target(),
684+
type: "contextmenu",
685+
});
686+
}}
686687
></span>
687688
);
688689
}
@@ -725,7 +726,7 @@ openFolder.renameItem = (oldFile, newFile, newFilename) => {
725726
}, 0);
726727
} else {
727728
$el.querySelector(":scope>span").className =
728-
helpers.getIconForFile(newFilename);
729+
helpers.getIconForFile(newFilename);
729730
}
730731

731732
$el.dataset.url = newFile;

0 commit comments

Comments
 (0)