How to override the keyboard copy function?

I want to use keyboard to copy and past . The default Ctrl+c and Ctrl+v Copy function is copy all attributes.But I just want to copy some attributes what I need . For example , there are a NodeDataArray:

[ { “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:330, “stockNo”:""} ]

When I copy the node use ctrl+c and ctrl+v , the new NodeDataArray is :

[ { “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:330, “stockNo”:""},
{ “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:330, “stockNo”:""} ]

You can see the json are same . But Iin this way , I can’t update those node into dataBase because the two node’s Id is same!I need to override the keyboard copy event and don’t copy the “id” attribute.

By the way,I had override the deleteEnent:

        // notice whenever the selection may have changed
        myDiagram.addDiagramListener("ChangedSelection", function(e) {
            enableAll();
        });
        // notice when the Paste command may need to be reenabled
        myDiagram.addDiagramListener("ClipboardChanged", function(e) {
            enableAll();
        });
        // notice whenever a transaction or undo/redo has occurred
        myDiagram.addModelChangedListener(function(e) {
            if (e.isTransactionFinished) enableAll();
        });
function enableAll() {
        var cmdhnd = myDiagram.commandHandler;
        var delFlag = false;
        cmdhnd.canDeleteSelection = function () {
            //to do my delete ajax and something
           //return boolean
        };
    }

I know the return value of canDeleteSelection is boolean .But what is the “copySelection()” and the "copyToClipboard(coll)"function’s return value? And how to override it?

There’s something wrong in your app – by default each Model will ensure that the key for each of its node data is unique within the model.

For example, open up the Flow Chart sample, https://gojs.net/latest/samples/flowchart.html, select all but one node and delete, then “Save” the model so that you can see the data in the textarea below. For example:

{ "class": "go.GraphLinksModel",
  "linkFromPortIdProperty": "fromPort",
  "linkToPortIdProperty": "toPort",
  "nodeDataArray": [ {"key":6, "loc":"175 440", "text":"Sprinkle nuts on top"} ],
  "linkDataArray": []}

Then select, copy, and paste that node, and “Save” again:

{ "class": "go.GraphLinksModel",
  "linkFromPortIdProperty": "fromPort",
  "linkToPortIdProperty": "toPort",
  "nodeDataArray": [ 
{"key":6, "loc":"175 440", "text":"Sprinkle nuts on top"},
{"key":-2, "loc":"175 440", "text":"Sprinkle nuts on top"}
 ],
  "linkDataArray": []}

Note that the key of the copied node is different, as one would expect. Yes, all of the other properties are copied faithfully, so in your case that would include the “id” property.

I don’t know how it’s possible that the node data in a model could have duplicate values for the key. Have you overridden any methods? In what manners are you modifying the Model.nodeDataArray or its data?

Back to your question:

If you want a different “id” property when a node data object is copied, you’ll need to supply a custom data copying function as the value of Model.copyNodeDataFunction.

OR, maybe the “id” property is really the unique identifier property, so you can just set Model.nodeKeyProperty to “id”, and completely avoid using a “key” property.

Your choice.

Hello Mr.Walter , thank for your answer.
In my Model,the unique value is KEY.There are a Error in my question,its my fail,sorry .
When I copy a Node , The NodeDataArray should be :

[ { “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:-330, “stockNo”:""},
{ “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:-331, “stockNo”:""} ]

When I copy a node ,the KEY value will decrement one. from “-330” to “-331” .The “id” isn’t the unique .
I want to do this :
here are a node like this :

[ { “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:-330, “stockNo”:""} ]

the first node ,it has a unique value : “key”:-330
when I copy it , I want the json is :

[ { “size”:"", “pos”:“705 -35”, “text2”:“0”, “id”:476, “category”:“shelf”, “key”:-330, “stockNo”:""},
{ “size”:"", “pos”:“705 -35”, “text2”:“0”, “category”:“shelf”, “key”:-331, “stockNo”:""} ]

don’t copy the “id” property.
So , I need a way to override the keyboard copy function so that I can don’t need copy the id property.

Model.copyNodeDataFunction is what you want to set to a function that makes a new object with exactly the properties and values that you want.

Thank you for your answer. I had use the Model.copyNodeDataFunction in this way :

    myDiagram.model.copyNodeDataFunction = function(obj,model){
        var a = {}; 
         a.stockNo= "stockNo";
         a.category= obj.category; 
         a.text2= obj.text2;
         a.pos= obj.pos;
         a.size= obj.size;
        return a;
    }

The “a” object is the new object . If I don’t give “a” the id property , the new node will not have the property.

A post was split to a new topic: Pasting data into diagram