Skip to content

Commit

Permalink
fix: change getCandidate_ and showInsertionMarker_ to be more dynamic (
Browse files Browse the repository at this point in the history
…#5722)

* Change getCandidate_ and showInsertionMarker_ to be more dynamic

* Ran clang formatter

* Fix typos

* Fix issue with dragging stacks

* From PR comment
  • Loading branch information
mark-friedman authored Dec 15, 2021
1 parent da07cbd commit 68d8113
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions core/insertion_marker_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ InsertionMarkerManager.prototype.getCandidate_ = function(dxy) {
let candidateClosest = null;
let candidateLocal = null;

// It's possible that a block has added or removed connections during a drag,
// (e.g. in a drag/move event handler), so let's update the available
// connections. Note that this will be called on every move while dragging, so
// it might cause slowness, especially if the block stack is large. If so,
// maybe it could be made more efficient. Also note that we won't update the
// connections if we've already connected the insertion marker to a block.
if (!this.markerConnection_ || !this.markerConnection_.isConnected()) {
this.updateAvailableConnections();
}

for (let i = 0; i < this.availableConnections_.length; i++) {
const myConnection = this.availableConnections_[i];
const neighbour = myConnection.closest(radius, dxy);
Expand Down Expand Up @@ -617,8 +627,21 @@ InsertionMarkerManager.prototype.showInsertionMarker_ = function() {
const closest = this.closestConnection_;

const isLastInStack = this.lastOnStack_ && local === this.lastOnStack_;
const imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
const imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
let imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
let imConn;
try {
imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
} catch (e) {
// It's possible that the number of connections on the local block has
// changed since the insertion marker was originally created. Let's
// recreate the insertion marker and try again. In theory we could probably
// recreate the marker block (e.g. in getCandidate_), which is called more
// often during the drag, but creating a block that often might be too slow,
// so we only do it if necessary.
this.firstMarker_ = this.createMarkerBlock_(this.topBlock_);
imBlock = isLastInStack ? this.lastMarker_ : this.firstMarker_;
imConn = imBlock.getMatchingConnection(local.getSourceBlock(), local);
}

if (imConn === this.markerConnection_) {
throw Error(
Expand Down

0 comments on commit 68d8113

Please sign in to comment.