Trigger proper TFile file-menu

In my plugin, in the nav tree, in some scenarios, I put the MD file HTML element inside the folder HTML element. However, when I right-click on the MD file element, it propagates and triggers the folder’s ‘context-menu’. I used this script to fix it, But I lost the original MD file’s ‘context-menu’ features like rename or delete.

toMove.addEventListener("contextmenu", (e) => {
      e.stopPropagation(); // Prevent event from bubbling up
      e.preventDefault(); // Prevent the browser's default context menu

      const file = plugin.app.vault.getAbstractFileByPath(dataPath as string);
      if (file instanceof TFile) {
        // Create a new Menu object
        const menu = new Menu();

        // Trigger the 'file-menu' event
        //@ts-ignore
        plugin.app.workspace.trigger("file-menu", menu, file, "file-explorer");

        // Show the menu at the mouse position
        menu.showAtMouseEvent(e);
      }
    });

How to trigger the original file’s menu , or how to trigger proper TFile file-menu menu ?

Use this event instead of hooking into the DOM event: on('file-menu') - Developer Documentation

This is my problem , when I right-click on the MD file element, it propagates and triggers the folder’s ‘context-menu’.

The default context menu should not be triggered in this case, can you share the full code of your plugin?

plugin repo :GitHub - lorens-osman-dev/cluster: Cluster plugin mainly designed to simplify the process of note clustering on mobile devices and work well on PCs ether

the file that triggers file-menu :cluster/src/navtree/naveTree.ts at master · lorens-osman-dev/cluster · GitHub

If you want to reproduce:

  • install cluster plugin
  • make new cluster
  • make new child to the cluster ‘foo’
  • make new child to ‘foo’ with ‘bar’ name

The plugin will create folder with ‘foo’ name inside it ‘bar.md’ file but in nav Tree i moved ‘foo.md’ html element to be inside ‘foo’ folder element with yellow color .