Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Creating node id in svg node (Read 3373 times)
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Creating node id in svg node
Jan 27th, 2022 at 6:52am
Print Post  
hello, it's me again, i have a question about textfield in mindfusion, as you know all of my nodes are svg and a text can be inputted into the node, now is it possible to have a dedicated text field to display the node id and then another field to display the content of the node, something like the picture below
  

Screenshot_23.png ( 34 KB | 92 Downloads )
Screenshot_23.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #1 - Jan 27th, 2022 at 8:12am
Print Post  
Hi,

You have couple of options.

1. Customize SvgNode rendering code to add an extra MindFusion.Drawing.Text instance as shown in following threads. In your case change SvgNode.prototype instead of ShapeNode.prototype -

https://mindfusion.eu/Forum/YaBB.pl?num=1488530760/4#4
https://mindfusion.eu/Forum/YaBB.pl?num=1435322031/0#0

2. Attach a small helper ShapeNode to the SvgNode and display the id text there. Set helper's Transparent property to hide the geometry and leave only text visible. Set Locked property to prevent users from selecting it.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Re: Creating node id in svg node
Reply #2 - Jan 27th, 2022 at 8:38am
Print Post  
so the example clone the existing bound, how do i set the bound of x and y relative to the existing node, or to be clear how do i set it to be on the top left of the node
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #3 - Jan 27th, 2022 at 8:51am
Print Post  
You could keep the cloned bounds but change extraText's alignments to Near/Near for top/left. Otherwise change clonedRect.width/height values.
  
Back to top
 
IP Logged
 
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Re: Creating node id in svg node
Reply #4 - Jan 28th, 2022 at 1:50am
Print Post  
sorry, i was trying to put in in my loop but it gave me an error, is this correct?

Code (Javascript)
Select All
Object.values(nodeDict).forEach((dict) => {
      const node = new mf.Diagramming.SvgNode();
      var Alignment = mf.Diagramming.Alignment;
      var Text = mf.Drawing.Text;

      node.prototype.onUpdateVisuals = function () {
        var content = this.graphicsContainer.content;

        var extraText = new Text(
          "Id", // replace with this.yourFieldName
          this.getBounds().clone()
        );
        extraText.textAlignment = Alignment.Center;
        extraText.lineAlignment = Alignment.Far;
        extraText.fitInBounds = true;
        extraText.font = this.getEffectiveFont();
        content.push(extraText);
      };
      // var Size = new mf.Drawing.Size(49, 23);
      // node.setBounds(new cm.Drawing.Rect(25, 15, 42, 25));
      node.setContent(dict.content);
      node.setImageAlign(mf.Diagramming.ImageAlign.Fit);
      node.image.svg = false;
      node.image.image.width = node.image.image.naturalWidth;
      node.image.image.height = node.image.image.naturalHeight;
      node.brush = "white";
      node.setTransparent(true);
      shapeNodes.push(node);
      shapeIds.push(dict.name);
      node.setAnchorPattern(pattern);

      // console.log(node);
    });
 



  

Screenshot_24_001.png ( 19 KB | 102 Downloads )
Screenshot_24_001.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #5 - Jan 28th, 2022 at 7:09am
Print Post  
If you need instance-specific override, set node.onUpdateVisuals without the prototype part. Otherwise assign the function to "SvgNode.prototype" using static access, and move it outside the loop.
  
Back to top
 
IP Logged
 
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Re: Creating node id in svg node
Reply #6 - Jan 29th, 2022 at 12:47pm
Print Post  
or another alternative that i can use is using the default Text field and loading the node id there so it can be edited, and for that i want to know how to edit the text bound and alignment

