Get current selected card id in canvas

how can i get the currently selected card in the canvas?

I’ve tried using

this.registerEvent(this.app.workspace.on('file-open', (file: TFile ) => {
   //file given doesn't contain the id of CanvasTextData within the CanvasNodeData of the canvas
  //all i can get is the name
  file.name 
  //the file name will either be the canvas ur currently open in, 
  //or if it's a CanvasFileData type you will get back the name of the file
}));

I wanted to be able to get the id of the current selected node in the canvas

the canvas file looks like this

{
  "nodes": [
    {
      "id": "fd54f4c821dbc4cc",
      "type": "file",
      "file": "mydocument.md",
      "x": -100,
      "y": -840,
      "width": 260,
      "height": 241
    },
    {
      "id": "AXfxt6LMbEHsolL3",
      "type": "text",
      "text": "random text for testing",
      "x": 280,
      "y": -840,
      "width": 420,
      "height": 420
    },
    {
      "id": "62Db8TU532JSyt2N",
      "type": "text",
      "text": "",
      "x": 280,
      "y": -360,
      "width": 420,
      "height": 420
    }
  ],
  "edges": [],
  "metadata": {}
}

i feel like im looking for a needle in a haystack, surely there must be a method on either he workspace or workspaceleaf that will give me the id of the currently selected node in the canvas??

1 Like

I figured it out
I guess the api docs haven’t been updated with the canvas? i have no idea
but u can get the canvas selection like so

const canvasView = this.app.workspace.getActiveViewOfType(ItemView);
if (canvasView?.getViewType() !== 'canvas') return;
const canvas = (canvasView as any).canvas;
const selection: any = Array.from(canvas.selection);

which will give you an array of all the selected nodes, with their corresponding id’s.

1 Like

Just saw your post and it got me thinking. I think that to get the currently selected card ID in the canvas, you can access the active view and then get the selection. This will give you a list of all the selected nodes and their IDs.

If you need to verify documents or IDs for your projects, try www.idanalyzer.com. They have an API that helps verify the authenticity of documents and IDs, which can be really useful for many tasks.