-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
paste.js
75 lines (54 loc) · 2.29 KB
/
paste.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { actionCopyEntities } from '../actions/copy_entities';
import { actionMove } from '../actions/move';
import { geoExtent, geoPointInPolygon, geoVecSubtract } from '../geo';
import { modeMove } from '../modes/move';
import { uiCmd } from '../ui/cmd';
// see also `operationPaste`
export function behaviorPaste(context) {
function doPaste(d3_event) {
// prevent paste during low zoom selection
if (!context.map().withinEditableZoom()) return;
d3_event.preventDefault();
var baseGraph = context.graph();
var mouse = context.map().mouse();
var projection = context.projection;
var viewport = geoExtent(projection.clipExtent()).polygon();
if (!geoPointInPolygon(mouse, viewport)) return;
var oldIDs = context.copyIDs();
if (!oldIDs.length) return;
var extent = geoExtent();
var oldGraph = context.copyGraph();
var newIDs = [];
var action = actionCopyEntities(oldIDs, oldGraph);
context.perform(action);
var copies = action.copies();
var originals = new Set();
Object.values(copies).forEach(function(entity) { originals.add(entity.id); });
for (var id in copies) {
var oldEntity = oldGraph.entity(id);
var newEntity = copies[id];
extent._extend(oldEntity.extent(oldGraph));
// Exclude child nodes from newIDs if their parent way was also copied.
var parents = context.graph().parentWays(newEntity);
var parentCopied = parents.some(function(parent) {
return originals.has(parent.id);
});
if (!parentCopied) {
newIDs.push(newEntity.id);
}
}
// Put pasted objects where mouse pointer is..
var copyPoint = (context.copyLonLat() && projection(context.copyLonLat())) || projection(extent.center());
var delta = geoVecSubtract(mouse, copyPoint);
context.perform(actionMove(newIDs, delta, projection));
context.enter(modeMove(context, newIDs, baseGraph));
}
function behavior() {
context.keybinding().on(uiCmd('⌘V'), doPaste);
return behavior;
}
behavior.off = function() {
context.keybinding().off(uiCmd('⌘V'));
};
return behavior;
}