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

Delete key binding #46

Closed
martinRenou opened this issue Dec 5, 2022 · 3 comments · Fixed by #388
Closed

Delete key binding #46

martinRenou opened this issue Dec 5, 2022 · 3 comments · Fixed by #388
Labels
enhancement New feature or request

Comments

@martinRenou
Copy link
Member

Add a Delete key binding for removing shapes

@martinRenou martinRenou added the enhancement New feature or request label Dec 5, 2022
@arjxn-py
Copy link
Member

Hi, I did some workaround to try having a (Ctrl + x) key binding for this -

  componentDidMount() {
    document.addEventListener('keydown', this.handleKeyDown);
  }

  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyDown);
  }

  handleKeyDown = (event: KeyboardEvent) => {
    if (event.ctrlKey && event.key === 'x') {
      this.removeSelectedObject();
    }
  };

  removeSelectedObject = () => {
    const { selectedNodes } = this.state;
    if (selectedNodes.length === 0) {return;}

    const objectId = selectedNodes[0]; // Assuming only one object is selected

    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 key={dependant}>{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({});
  };

And it works, I just copied the body of onClick() inside a function removeSelectedObject, of the remove object button in the ObjectTree component & invoking that on KeyboardEvent. Again this is a rough approach which doesn't follow DRY principle yet but I can work on standardizing the same if this is what is expected by the delete key binding.

@trungleduc
Copy link
Member

you might want to take a look at this PR geojupyter/jupytergis#58. jupytergis and jupytercad share a lot of codebase

@arjxn-py
Copy link
Member

Thanks, this helps a lot.

arjxn-py added a commit to arjxn-py/JupyterCAD that referenced this issue Jul 27, 2024
arjxn-py added a commit to arjxn-py/JupyterCAD that referenced this issue Jul 27, 2024
martinRenou pushed a commit that referenced this issue Jul 30, 2024
* #46 Define function to `handleRemoveObject`

* #46 Bring `handleRemoveObject` in commands

* `Ctrl` + `X` keybinding to remove object

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

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

* Add handling if no object is selected

* `[Delete]` Keybinding & Enable it on ObjectTree too

* [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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants