Skip to content

Commit 381c840

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

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

packages/base/src/panelview/objecttree.tsx

Lines changed: 59 additions & 2 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,8 +468,61 @@ 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}
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(
485+
'Updated name:',
486+
this.state.editingNodeValue
487+
);
488+
const updatedName = this.state.editingNodeValue;
489+
const objectName = opts.node.id;
490+
491+
if (this.props.cpModel.mainViewModel) {
492+
const obj =
493+
this.props.cpModel.jcadModel?.sharedModel.getObjectByName(
494+
objectName as string
495+
);
496+
if (obj) {
497+
this.props.cpModel.sharedModel?.updateObjectByName(
498+
objectName as string,
499+
{
500+
data: {
501+
key: 'name',
502+
value: updatedName
503+
}
504+
}
505+
);
506+
}
507+
}
508+
this.setState({
509+
editingNode: null,
510+
editingNodeValue: null
511+
});
512+
}
513+
}}
514+
style={{
515+
width: '100%',
516+
background: 'transparent',
517+
border: 'none',
518+
outline: 'none',
519+
fontSize: 'inherit',
520+
color: 'inherit'
521+
}}
522+
/>
523+
) : (
524+
opts.node.label
525+
)}
469526
</span>
470527
{opts.type === 'leaf' ? (
471528
<div style={{ display: 'flex' }}>

0 commit comments

Comments
 (0)