Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tree #110

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
coverage
**/*.d.ts
tests
**/_interface
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
**/lib
**/package.json
jupytercad
**/_interface
100 changes: 82 additions & 18 deletions src/panelview/objecttree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
closeIcon
} from '@jupyterlab/ui-components';
import { Panel } from '@lumino/widgets';
import { ReactTree, ThemeSettings, TreeNodeList } from '@naisutech/react-tree';
import {
ReactTree,
ThemeSettings,
TreeNode,
TreeNodeList
} from '@naisutech/react-tree';

import visibilitySvg from '../../style/icon/visibility.svg';
import visibilityOffSvg from '../../style/icon/visibilityOff.svg';
Expand Down Expand Up @@ -145,34 +150,89 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
}

stateToTree = () => {
if (this.state.jcadObject) {
return this.state.jcadObject.map(obj => {
const name = obj.name;
if (!this.state.jcadObject) {
return [];
}

const objects = this.state.jcadObject;
const rootNodes: TreeNodeList = [];
const nodes = new Map<string, TreeNode | string>();

objects.forEach(obj => {
console.debug('Object:', obj);
const node: TreeNode = {
id: obj.name,
label: obj.name,
parentId: nodes.has(obj.name) ? (nodes.get(obj.name) as string) : null,
items: undefined
};

if (obj.parameters && 'Group' in obj.parameters) {
node.label = `Group (#${obj.name})`;
node.parentId = nodes.has(obj.name)
? (nodes.get(obj.name) as string)
: null;
nodes.set(obj.name, node);

console.debug('\tGroup:', obj.parameters['Group']);
obj.parameters['Group'].forEach(name => {
if (!nodes.has(name)) {
nodes.set(name, obj.name);
} else {
const node = nodes.get(name) as TreeNode;
node.parentId = obj.name;
nodes.set(name, node);
}
});

console.debug('\tOriginFeatures:', obj.parameters['OriginFeatures']);
obj.parameters['OriginFeatures']?.forEach(name => {
if (!nodes.has(name)) {
nodes.set(name, obj.name);
} else {
const node = nodes.get(name) as TreeNode;
node.parentId = obj.name;
nodes.set(name, node);
}
});

if ('Origin' in obj.parameters) {
const name = obj.parameters['Origin'];
if (!nodes.has(name)) {
nodes.set(name, obj.name);
} else {
const node = nodes.get(name) as TreeNode;
node.parentId = obj.name;
nodes.set(name, node);
}
}
} else {
const items: TreeNodeList = [];
if (obj.shape) {
items.push({
id: `${name}#shape#${obj.shape}#${this.state.filePath}`,
id: `${obj.name}#shape#${obj.shape}#${this.state.filePath}`,
label: 'Shape',
parentId: name
parentId: obj.name
});
}
if (obj.operators) {
items.push({
id: `${name}#operator#${this.state.filePath}`,
id: `${obj.name}#operator#${this.state.filePath}`,
label: 'Operators',
parentId: name
parentId: obj.name
});
}
return {
id: name,
label: obj.name ?? `Object (#${name})`,
parentId: null,
items
};
});
}
//node.label = `Object (#${obj.name})`;
node.items = items;
nodes.set(obj.name, node);
}

rootNodes.push(node);
});

return [];
console.debug('rootNodes:', rootNodes);
console.debug('\n\n');
return rootNodes;
};

getObjectFromName(name: string | null): IJCadObject | undefined {
Expand Down Expand Up @@ -249,7 +309,11 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
let selectedNodes: (number | string)[] = [];
if (selectedNode) {
const parentNode = data.filter(node => node.id === selectedNode);
if (parentNode.length > 0 && parentNode[0].items.length > 0) {
if (
parentNode.length > 0 &&
parentNode[0].items &&
parentNode[0].items.length > 0
) {
selectedNodes = [parentNode[0].items[0].id];
}
}
Expand Down