-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from flowhub/clipboard-noglobal
Use proper CommonJS for clipboard code
- Loading branch information
Showing
6 changed files
with
145 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
## dev | ||
|
||
## 0.7.0 (unreleased) | ||
|
||
|
||
## 0.7.0 (2017 March 2) | ||
|
||
Breaking changes | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
var clipboardContent = {}; // XXX: hidden state | ||
|
||
function cloneObject(obj) { | ||
return JSON.parse(JSON.stringify(obj)); | ||
} | ||
|
||
function makeNewId(label) { | ||
var num = 60466176; // 36^5 | ||
num = Math.floor(Math.random() * num); | ||
var id = label + '_' + num.toString(36); | ||
return id; | ||
} | ||
|
||
function copy(graph, keys) { | ||
//Duplicate all the nodes before putting them in clipboard | ||
//this will make this work also with cut/Paste and once we | ||
//decide if/how we will implement cross-document copy&paste will work there too | ||
clipboardContent = {nodes:[], edges:[]}; | ||
var map = {}; | ||
var i, len; | ||
for (i = 0, len = keys.length; i < len; i++) { | ||
var node = graph.getNode(keys[i]); | ||
var newNode = cloneObject(node); | ||
newNode.id = makeNewId(node.component); | ||
clipboardContent.nodes.push(newNode); | ||
map[node.id] = newNode.id; | ||
} | ||
for (i = 0, len = graph.edges.length; i < len; i++) { | ||
var edge = graph.edges[i]; | ||
var fromNode = edge.from.node; | ||
var toNode = edge.to.node; | ||
if (map.hasOwnProperty(fromNode) && map.hasOwnProperty(toNode)) { | ||
var newEdge = cloneObject(edge); | ||
newEdge.from.node = map[fromNode]; | ||
newEdge.to.node = map[toNode]; | ||
clipboardContent.edges.push(newEdge); | ||
} | ||
} | ||
|
||
} | ||
|
||
function paste(graph) { | ||
var map = {}; | ||
var pasted = {nodes:[], edges:[]}; | ||
var i, len; | ||
for (i = 0, len = clipboardContent.nodes.length; i < len; i++) { | ||
var node = clipboardContent.nodes[i]; | ||
var meta = cloneObject(node.metadata); | ||
meta.x += 36; | ||
meta.y += 36; | ||
var newNode = graph.addNode(makeNewId(node.component), node.component, meta); | ||
map[node.id] = newNode.id; | ||
pasted.nodes.push(newNode); | ||
} | ||
for (i = 0, len = clipboardContent.edges.length; i < len; i++) { | ||
var edge = clipboardContent.edges[i]; | ||
var newEdgeMeta = cloneObject(edge.metadata); | ||
var newEdge; | ||
if (edge.from.hasOwnProperty('index') || edge.to.hasOwnProperty('index')) { | ||
// One or both ports are addressable | ||
var fromIndex = edge.from.index || null; | ||
var toIndex = edge.to.index || null; | ||
newEdge = graph.addEdgeIndex(map[edge.from.node], edge.from.port, fromIndex, map[edge.to.node], edge.to.port, toIndex, newEdgeMeta); | ||
} else { | ||
newEdge = graph.addEdge(map[edge.from.node], edge.from.port, map[edge.to.node], edge.to.port, newEdgeMeta); | ||
} | ||
pasted.edges.push(newEdge); | ||
} | ||
return pasted; | ||
} | ||
|
||
module.exports = { | ||
copy: copy, | ||
paste: paste, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.