My custom rule for elements.delete is being run when elements are clicked as well as when they are deleted

I created a custom rule to prevent elements from being deleted.

It is based off of this code and this repo: vite-vue-bpmn-process/src/additional-modules/Rules/CustomRules.ts at ba2b936157e35af86fa4e1b628b41bafd7dd4bf5 · moon-studio/vite-vue-bpmn-process · GitHub

It works correctly as far as preventing the deletion. I want to provide feedback to the user when they attempt to delete something and cannot, and the problem is that the logic inside my custom rule is executed, not only when a user actually attempts to delete an element (wanted) but also when a user has just clicked/selected any element.

Is this supposed to happen? Currently I cannot figure out how to differentiate, inside my custom rule, whether a user is actually attempting to delete an element (with the DEL keyboard key) or if they just selected an element.

Would really appreciate some insight on how to execute something inside my rule ONLY when a user is actually attempting to delete an element with the keyboard.

Many thanks!

Did you reach out to the maintainer of that extension? If you’d like us to support on a case, please provide a minimal code sandbox that allows us to reproduce your issue.

Selection an deletion are two entirely different things. What you’d want to do is to figure out the chain of action that leads to deletion, i.e. by examining the executions stack trace.

The “delete” in ContextPadProvider needs to be shown or not by Rules.

// bpmn-js/lib/features/context-pad/ContextPadProvider.js
  // delete element entry, only show if allowed by rules
  var deleteAllowed = rules.allowed('elements.delete', { elements: [ element ] });

  if (isArray(deleteAllowed)) {

    // was the element returned as a deletion candidate?
    deleteAllowed = deleteAllowed[0] === element;
  }

  if (deleteAllowed) {
    assign(actions, {
      'delete': {
        group: 'edit',
        className: 'bpmn-icon-trash',
        title: translate('Remove'),
        action: {
          click: removeElement
        }
      }
    });
  }