-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathinsertion_marker_manager.js
832 lines (750 loc) · 27.1 KB
/
insertion_marker_manager.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Class that controls updates to connections during drags.
* @author [email protected] (Rachel Fenichel)
*/
'use strict';
goog.provide('Blockly.InsertionMarkerManager');
goog.require('Blockly.blockAnimations');
goog.require('Blockly.ComponentManager');
goog.require('Blockly.connectionTypes');
/** @suppress {extraRequire} */
goog.require('Blockly.constants');
goog.require('Blockly.Events');
goog.requireType('Blockly.BlockSvg');
goog.requireType('Blockly.RenderedConnection');
goog.requireType('Blockly.utils.Coordinate');
goog.requireType('Blockly.WorkspaceSvg');
/**
* Class that controls updates to connections during drags. It is primarily
* responsible for finding the closest eligible connection and highlighting or
* unhiglighting it as needed during a drag.
* @param {!Blockly.BlockSvg} block The top block in the stack being dragged.
* @constructor
*/
Blockly.InsertionMarkerManager = function(block) {
Blockly.selected = block;
/**
* The top block in the stack being dragged.
* Does not change during a drag.
* @type {!Blockly.BlockSvg}
* @private
*/
this.topBlock_ = block;
/**
* The workspace on which these connections are being dragged.
* Does not change during a drag.
* @type {!Blockly.WorkspaceSvg}
* @private
*/
this.workspace_ = block.workspace;
/**
* The last connection on the stack, if it's not the last connection on the
* first block.
* Set in initAvailableConnections, if at all.
* @type {Blockly.RenderedConnection}
* @private
*/
this.lastOnStack_ = null;
/**
* The insertion marker corresponding to the last block in the stack, if
* that's not the same as the first block in the stack.
* Set in initAvailableConnections, if at all
* @type {Blockly.BlockSvg}
* @private
*/
this.lastMarker_ = null;
/**
* The insertion marker that shows up between blocks to show where a block
* would go if dropped immediately.
* @type {Blockly.BlockSvg}
* @private
*/
this.firstMarker_ = this.createMarkerBlock_(this.topBlock_);
/**
* The connection that this block would connect to if released immediately.
* Updated on every mouse move.
* This is not on any of the blocks that are being dragged.
* @type {Blockly.RenderedConnection}
* @private
*/
this.closestConnection_ = null;
/**
* The connection that would connect to this.closestConnection_ if this block
* were released immediately.
* Updated on every mouse move.
* This is on the top block that is being dragged or the last block in the
* dragging stack.
* @type {Blockly.RenderedConnection}
* @private
*/
this.localConnection_ = null;
/**
* pxt-blockly A line between the local connection and closest connection, to indicate
* where the inserted block will appear.
* @type {SVGElement}
* @private
*/
this.connectionLine_ = null;
/**
* pxt-blockly A circle at the location of the closest input connection.
* @type {SVGElement}
* @private
*/
this.connectionIndicator_ = null;
/**
* Whether the block would be deleted if it were dropped immediately.
* Updated on every mouse move.
* @type {boolean}
* @private
*/
this.wouldDeleteBlock_ = false;
/**
* Connection on the insertion marker block that corresponds to
* this.localConnection_ on the currently dragged block.
* @type {Blockly.RenderedConnection}
* @private
*/
this.markerConnection_ = null;
/**
* The block that currently has an input being highlighted, or null.
* @type {Blockly.BlockSvg}
* @private
*/
this.highlightedBlock_ = null;
/**
* The block being faded to indicate replacement, or null.
* @type {Blockly.BlockSvg}
* @private
*/
this.fadedBlock_ = null;
/**
* The connections on the dragging blocks that are available to connect to
* other blocks. This includes all open connections on the top block, as well
* as the last connection on the block stack.
* Does not change during a drag.
* @type {!Array<!Blockly.RenderedConnection>}
* @private
*/
this.availableConnections_ = this.initAvailableConnections_();
};
/**
* An enum describing different kinds of previews the InsertionMarkerManager
* could display.
* @enum {number}
*/
Blockly.InsertionMarkerManager.PREVIEW_TYPE = {
INSERTION_MARKER: 0,
INPUT_OUTLINE: 1,
REPLACEMENT_FADE: 2,
};
/**
* An error message to throw if the block created by createMarkerBlock_ is
* missing any components.
* @type {string}
* @const
*/
Blockly.InsertionMarkerManager.DUPLICATE_BLOCK_ERROR = 'The insertion marker ' +
'manager tried to create a marker but the result is missing %1. If ' +
'you are using a mutator, make sure your domToMutation method is ' +
'properly defined.';
/**
* Sever all links from this object.
* @package
*/
Blockly.InsertionMarkerManager.prototype.dispose = function() {
this.availableConnections_.length = 0;
Blockly.Events.disable();
try {
if (this.firstMarker_) {
this.firstMarker_.dispose();
}
if (this.lastMarker_) {
this.lastMarker_.dispose();
}
} finally {
Blockly.Events.enable();
}
};
/**
* Update the available connections for the top block. These connections can
* change if a block is unplugged and the stack is healed.
* @package
*/
Blockly.InsertionMarkerManager.prototype.updateAvailableConnections = function() {
this.availableConnections_ = this.initAvailableConnections_();
};
/**
* Return whether the block would be deleted if dropped immediately, based on
* information from the most recent move event.
* @return {boolean} True if the block would be deleted if dropped immediately.
* @package
*/
Blockly.InsertionMarkerManager.prototype.wouldDeleteBlock = function() {
return this.wouldDeleteBlock_;
};
/**
* Return whether the block would be connected if dropped immediately, based on
* information from the most recent move event.
* @return {boolean} True if the block would be connected if dropped
* immediately.
* @package
*/
Blockly.InsertionMarkerManager.prototype.wouldConnectBlock = function() {
return !!this.closestConnection_;
};
/**
* Connect to the closest connection and render the results.
* This should be called at the end of a drag.
* @package
*/
Blockly.InsertionMarkerManager.prototype.applyConnections = function() {
if (this.closestConnection_) {
// Don't fire events for insertion markers.
Blockly.Events.disable();
this.hidePreview_();
Blockly.Events.enable();
// Connect two blocks together.
this.localConnection_.connect(this.closestConnection_);
if (this.topBlock_.rendered) {
// Trigger a connection animation.
// Determine which connection is inferior (lower in the source stack).
var inferiorConnection = this.localConnection_.isSuperior() ?
this.closestConnection_ : this.localConnection_;
Blockly.blockAnimations.connectionUiEffect(
inferiorConnection.getSourceBlock());
// Bring the just-edited stack to the front.
var rootBlock = this.topBlock_.getRootBlock();
rootBlock.bringToFront();
}
}
};
/**
* Update connections based on the most recent move location.
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @param {?Blockly.IDragTarget} dragTarget The drag target that the block is
* currently over.
* @package
*/
Blockly.InsertionMarkerManager.prototype.update = function(dxy, dragTarget) {
var candidate = this.getCandidate_(dxy);
this.wouldDeleteBlock_ = this.shouldDelete_(candidate, dragTarget);
var shouldUpdate = this.wouldDeleteBlock_ ||
this.shouldUpdatePreviews_(candidate, dxy);
if (shouldUpdate) {
// Don't fire events for insertion marker creation or movement.
Blockly.Events.disable();
this.maybeHidePreview_(candidate);
this.maybeShowPreview_(candidate);
Blockly.Events.enable();
}
this.updateConnectionLine_(dxy); // pxt-blockly
};
/**
* Create an insertion marker that represents the given block.
* @param {!Blockly.BlockSvg} sourceBlock The block that the insertion marker
* will represent.
* @return {!Blockly.BlockSvg} The insertion marker that represents the given
* block.
* @private
*/
Blockly.InsertionMarkerManager.prototype.createMarkerBlock_ = function(sourceBlock) {
var imType = sourceBlock.type;
Blockly.Events.disable();
try {
this.workspace_.loadingEventsDisabled = true;
var result = this.workspace_.newBlock(imType);
this.workspace_.loadingEventsDisabled = false;
result.setInsertionMarker(true);
if (sourceBlock.mutationToDom) {
var oldMutationDom = sourceBlock.mutationToDom();
if (oldMutationDom) {
result.domToMutation(oldMutationDom);
}
}
// Copy field values from the other block. These values may impact the
// rendered size of the insertion marker. Note that we do not care about
// child blocks here.
for (var i = 0; i < sourceBlock.inputList.length; i++) {
var sourceInput = sourceBlock.inputList[i];
if (sourceInput.name == Blockly.constants.COLLAPSED_INPUT_NAME
|| sourceInput.isVisible()) { // pxt-blockly
continue; // Ignore the collapsed input.
}
var resultInput = result.inputList[i];
if (!resultInput) {
throw new Error(Blockly.InsertionMarkerManager.DUPLICATE_BLOCK_ERROR
.replace('%1', 'an input'));
}
for (var j = 0; j < sourceInput.fieldRow.length; j++) {
var sourceField = sourceInput.fieldRow[j];
var resultField = resultInput.fieldRow[j];
if (!resultField) {
throw new Error(Blockly.InsertionMarkerManager.DUPLICATE_BLOCK_ERROR
.replace('%1', 'a field'));
}
resultField.setValue(sourceField.getValue());
}
}
result.setCollapsed(sourceBlock.isCollapsed());
result.setInputsInline(sourceBlock.getInputsInline());
result.initSvg();
result.getSvgRoot().setAttribute('visibility', 'hidden');
} finally {
Blockly.Events.enable();
}
return result;
};
/**
* Populate the list of available connections on this block stack. This should
* only be called once, at the beginning of a drag.
* If the stack has more than one block, this function will populate
* lastOnStack_ and create the corresponding insertion marker.
* @return {!Array<!Blockly.RenderedConnection>} A list of available
* connections.
* @private
*/
Blockly.InsertionMarkerManager.prototype.initAvailableConnections_ = function() {
var available = this.topBlock_.getConnections_(false);
// Also check the last connection on this stack
var lastOnStack = this.topBlock_.lastConnectionInStack(true);
if (lastOnStack && lastOnStack != this.topBlock_.nextConnection) {
available.push(lastOnStack);
this.lastOnStack_ = lastOnStack;
if (this.lastMarker_) {
Blockly.Events.disable();
try {
this.lastMarker_.dispose();
} finally {
Blockly.Events.enable();
}
}
this.lastMarker_ = this.createMarkerBlock_(lastOnStack.getSourceBlock());
}
return available;
};
/**
* Whether the previews (insertion marker and replacement marker) should be
* updated based on the closest candidate and the current drag distance.
* @param {!Object} candidate An object containing a local connection, a closest
* connection, and a radius. Returned by getCandidate_.
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @return {boolean} Whether the preview should be updated.
* @private
*/
Blockly.InsertionMarkerManager.prototype.shouldUpdatePreviews_ = function(
candidate, dxy) {
var candidateLocal = candidate.local;
var candidateClosest = candidate.closest;
var radius = candidate.radius;
// Found a connection!
if (candidateLocal && candidateClosest) {
// We're already showing an insertion marker.
// Decide whether the new connection has higher priority.
if (this.localConnection_ && this.closestConnection_) {
// The connection was the same as the current connection.
if (this.closestConnection_ == candidateClosest &&
this.localConnection_ == candidateLocal) {
return false;
}
var xDiff = this.localConnection_.x + dxy.x - this.closestConnection_.x;
var yDiff = this.localConnection_.y + dxy.y - this.closestConnection_.y;
var curDistance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
// Slightly prefer the existing preview over a new preview.
return !(candidateClosest && radius > curDistance -
Blockly.CURRENT_CONNECTION_PREFERENCE);
} else if (!this.localConnection_ && !this.closestConnection_) {
// We weren't showing a preview before, but we should now.
return true;
} else {
console.error('Only one of localConnection_ and closestConnection_ was set.');
}
} else { // No connection found.
// Only need to update if we were showing a preview before.
return !!(this.localConnection_ && this.closestConnection_);
}
console.error('Returning true from shouldUpdatePreviews, but it\'s not clear why.');
return true;
};
/**
* Find the nearest valid connection, which may be the same as the current
* closest connection.
* @param {!Blockly.utils.Coordinate} dxy Position relative to drag start,
* in workspace units.
* @return {!Object} An object containing a local connection, a closest
* connection, and a radius.
* @private
*/
Blockly.InsertionMarkerManager.prototype.getCandidate_ = function(dxy) {
var radius = this.getStartRadius_();
var candidateClosest = null;
var candidateLocal = null;
for (var i = 0; i < this.availableConnections_.length; i++) {
var myConnection = this.availableConnections_[i];
var neighbour = myConnection.closest(radius, dxy);
if (neighbour.connection) {
candidateClosest = neighbour.connection;
candidateLocal = myConnection;
radius = neighbour.radius;
}
}
return {
closest: candidateClosest,
local: candidateLocal,
radius: radius
};
};
/**
* Decide the radius at which to start searching for the closest connection.
* @return {number} The radius at which to start the search for the closest
* connection.
* @private
*/
Blockly.InsertionMarkerManager.prototype.getStartRadius_ = function() {
// If there is already a connection highlighted,
// increase the radius we check for making new connections.
// Why? When a connection is highlighted, blocks move around when the insertion
// marker is created, which could cause the connection became out of range.
// By increasing radiusConnection when a connection already exists,
// we never "lose" the connection from the offset.
if (this.closestConnection_ && this.localConnection_) {
return Blockly.CONNECTING_SNAP_RADIUS;
}
return Blockly.SNAP_RADIUS;
};
/**
* Whether ending the drag would delete the block.
* @param {!Object} candidate An object containing a local connection, a closest
* connection, and a radius.
* @param {?Blockly.IDragTarget} dragTarget The drag target that the block is
* currently over.
* @return {boolean} Whether dropping the block immediately would delete the
* block.
* @private
*/
Blockly.InsertionMarkerManager.prototype.shouldDelete_ = function(
candidate, dragTarget) {
if (dragTarget) {
var componentManager = this.workspace_.getComponentManager();
var isDeleteArea = componentManager.hasCapability(dragTarget.id,
Blockly.ComponentManager.Capability.DELETE_AREA);
if (isDeleteArea) {
return (
/** @type {!Blockly.IDeleteArea} */ (dragTarget))
.wouldDelete(this.topBlock_, candidate && !!candidate.closest);
}
}
return false;
};
/**
* Show an insertion marker or replacement highlighting during a drag, if
* needed.
* At the beginning of this function, this.localConnection_ and
* this.closestConnection_ should both be null.
* @param {!Object} candidate An object containing a local connection, a closest
* connection, and a radius.
* @private
*/
Blockly.InsertionMarkerManager.prototype.maybeShowPreview_ = function(candidate) {
// Nope, don't add a marker.
if (this.wouldDeleteBlock_) {
return;
}
var closest = candidate.closest;
var local = candidate.local;
// Nothing to connect to.
if (!closest) {
return;
}
// Something went wrong and we're trying to connect to an invalid connection.
if (closest == this.closestConnection_ ||
closest.getSourceBlock().isInsertionMarker()) {
console.log('Trying to connect to an insertion marker');
return;
}
// Add an insertion marker or replacement marker.
this.closestConnection_ = closest;
this.localConnection_ = local;
this.showPreview_();
};
/**
* A preview should be shown. This function figures out if it should be a block
* highlight or an insertion marker, and shows the appropriate one.
* @private
*/
Blockly.InsertionMarkerManager.prototype.showPreview_ = function() {
var closest = this.closestConnection_;
var renderer = this.workspace_.getRenderer();
var method = renderer.getConnectionPreviewMethod(
/** @type {!Blockly.RenderedConnection} */ (closest),
/** @type {!Blockly.RenderedConnection} */ (this.localConnection_),
this.topBlock_);
switch (method) {
case Blockly.InsertionMarkerManager.PREVIEW_TYPE.INPUT_OUTLINE:
this.showInsertionInputOutline_();
break;
case Blockly.InsertionMarkerManager.PREVIEW_TYPE.INSERTION_MARKER:
this.showInsertionMarker_();
break;
case Blockly.InsertionMarkerManager.PREVIEW_TYPE.REPLACEMENT_FADE:
this.showReplacementFade_();
break;
}
// Optionally highlight the actual connection, as a nod to previous behaviour.
if (closest && renderer.shouldHighlightConnection(closest)) {
closest.highlight();
}
};
/**
* pxt-blockly Create the SVG line element to render between two highlighted connections
* @private
*/
Blockly.InsertionMarkerManager.prototype.createConnectionLine_ = function() {
if (!this.connectionLine_) {
this.connectionLine_ = Blockly.utils.dom.createSvgElement(
'line',
{
'class': 'blocklyConnectionLine',
'x1': 0,
'y1': 0,
'x2': 0,
'y2': 0
},
this.localConnection_.sourceBlock_.getSvgRoot());
// Create connection indicator for target/closes connection
this.connectionIndicator_ = Blockly.utils.dom.createSvgElement('g',
{'class': 'blocklyInputConnectionIndicator'},
this.closestConnection_.sourceBlock_.getSvgRoot());
Blockly.utils.dom.createSvgElement('circle',
{'r': Blockly.CONNECTION_INDICATOR_RADIUS}, this.connectionIndicator_);
var offset = this.closestConnection_.offsetInBlock_;
this.connectionIndicator_.setAttribute('transform',
'translate(' + offset.x + ',' + offset.y + ')');
}
}
/**
* pxt-blockly Update the position of the connection line element while block dragged
* @private
*/
Blockly.InsertionMarkerManager.prototype.updateConnectionLine_ = function(dxy) {
if (this.closestConnection_ && this.localConnection_ && this.connectionLine_) {
var radius = Blockly.CONNECTION_INDICATOR_RADIUS;
var offset = this.localConnection_.offsetInBlock_;
var x2 = this.closestConnection_.x - this.localConnection_.x - dxy.x + offset.x;
var y2 = this.closestConnection_.y - this.localConnection_.y - dxy.y + offset.y;
// Offset the line by the radius of the indicator to prevent overlap
var atan = Math.atan2(y2 - offset.y, x2 - offset.x);
var len = Math.sqrt(Math.pow((x2 - offset.x), 2) + Math.pow((y2 - offset.y), 2));
// When the indicators are overlapping, we hide the line
if (len < radius * 2 + 1) {
Blockly.utils.dom.addClass(this.connectionLine_, "hidden");
} else {
Blockly.utils.dom.removeClass(this.connectionLine_, "hidden");
this.connectionLine_.setAttribute("x1", offset.x + Math.cos(atan) * radius);
this.connectionLine_.setAttribute("y1", offset.y + Math.sin(atan) * radius);
this.connectionLine_.setAttribute("x2", x2 - Math.cos(atan) * radius);
this.connectionLine_.setAttribute("y2", y2 - Math.sin(atan) * radius);
}
}
};
/**
* pxt-blockly Hide the connection line element when unhighlighting
* @private
*/
Blockly.InsertionMarkerManager.prototype.hideConnectionLine_ = function() {
if (this.localConnection_ && this.connectionLine_) {
this.localConnection_.sourceBlock_.getSvgRoot().removeChild(this.connectionLine_);
this.connectionLine_ = null;
this.closestConnection_.sourceBlock_.getSvgRoot().removeChild(this.connectionIndicator_);
this.connectionIndicator_ = null;
}
}
/**
* Show an insertion marker or replacement highlighting during a drag, if
* needed.
* At the end of this function, this.localConnection_ and
* this.closestConnection_ should both be null.
* @param {!Object} candidate An object containing a local connection, a closest
* connection, and a radius.
* @private
*/
Blockly.InsertionMarkerManager.prototype.maybeHidePreview_ = function(candidate) {
// If there's no new preview, remove the old one but don't bother deleting it.
// We might need it later, and this saves disposing of it and recreating it.
if (!candidate.closest) {
this.hidePreview_();
} else {
// If there's a new preview and there was an preview before, and either
// connection has changed, remove the old preview.
var hadPreview = this.closestConnection_ && this.localConnection_;
var closestChanged = this.closestConnection_ != candidate.closest;
var localChanged = this.localConnection_ != candidate.local;
// Also hide if we had a preview before but now we're going to delete instead.
if (hadPreview && (closestChanged || localChanged || this.wouldDeleteBlock_)) {
this.hidePreview_();
}
}
// Either way, clear out old state.
this.markerConnection_ = null;
this.closestConnection_ = null;
this.localConnection_ = null;
};
/**
* A preview should be hidden. This function figures out if it is a block
* highlight or an insertion marker, and hides the appropriate one.
* @private
*/
Blockly.InsertionMarkerManager.prototype.hidePreview_ = function() {
if (this.closestConnection_ && this.closestConnection_.targetBlock() &&
this.workspace_.getRenderer()
.shouldHighlightConnection(this.closestConnection_)) {
this.closestConnection_.unhighlight();
}
if (this.fadedBlock_) {
this.hideReplacementFade_();
} else if (this.highlightedBlock_) {
this.hideInsertionInputOutline_();
} else if (this.markerConnection_) {
this.hideInsertionMarker_();
}
};
/**
* Shows an insertion marker connected to the appropriate blocks (based on
* manager state).
* @private
*/
Blockly.InsertionMarkerManager.prototype.showInsertionMarker_ = function() {
var local = this.localConnection_;
var closest = this.closestConnection_;
var isLastInStack = this.lastOnStack_ && local == this.lastOnStack_;
var imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
var imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
if (imConn == this.markerConnection_) {
throw Error('Made it to showInsertionMarker_ even though the marker isn\'t ' +
'changing');
}
// Render disconnected from everything else so that we have a valid
// connection location.
imBlock.render();
imBlock.rendered = true;
imBlock.getSvgRoot().setAttribute('visibility', 'visible');
if (imConn && closest) {
// Position so that the existing block doesn't move.
imBlock.positionNearConnection(imConn, closest);
}
if (closest) {
// Connect() also renders the insertion marker.
imConn.connect(closest);
}
this.markerConnection_ = imConn;
};
/**
* Disconnects and hides the current insertion marker. Should return the blocks
* to their original state.
* @private
*/
Blockly.InsertionMarkerManager.prototype.hideInsertionMarker_ = function() {
if (!this.markerConnection_) {
console.log('No insertion marker connection to disconnect');
return;
}
var imConn = this.markerConnection_;
var imBlock = imConn.getSourceBlock();
var markerNext = imBlock.nextConnection;
var markerPrev = imBlock.previousConnection;
var markerOutput = imBlock.outputConnection;
var isFirstInStatementStack =
(imConn == markerNext && !(markerPrev && markerPrev.targetConnection));
var isFirstInOutputStack =
imConn.type == Blockly.connectionTypes.INPUT_VALUE &&
!(markerOutput && markerOutput.targetConnection);
// The insertion marker is the first block in a stack. Unplug won't do
// anything in that case. Instead, unplug the following block.
if (isFirstInStatementStack || isFirstInOutputStack) {
imConn.targetBlock().unplug(false);
}
// Inside of a C-block, first statement connection.
else if (imConn.type == Blockly.connectionTypes.NEXT_STATEMENT &&
imConn != markerNext) {
var innerConnection = imConn.targetConnection;
innerConnection.getSourceBlock().unplug(false);
var previousBlockNextConnection =
markerPrev ? markerPrev.targetConnection : null;
imBlock.unplug(true);
if (previousBlockNextConnection) {
previousBlockNextConnection.connect(innerConnection);
}
} else {
imBlock.unplug(true /* healStack */);
}
if (imConn.targetConnection) {
throw Error('markerConnection_ still connected at the end of ' +
'disconnectInsertionMarker');
}
this.markerConnection_ = null;
var svg = imBlock.getSvgRoot();
if (svg) {
svg.setAttribute('visibility', 'hidden');
}
};
/**
* Shows an outline around the input the closest connection belongs to.
* @private
*/
Blockly.InsertionMarkerManager.prototype.showInsertionInputOutline_ = function() {
var closest = this.closestConnection_;
this.highlightedBlock_ = closest.getSourceBlock();
this.highlightedBlock_.highlightShapeForInput(closest, true);
this.createConnectionLine_(); // pxt-blockly
};
/**
* Hides any visible input outlines.
* @private
*/
Blockly.InsertionMarkerManager.prototype.hideInsertionInputOutline_ = function() {
this.highlightedBlock_.highlightShapeForInput(this.closestConnection_, false);
this.highlightedBlock_ = null;
this.hideConnectionLine_(); // pxt-blockly
};
/**
* Shows a replacement fade affect on the closest connection's target block
* (the block that is currently connected to it).
* @private
*/
Blockly.InsertionMarkerManager.prototype.showReplacementFade_ = function() {
this.fadedBlock_ = this.closestConnection_.targetBlock();
this.fadedBlock_.fadeForReplacement(true);
this.createConnectionLine_(); // pxt-blockly
};
/**
* Hides/Removes any visible fade affects.
* @private
*/
Blockly.InsertionMarkerManager.prototype.hideReplacementFade_ = function() {
this.fadedBlock_.fadeForReplacement(false);
this.fadedBlock_ = null;
this.hideConnectionLine_(); // pxt-blockly
};
/**
* Get a list of the insertion markers that currently exist. Drags have 0, 1,
* or 2 insertion markers.
* @return {!Array<!Blockly.BlockSvg>} A possibly empty list of insertion
* marker blocks.
* @package
*/
Blockly.InsertionMarkerManager.prototype.getInsertionMarkers = function() {
var result = [];
if (this.firstMarker_) {
result.push(this.firstMarker_);
}
if (this.lastMarker_) {
result.push(this.lastMarker_);
}
return result;
};