fix(sync): sync feature layer changes

This commits adds a way to differenciate "end of edit" events that are
triggered rightfully and the one which are triggered during the deletion
of a feature.

Also, directly call the `sync.upsert()` method after the move happened.
This commit is contained in:
Alexis Métaireau 2024-06-24 19:05:29 +02:00
parent 4f19824805
commit 0dcedeb525
2 changed files with 20 additions and 10 deletions

View file

@ -15,7 +15,18 @@ U.FeatureMixin = {
onCommit: function () {
// When the layer is a remote layer, we don't want to sync the creation of the
// points via the websocket, as the other peers will get them themselves.
if (this.datalayer.isRemoteLayer()) return
if (this.datalayer?.isRemoteLayer()) return
// The "endEdit" event is triggered at the end of an edition,
// and will trigger the sync.
// In the case of a deletion (or a change of layer), we don't want this
// event triggered to cause a sync event, as it would reintroduce
// deleted features.
// The `._marked_for_deletion` private property is here to track this status.
if (this._marked_for_deletion == true) {
this._marked_for_deletion = false
return
}
this.sync.upsert(this.toGeoJSON())
},
@ -23,13 +34,10 @@ U.FeatureMixin = {
return this.toGeoJSON().geometry
},
syncDelete: function () {
this.sync.delete()
},
initialize: function (map, latlng, options, id) {
this.map = map
this.sync = map.sync_engine.proxy(this)
this._mark_for_deletion = false
if (typeof options === 'undefined') {
options = {}
@ -271,14 +279,12 @@ U.FeatureMixin = {
}
return false
},
del: function (sync) {
this.isDirty = true
this.map.closePopup()
if (this.datalayer) {
this.datalayer.removeLayer(this)
this.disconnectFromDataLayer(this.datalayer)
if (sync !== false) this.syncDelete()
this.datalayer.removeLayer(this, sync)
}
},
@ -321,7 +327,9 @@ U.FeatureMixin = {
this.datalayer.isDirty = true
this.datalayer.removeLayer(this)
}
datalayer.addLayer(this)
this.sync.upsert(this.toGeoJSON())
datalayer.isDirty = true
this._redraw()
},
@ -499,6 +507,7 @@ U.FeatureMixin = {
onRemove: function (map) {
this.parentClass.prototype.onRemove.call(this, map)
if (this.map.editedFeature === this) {
this._marked_for_deletion = true
this.endEdit()
this.map.editPanel.close()
}

View file

@ -896,8 +896,9 @@ U.DataLayer = L.Evented.extend({
if (this.hasDataLoaded()) this.fire('datachanged')
},
removeLayer: function (feature) {
removeLayer: function (feature, sync) {
const id = L.stamp(feature)
if (sync !== false) feature.sync.delete()
this.layer.removeLayer(feature)
feature.disconnectFromDataLayer(this)
this._index.splice(this._index.indexOf(id), 1)