jerosoler / Drawflow

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

How to make the id starting index everytime an item dropped. #720

Open sumanthmadey opened 1 year ago

sumanthmadey commented 1 year ago

There is a id counter which keep incrementing for even new item that is dragged.

Editor.export() keep returning items with incremeted id carried from previous diagram.

These ID's are nested inside drawflow==> Home ==>data{1:{id:1, name},2:{id:2,name}}

Next diagram.. the count of id are starting from 2 and it keep on going

Is there anyway we can get id count reset

jerosoler commented 1 year ago

Hello,

I could do something like that.

editor.changeModule = function (name) {    
        this.module = name;
        this.precanvas.innerHTML = "";
        this.canvas_x = 0;
        this.canvas_y = 0;
        this.pos_x = 0;
        this.pos_y = 0;
        this.mouse_x = 0;
        this.mouse_y = 0;
        this.zoom = 1;
        this.zoom_last_value = 1;
        this.precanvas.style.transform = '';
        this.import(this.drawflow, false);
        this.dispatch('moduleChanged', name);
    }

    editor.on("moduleChanged", (name) => {
        let lastNode = 0;
        Object.entries(editor.drawflow.drawflow[name].data).forEach((item) => {
            console.log(item);
            lastNode = item[0];
        });
        editor.nodeId = Number(lastNode) + 1;
    })

Although the numbers will not always be correlative.

Since if you delete any node this will create a jump.