Paste in one diagram from another diagram

I am trying to paste from one diagram to another diagram.When i am pasting in same diagram, different guid is coming for the pasted node.but when coping in different diagram, same guid is coming.But i want different guid.

Set Model.copyNodeDataFunction on each of your models to be a function that copies the properties you want and also makes sure each “key” or “id” is a unique GUID.

I tried Model.copyNodeDataFunction.But when i am droping a node form pallet to diagram,that time also this method is calling.
So getting some problem.When i am droping a node 2nd time from pallet to diagram, getting error.Can u tell me what should i write inside the mathod.
According my problem,I am using model.makeNodeDataKeyUnique to create guid for node.So my understanding is that,
when i am pasting in same diagram,as the guid exists,it is creating new guid.But when pasting in different diagram,as the guid
not exists in that diagram,so taking the same guid.
So, I tried to change the guid in ClipboardPasted event using setKeyForNodeData.But not working.
Is there any way that before pasting we can change the guid of the node(in pasteSelection or pasteFromClipboard or any other method) .

When Model.copyNodeDataFunction is null, the default value, the model will make a shallow copy of a node data object. That means the key will be the same.

I was suggesting that that provide a custom object copying function that makes sure the copied key is always a new GUID. Then there could never be any conflict with keys.

I overrided customhandler.copySelection method where i was changing the guid of the node.But if i change the guid, it aslo changing the guid of the main node.
So i tryed like :
public pasteFromClipboard() {
var collNode = super.pasteFromClipboard();
collNode.each(e => {
e.data[“uuid”] = NewGuid.generateGuid();//create guid
});
return collNode;
}
this is working i.e after pasting getting new guid.But for link and group some issues are coming.Is that a correct way??

I don’t know if that is safe – it depends on whether there is any code that depends on the “uuid” property in the meantime. It would be safer if you called Model.setDataProperty, in case there are any Bindings on the “uuid” property.

But I still think it would be most safe to do what I suggested. That way you can be sure every new node data object will have a unique value for “uuid”.

I used copyNodeDataFunction.But is calling two times only.When i am droping a node from pallet to diagram and when am pasting.Not at the time of coping.

The user can paste many times without modifying the clipboard, so it is important to do the correct copying for each paste.

If you really want to have the copy happen at the time of the copy to the clipboard, set CommandHandler.copiesClipboardData to true. We intend to change the default value of this undocumented property to true for version 2.0, whenever that happens.

Hi Walter,
I am also facing the same issue.

Instead of setting key using Model.setDataProperty any other way is there to create new Guid for node.

I am also tried to use copyNodeDataFunction but i when i dropped second object I am getting null reference exception.
And also I am not able to find CommandHandler.copiesClipboardData anywhere in typescript version go.d.ts.

Can you suggest me how to create new Guid for pasted objects

That’s what Model | GoJS API is for.

Although I suppose you could implement generating the GUID in copyNodeDataFunction,

I am already using Model.makeUniqueKeyFunction for generating GUID.

But while pasting the node from one diagram to another diagram, nodes taking same GUID.

I am also tried to override copyNodeDataFunction but when i dropped second object from palette I am getting null reference exception.

Can you give some snippet on forcing the gojs to generate UUID in copyNodeDataFunction.

In your copying function that is the value of Model.copyNodeDataFunction, just set the key to be the result of creating a new GUID string and copy all of the other properties that you want to be copied.

I am using copyNodeDataFunction as this:

myDiagram.model.copyNodeDataFunction = function (object, mode) {
var shallowCopiedObject = JSON.parse(JSON.stringify(object))
shallowCopiedObject.uuid = Guid.generateUUID();
return shallowCopiedObject;
}

If we are using copyNodeDataFunction then no need for makeUniqueKeyFunction right?

Yes, that’s basically right. But copying by stringify-and-parse is a very inefficient way to go.

If you can ensure that there is never any node data object added to the model with no key or with a duplicate key (data.uuid property), then I suppose there’s no need for Model.makeUniqueKeyFunction. But I would keep it just to be safe.

Hi Walter,

While copy pasting nodes and links between the diagram, Is that possible restrict to generate GUID?.
Like In one cases need to create new GUID and another case no need to create new GUID for nodes and links.

Example code:

myDiagram.model.copyNodeDataFunction = function (object, model) {

if(createNewGuid){
	var shallowCopiedObject = JSON.parse(JSON.stringify(object))
	shallowCopiedObject.uuid = Guid.generateUUID();
	return shallowCopiedObject;
}else{
	return object;
}	

}

myDiagram.model.copyLinkDataFunction = function (object, model) {

if(createNewGuid){
	var shallowCopiedObject = JSON.parse(JSON.stringify(object))
	shallowCopiedObject.uuid = Guid.generateUUID();
	return shallowCopiedObject;
}else{
	return object;
}	

}

Does this would cause any other issues when we restrict to create new GUID for nodes/links?

I believe that JSON.stringify will include the “__gohashid” property, which you must not read or write, and that it will not do the right serialization for a number of kinds of values and objects. So that will trip you up when trying to copy an object.

A unique identifier is required for every node data object in the model, so if the value of Model.nodeKeyProperty is “uuid” and you try to add a node data object with a duplicate GUID, it will replace it with a new identifier which might not satisfy your requirements.

I thinks this should work as expected :

myDiagram.model.copyNodeDataFunction = function (object, model) {

if(createNewGuid){	
	var newObject = {}
	Object.keys(object).forEach(function(itm){
		if(itm != "__gohashid" && itm !="uuid" && itm !="key")
			newObject[itm] = object[itm]
	});
	return newObject;
}else{
	return object;

}

}

myDiagram.model.copyLinkDataFunction = function (object, model) {

if(createNewGuid){
	var newObject = {}
	Object.keys(object).forEach(function(itm){
		if(itm != "__gohashid" && itm !="uuid" && itm !="key")
			newObject[itm] = object[itm]
	});
	return newObject;
}else{
	return object;	
}

}

That looks about right for your app.