-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
draggable.js
147 lines (129 loc) · 4.42 KB
/
draggable.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import Component from "@pencil.js/component";
import MouseEvent from "@pencil.js/mouse-event";
import Vector from "@pencil.js/vector";
/**
* @module Draggable
*/
/**
* @typedef {Object} DraggableOptions
* @prop {Boolean} [x=true] - Can move along vertical axis
* @prop {Boolean} [y=true] - Can move along horizontal axis
* @prop {Vector} [constrain] - Relative limit of freedom
*/
/**
* @typedef {Object} DraggableAPI
* @prop {Function} x - Change the "x" value in the draggable's options
* @prop {Function} y - Change the "y" value in the draggable's options
* @prop {Function} constrain - Change the "constrain" value in the draggable's options
* @prop {Function} stop - Stop the component from being draggable
*/
/**
* Set this component draggable
* @param {DraggableOptions} [options] - Additional options
* @return {DraggableAPI}
* @memberOf module:Component#
*/
Component.prototype.draggable = function draggable (options) {
if (this.isRotatable) {
throw new Error("Component can't be both rotatable and draggable.");
}
const cursorNotSet = this.options.cursor === Component.cursors.default;
if (cursorNotSet) {
this.options.cursor = Component.cursors.grab;
}
this.isDraggable = true;
this.isDragged = false;
const mergedOptions = {
x: true,
y: true,
...options,
};
if (mergedOptions.constrain) {
mergedOptions.constrain = Vector.from(mergedOptions.constrain);
}
let startPosition = null;
let originPosition = null;
const downHandler = ({ position }) => {
if (cursorNotSet) {
this.options.cursor = Component.cursors.grabbing;
}
startPosition = position;
originPosition = this.position.clone();
this.isDragged = true;
this.fire(new MouseEvent(MouseEvent.events.grab, this, this.position.clone()));
};
this.on(MouseEvent.events.down, downHandler, true);
const moveHandler = (event) => {
if (this.isDragged && startPosition) {
const difference = event.position.clone().subtract(startPosition);
const move = originPosition.clone().add(mergedOptions.x && difference.x, mergedOptions.y && difference.y);
this.position.set(move);
if (mergedOptions.constrain) {
this.position.constrain(mergedOptions.constrain.start, mergedOptions.constrain.end);
}
this.fire(new MouseEvent(MouseEvent.events.drag, this, this.position.clone()));
}
};
const upHandler = () => {
if (cursorNotSet) {
this.options.cursor = Component.cursors.grab;
}
if (this.isDragged && startPosition) {
startPosition = null;
this.isDragged = false;
this.fire(new MouseEvent(MouseEvent.events.drop, this, this.position.clone()));
}
};
let scene;
this.getScene().then((theScene) => {
if (this.isDraggable) {
scene = theScene;
scene
.on(MouseEvent.events.move, moveHandler)
.on(MouseEvent.events.up, upHandler);
}
});
return {
/**
* Set the draggable "x" option
* @param {Boolean} x - New value for "x"
* @memberOf DraggableAPI#
*/
set x (x) {
mergedOptions.x = x;
},
/**
* Set the draggable "y" option
* @param {Boolean} y - New value for "y"
* @memberOf DraggableAPI#
*/
set y (y) {
mergedOptions.y = y;
},
/**
* Set the draggable "constrain" option
* @param {Vector} constrain - New value for "constrain"
* @memberOf DraggableAPI#
*/
set constrain (constrain) {
mergedOptions.constrain = constrain ? Vector.from(constrain) : constrain;
},
/**
* Stop the component from being draggable
* @memberOf DraggableAPI#
*/
stop: () => {
this.removeListener(MouseEvent.events.down, downHandler);
if (scene) {
scene
.removeListener(MouseEvent.events.move, moveHandler)
.removeListener(MouseEvent.events.up, upHandler);
}
if (cursorNotSet) {
this.options.cursor = Component.cursors.default;
}
this.isDraggable = false;
this.isDragged = false;
},
};
};