Add metadata to zipArchives (display Github links in explorer)

This commit is contained in:
ochafik 2023-03-27 19:34:52 +01:00
commit 0ed2d658d0
5 changed files with 186 additions and 56 deletions

View file

@ -7,6 +7,7 @@ import { ModelContext, FSContext } from './contexts';
// import { isFileWritable } from '../state/model';
import { join } from '../fs/filesystem';
import { defaultSourcePath } from '../state/initial-state';
import { zipArchives } from '../fs/zip-archives';
function listFilesAsNodes(fs: FS, path: string, accept?: (path: string) => boolean): TreeNode[] {
const files: [string, string][] = []
@ -15,7 +16,7 @@ function listFilesAsNodes(fs: FS, path: string, accept?: (path: string) => boole
if (name.startsWith('.')) {
continue;
}
const childPath = join(path, name);//`${path}/${name}`;
const childPath = join(path, name);
if (accept && !accept(childPath)) {
continue;
}
@ -31,19 +32,47 @@ function listFilesAsNodes(fs: FS, path: string, accept?: (path: string) => boole
const nodes: TreeNode[] = []
for (const [arr, isDirectory] of [[files, false], [dirs, true]] as [[string, string][], boolean][]) {
for (const [name, path] of arr) {
const children = isDirectory ? listFilesAsNodes(fs, path) : undefined;
if (isDirectory && children!.length == 0) {
continue;
let children: TreeNode[] = [];
let label = name;
if (path.lastIndexOf('/') === 0) {
const config = zipArchives[name];
if (config && config.gitOrigin) {
const repoUrl = config.gitOrigin.repoUrl;
if (!children) children = [];
children.push({
icon: 'pi pi-github',
label: repoUrl,
key: repoUrl,
selectable: true,
});
for (const [label, link] of Object.entries(config.docs ?? [])) {
children.push({
icon: 'pi pi-book',
label,
key: link,
selectable: true,
});
}
}
}
if (isDirectory) {
children = [...children, ...listFilesAsNodes(fs, path, accept)];
if (children.length == 0) {
continue;
}
}
nodes.push({
// icon: path == '/home' ? 'pi-home' : ...
// icon: isDirectory ? 'pi pi-folder' : isFileWritable(path) ? 'pi pi-file' : 'pi pi-lock',
icon: isDirectory ? 'pi pi-folder' : path === defaultSourcePath ? 'pi pi-home' : 'pi pi-file',
label: name,
label,
data: path,
key: path,
children,
selectable: !isDirectory // && (name == 'LICENSE' || name.endsWith('.scad') || name.endsWith('.scad')
selectable: !isDirectory
});
}
}
@ -58,34 +87,26 @@ export default function FilePicker({className, style}: {className?: string, styl
const fs = useContext(FSContext);
const fsItems = fs && listFilesAsNodes(fs, '/')
// [
// {
// icon: 'pi pi-home',
// label: 'User files',
// key: '/',
// children: listFilesAsNodes(fs, '/'),//
// // children: listFilesAsNodes(fs, '/', f => f != librariesFolder && !f.startsWith(`${librariesFolder}/`)),
// selectable: false
// },
// {
// icon: 'pi pi-database',
// label: 'Builtin libraries',
// key: '/libraries',
// children: listFilesAsNodes(fs, '/libraries'),
// selectable: false
// },
// ] || [];
return (
<TreeSelect
className={className}
title='OpenSCAD Playground Files'
value={state.params.sourcePath}
onChange={(e) => model.openFile(String(e.value))}
// dropdownIcon="pi pi-folder-open"
resetFilterOnHide={true}
filterBy="key"
onChange={e => {
const key = e.value;
if (typeof key === 'string') {
if (key.startsWith('https://')) {
window.open(key, '_blank')
} else {
model.openFile(key);
}
}
}}
filter
style={style}
// style={{style}}
options={fsItems} />
)
}