sample-ui-component-library 0.0.0-beta → 0.0.0-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/LibraryManager-V1.svg +3 -0
- package/dist/cjs/index.js +7 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +7 -7
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/FileBrowser/FileBrowser.jsx +127 -0
- package/src/components/FileBrowser/FileBrowser.scss +40 -0
- package/src/components/FileBrowser/helper.js +70 -0
- package/src/components/FileBrowser/index.js +1 -0
- package/src/components/FlowDiagram/FlowDiagram.jsx +2 -2
- package/src/components/FlowDiagram/helper.js +5 -4
- package/src/index.js +2 -1
- package/src/stories/FileBrowser.stories.js +48 -0
- package/src/stories/FileBrowserStories.scss +19 -0
- package/src/stories/data/FileBrowser/Tree1.json +57 -0
- package/src/stories/data/FileBrowser/Tree2.json +60 -0
package/package.json
CHANGED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { useEffect, useState, useRef, useLayoutEffect, useCallback } from "react";
|
|
2
|
+
import "./FileBrowser.scss";
|
|
3
|
+
|
|
4
|
+
import { setDefaultCollapsed, collapseTree, selectNode, flattenTree } from "./helper";
|
|
5
|
+
|
|
6
|
+
import { FileCode, ChevronRight, ChevronDown, Braces, FiletypeScss, FiletypeJs, FiletypePy} from "react-bootstrap-icons";
|
|
7
|
+
import PropTypes from 'prop-types';
|
|
8
|
+
|
|
9
|
+
const INDENT_WIDTH = 20;
|
|
10
|
+
const SELECTED_FILE_COLOR = "#00426b";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Used for creating a stable callback function.
|
|
14
|
+
*/
|
|
15
|
+
function useEvent(fn) {
|
|
16
|
+
const fnRef = useRef(fn);
|
|
17
|
+
|
|
18
|
+
useLayoutEffect(() => {
|
|
19
|
+
fnRef.current = fn;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return useCallback((...args) => {
|
|
23
|
+
return fnRef.current(...args);
|
|
24
|
+
}, []);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Renders a single node in the file tree.
|
|
29
|
+
*/
|
|
30
|
+
const TreeNode = ({node, onRowClick}) => {
|
|
31
|
+
/**
|
|
32
|
+
* Gets the appropriate icon for the node based on its type and collapsed state.
|
|
33
|
+
* @returns <JSX>
|
|
34
|
+
*/
|
|
35
|
+
const getCollapsedIcon = () => {
|
|
36
|
+
if (node.collapsed) {
|
|
37
|
+
return <ChevronRight />;
|
|
38
|
+
} else {
|
|
39
|
+
return <ChevronDown />;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Sets the background color of the row if the node is selected.
|
|
45
|
+
*/
|
|
46
|
+
const getRowStyle = () => {
|
|
47
|
+
if (node.selected) {
|
|
48
|
+
return {"backgroundColor": SELECTED_FILE_COLOR};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the file icon based on the extension.
|
|
55
|
+
*
|
|
56
|
+
* TODO: This can be improved by having a mapping of extensions to icons and color.
|
|
57
|
+
*/
|
|
58
|
+
const getFileIcon = () => {
|
|
59
|
+
const extension = node.name.split('.').pop();
|
|
60
|
+
switch (extension) {
|
|
61
|
+
case "js":
|
|
62
|
+
return <FiletypeJs color="#f1e05a" />;
|
|
63
|
+
case "json":
|
|
64
|
+
return <Braces color="#fffb00" />;
|
|
65
|
+
case "scss":
|
|
66
|
+
return <FiletypeScss color="#f12727" />;
|
|
67
|
+
case "py":
|
|
68
|
+
return <FiletypePy color="#686affbd" />;
|
|
69
|
+
default:
|
|
70
|
+
return <FileCode color="#ffffff" />;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div className="file-node-row" style={getRowStyle()} onClick={() => onRowClick(node)}>
|
|
76
|
+
<div className="indent" style={{ width: node.level * INDENT_WIDTH + "px"}} />
|
|
77
|
+
{
|
|
78
|
+
node.type === "folder" ?
|
|
79
|
+
<span className="center folder-icon">{getCollapsedIcon()}</span> :
|
|
80
|
+
<span className="center file-icon">{getFileIcon()}</span>
|
|
81
|
+
}
|
|
82
|
+
<span className="center file-name">{node.name}</span>
|
|
83
|
+
</div>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Renders a file browser.
|
|
89
|
+
*
|
|
90
|
+
* @return {JSX}
|
|
91
|
+
*/
|
|
92
|
+
export const FileBrowser = ({tree, onNodeSelect}) => {
|
|
93
|
+
|
|
94
|
+
const [nodes, setNodes] = useState([]);
|
|
95
|
+
const treeRef = useRef();
|
|
96
|
+
|
|
97
|
+
const handleFileClick = useEvent((node) => {
|
|
98
|
+
selectNode(treeRef.current, node);
|
|
99
|
+
drawTree();
|
|
100
|
+
if (onNodeSelect) {
|
|
101
|
+
onNodeSelect(node);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
treeRef.current = flattenTree(tree);
|
|
107
|
+
setDefaultCollapsed(treeRef.current);
|
|
108
|
+
drawTree();
|
|
109
|
+
}, []);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Draws the tree given the nodes and the collapsed state of each node.
|
|
113
|
+
*/
|
|
114
|
+
const drawTree = () => {
|
|
115
|
+
const nodes = collapseTree(treeRef.current);
|
|
116
|
+
const rows = [];
|
|
117
|
+
nodes.forEach((node) => {
|
|
118
|
+
rows.push(<TreeNode key={node.id} node={node} onRowClick={handleFileClick}/>);
|
|
119
|
+
});
|
|
120
|
+
setNodes(rows);
|
|
121
|
+
}
|
|
122
|
+
return (
|
|
123
|
+
<div className="file-browser">
|
|
124
|
+
{nodes}
|
|
125
|
+
</div>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
.file-browser{
|
|
2
|
+
width: 100%;
|
|
3
|
+
height:100%;
|
|
4
|
+
padding:5px;
|
|
5
|
+
color:white;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.file-node-row{
|
|
9
|
+
width:100%;
|
|
10
|
+
min-width: 200px;
|
|
11
|
+
display:flex;
|
|
12
|
+
padding: 5px 0;
|
|
13
|
+
font-size:13px;
|
|
14
|
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
|
15
|
+
cursor: pointer;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.indent {
|
|
19
|
+
height:100%;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.center {
|
|
23
|
+
display:flex;
|
|
24
|
+
align-self: center;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
.folder-icon {
|
|
29
|
+
display:flex;
|
|
30
|
+
margin-right: 5px;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.file-icon {
|
|
34
|
+
margin-right: 5px;
|
|
35
|
+
font-size:16px;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.file-name {
|
|
39
|
+
color: #CCC;
|
|
40
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets the default collapsed state to false.
|
|
3
|
+
*/
|
|
4
|
+
export const setDefaultCollapsed = (tree) => {
|
|
5
|
+
tree.forEach((node) => {
|
|
6
|
+
node.collapsed = false;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Given a file tree, it flattens the tree into a list of nodes with the level of each node in the tree.
|
|
13
|
+
*
|
|
14
|
+
* @param {Array} tree - The file tree to flatten.
|
|
15
|
+
* @param {number} level - The current level of the tree (used for indentation).
|
|
16
|
+
* @return {Array} - The flattened tree with level information.
|
|
17
|
+
*/
|
|
18
|
+
export const flattenTree = (tree, level) => {
|
|
19
|
+
if (!level) {
|
|
20
|
+
level = 0;
|
|
21
|
+
}
|
|
22
|
+
let rows = [];
|
|
23
|
+
for (let i = 0; i < tree.length; i++) {
|
|
24
|
+
const node = tree[i];
|
|
25
|
+
node.level = level;
|
|
26
|
+
rows.push(node);
|
|
27
|
+
if (node?.children) {
|
|
28
|
+
rows = rows.concat(flattenTree(node.children, level + 1));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return rows;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Draws the tree given the nodes and the collapsed state of each node.
|
|
36
|
+
*/
|
|
37
|
+
export const collapseTree = (tree) => {
|
|
38
|
+
const rows = [];
|
|
39
|
+
|
|
40
|
+
let collapsing, collapseLevel;
|
|
41
|
+
for (let i = 0; i < tree.length; i++) {
|
|
42
|
+
|
|
43
|
+
if (collapsing) {
|
|
44
|
+
if (tree[i].level <= collapseLevel) {
|
|
45
|
+
collapsing = false;
|
|
46
|
+
} else {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (tree[i].collapsed) {
|
|
52
|
+
collapsing = true;
|
|
53
|
+
collapseLevel = tree[i].level;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
rows.push(tree[i]);
|
|
57
|
+
}
|
|
58
|
+
return rows;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Selects the given node and deselects all other nodes in the tree.
|
|
63
|
+
*/
|
|
64
|
+
export const selectNode = (tree, node) => {
|
|
65
|
+
tree.forEach((n) => {
|
|
66
|
+
n.selected = false;
|
|
67
|
+
});
|
|
68
|
+
node.selected = true;
|
|
69
|
+
node.collapsed = !node.collapsed;
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./FileBrowser.jsx"
|
|
@@ -15,11 +15,11 @@ import { getLayoutInfoFromTree } from "./helper.js"
|
|
|
15
15
|
const Flow = ({tree}) => {
|
|
16
16
|
const { fitView } = useReactFlow();
|
|
17
17
|
const [nodes, setNodes, onNodesChange] = useNodesState([]);
|
|
18
|
-
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
|
|
18
|
+
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
|
|
19
19
|
|
|
20
20
|
useEffect(() => {
|
|
21
21
|
if (tree) {
|
|
22
|
-
const flowInfo = getLayoutInfoFromTree(tree.data);
|
|
22
|
+
const flowInfo = getLayoutInfoFromTree(tree.data, tree.animated ?? false);
|
|
23
23
|
|
|
24
24
|
//direction: TB, BT, LR, or RL, where T = top, B = bottom, L = left, and R = right.
|
|
25
25
|
const layouted = getLayoutedElements(
|
|
@@ -14,10 +14,11 @@ const arrowStyle = {
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Returns react flow nodes and edges from the given tree.
|
|
17
|
-
* @param {Object} tree
|
|
18
|
-
* @
|
|
17
|
+
* @param {Object} tree Layout tree object.
|
|
18
|
+
* @param {boolean} animated Indicates if the edges should be animated.
|
|
19
|
+
* @returns {Array} An object containing nodes and edges arrays.
|
|
19
20
|
*/
|
|
20
|
-
export const getLayoutInfoFromTree = (tree) => {
|
|
21
|
+
export const getLayoutInfoFromTree = (tree, animated) => {
|
|
21
22
|
const edges = [];
|
|
22
23
|
const nodes = [];
|
|
23
24
|
Object.keys(tree).forEach((branchName, index1) => {
|
|
@@ -41,7 +42,7 @@ export const getLayoutInfoFromTree = (tree) => {
|
|
|
41
42
|
id: prevNode.flowId + "-" + branchName + "-" + index + "-" + flowNode.flowId,
|
|
42
43
|
source: prevNode.flowId,
|
|
43
44
|
target: flowNode.flowId,
|
|
44
|
-
animated:
|
|
45
|
+
animated: animated,
|
|
45
46
|
markerEnd: marker,
|
|
46
47
|
style: arrowStyle
|
|
47
48
|
}
|
package/src/index.js
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { FileBrowser } from "../components/FileBrowser";
|
|
3
|
+
import { useArgs } from "@storybook/preview-api";
|
|
4
|
+
import { action } from "@storybook/addon-actions";
|
|
5
|
+
|
|
6
|
+
import FileTree1 from "./data/FileBrowser/Tree1.json"
|
|
7
|
+
import FileTree2 from "./data/FileBrowser/Tree2.json"
|
|
8
|
+
|
|
9
|
+
import "./FileBrowserStories.scss"
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
title: 'FileBrowser',
|
|
13
|
+
component: FileBrowser,
|
|
14
|
+
argTypes: {}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const Template = (args) => {
|
|
19
|
+
const [, updateArgs] = useArgs();
|
|
20
|
+
|
|
21
|
+
const onNodeSelect = (selectedFile) => {
|
|
22
|
+
action('Selected Node:')(selectedFile);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
updateArgs({onNodeSelect : onNodeSelect});
|
|
27
|
+
}, []);
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div className="viewerStoryWrapper">
|
|
31
|
+
<div className="file-browser">
|
|
32
|
+
<FileBrowser {...args} />
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const Default = Template.bind({});
|
|
39
|
+
|
|
40
|
+
Default.args = {
|
|
41
|
+
tree: FileTree1.tree
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const Tree2 = Template.bind({});
|
|
45
|
+
|
|
46
|
+
Tree2.args = {
|
|
47
|
+
tree: FileTree2.tree
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
.viewerStoryWrapper{
|
|
2
|
+
position: absolute;
|
|
3
|
+
top:0;
|
|
4
|
+
bottom:0;
|
|
5
|
+
left:0;
|
|
6
|
+
right:0;
|
|
7
|
+
display:flex;
|
|
8
|
+
align-items: center;
|
|
9
|
+
justify-content: center;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.viewerStoryWrapper > .file-browser{
|
|
13
|
+
height: 300px;
|
|
14
|
+
background-color: #333;
|
|
15
|
+
overflow: auto;
|
|
16
|
+
scrollbar-gutter: stable;
|
|
17
|
+
scrollbar-color: #47474766 #252526;
|
|
18
|
+
scrollbar-width: thin;
|
|
19
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"tree": [
|
|
3
|
+
{
|
|
4
|
+
"id": 1,
|
|
5
|
+
"name": "root",
|
|
6
|
+
"type": "folder",
|
|
7
|
+
"children": [
|
|
8
|
+
{
|
|
9
|
+
"id": 2,
|
|
10
|
+
"name": "TraceViewer",
|
|
11
|
+
"type": "folder",
|
|
12
|
+
"children": [
|
|
13
|
+
{
|
|
14
|
+
"id": 3,
|
|
15
|
+
"name": "file1.js",
|
|
16
|
+
"type": "file"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": 4,
|
|
20
|
+
"name": "fileTree.json",
|
|
21
|
+
"type": "file"
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"id": 5,
|
|
27
|
+
"name": "LogInjector",
|
|
28
|
+
"type": "folder",
|
|
29
|
+
"children": [
|
|
30
|
+
{
|
|
31
|
+
"id": 6,
|
|
32
|
+
"name": "injector.py",
|
|
33
|
+
"type": "file"
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"id": 7,
|
|
41
|
+
"name": "sample.py",
|
|
42
|
+
"type": "file"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"id": 8,
|
|
46
|
+
"name": "DesignViewer",
|
|
47
|
+
"type": "folder",
|
|
48
|
+
"children": [
|
|
49
|
+
{
|
|
50
|
+
"id": 9,
|
|
51
|
+
"name": "test.js",
|
|
52
|
+
"type": "file"
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"tree": [
|
|
3
|
+
{
|
|
4
|
+
"id": 1,
|
|
5
|
+
"name": "TraceViewer",
|
|
6
|
+
"type": "folder",
|
|
7
|
+
"children": [
|
|
8
|
+
{
|
|
9
|
+
"id": 2,
|
|
10
|
+
"name": "FileBrowser.stories.js",
|
|
11
|
+
"type": "file"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"id": 3,
|
|
15
|
+
"name": "FileBrowserStories.scss",
|
|
16
|
+
"type": "file"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": 4,
|
|
20
|
+
"name": "FlowDiagram.stories.js",
|
|
21
|
+
"type": "file"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"id": 5,
|
|
25
|
+
"name": "FlowDiagram.scss",
|
|
26
|
+
"type": "file"
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"id": 6,
|
|
32
|
+
"name": "Data",
|
|
33
|
+
"type": "folder",
|
|
34
|
+
"children": [
|
|
35
|
+
{
|
|
36
|
+
"id": 7,
|
|
37
|
+
"name": "fileTree.json",
|
|
38
|
+
"type": "file"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"id": 8,
|
|
42
|
+
"name": "FileBrowser",
|
|
43
|
+
"type": "folder",
|
|
44
|
+
"children": [
|
|
45
|
+
{
|
|
46
|
+
"id": 9,
|
|
47
|
+
"name": "fileTree.json",
|
|
48
|
+
"type": "file"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"id": 10,
|
|
52
|
+
"name": "Tree2.json",
|
|
53
|
+
"type": "file"
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|