wip fix file tree

This commit is contained in:
Rizky 2024-02-24 16:42:56 +07:00
parent 4d60d67f04
commit 79acd79f6c
2 changed files with 58 additions and 22 deletions

View File

@ -169,7 +169,7 @@ export const EdFileBrowser = () => {
className={cx( className={cx(
"border-r", "border-r",
css` css`
width: 10px; width: 5px;
` `
)} )}
/> />

View File

@ -287,10 +287,12 @@ const toggleDir = (p: PG, path: string, forceExpand?: boolean) => {
if (expanded) { if (expanded) {
if (expanded.includes(path) && !forceExpand) { if (expanded.includes(path) && !forceExpand) {
p.ui.popup.file.expanded[p.site.id] = expanded.filter((e) => e !== path); p.ui.popup.file.expanded[p.site.id] = expanded.filter((e) => e !== path);
refreshTree(p);
} else { } else {
p.ui.popup.file.expanded[p.site.id] = [...expanded, path]; p.ui.popup.file.expanded[p.site.id] = [...expanded, path];
} }
} }
localStorage.setItem( localStorage.setItem(
"panel-file-expanded", "panel-file-expanded",
JSON.stringify(p.ui.popup.file.expanded) JSON.stringify(p.ui.popup.file.expanded)
@ -307,32 +309,51 @@ export const reloadFileTree = async (p: PG) => {
} }
if (exp) { if (exp) {
for (const [k, v] of Object.entries(p.ui.popup.file.entry)) { const promises: Promise<any>[] = [];
if ( const added = new Set<string>();
p.ui.popup.file.entry[k] && const fetched = new Set<string>();
!exp.includes(k) && for (const e of exp.sort((a, b) => a.length - b.length)) {
k !== "/" && if (e) {
k !== p.ui.popup.file.path if (!p.ui.popup.file.entry[e]) {
) { let exists = false;
delete p.ui.popup.file.entry[k];
if (e.split("/").length <= 2) {
added.add(e);
exists = true;
} else {
for (const a of added) {
if (e.startsWith(a)) {
exists = true;
break;
}
} }
} }
const promises = []; if (exists) {
for (const e of exp) { const url = `/_file${e}/?dir`;
if (e) { if (!fetched.has(url)) {
if (!p.ui.popup.file.entry[e]) { fetched.add(url);
promises.push( promises.push(
p.script.api._raw(`/_file${e}/?dir`).then((fe: FEntry[]) => { p.script.api._raw(url).then((fe: FEntry[]) => {
if (Array.isArray(fe)) { if (Array.isArray(fe)) {
p.ui.popup.file.entry[e] = fe; p.ui.popup.file.entry[e] = fe;
} else {
p.ui.popup.file.expanded[p.site.id] = exp.filter(
(item) => item !== e
);
} }
}) })
); );
} }
} }
} }
}
}
await Promise.all(promises); await Promise.all(promises);
localStorage.setItem(
"panel-file-expanded",
JSON.stringify(p.ui.popup.file.expanded)
);
} }
const f = p.ui.popup.file; const f = p.ui.popup.file;
@ -345,8 +366,23 @@ export const reloadFileTree = async (p: PG) => {
}); });
} }
const tree: NodeModel<FEntry>[] = p.ui.popup.file.tree; refreshTree(p);
};
const refreshTree = (p: PG) => {
const exp = p.ui.popup.file.expanded[p.site.id];
for (const [k, v] of Object.entries(p.ui.popup.file.entry)) {
if (
p.ui.popup.file.entry[k] &&
!exp.includes(k) &&
k !== "/" &&
k !== p.ui.popup.file.path
) {
delete p.ui.popup.file.entry[k];
}
}
const tree: NodeModel<FEntry>[] = p.ui.popup.file.tree;
tree.length = 0; tree.length = 0;
tree.push({ id: "/", text: "/", parent: "" }); tree.push({ id: "/", text: "/", parent: "" });
const added = new Set<string>(["/"]); const added = new Set<string>(["/"]);