Skip to content

Commit

Permalink
Merge pull request #985 from 3DStreet/delete-segment-with-flow
Browse files Browse the repository at this point in the history
Delete segment with flow
  • Loading branch information
kfarr authored Dec 24, 2024
2 parents a358304 + dd8a49a commit 172d360
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/components/managed-street.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ AFRAME.registerComponent('managed-street', {
// Bind the method to preserve context
this.refreshFromSource = this.refreshFromSource.bind(this);
},
setupMutationObserver: function () {
// Create mutation observer
if (this.observer) {
this.observer.disconnect();
}
this.observer = new MutationObserver((mutations) => {
let needsReflow = false;

mutations.forEach((mutation) => {
if (mutation.type === 'childList' && mutation.removedNodes.length > 0) {
// Check if any of the removed nodes were street segments
mutation.removedNodes.forEach((node) => {
if (node.hasAttribute && node.hasAttribute('street-segment')) {
needsReflow = true;
}
});
}
});

// If segments were removed, trigger reflow
if (needsReflow) {
this.refreshManagedEntities();
this.applyJustification();
this.createOrUpdateJustifiedDirtBox();
}
});

// Start observing the managed-street element
this.observer.observe(this.el, {
childList: true // watch for child additions/removals
});
},
update: function (oldData) {
const data = this.data;
const dataDiff = AFRAME.utils.diff(oldData, data);
Expand Down Expand Up @@ -155,6 +187,7 @@ AFRAME.registerComponent('managed-street', {
this.managedEntities = Array.from(
this.el.querySelectorAll('[street-segment]')
);
this.setupMutationObserver();
},
createOrUpdateJustifiedDirtBox: function () {
const data = this.data;
Expand Down Expand Up @@ -326,6 +359,9 @@ AFRAME.registerComponent('managed-street', {
}
},
remove: function () {
if (this.observer) {
this.observer.disconnect();
}
this.managedEntities.forEach(
(entity) => entity.parentNode && entity.remove()
);
Expand Down

0 comments on commit 172d360

Please sign in to comment.