sample-ui-component-library 0.0.8-dev → 0.0.10-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.8-dev",
3
+ "version": "0.0.10-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",
@@ -37,8 +37,8 @@ export const Editor = forwardRef(({ }, ref) => {
37
37
  dispatch({ type: "MOVE_TAB", payload: { tabId, newIndex } });
38
38
  }, []);
39
39
 
40
- const addTab = useCallback((tab) => {
41
- dispatch({ type: "ADD_TAB", payload: tab });
40
+ const addTab = useCallback((tab, index) => {
41
+ dispatch({ type: "ADD_TAB", payload: { tab, index } });
42
42
  }, []);
43
43
 
44
44
  const setTabGroupId = useCallback((id) => {
@@ -8,11 +8,29 @@ export const editorReducer = (state, action) => {
8
8
  switch (action.type) {
9
9
 
10
10
  case "ADD_TAB": {
11
- // TODO: Add some validation for the payload here.
11
+ const { tab, index } = action.payload;
12
+
13
+ const tabInfo = state.tabs.find(obj => obj.uid === tab.uid);
14
+ if (tabInfo) {
15
+ console.warn(`Tab with id ${tabInfo.uid} already exists`);
16
+ return {
17
+ ...state,
18
+ activeTab: tabInfo
19
+ };
20
+ }
21
+
22
+ // Insert tab at specific location if it was provided.
23
+ let tabs = [...state.tabs];
24
+ if (index !== undefined && index < state.tabs.length) {
25
+ tabs.splice(index, 0, tab);
26
+ } else {
27
+ tabs.push(tab);
28
+ }
29
+
12
30
  return {
13
31
  ...state,
14
- tabs: [...state.tabs, action.payload],
15
- activeTab: action.payload
32
+ tabs: tabs,
33
+ activeTab: tab
16
34
  };
17
35
  }
18
36
 
@@ -13,7 +13,7 @@ export const Gutter = ({ id, index, parentId }) => {
13
13
  const { setNodeRef, isOver } = useDroppable({
14
14
  id,
15
15
  data: {
16
- type: "tab-gutter",
16
+ type: "EditorTabGutter",
17
17
  parentId: parentId,
18
18
  index: index,
19
19
  },
@@ -28,7 +28,7 @@ export const Tab = ({ id, parentId, node }) => {
28
28
  const { attributes, listeners, setNodeRef, transform } = useDraggable({
29
29
  id,
30
30
  data: {
31
- type: "tab-draggable",
31
+ type: "EditorTab",
32
32
  parentId: parentId,
33
33
  node: node
34
34
  },
@@ -5,6 +5,7 @@ import {
5
5
  useCallback,
6
6
  useContext,
7
7
  useImperativeHandle,
8
+ useEffect,
8
9
  } from "react";
9
10
  import "./FileBrowser.scss";
10
11
 
@@ -20,13 +21,18 @@ import { fileBrowserReducer, initialState } from "./FileBrowserReducer";
20
21
  *
21
22
  * @return {JSX}
22
23
  */
23
- export const FileBrowser = forwardRef(({ }, ref) => {
24
+ export const FileBrowser = forwardRef(({onSelectFile}, ref) => {
24
25
  const [state, dispatch] = useReducer(fileBrowserReducer, initialState);
25
26
 
26
27
  const addFileTree = useCallback((tree) => {
27
28
  dispatch({ type: "ADD_FILE_TREE", payload: tree });
28
29
  }, []);
29
30
 
31
+ useEffect(() => {
32
+ if (state.selectedNode && state.selectedNode.type === "file") {
33
+ onSelectFile(state.selectedNode);
34
+ }
35
+ }, [state.selectedNode]);
30
36
 
31
37
  const selectNode = useCallback((node) => {
32
38
  dispatch({ type: "SELECT_NODE", payload: node });
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  setDefaultCollapsed,
3
3
  collapseTree,
4
- selectNode,
5
4
  flattenTree,
6
5
  } from "./helper";
7
6
 
8
7
  export const initialState = {
9
8
  tree: {},
10
9
  flattenedTree: {},
10
+ selectedNode: null,
11
11
  collapsedTree: [],
12
12
  };
13
13
 
@@ -28,8 +28,10 @@ export const fileBrowserReducer = (state, action) => {
28
28
 
29
29
  case "SELECT_NODE": {
30
30
  const tree = [...state.flattenedTree];
31
+ let selectedNode;
31
32
  tree.forEach((n) => {
32
33
  if (n.uid === action.payload) {
34
+ selectedNode = n;
33
35
  n.selected = true;
34
36
  n.collapsed = !n.collapsed;
35
37
  } else {
@@ -40,7 +42,8 @@ export const fileBrowserReducer = (state, action) => {
40
42
  return {
41
43
  ...state,
42
44
  flattenedTree: tree,
43
- collapsedTree: collapsedTree
45
+ collapsedTree: collapsedTree,
46
+ selectedNode: selectedNode
44
47
  }
45
48
  }
46
49
  }
@@ -22,6 +22,7 @@ export const TreeNode = ({ id, node }) => {
22
22
  {
23
23
  id,
24
24
  data: {
25
+ type: "FileTreeNode",
25
26
  node: node
26
27
  }
27
28
  }
@@ -8,7 +8,6 @@ export const setDefaultCollapsed = (tree) => {
8
8
  return tree;
9
9
  }
10
10
 
11
-
12
11
  /**
13
12
  * Given a file tree, it flattens the tree into a list of nodes with the level of each node in the tree.
14
13
  *
@@ -58,14 +57,3 @@ export const collapseTree = (tree) => {
58
57
  }
59
58
  return rows;
60
59
  }
61
-
62
- /**
63
- * Selects the given node and deselects all other nodes in the tree.
64
- */
65
- export const selectNode = (tree, node) => {
66
- tree.forEach((n) => {
67
- n.selected = false;
68
- });
69
- node.selected = true;
70
- node.collapsed = !node.collapsed;
71
- }
@@ -51,6 +51,16 @@ const Template = (args) => {
51
51
  editorRef.current.addTab(node);
52
52
  }
53
53
  });
54
+
55
+
56
+ const node = {
57
+ "name": "SAMPLE",
58
+ "type": "file",
59
+ "uid": "dissr-f6459410-1634-4dbc-8d76-35896822158d",
60
+ "content": "1234"
61
+ }
62
+
63
+ editorRef.current.addTab(node,2);
54
64
  }, []);
55
65
 
56
66
  const onDragStart = (event) => {
@@ -71,7 +81,7 @@ const Template = (args) => {
71
81
  return;
72
82
  }
73
83
 
74
- if (active.data.current.type === "tab-draggable" && over.data.current.type === "tab-gutter") {
84
+ if (active.data.current.type === "EditorTab" && over.data.current.type === "EditorTabGutter") {
75
85
  editorRef.current.moveTab(active.data.current.node.uid, over.data.current.index);
76
86
  }
77
87
  }
@@ -39,18 +39,16 @@ const Template = (args) => {
39
39
 
40
40
  const [dragPreviewLabel, setDragPreviewLabel] = useState(<></>);
41
41
 
42
- const onNodeSelect = (selectedFile) => {
42
+ const onSelectFile = (selectedFile) => {
43
43
  action('Selected Node:')(selectedFile);
44
44
  }
45
45
 
46
46
  useEffect(() => {
47
- updateArgs({onNodeSelect : onNodeSelect});
47
+ updateArgs({onSelectFile : onSelectFile});
48
48
  }, []);
49
49
 
50
-
51
50
  useLayoutEffect(() => {
52
51
  fileBrowserRef.current.addFileTree(args.tree);
53
-
54
52
  setTimeout(() => {
55
53
  fileBrowserRef.current.selectNode("dir-f6459410-1634-4dbc-8d76-35896822158d");
56
54
  }, 3000);