Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change getCandidate_ and showInsertionMarker_ to be more dynamic #5722

Merged
merged 4 commits into from
Dec 15, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions core/insertion_marker_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ 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 en every move while dragging, so
alschmiedt marked this conversation as resolved.
Show resolved Hide resolved
// ir might cause slowness, especially if the block stack is large. If so,
alschmiedt marked this conversation as resolved.
Show resolved Hide resolved
// maybe it could be made more efficient.
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 +624,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