Skip to content

Commit d6882ea

Browse files
committed
Enable editing object name from Object Tree
1 parent 24d2eec commit d6882ea

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

packages/base/src/panelview/objecttree.tsx

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ interface IStates {
8989
selectedNodes: string[];
9090
clientId: number | null; // ID of the yjs client
9191
id: string; // ID of the component, it is used to identify which component
92+
editingNode: TreeNodeId | null;
93+
editingNodeValue: string | null;
9294
//is the source of awareness updates.
9395
openNodes: (number | string)[];
9496
}
@@ -154,7 +156,9 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
154156
selectedNodes: [],
155157
clientId: null,
156158
id: uuid(),
157-
openNodes: []
159+
openNodes: [],
160+
editingNode: null,
161+
editingNodeValue: ''
158162
};
159163
this.props.cpModel.jcadModel?.sharedObjectsChanged.connect(
160164
this._sharedJcadModelChanged
@@ -464,9 +468,54 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
464468
textOverflow: 'ellipsis',
465469
overflowX: 'hidden'
466470
}}
471+
onClick={() => this.setState({ editingNode: opts.node.id })}
467472
>
468-
{opts.node.label}
469-
</span>
473+
{this.state.editingNode === opts.node.id ? (
474+
<input
475+
type="text"
476+
value={this.state.editingNodeValue || opts.node.label}
477+
autoFocus
478+
onBlur={() => this.setState({ editingNode: null })}
479+
onChange={(e) =>
480+
this.setState({ editingNodeValue: e.target.value })
481+
}
482+
onKeyDown={(e) => {
483+
if (e.key === 'Enter') {
484+
console.log('Updated name:', this.state.editingNodeValue);
485+
const updatedName = this.state.editingNodeValue;
486+
const objectName = opts.node.id;
487+
488+
if(this.props.cpModel.mainViewModel) {
489+
const obj = this.props.cpModel.jcadModel?.sharedModel.getObjectByName(objectName as string);
490+
if (obj) {
491+
this.props.cpModel.sharedModel?.updateObjectByName(objectName as string, {
492+
data: {
493+
key: 'name',
494+
value: updatedName
495+
}
496+
}
497+
);
498+
}
499+
}
500+
this.setState({
501+
editingNode: null,
502+
editingNodeValue: null
503+
});
504+
}
505+
}}
506+
style={{
507+
width: '100%',
508+
background: 'transparent',
509+
border: 'none',
510+
outline: 'none',
511+
fontSize: 'inherit',
512+
color: 'inherit'
513+
}}
514+
/>
515+
) : (
516+
opts.node.label
517+
)}
518+
</span>
470519
{opts.type === 'leaf' ? (
471520
<div style={{ display: 'flex' }}>
472521
<ToolbarButtonComponent

0 commit comments

Comments
 (0)