File editor tree improvements

Sort files and folders by type, then by name

Hide hidden files/folders (starting with “.”) - WIP
This commit is contained in:
emizzle 2018-10-08 16:43:51 +11:00 committed by Pascal Precht
parent 0e872c99e6
commit f3431eecbd
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 54 additions and 9 deletions

View File

@ -1,6 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import {Treebeard, decorators} from 'react-treebeard';
import {Form} from 'tabler-react';
const Header = ({style, node}) => {
const iconType = node.children ? 'folder' : 'file';
@ -27,7 +28,9 @@ decorators.Header = Header;
class FileExplorer extends React.Component {
constructor(props){
super(props);
this.state = {};
this.state = {
files: this.props.files
};
}
onToggle(node, toggled){
@ -44,13 +47,23 @@ class FileExplorer extends React.Component {
this.setState({ cursor: node });
}
onHiddenToggle(value){
this.setState({files: this.props.files.filter((file) => {
return;
})
});
}
render(){
return (
<Treebeard
data={this.props.files}
decorators={decorators}
onToggle={this.onToggle.bind(this)}
/>
<React.Fragment>
<Form.Switch type="checkbox" name="toggle" value={true} label="Show hidden files" ref={(input) => { this.showHidden = input; }} onToggle={this.onHiddenToggle.bind(this)} />
<Treebeard
data={this.props.files}
decorators={decorators}
onToggle={this.onToggle.bind(this)}
/>
</React.Fragment>
);
}
}

View File

@ -73,14 +73,46 @@ class Pipeline {
'/embark-api/files',
(req, res) => {
const rootPath = fs.dappPath();
function fileTreeSort(objects){
function isFolder(object) {
return object.children && object.children.length;
}
function isNotFolder(object){
return !isFolder(object);
}
function byName(a, b) {
return a.name.localeCompare(b.name);
}
const folders = objects.filter(isFolder).sort(byName);
const files = objects.filter(isNotFolder).sort(byName);
return folders.concat(files);
}
const walk = (dir, filelist = []) => fs.readdirSync(dir).map(name => {
let isRoot = rootPath === dir;
if (fs.statSync(path.join(dir, name)).isDirectory()) {
return { isRoot, name, dirname: dir, children: walk(path.join(dir, name), filelist)};
return {
isRoot,
name,
dirname: dir,
path: path.join(dir, name),
isHidden: name.indexOf('.') === 0,
children: fileTreeSort(walk(path.join(dir, name), filelist))};
}
return {name, isRoot, path: path.join(dir, name), dirname: dir};
return {
name,
isRoot,
path: path.join(dir, name),
dirname: dir,
isHidden: name.indexOf('.') === 0
};
});
const files = walk(fs.dappPath());
const files = fileTreeSort(walk(fs.dappPath()));
res.send(files);
}
);