Skip to content

Commit

Permalink
Merge pull request #346 from flowhub/clipboard-noglobal
Browse files Browse the repository at this point in the history
Use proper CommonJS for clipboard code
  • Loading branch information
jonnor authored Apr 19, 2017
2 parents e156e43 + 6e5c073 commit 1ad37ac
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 87 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
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

Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ require("./the-graph/the-graph-iip.js").register(g);
require("./the-graph/the-graph-group.js").register(g);
require("./the-graph/the-graph-tooltip.js").register(g);
require("./the-graph/the-graph-menu.js").register(g);
require("./the-graph/the-graph-clipboard.js").register(g);
require("./the-graph/font-awesome-unicode-map.js").register(g);

g.TheGraph.thumb = require('./the-graph-thumb/the-graph-thumb.js');

g.TheGraph.nav = require('./the-graph-nav/the-graph-nav.js');

g.TheGraph.autolayout = require('./the-graph/the-graph-autolayout.js');
g.TheGraph.library = require('./the-graph/the-graph-library.js');
g.TheGraph.editor = require('./the-graph-editor/the-graph-editor.js');

g.TheGraph.clipboard = require("./the-graph-editor/clipboard.js");
g.TheGraph.editor = require('./the-graph-editor/menus.js');

module.exports = g.TheGraph;
76 changes: 76 additions & 0 deletions the-graph-editor/clipboard.js
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,
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

var Clipboard = require('./clipboard');

