Skip to content

Commit

Permalink
Improve deletion behavior (#369)
Browse files Browse the repository at this point in the history
* Improve deletion behavior

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Handle updating objects parameters

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update ui-tests

* Try fixing UI-tests

* Trung's comment

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
martinRenou and pre-commit-ci[bot] committed Jun 26, 2024
1 parent 71e093c commit dabfb28
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 20 deletions.
18 changes: 12 additions & 6 deletions packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ const OPERATORS = {
shape: 'Part::Cut',
parameters,
visible: true,
name: Name
name: Name,
dependencies: [parameters['Base'], parameters['Tool']]
};

return executeOperator(
Expand Down Expand Up @@ -287,7 +288,8 @@ const OPERATORS = {
shape: 'Part::Extrusion',
parameters,
visible: true,
name: Name
name: Name,
dependencies: [parameters['Base']]
};

return executeOperator(
Expand Down Expand Up @@ -325,7 +327,8 @@ const OPERATORS = {
shape: 'Part::MultiFuse',
parameters,
visible: true,
name: Name
name: Name,
dependencies: parameters['Shapes']
};

return executeOperator(
Expand Down Expand Up @@ -365,7 +368,8 @@ const OPERATORS = {
shape: 'Part::MultiCommon',
parameters,
visible: true,
name: Name
name: Name,
dependencies: parameters['Shapes']
};

return executeOperator(
Expand Down Expand Up @@ -404,7 +408,8 @@ const OPERATORS = {
shape: 'Part::Chamfer',
parameters,
visible: true,
name: Name
name: Name,
dependencies: [parameters['Base']]
};

return executeOperator(
Expand Down Expand Up @@ -441,7 +446,8 @@ const OPERATORS = {
shape: 'Part::Fillet',
parameters,
visible: true,
name: Name
name: Name,
dependencies: [parameters['Base']]
};

return executeOperator(
Expand Down
43 changes: 39 additions & 4 deletions packages/base/src/panelview/objecttree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
IJupyterCadModel,
ISelection
} from '@jupytercad/schema';
import { ReactWidget } from '@jupyterlab/apputils';
import { Dialog, ReactWidget, showDialog } from '@jupyterlab/apputils';
import {
closeIcon,
LabIcon,
Expand Down Expand Up @@ -394,9 +394,44 @@ class ObjectTreeReact extends React.Component<IProps, IStates> {
className={'jp-ToolbarButtonComponent'}
onClick={() => {
const objectId = opts.node.parentId as string;
this.props.cpModel.jcadModel?.sharedModel.removeObjectByName(
objectId
);
const sharedModel =
this.props.cpModel.jcadModel?.sharedModel;
if (!sharedModel) {
return;
}

const dependants =
sharedModel.getDependants(objectId);

let body: React.JSX.Element;
if (dependants.length) {
body = (
<div>
{
'Removing this object will also result in removing:'
}
<ul>
{dependants.map(dependant => (
<li>{dependant}</li>
))}
</ul>
</div>
);
} else {
body = <div>Are you sure?</div>;
}

showDialog({
title: `Removing ${objectId}`,
body,
buttons: [Dialog.okButton(), Dialog.cancelButton()]
}).then(({ button: { accept } }) => {
if (accept) {
const toRemove = dependants.concat([objectId]);
sharedModel.removeObjects(toRemove);
}
});

this.props.cpModel.jcadModel?.syncSelected({});
}}
icon={closeIcon}
Expand Down
86 changes: 76 additions & 10 deletions packages/schema/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,48 @@ export class JupyterCadDoc
return undefined;
}

getDependants(name: string): string[] {
const dependants: string[] = [];
const dependantMap = new Map<string, Set<string>>();

for (const obj of this._objects) {
const deps: string[] = obj.get('dependencies') || [];
const objName = obj.get('name');
deps.forEach(dep => {
const currentSet = dependantMap.get(dep);
if (currentSet) {
currentSet.add(objName);
} else {
dependantMap.set(dep, new Set([objName]));
}
});
}
const selectedDeps = dependantMap.get(name);
if (!selectedDeps) {
return [];
}
while (selectedDeps.size) {
const depsList = [...selectedDeps];
depsList.forEach(it => {
dependants.push(it);
selectedDeps.delete(it);
dependantMap.get(it)?.forEach(newIt => selectedDeps.add(newIt));
});
}

return dependants;
}

removeObjects(names: string[]): void {
this.transact(() => {
for (const name of names) {
this.removeObjectByName(name);
}
});
}

removeObjectByName(name: string): void {
// Get object index
let index = 0;
for (const obj of this._objects) {
if (obj.get('name') === name) {
Expand All @@ -91,15 +132,13 @@ export class JupyterCadDoc
}

if (this._objects.length > index) {
this.transact(() => {
this._objects.delete(index);
const guidata = this.getOption('guidata');
if (guidata) {
delete guidata[name];
this.setOption('guidata', guidata);
}
this.removeOutput(name);
});
this._objects.delete(index);
const guidata = this.getOption('guidata');
if (guidata) {
delete guidata[name];
this.setOption('guidata', guidata);
}
this.removeOutput(name);
}
}

Expand All @@ -124,7 +163,34 @@ export class JupyterCadDoc
if (!obj) {
return;
}
this.transact(() => obj.set(key, value));

this.transact(() => {
// Special case for changing parameters, we may need to update dependencies
console.log('update ', key);
if (key === 'parameters') {
switch (obj.get('shape')) {
case 'Part::Cut': {
obj.set('dependencies', [value['Base'], value['Tool']]);
break;
}
case 'Part::Extrusion':
case 'Part::Fillet':
case 'Part::Chamfer': {
obj.set('dependencies', [value['Base']]);
break;
}
case 'Part::MultiCommon':
case 'Part::MultiFuse': {
obj.set('dependencies', value['Shapes']);
break;
}
default:
break;
}
}

obj.set(key, value);
});
}

getOption(key: keyof IJCadOptions): IDict | undefined {
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ export interface IJupyterCadDoc extends YDocument<IJupyterCadDocChange> {

objectExists(name: string): boolean;
getObjectByName(name: string): IJCadObject | undefined;
removeObjects(names: string[]): void;
removeObjectByName(name: string): void;
addObject(value: IJCadObject): void;
addObjects(value: Array<IJCadObject>): void;
updateObjectByName(name: string, key: string, value: any): void;
getDependants(name: string): string[];

getOption(key: keyof IJCadOptions): IDict | undefined;
setOption(key: keyof IJCadOptions, value: IDict): void;
Expand Down
4 changes: 4 additions & 0 deletions ui-tests/tests/ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ test.describe('UI Test', () => {
.nth(1)
.click();

if (await page.getByRole('button', { name: 'Ok' }).isVisible()) {
await page.getByRole('button', { name: 'Ok' }).click();
}

await page
.getByRole('tablist', { name: 'main sidebar' })
.getByRole('tab', { name: 'JupyterCad Control Panel' })
Expand Down

0 comments on commit dabfb28

Please sign in to comment.