/
scroll_on_block_drag_handler.js
305 lines (268 loc) · 8.08 KB
/
scroll_on_block_drag_handler.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Visual Blocks Editor
*
* Copyright 2011 Google Inc.
* http://blockly.googlecode.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Scrolling-on-block-drag interaction handler
*/
'use strict';
goog.provide('Blockly.ScrollOnBlockDragHandler');
goog.require('Blockly.AutoScroll');
goog.require('goog.math.Vec2');
/**
* @param {!Blockly.BlockSpace} blockSpace
* @constructor
*/
Blockly.ScrollOnBlockDragHandler = function(blockSpace) {
/**
* BlockSpace to scroll
* @private {Blockly.BlockSpace}
*/
this.blockSpace_ = blockSpace;
/**
* Direction names to Vec2 1/-1 representations
* @type {{top: goog.math.Vec2, bottom: goog.math.Vec2,
* left: goog.math.Vec2, right: goog.math.Vec2}}
*/
this.SCROLL_DIRECTION_VECTORS = {
top: new goog.math.Vec2(0, -1),
bottom: new goog.math.Vec2(0, 1),
left: new goog.math.Vec2(-1, 0),
right: new goog.math.Vec2(1, 0)
};
};
Blockly.ScrollOnBlockDragHandler.prototype.stopAutoScrolling = function() {
if (this.activeAutoScroll_) {
this.activeAutoScroll_.stopAndDestroy();
}
this.activeAutoScroll_ = null;
};
/**
* Pixels-per-millisecond scrolling speeds for mouse-location triggered scrolling
* @type {number}
*/
var MOUSE_SPEED_SLOW = 0.5;
var MOUSE_SPEED_FAST = 1.6;
/**
* Pixels-per-millisecond scrolling speeds for block-location triggered scrolling
* @type {number}
*/
var BLOCK_SPEED_SLOW = 0.28;
var BLOCK_SPEED_FAST = 1.4;
/**
* Mouse distance from side when to start slow scrolling
* @type {number}
*/
var MOUSE_START_DISTANCE = 0;
/**
* Mouse distance outside of viewport side when to start fast scrolling
* @type {number}
*/
var MOUSE_START_FAST_DISTANCE = 35;
/**
* Distance from block edge to side when to start slow scrolling
* @type {number}
*/
var BLOCK_START_DISTANCE = 0;
/**
* Distance from block edge to side when to start fast scrolling
* @type {number}
*/
var BLOCK_START_FAST_DISTANCE = 50;
/**
* A block is considered 'oversized' on a given dimension if it takes up more
* than this percentage of the viewport size.
* @type {number}
*/
var OVERSIZE_BLOCK_THRESHOLD = 0.85;
/**
* Drag 'margin' around cursor to use instead of block bounds when the dragged
* block is oversized. Given in block space units.
* @type {number}
*/
var FALLBACK_DRAG_MARGIN = 15;
/**
* Enables debug drawing of various block drag scrolling operations
* @type {boolean}
*/
Blockly.ScrollOnBlockDragHandler.DEBUG = false;
/**
* Pans the blockspace in the direction of a block if it's hanging off the
* edge of the blockspace.
*
* - Will only expand the blockspace vertically if vertical scrollbars are
* enabled
* - Will only expand the blockspace horizontally if horizontal scrollbars are
* enabled
*
* @see Scrolling behavior inspiration:
* {@url http://ux.stackexchange.com/a/73902}
*
* @param {Blockly.Block} block
* @param {number} mouseClientX
* @param {number} mouseClientY
*/
Blockly.ScrollOnBlockDragHandler.prototype.panIfOverEdge = function(
block,
mouseClientX,
mouseClientY
) {
if (!this.blockSpace_.currentlyScrollable()) {
// Don't perform potentially expensive panning calculations if not currently
// scrollable
return;
}
var SCROLLABLE_DIRECTIONS = [];
if (
this.blockSpace_.scrollbarPair &&
this.blockSpace_.scrollbarPair.canScrollHorizontally()
) {
SCROLLABLE_DIRECTIONS.push('left', 'right');
}
if (
this.blockSpace_.scrollbarPair &&
this.blockSpace_.scrollbarPair.canScrollVertically()
) {
SCROLLABLE_DIRECTIONS.push('top', 'bottom');
}
if (SCROLLABLE_DIRECTIONS.length === 0) {
return;
}
var mouseSvg = Blockly.mouseCoordinatesToSvg(
mouseClientX,
mouseClientY,
this.blockSpace_.blockSpaceEditor.svg_
);
var mouseViewport = Blockly.svgCoordinatesToViewport(
new goog.math.Coordinate(mouseSvg.x, mouseSvg.y),
this.blockSpace_
);
var mouseBlockSpace = Blockly.viewportCoordinateToBlockSpace(
mouseViewport,
this.blockSpace_
);
var viewportBox = this.blockSpace_.getViewportBox();
var blockBox = block.getBox();
// When dragged block is almost as tall as the viewport, use a small bounding
// region around the cursor instead of the whole block bounding box to trigger
// autoscrolling.
var blockHeight = Blockly.getBoxHeight(blockBox);
var viewportHeight = Blockly.getBoxHeight(viewportBox);
if (blockHeight > viewportHeight * OVERSIZE_BLOCK_THRESHOLD) {
// Center box around cursor with fallback radius
blockBox.top = Math.max(
blockBox.top,
mouseBlockSpace.y - FALLBACK_DRAG_MARGIN
);
blockBox.bottom = Math.min(
blockBox.bottom,
mouseBlockSpace.y + FALLBACK_DRAG_MARGIN
);
}
// Same rule, but horizontal
var blockWidth = Blockly.getBoxWidth(blockBox);
var viewportWidth = Blockly.getBoxWidth(viewportBox);
if (blockWidth > viewportWidth * OVERSIZE_BLOCK_THRESHOLD) {
// Center box around cursor with fallback radius
blockBox.left = Math.max(
blockBox.left,
mouseBlockSpace.x - FALLBACK_DRAG_MARGIN
);
blockBox.right = Math.min(
blockBox.right,
mouseBlockSpace.x + FALLBACK_DRAG_MARGIN
);
}
// Calculate how far out-of-bounds the dragged block/cursor is.
var blockOverflows = Blockly.getBoxOverflow(viewportBox, blockBox);
var mouseOverflows = Blockly.getPointBoxOverflow(
viewportBox,
new goog.math.Coordinate(mouseBlockSpace.x, mouseBlockSpace.y)
);
if (Blockly.ScrollOnBlockDragHandler.DEBUG) {
this.blockSpace_.drawDebugCircle(
'mouse circle',
new goog.math.Coordinate(mouseBlockSpace.x, mouseBlockSpace.y),
'orange'
);
this.blockSpace_.drawDebugBox('block box' + block.id, blockBox, 'purple');
this.blockSpace_.drawDebugBox('block space box', viewportBox, 'blue');
}
var overallScrollVector = new goog.math.Vec2(0, 0);
SCROLLABLE_DIRECTIONS.forEach(function(direction) {
var mouseOverflow = mouseOverflows[direction];
var blockOverflow = blockOverflows[direction];
var scrollVector = this.SCROLL_DIRECTION_VECTORS[direction];
var candidateScrolls = [];
if (
Blockly.numberWithin(
blockOverflow,
BLOCK_START_DISTANCE,
BLOCK_START_FAST_DISTANCE,
false
)
) {
candidateScrolls.push(scrollVector.clone().scale(BLOCK_SPEED_SLOW));
}
if (blockOverflow > BLOCK_START_FAST_DISTANCE) {
candidateScrolls.push(scrollVector.clone().scale(BLOCK_SPEED_FAST));
}
if (
Blockly.numberWithin(
mouseOverflow,
MOUSE_START_DISTANCE,
MOUSE_START_FAST_DISTANCE,
false
)
) {
candidateScrolls.push(scrollVector.clone().scale(MOUSE_SPEED_SLOW));
}
if (mouseOverflow > MOUSE_START_FAST_DISTANCE) {
candidateScrolls.push(scrollVector.clone().scale(MOUSE_SPEED_FAST));
}
var greatestScrollVector = candidateScrolls.reduce(function(
fastestScroll,
candidateScroll
) {
if (!fastestScroll) {
return candidateScroll;
}
return fastestScroll.magnitude() > candidateScroll.magnitude()
? fastestScroll
: candidateScroll;
},
null);
if (greatestScrollVector) {
overallScrollVector = goog.math.Vec2.sum(
overallScrollVector,
greatestScrollVector
);
}
}, this);
if (overallScrollVector.equals(new goog.math.Vec2(0, 0))) {
this.stopAutoScrolling();
return;
}
this.activeAutoScroll_ =
this.activeAutoScroll_ ||
new Blockly.AutoScroll(this.blockSpace_, overallScrollVector);
this.activeAutoScroll_.updateProperties(
overallScrollVector,
mouseClientX,
mouseClientY
);
};