// Returns a new datastructure to prevent accidental sharing between diffent editor instances
function getDefaultMenus(editor) {

// FIXME: provide a proper interface for actions to manipulate section, remove @editor
var pasteAction = function (graph, itemKey, item) {
var pasted = TheGraph.Clipboard.paste(graph);
var pasted = Clipboard.paste(graph);
this.selectedNodes = pasted.nodes;
this.selectedEdges = [];
}.bind(editor);
Expand All @@ -29,7 +31,7 @@ function getDefaultMenus(editor) {
this.selectedNodes = newSelection;
}.bind(editor),
copy: function (graph, itemKey, item) {
TheGraph.Clipboard.copy(graph, [itemKey]);
Clipboard.copy(graph, [itemKey]);
}
}, edgeActions = {
delete: function (graph, itemKey, item) {
Expand Down Expand Up @@ -161,7 +163,7 @@ function getDefaultMenus(editor) {
icon: "copy",
iconLabel: "copy",
action: function (graph, itemKey, item) {
TheGraph.Clipboard.copy(graph, item.nodes);
Clipboard.copy(graph, item.nodes);
}
},
e4: pasteMenu
Expand Down
56 changes: 56 additions & 0 deletions the-graph/font-awesome-unicode-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports.register = function (context) {

context.TheGraph.FONT_AWESOME = {
"500px": "",
"address-book": "",
"address-book-o": "",
"address-card": "",
"address-card-o": "",
"adjust": "",
"adn": "",
"align-center": "",
Expand Down Expand Up @@ -54,11 +58,15 @@ context.TheGraph.FONT_AWESOME = {
"backward": "",
"balance-scale": "",
"ban": "",
"bandcamp": "",
"bank": "",
"bar-chart": "",
"bar-chart-o": "",
"barcode": "",
"bars": "",
"bath": "",
"bathtub": "",
"battery": "",
"battery-0": "",
"battery-1": "",
"battery-2": "",
Expand Down Expand Up @@ -208,19 +216,25 @@ context.TheGraph.FONT_AWESOME = {
"dot-circle-o": "",
"download": "",
"dribbble": "",
"drivers-license": "",
"drivers-license-o": "",
"dropbox": "",
"drupal": "",
"edge": "",
"edit": "",
"eercast": "",
"eject": "",
"ellipsis-h": "",
"ellipsis-v": "",
"empire": "",
"envelope": "",
"envelope-o": "",
"envelope-open": "",
"envelope-open-o": "",
"envelope-square": "",
"envira": "",
"eraser": "",
"etsy": "",
"eur": "",
"euro": "",
"exchange": "",
Expand Down Expand Up @@ -288,6 +302,7 @@ context.TheGraph.FONT_AWESOME = {
"forumbee": "",
"forward": "",
"foursquare": "",
"free-code-camp": "",
"frown-o": "",
"futbol-o": "",
"gamepad": "",
Expand Down Expand Up @@ -320,6 +335,7 @@ context.TheGraph.FONT_AWESOME = {
"google-wallet": "",
"graduation-cap": "",
"gratipay": "",
"grav": "",
"group": "",
"h-square": "",
"hacker-news": "",
Expand All @@ -336,6 +352,7 @@ context.TheGraph.FONT_AWESOME = {
"hand-scissors-o": "",
"hand-spock-o": "",
"hand-stop-o": "",
"handshake-o": "",
"hard-of-hearing": "",
"hashtag": "",
"hdd-o": "",
Expand All @@ -359,8 +376,12 @@ context.TheGraph.FONT_AWESOME = {
"houzz": "",
"html5": "",
"i-cursor": "",
"id-badge": "",
"id-card": "",
"id-card-o": "",
"ils": "",
"image": "",
"imdb": "",
"inbox": "",
"indent": "",
"industry": "",
Expand Down Expand Up @@ -398,6 +419,7 @@ context.TheGraph.FONT_AWESOME = {
"link": "",
"linkedin": "",
"linkedin-square": "",
"linode": "",
"linux": "",
"list": "",
"list-alt": "",
Expand Down Expand Up @@ -430,8 +452,10 @@ context.TheGraph.FONT_AWESOME = {
"meanpath": "",
"medium": "",
"medkit": "",
"meetup": "",
"meh-o": "",
"mercury": "",
"microchip": "",
"microphone": "",
"microphone-slash": "",
"minus": "",
Expand Down Expand Up @@ -496,6 +520,7 @@ context.TheGraph.FONT_AWESOME = {
"plus-circle": "",
"plus-square": "",
"plus-square-o": "",
"podcast": "",
"power-off": "",
"print": "",
"product-hunt": "",
Expand All @@ -505,10 +530,12 @@ context.TheGraph.FONT_AWESOME = {
"question": "",
"question-circle": "",
"question-circle-o": "",
"quora": "",
"quote-left": "",
"quote-right": "",
"ra": "",
"random": "",
"ravelry": "",
"rebel": "",
"recycle": "",
"reddit": "",
Expand All @@ -535,6 +562,7 @@ context.TheGraph.FONT_AWESOME = {
"rub": "",
"ruble": "",
"rupee": "",
"s15": "",
"safari": "",
"save": "",
"scissors": "",
Expand All @@ -559,6 +587,7 @@ context.TheGraph.FONT_AWESOME = {
"shopping-bag": "",
"shopping-basket": "",
"shopping-cart": "",
"shower": "",
"sign-in": "",
"sign-language": "",
"sign-out": "",
Expand All @@ -575,6 +604,7 @@ context.TheGraph.FONT_AWESOME = {
"snapchat": "",
"snapchat-ghost": "",
"snapchat-square": "",
"snowflake-o": "",
"soccer-ball-o": "",
"sort": "",
"sort-alpha-asc": "",
Expand Down Expand Up @@ -620,6 +650,7 @@ context.TheGraph.FONT_AWESOME = {
"subway": "",
"suitcase": "",
"sun-o": "",
"superpowers": "",
"superscript": "",
"support": "",
"table": "",
Expand All @@ -629,6 +660,7 @@ context.TheGraph.FONT_AWESOME = {
"tags": "",
"tasks": "",
"taxi": "",
"telegram": "",
"television": "",
"tencent-weibo": "",
"terminal": "",
Expand All @@ -638,6 +670,17 @@ context.TheGraph.FONT_AWESOME = {
"th-large": "",
"th-list": "",
"themeisle": "",
"thermometer": "",
"thermometer-0": "",
"thermometer-1": "",
"thermometer-2": "",
"thermometer-3": "",
"thermometer-4": "",
"thermometer-empty": "",
"thermometer-full": "",
"thermometer-half": "",
"thermometer-quarter": "",
"thermometer-three-quarters": "",
"thumb-tack": "",
"thumbs-down": "",
"thumbs-o-down": "",
Expand All @@ -647,6 +690,8 @@ context.TheGraph.FONT_AWESOME = {
"times": "",
"times-circle": "",
"times-circle-o": "",
"times-rectangle": "",
"times-rectangle-o": "",
"tint": "",
"toggle-down": "",
"toggle-left": "",
Expand Down Expand Up @@ -687,11 +732,16 @@ context.TheGraph.FONT_AWESOME = {
"usb": "",
"usd": "",
"user": "",
"user-circle": "",
"user-circle-o": "",
"user-md": "",
"user-o": "",
"user-plus": "",
"user-secret": "",
"user-times": "",
"users": "",
"vcard": "",
"vcard-o": "",
"venus": "",
"venus-double": "",
"venus-mars": "",
Expand All @@ -716,10 +766,16 @@ context.TheGraph.FONT_AWESOME = {
"wheelchair-alt": "",
"wifi": "",
"wikipedia-w": "",
"window-close": "",
"window-close-o": "",
"window-maximize": "",
"window-minimize": "",
"window-restore": "",
"windows": "",
"won": "",
"wordpress": "",
"wpbeginner": "",
"wpexplorer": "",
"wpforms": "",
"wrench": "",
"xing": "",
Expand Down
Loading

0 comments on commit 1ad37ac

Please sign in to comment.