import React from 'react'; function sortByType(a, b) { if (a.type == "dir" && b.type != "dir") { return -1; } else if (a.type != "dir" && b.type == "dir") { return 1; } else { return a.name.localeCompare(b.name); } } class TreeNodeView extends React.Component { renderCollapse = () => { const { node } = this.props; if (node.hasChildren()) { const { isExpanded } = node; return ( e.stopPropagation()} onClick={this.handleCollapse} /> ); } return null; } renderChildren = () => { const { node } = this.props; if (node.children && node.children.length) { const childrenStyles = { paddingLeft: this.props.paddingLeft }; var l = node.children.sort(sortByType); l = l.filter((node) => { return node.type == "dir" || node.isMarkdown(); }) /* the `key` property is needed. Otherwise there is a warning in the console */ return (
{l.map(child => { return ( ); })}
); } return null; } render() { const { node } = this.props; const styles = {}; var icon, type; if (node.type === "dir") { icon = ; type = 'dir'; } else { let index = node.name.lastIndexOf("."); if (index === -1) { icon = ; type = 'file'; } else { type = node.name.substring(index).toLowerCase(); if (type === ".png" || type === ".jpg") { icon = ; type = 'image'; } else { icon = ; type = 'file'; } } } return (
{this.renderCollapse()} {icon} {node.name}
{node.isExpanded ? this.renderChildren() : null}
); } onClick = e => { let { node } = this.props; this.props.treeView.onClick(e, node); } onMouseEnter = e => { let { node } = this.props; this.props.treeView.showImagePreview(e, node); } onMouseLeave = e => { this.props.treeView.hideImagePreview(e); } handleCollapse = e => { e.stopPropagation(); const { node } = this.props; if (this.props.treeView.toggleCollapse) { this.props.treeView.toggleCollapse(node); } } onDragStart = e => { const { node } = this.props; this.props.treeView.onDragStart(e, node); } } export default TreeNodeView;