1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-05 17:02:47 +00:00

update tree class

This commit is contained in:
Michael An
2019-05-17 15:49:34 +08:00
parent 33b0e6a5c8
commit 86371553c5
2 changed files with 88 additions and 74 deletions

View File

@@ -11,12 +11,33 @@ const viewerPropTypes = {
onLinkClick: PropTypes.func.isRequired, onLinkClick: PropTypes.func.isRequired,
}; };
class TreeNode {
constructor({ name, href, parentNode }) {
this.name = name;
this.href = href;
this.parentNode = parentNode || null;
this.children = [];
}
setParent(parentNode) {
this.parentNode = parentNode;
}
addChildren(nodeList) {
nodeList.forEach((node) => {
node.setParent(this);
});
this.children = nodeList;
}
}
class IndexContentViewer extends React.Component { class IndexContentViewer extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.links = []; this.links = [];
this.rootNode = {}; this.treeRoot = new TreeNode({ name: '', href: '' });
} }
componentWillMount() { componentWillMount() {
@@ -92,7 +113,6 @@ class IndexContentViewer extends React.Component {
else if (item.type == 'link') { else if (item.type == 'link') {
url = item.data.href; url = item.data.href;
/* eslint-disable */ /* eslint-disable */
let expression = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/ let expression = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/
/* eslint-enable */ /* eslint-enable */
@@ -120,37 +140,45 @@ class IndexContentViewer extends React.Component {
return item; return item;
} }
modifyValueBeforeRender = (value) => {
let nodes = value.document.nodes;
let newNodes = Utils.changeMarkdownNodes(nodes, this.changeInlineNode);
value.document.nodes = newNodes;
return value;
}
getRootNode = () => { getRootNode = () => {
let value = deserialize(this.props.indexContent); let value = deserialize(this.props.indexContent);
value = value.toJSON(); value = value.toJSON();
value = this.modifyValueBeforeRender(value); const newNodes = Utils.changeMarkdownNodes(value.document.nodes, this.changeInlineNode);
value = Value.fromJSON(value); newNodes.map((node) => {
const nodes = value.document.nodes; if (node.type === 'unordered_list' || node.type === 'ordered_list') {
nodes.map((node) => { this.treeRoot = this.transSlateToTree(node.nodes, this.treeRoot);
if (node.type ==='unordered_list' || node.type === 'ordered_list') {
this.rootNode = node;
} }
}); });
} }
transSlateToTree = (slateNodes, treeRoot) => {
let treeNodes = slateNodes.map((slateNode) => {
const inline = slateNode.nodes[0].nodes[0];
if (slateNode.nodes.length === 2 && slateNode.nodes[1].type === 'unordered_list') {
let treeNode = new TreeNode({ name: inline.nodes[0].leaves[0].text, href: inline.data.href });
return this.transSlateToTree(slateNode.nodes[1].nodes, treeNode);
} else {
return new TreeNode({ name: inline.nodes[0].leaves[0].text, href: inline.data.href });
}
});
treeRoot.addChildren(treeNodes);
return treeRoot;
}
render() { render() {
return <FolderItem rootNode={this.rootNode} bindClickEvent={this.bindClickEvent} hasChild={false}/>; return (
<div className="mx-2 o-hidden">
<FolderItem node={this.treeRoot} bindClickEvent={this.bindClickEvent}/>
</div>
);
} }
} }
IndexContentViewer.propTypes = viewerPropTypes; IndexContentViewer.propTypes = viewerPropTypes;
const FolderItemPropTypes = { const FolderItemPropTypes = {
rootNode: PropTypes.object.isRequired, node: PropTypes.object.isRequired,
bindClickEvent: PropTypes.func.isRequired, bindClickEvent: PropTypes.func.isRequired,
hasChild: PropTypes.bool,
}; };
class FolderItem extends React.Component { class FolderItem extends React.Component {
@@ -158,65 +186,45 @@ class FolderItem extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
showChild: true expanded: true
}; };
} }
toggleShowChild = () => { toggleExpanded = () => {
this.setState({showChild: !this.state.showChild}, () => { this.setState({ expanded: !this.state.expanded }, () => {
if (this.state.showChild) { if (this.state.expanded) this.props.bindClickEvent();
this.props.bindClickEvent();
}
}); });
} }
componentDidMount() { renderLink = (node) => {
if (this.props.hasChild) { return <div className="wiki-nav-content"><a href={node.href}>{node.name}</a></div>;
this.setState({ showChild: false });
}
} }
render() { render() {
const { rootNode } = this.props; const { node } = this.props;
return ( if (node.children.length > 0) {
<div className="folder-item px-2"> return (
{ <React.Fragment>
rootNode.nodes.map((node) => { {node.parentNode &&
if (node.type === 'paragraph') { <React.Fragment>
let href = ''; <span className="switch-btn" onClick={this.toggleExpanded}>
node.nodes.map((linkNode) => { {this.state.expanded ? <i className="fa fa-caret-down"></i> : <i className="fa fa-caret-right"></i>}
// deal with node isn't a link </span>
href = linkNode.data ? encodeURI(linkNode.data.get('href')) : 'javascript:void(0);'; {this.renderLink(node)}
}); </React.Fragment>
return ( }
<div key={node.key} className="px-4 wiki-nav-content"> {this.state.expanded && node.children.map((child, index) => {
<a href={href}>{node.text}</a> return (
</div> <div className="pl-4 position-relative" key={index}>
); <FolderItem node={child} bindClickEvent={this.props.bindClickEvent}/>
} else { </div>
const hasChild = (rootNode.type === 'unordered_list' && rootNode.nodes.size > 1); );
return ( })}
<React.Fragment key={node.key}> </React.Fragment>
{this.props.hasChild && );
<span className="switch-btn" onClick={this.toggleShowChild}> } else {
{this.state.showChild ? <i className="fa fa-caret-down"></i> : <i className="fa fa-caret-right"></i>} return this.renderLink(node);
</span> }
}
{this.state.showChild &&
<FolderItem
rootNode={node}
key={node.key}
hasChild={hasChild}
bindClickEvent={this.props.bindClickEvent}
/>
}
</React.Fragment>
);
}
})
}
</div>
);
} }
} }

View File

@@ -1,16 +1,22 @@
.folder-item { .wiki-nav-content {
position: relative; margin-bottom: 5px;
margin-top: 5px;
} }
.folder-item .wiki-nav-content a { .wiki-nav-content a {
color: #333; color: #333;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
display: block;
} }
.folder-item .wiki-nav-content a:hover { .wiki-nav-content a:hover {
text-decoration: none; text-decoration: none;
color: #eb8205;
} }
.switch-btn { .switch-btn {
width: 1rem;
position: absolute; position: absolute;
left: 0.5rem; left: 0;
top: 0; top: 0;
color: #c0c0c0; color: #c0c0c0;
cursor: pointer;
} }