jerosoler / Drawflow

Simple flow library 🖥️🖱️
https://jerosoler.github.io/Drawflow/
MIT License
4.7k stars 728 forks source link

[FEATURE-REQUEST] Method to select a node #643

Open psociety opened 1 year ago

psociety commented 1 year ago

Thank you for creating drawflow!

I would like to select a node but i can't find a method to do it :(

It would be cool to be able to do editor.selectNode(NODE_ID).

jerosoler commented 1 year ago

You can use this method:

Or use:

editor.selectNode = function(id) {
      if(this.node_selected != null) {
          this.node_selected.classList.remove("selected");
          if(this.node_selected != this.ele_selected) {
            this.dispatch('nodeUnselected', true);
          }
        }
      const element = document.querySelector(`#node-${id}`);
      this.ele_selected = element;
      this.node_selected = element;
      this.node_selected.classList.add("selected");
      if(this.node_selected != this.ele_selected) {
          this.dispatch('nodeSelected', this.ele_selected.id.slice(5));
        }
    }
    editor.selectNode(6);
psociety commented 1 year ago

I'm also doing a similar workaround:

// @ts-ignore
this.editor.click({
    target: block.$html
})

But i thought it wasn't a good idea to do it that way xD

eviltik commented 1 year ago

You can use this method:

Or use:

editor.selectNode = function(id) {
      if(this.node_selected != null) {
          this.node_selected.classList.remove("selected");
          if(this.node_selected != this.ele_selected) {
            this.dispatch('nodeUnselected', true);
          }
        }
      const element = document.querySelector(`#node-${id}`);
      this.ele_selected = element;
      this.node_selected = element;
      this.node_selected.classList.add("selected");
      if(this.node_selected != this.ele_selected) {
          this.dispatch('nodeSelected', this.ele_selected.id.slice(5));
        }
    }
    editor.selectNode(6);

My 2 cents:

this.node_selected should be "initialized" after this.el_selected comparaison, i.e

this.ele_selected = element;
if(this.node_selected != this.ele_selected) {
  this.node_selected = element;
  this.node_selected.classList.add('selected');
  this.dispatch('nodeSelected', this.ele_selected.id.slice(5));
}