sample-ui-component-library 0.0.2-beta → 0.0.2-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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sample-ui-component-library",
3
- "version": "0.0.2-beta",
3
+ "version": "0.0.2-dev",
4
4
  "description": "A library which contains sample UI elements that can be used for populating layouts.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -63,6 +63,7 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@dagrejs/dagre": "^1.1.4",
66
+ "@dnd-kit/core": "^6.3.1",
66
67
  "@monaco-editor/react": "^4.7.0",
67
68
  "@xyflow/react": "^12.6.0",
68
69
  "bootstrap": "^5.3.4",
@@ -0,0 +1,59 @@
1
+ import "./Editor.scss";
2
+ import PropTypes from 'prop-types';
3
+
4
+ import { MonacoInstance } from "./MonacoInstance/MonacoInstance";
5
+ import { EditorTabs } from "./EditorTabs/EditorTabs";
6
+ import { useEffect, useState } from "react";
7
+
8
+ /**
9
+ * Renders the editor component with support for tabs.
10
+ *
11
+ * @return {JSX}
12
+ */
13
+ export const Editor = ({systemTree, onFileSelect}) => {
14
+
15
+ const [editorContent, setEditorContent] = useState("asdf");
16
+
17
+ const [activeTab, setActiveTab] = useState();
18
+ const [tabs, setTabs] = useState([
19
+ { id: "tab1", label: "Tab 1" },
20
+ { id: "tab2", label: "Tab 2" },
21
+ { id: "tab3", label: "Tab 3" },
22
+ ]);
23
+
24
+ useEffect(() => {
25
+ if (activeTab) {
26
+ setEditorContent(tabs[activeTab - 1].label);
27
+ }
28
+ }, [activeTab]);
29
+
30
+
31
+ useEffect(() => {
32
+ if (tabs) {
33
+ if ((activeTab && activeTab > tabs.length) || activeTab == null) {
34
+ setActiveTab(tabs.length);
35
+ }
36
+ }
37
+ }, [tabs]);
38
+
39
+ const onTabClick = (event) => {
40
+ const tabIndex = tabs.findIndex(obj => obj.id === event.target.id);
41
+ setActiveTab(tabIndex + 1);
42
+ }
43
+
44
+ return (
45
+ <div className="editorContainer">
46
+ <div className="tabContainer">
47
+ <EditorTabs activeTab={1} tabs={tabs} selectTab={onTabClick} />
48
+ </div>
49
+ <div className="monacoContainer">
50
+ <MonacoInstance editorContent={editorContent}/>
51
+ </div>
52
+ </div>
53
+ );
54
+ }
55
+
56
+ Editor.propTypes = {
57
+ systemTree: PropTypes.object,
58
+ onFileSelect: PropTypes.func
59
+ }
@@ -0,0 +1,16 @@
1
+ .editorContainer {
2
+ width:100%;
3
+ height:100%;
4
+ display:flex;
5
+ flex-direction:column;
6
+ overflow:hidden;
7
+ }
8
+
9
+ .tabContainer{
10
+ width:100%;
11
+ height: 40px;
12
+ }
13
+
14
+ .monacoContainer{
15
+ flex-grow: 1;
16
+ }
@@ -0,0 +1,76 @@
1
+ import "./EditorTabs.scss";
2
+ import PropTypes from "prop-types";
3
+
4
+ import { useEffect, useState } from "react";
5
+ import {
6
+ DndContext,
7
+ DragOverlay,
8
+ useDraggable,
9
+ useDroppable,
10
+ } from "@dnd-kit/core";
11
+
12
+ /**
13
+ * Tab Component
14
+ * @param {String} id
15
+ * @param {String} label
16
+ * @returns
17
+ */
18
+ function Tab({id, label, onSelectTab}) {
19
+ const { attributes, listeners, setNodeRef, transform } = useDraggable({id});
20
+
21
+ return (
22
+ <div ref={setNodeRef} id={id} onClick={onSelectTab} className="tab" {...listeners} {...attributes}>
23
+ {label}
24
+ </div>
25
+ );
26
+ }
27
+
28
+ /**
29
+ * Tab Gutter Component
30
+ * @param {String} id
31
+ * @returns
32
+ */
33
+ function Gutter({id}) {
34
+ const { setNodeRef, isOver } = useDroppable({id});
35
+ return (
36
+ <div
37
+ className="gutter"
38
+ ref={setNodeRef}
39
+ style={{background: isOver ? "white" : "#4da3ff33"}}
40
+ ></div>
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Tabs component.
46
+ * @param {Object} activeTab
47
+ * @param {Array} tabs
48
+ * @returns
49
+ */
50
+ export const EditorTabs = ({activeTab, tabs, selectTab}) => {
51
+
52
+ const [tabsList, setTabsList] = useState();
53
+
54
+ useEffect(() => {
55
+ if (tabs) {
56
+ const list = [];
57
+ tabs.forEach((tab, index) => {
58
+ list.push(
59
+ <>
60
+ <Gutter id={tab.id} />
61
+ <Tab onSelectTab={selectTab} key={tab.id} {...tab} />
62
+ </>
63
+ );
64
+ })
65
+ setTabsList(list);
66
+ }
67
+ }, [tabs]);
68
+
69
+ return (
70
+ <div style={{ display: "flex", background: "#222425" }}>
71
+ {tabsList}
72
+ </div>
73
+ );
74
+ };
75
+
76
+ EditorTabs.propTypes = {};
@@ -0,0 +1,22 @@
1
+ .tabs {
2
+ display: flex;
3
+ flex-direction: row;
4
+ background-color: #1e1e1e;
5
+ height: 100%;
6
+ width: 100%;
7
+ }
8
+
9
+ .tab {
10
+ padding: 0px 20px;
11
+ background-color: #2d2d2d;
12
+ color: #fff;
13
+ cursor: pointer;
14
+ height: 40px;
15
+ display:flex;
16
+ align-items: center;
17
+ }
18
+
19
+ .gutter {
20
+ height: 40px;
21
+ width: 1px;
22
+ }
@@ -0,0 +1,66 @@
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+
4
+ import Editor from '@monaco-editor/react';
5
+
6
+ import "./MonacoInstance.scss"
7
+
8
+ function Gutter({ id }) {
9
+ const { setNodeRef, isOver } = useDroppable({
10
+ id,
11
+ });
12
+
13
+ return (
14
+ <div
15
+ ref={setNodeRef}
16
+ style={{
17
+ height: 40,
18
+ width: 1,
19
+ background: isOver ? "white" : "#4da3ff33",
20
+ display: "flex",
21
+ alignItems: "center",
22
+ justifyContent: "center"
23
+ }}
24
+ ></div>
25
+ );
26
+ }
27
+
28
+ export const MonacoInstance = ({editorContent}) => {
29
+ const editorRef = useRef(null);
30
+
31
+ const content = useRef();
32
+
33
+ const handleEditorDidMount = (editor, monaco) => {
34
+ editorRef.current = editor;
35
+ if(content?.current) {
36
+ editorRef.current.setValue(content.current);
37
+ }
38
+ }
39
+
40
+ useEffect(() => {
41
+ content.current = editorContent;
42
+ if (editorRef?.current && content.current) {
43
+ editorRef.current.setValue(content.current);
44
+ }
45
+ }, [editorContent]);
46
+
47
+ return (
48
+ <Editor
49
+ defaultLanguage="python"
50
+ defaultValue=""
51
+ onMount={handleEditorDidMount}
52
+ theme="vs-dark"
53
+ options={{
54
+ scrollBeyondLastLine:false,
55
+ fontSize:"12px",
56
+ minimap: {
57
+ enabled: false
58
+ }
59
+ }}
60
+ />
61
+ );
62
+ }
63
+
64
+ MonacoInstance.propTypes = {
65
+ editorContent: PropTypes.string,
66
+ }
@@ -0,0 +1 @@
1
+ export * from "./Editor.jsx"
@@ -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"
package/src/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "./components/StackList";
2
2
  export * from "./components/Viewer";
3
- export * from "./components/FlowDiagram";
3
+ export * from "./components/FlowDiagram";
4
+ export * from "./components/FileBrowser";
5
+ export * from "./components/Editor";
@@ -0,0 +1,38 @@
1
+ import { useEffect } from "react";
2
+ import { Editor } from "../components/Editor";
3
+ import { useArgs } from "@storybook/preview-api";
4
+ import { action } from "@storybook/addon-actions";
5
+
6
+ import fileTrees from "./data/filetree.json";
7
+
8
+ import "./EditorStories.scss"
9
+
10
+ export default {
11
+ title: 'Editor',
12
+ component: Editor,
13
+ argTypes: {}
14
+ };
15
+
16
+ const Template = (args) => {
17
+ const [, updateArgs] = useArgs();
18
+
19
+ const onFileSelect = (selectedFile) => {
20
+ action('Selected Stack Position:')(selectedFile);
21
+ }
22
+
23
+ useEffect(() => {
24
+ updateArgs({onFileSelect : onFileSelect});
25
+ }, []);
26
+
27
+ return (
28
+ <div className="editorStoryWrapper">
29
+ <Editor {...args} />
30
+ </div>
31
+ )
32
+ }
33
+
34
+ export const Default = Template.bind({});
35
+
36
+ Default.args = {
37
+ systemTree: fileTrees.fileTrees
38
+ }
@@ -0,0 +1,7 @@
1
+ .editorStoryWrapper{
2
+ position: absolute;
3
+ top:0;
4
+ bottom:0;
5
+ left:0;
6
+ right:0;
7
+ }
@@ -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
+ }