Skip to content

Commit

Permalink
Many instance and keydown listeners bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsfy committed Jun 29, 2016
1 parent 861fd9c commit df01228
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
128 changes: 64 additions & 64 deletions cytoscape-clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
} // can't register if cytoscape unspecified


var _instance;
var initialized = false;
cytoscape('core', 'clipboard', function (opts) {
var cy = this;

Expand All @@ -22,28 +24,6 @@

$.extend(true, options, opts);

var ur;
var clipboard = {};
if (options.shortcuts.enabled) {
var undoable = options.shortcuts.undoable && (cy.undoRedo ? true : false);
if (undoable)
ur = cy.undoRedo({}, true);

document.addEventListener("keydown", function (e) {
if (e.ctrlKey)
if (e.which == 67) // CTRL + C
_instance.copy(cy.$(":selected"));
else if (e.which == 86) // CTRL + V
if (undoable)
ur.do("paste");
else
_instance.paste();
else if (e.which == 65){
cy.elements().select();
e.preventDefault();
}
});
}

var counter = 0;

Expand All @@ -58,20 +38,18 @@
s4() + '-' + s4() + s4() + s4();
}

var lastGuid;

function getItemId(last) {
return last ? "item_" + counter : "item_" + (++counter);
}

function getCloneId(last) {
if (!last)
lastGuid = guid();
return lastGuid;
function getCloneId() {
return guid();
}


var oldIdToNewId = {};

function changeIds(jsons) {
jsons = $.extend(true, [], jsons);
for (var i = 0; i < jsons.length; i++) {
Expand All @@ -86,11 +64,9 @@
var fields = ["source", "target", "parent"];
for (var k = 0; k < fields.length; k++) {
var field = fields[k];
if (json.data[field] && oldIdToNewId[json.data[field]]){
if (json.data[field] && oldIdToNewId[json.data[field]])
json.data[field] = oldIdToNewId[json.data[field]];
if (field == "parent")
console.log(json.data[field], oldIdToNewId[json.data[field]]);
}


}
if (json.position.x) {
Expand All @@ -103,45 +79,69 @@

}

if (!initialized) {
initialized = true;
var ur;
var clipboard = {};
if (options.shortcuts.enabled) {
var undoable = options.shortcuts.undoable && (cy.undoRedo ? true : false);
if (undoable)
ur = cy.undoRedo({}, true);

document.addEventListener("keydown", function (e) {
if (e.ctrlKey)
if (e.which == 67) // CTRL + C
_instance.copy(cy.$(":selected"));
else if (e.which == 86) // CTRL + V
if (undoable)
ur.do("paste");
else
_instance.paste();
else if (e.which == 65) {
cy.elements().select();
e.preventDefault();
}
});
}

var _instance = {
copy: function (eles, _id) {
var id = _id ? _id : getItemId();
eles.unselect();
var descs = eles.nodes().descendants();
var nodes = eles.nodes().union(descs).filter(":visible");
var edges = nodes.edgesWith(nodes).filter(":visible");

clipboard[id] = { nodes: nodes.jsons(), edges: edges.jsons() };
return id;
},
paste: function (_id) {
var id = _id ? _id : getItemId(true);
var res = cy.collection();
if (clipboard[id]) {
var nodes = changeIds(clipboard[id].nodes);
var edges = changeIds(clipboard[id].edges);
oldIdToNewId = {};
cy.batch(function () {
res = cy.add(nodes).union(cy.add(edges));
res.select();
});
_instance = {
copy: function (eles, _id) {
var id = _id ? _id : getItemId();
eles.unselect();
var descs = eles.nodes().descendants();
var nodes = eles.nodes().union(descs).filter(":visible");
var edges = nodes.edgesWith(nodes).filter(":visible");

clipboard[id] = {nodes: nodes.jsons(), edges: edges.jsons()};
return id;
},
paste: function (_id) {
var id = _id ? _id : getItemId(true);
var res = cy.collection();
if (clipboard[id]) {
var nodes = changeIds(clipboard[id].nodes);
var edges = changeIds(clipboard[id].edges);
oldIdToNewId = {};
cy.batch(function () {
res = cy.add(nodes).union(cy.add(edges));
res.select();
});

}
return res;
}
return res;
};

if (cy.undoRedo) {
ur = cy.undoRedo({}, true);
ur.action("paste", function (eles) {
return eles.firstTime ? _instance.paste(eles.id) : eles.restore();
}, function (eles) {
return eles.remove();
});
}
};

if (cy.undoRedo){
ur = cy.undoRedo({}, true);
ur.action("paste", function (eles) {
return eles.firstTime ? _instance.paste(eles.id) : eles.restore();
}, function (eles) {
return eles.remove();
});
}


return _instance; // chainability
});

Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gulp.task('default', [], function( next ){
});

gulp.task('publish', [], function( next ){
runSequence('confver', 'lint', 'pkgver', 'push', 'tag', 'npm', next);
runSequence('confver', 'lint', 'pkgver',/* 'push', 'tag',*/ 'npm', next);
});

gulp.task('confver', ['version'], function(){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cytoscape-clipboard",
"version": "1.0.0",
"version": "1.0.2",
"description": "Adds copy-paste utilities to cytoscape.",
"main": "cytoscape-clipboard.js",
"spm": {
Expand Down

0 comments on commit df01228

Please sign in to comment.