diff --git a/inst/htmlwidgets/lib/draw/draw-bindings.js b/inst/htmlwidgets/lib/draw/draw-bindings.js index 0f497cf7..8300ccfc 100644 --- a/inst/htmlwidgets/lib/draw/draw-bindings.js +++ b/inst/htmlwidgets/lib/draw/draw-bindings.js @@ -89,7 +89,6 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti } var layer = e.layer; - editableFeatureGroup.addLayer(layer); // assign a unique key to the newly created feature var featureId = L.stamp(layer); @@ -106,6 +105,15 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti if(typeof layer.getRadius === 'function') { layer.feature.properties.radius = layer.getRadius(); } +debugger; + // add the newly drawn shape/feature to layerManager + // so that can be used from R side methods + map.layerManager.addLayer( + layer, + null, //category + layer.options.layerId || String(L.stamp(layer)), //layerId or if no layerId use stamp + targetGroup //group which is targetGroup + ); if (!HTMLWidgets.shinyMode) return; @@ -174,6 +182,33 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti if(typeof layer.getRadius === 'function') { layer.feature.properties.radius = layer.getRadius(); } + + + // remove the deleted shape/feature from layerManager + // for proper R side methods + // layerManager only supports removal with category and layerId + // as far as I can tell + + // if feature not from Leaflet.draw then need to find category + // probably should make this a separate testable function + var category = null; + // layerManager does not expose category getter for us + // so have to rely on internal _byCategory + if(Object.keys(map.layerManager._byCategory)) { + // surely there is a better way + Object.keys(map.layerManager._byCategory).forEach(function(cat) { + if( + Object.keys(map.layerManager._byCategory[cat]).indexOf(String(L.stamp(layer))) > -1 + ) { + category = cat; + } + }); + } + + map.layerManager.removeLayer( + category || "null", //category + layer.options.layerId || String(L.stamp(layer)) // layerId - we used L.stamp + ); }); if (!HTMLWidgets.shinyMode) return;