Code (Javascript)
Select All
nodes.forEach((node) => {
...
node.setText(idNum)
...
}

 



using the node.setText(value) will set the text to be in the center and middle of the node, but i need to initialize the text to be on the top left of the node and the rect/bound of the text field into something like the highlighted part

Code (Javascript)
Select All
 var Alignment = mf.Diagramming.Alignment;
            var Text = mf.Drawing.Text;
            var content = this.graphicsContainer.content;
var rect = this.getBounds().clone();

            rect.width = this.getEffectiveFontSize();
            rect.x += rect.width * 1;
            var extraText = new Text(
              "G" + Nomer, // replace with this.yourFieldName
              rect // this.getBounds().clone()
            );
            extraText.textAlignment = Alignment.Center;
            extraText.lineAlignment = Alignment.Near;
            extraText.fitInBounds = true;
            extraText.font = this.getEffectiveFont();
            content.push(extraText);
 



wich is esentially just to make the text to be on the top left but it start at the specified x axis of the bound
« Last Edit: Jan 30th, 2022 at 6:45am by Jessenberg »  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #7 - Jan 31st, 2022 at 7:34am
Print Post  
You'll have hard time centering your main text if you display id through it too, especially if you let users resize nodes.

You've edited out question about editing extraText in place, that could look like this -

Code
Select All
var originalGetObjectToEdit = SvgNode.prototype.getObjectToEdit;
SvgNode.prototype.getObjectToEdit = function(point, element) {
	if (point.x < this.bounds.x + 20 && point.y < this.bounds.y + 20)
		return new IdEditor(this);
	return originalGetObjectToEdit.apply(this, [point, element]);
}

class IdEditor {

	constructor(node) {
		this.node = node;
	}

	getTextToEdit() {
		return this.node.idText;
	}

	getEditRect(point) {
		var rect = this.node.bounds.clone();
		rect.width = 20;
		rect.height = 20;
		return rect;
	}

	createEditArgs(oldText, newText) {
		return new NodeEventArgs({ node: this.node, oldText: oldText, newText: newText });
	}

	setEditedText(newText) {
		this.node.idText = newText;
		this.node.invalidate();
	}

	getDiagramItem() {
		return this.node;
	}
} 



assuming idText custom field stores the text displayed by extraText object.

You can add some padding around extraText by setting extraText.padding.left and extraText.padding.top.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #8 - Jan 31st, 2022 at 7:42am
Print Post  
There was also edited out question about automatic id assignment. You could store a current id counter in diagram.tag, setting it to 0 when a new diagram is created. Then when you create node from code or handle user-created one in event handler, set this -

Code
Select All
// replace idText with your custom field name
node.idText = String(diagram.tag++); 



Diagram.tag is convenient because it's saved automatically in JSON when you serialize. Otherwise you could create a custom Diagram.nodeIdCounter field for storing next id, but would have to take care of its serialization.
  
Back to top
 
IP Logged
 
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Re: Creating node id in svg node
Reply #9 - Jan 31st, 2022 at 8:04am
Print Post  
i was thinking of using the text field because it's much easier to translate since i can just replace the string id from c to g using string.replace() while keeping the rest of the text and also the main text will begin where the id begin so it has the same allignment so i just need to set the text to begin at a certain x bound, this will be much faster because i need to present the functionality of my app first then give it a final tweak after it's approved, but if you're suggesting that way then where should i put it?

this is where i put the previous code:
Code (Javascript)
Select All
diagramId(diagram, args) {
      // var idNum = 1;
      var node = args.getNode();

      node.onUpdateVisuals = function () {
        var Alignment = mf.Diagramming.Alignment;
        var Text = mf.Drawing.Text;
        var content = this.graphicsContainer.content;
        var rect = this.getBounds().clone();

        if (node.content.fileName == "/static/media/Claim.0d97347d.svg") {
          rect.width = this.getEffectiveFontSize();
          rect.x += rect.width * 3.1;
          var extraText = new Text(
            "C" + Nomer, // replace with this.yourFieldName
            rect // this.getBounds().clone()
          );
        } else {
          rect.width = this.getEffectiveFontSize();
          rect.x += rect.width * 3.1;
          var extraText = new Text(
            "C", // replace with this.yourFieldName.1
            rect // this.getBounds().clone()
          );
        }

        extraText.textAlignment = Alignment.Center;
        extraText.lineAlignment = Alignment.Near;
        extraText.fitInBounds = true;
        extraText.font = this.getEffectiveFont();
        content.push(extraText);
        // idNum += 1;
      };
      Nomer += 1;
    }
 



that method is called when a node a created. i am fairly new in using react so i get confused a lot while learning new things

  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #10 - Jan 31st, 2022 at 8:36am
Print Post  
You can set main text's offset using DiagramItem.textPadding property / setTextPadding method in V3; that will effectively set X coordinate if text is also left-aligned.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Re: Creating node id in svg node
Reply #11 - Feb 1st, 2022 at 3:23am
Print Post  
thank you for your answer, i will apply that first then move on to your previous solution.

but i do have a question about removing nodes from the diagram

Code (Javascript)
Select All
        var nodes = [...this.state.diagram.nodes];
        var links = [...this.state.diagram.links];
        nodes.forEach((node) => {
          // var al = node.getText();
          this.state.diagram.removeItem(node);
          // console.log(node.getBounds());
          // console.log(node.content.fileName);
        });
        console.log(nodes);
 



the code above are used to remove the node from the diagram, while the node is removed from the diagram i can still log the previously deleted nodes wich hinder some funtionality, i don't know how i did it but i once managed to delete all of the nodes array without passing anything back when i log it but that code was lost and i haven't back it up yet, what can i do to achieve that again, thank you in advance
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #12 - Feb 1st, 2022 at 9:11am
Print Post  
You could loop to get your values and call diagram.clearAll after the loop. Foreach won't work correctly if you modify the array during loop's iteration (which removeItem does internally), but you could do a backwards loop from nodes.count - 1 to 0 if you prefer calling remove for each node.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Jessenberg
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 27
Joined: Dec 20th, 2021
Re: Creating node id in svg node
Reply #13 - Feb 2nd, 2022 at 5:07am
Print Post  
it's still logging the deleted node,

so the previous code that i send was for graph validation and if the graph is invalid then all of it will be deleted. now when i press a button, i want to initiate the validation and then the tranformation process. but since the nodes array still contain the invalid graph node then the invalid graph will be translated. if the validation and translation are on different button then it works fine but i want to automate it to make it simpler.

the code that i tried :

Code (Javascript)
Select All
const validation = () => {


        var nodes = [...this.state.diagram.nodes];
        var links = [...this.state.diagram.links];

        //Tried this

        // this.state.diagram.clearAll();

        // And this

        for (var i = nodes.length - 1; i >= 0; i--) {
           this.state.diagram.removeItem(nodes[i]);
           console.log('node deleted'
         }
        console.log(nodes);
        links.forEach((link) => {});
        // console.log(nodes.content.fileName);
      };
 



Code (Javascript)
Select All
const CAEToGSN = () => {
 const nodes = [...this.state.diagram.nodes];
 const links = [...this.state.diagram.links];
 nodes.forEach((node) => {
 validation()

}
 nodes.forEach((node) => {
 //....Transformation Process

}
}

 



but they still log the deleted node, i think it might be bacause of the state? but i can't really figure out how to work around it.

so below is a couple image for reference, the first is the validation log, the second is the nodes before i translate and the third one is after i translate, when a node is deleted the link got deleted but not the node, i'm confused
  

Screenshot_4_001.png ( 11 KB | 96 Downloads )
Screenshot_4_001.png
Screenshot_7.png ( 9 KB | 90 Downloads )
Screenshot_7.png
Screenshot_13.png ( 25 KB | 93 Downloads )
Screenshot_13.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Creating node id in svg node
Reply #14 - Feb 2nd, 2022 at 8:15am
Print Post  
If you mean console.log(nodes) shows an invalid node of yours that was removed from the diagram, it's because nodes is a copy of the diagram.nodes array created before calling removeItem. If you call console.log(state.diagram.nodes), it should no longer show the removed node.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint