Skip to content

Commit

Permalink
feat: fully customizable draw tooltips (simpleshape , edittoolbar, ed…
Browse files Browse the repository at this point in the history
…ithandlers)
  • Loading branch information
trafficonese committed Jul 22, 2024
1 parent 43fc70e commit efd435f
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export(drawPolylineOptions)
export(drawRectangleOptions)
export(drawShapeOptions)
export(editToolbarOptions)
export(edithandlersOptions)
export(edittoolbarOptions)
export(enableMeasurePath)
export(enableTileCaching)
export(gpsOptions)
Expand Down
11 changes: 10 additions & 1 deletion R/draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ drawDependencies <- function(drag = TRUE) {
#' @param singleFeature When set to TRUE, only one feature can be drawn at a time, the previous ones being removed.
#' @param toolbar See \code{\link{toolbarOptions}}. Set to \code{NULL} to take Leaflets default values.
#' @param handlers See \code{\link{handlersOptions}}. Set to \code{NULL} to take Leaflets default values.
#' @param edittoolbar See \code{\link{edittoolbarOptions}}. Set to \code{NULL} to take Leaflets default values.
#' @param edithandlers See \code{\link{edithandlersOptions}}. Set to \code{NULL} to take Leaflets default values.
#' @param drag When set to \code{TRUE}, the drawn features will be draggable during editing, utilizing
#' the \code{Leaflet.Draw.Drag} plugin. Otherwise, this library will not be included.
#'
Expand Down Expand Up @@ -92,13 +94,18 @@ addDrawToolbar <- function(
singleFeature = FALSE,
toolbar = NULL,
handlers = NULL,
edittoolbar = NULL,
edithandlers = NULL,
drag = TRUE) {

if (!is.null(targetGroup) && !is.null(targetLayerId)) {
stop("To edit existing features either specify a targetGroup or a targetLayerId, but not both")
}

if (!inherits(toolbar, "list")) toolbar <- NULL
if (!inherits(handlers, "list")) handlers <- NULL
if (!inherits(edittoolbar, "list")) edittoolbar <- NULL
if (!inherits(edithandlers, "list")) edithandlers <- NULL

map$dependencies <- c(map$dependencies, drawDependencies(drag))

Expand Down Expand Up @@ -132,7 +139,9 @@ addDrawToolbar <- function(
)),
edit = editOptions,
toolbar = toolbar,
handlers = handlers
handlers = handlers,
edittoolbar = edittoolbar,
edithandlers = edithandlers
)

leaflet::invokeMethod(
Expand Down
77 changes: 76 additions & 1 deletion R/drawOptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ editToolbarOptions <- function(
#' @param circle List of options for circle tooltips.
#' @param marker List of options for marker tooltips.
#' @param circlemarker List of options for circlemarker tooltips.
#' @param simpleshape List of options for simpleshape tooltips.
#' @export
#' @examples \dontrun{
#' library(leaflet)
Expand Down Expand Up @@ -279,6 +280,9 @@ handlersOptions <- function(
),
circlemarker = list(
tooltipStart = "Click and drag to draw circle."
),
simpleshape = list(
tooltipEnd = "Release mouse to finish drawing."
)) {
leaflet::filterNULL(list(
polyline = list(
Expand All @@ -302,7 +306,8 @@ handlersOptions <- function(
tooltip = list(start = circle$tooltipStart)
),
marker = list(tooltip = list(start = marker$tooltipStart)),
circlemarker = list(tooltip = list(start = circlemarker$tooltipStart))
circlemarker = list(tooltip = list(start = circlemarker$tooltipStart)),
simpleshape = list(tooltip = list(end = simpleshape$tooltipEnd))
))
}

Expand Down Expand Up @@ -377,3 +382,73 @@ toolbarOptions <- function(
)
))
}







#' Options for editing edit handlers
#' @description Customize edit handlers for \code{\link{addDrawToolbar}}
#' @param edit List of options for editing tooltips.
#' @param remove List of options for removing tooltips.
#' @export
edithandlersOptions <- function(
edit = list(
tooltipText = "Drag handles or markers to edit features.",
tooltipSubtext = "Click cancel to undo changes."
),
remove = list(
tooltipText = "Click on a feature to remove."
)) {

leaflet::filterNULL(list(
edit = list(
tooltip = list(
text = edit$tooltipText,
subtext = edit$tooltipSubtext
)
),
remove = list(
tooltip = list(
text = remove$tooltipText
)
)
))
}



#' Options for editing the toolbar
#' @description Customize the edit toolbar for \code{\link{addDrawToolbar}}
#' @param actions List of options for edit action tooltips.
#' @param buttons List of options for edit button tooltips.
#' @export
edittoolbarOptions <- function(
actions = list(
save = list(
title = "Save changes",
text = "Save"
),
cancel = list(
title = "Cancel editing, discards all changes",
text = "Cancel"
),
clearAll = list(
title = "Clear all layers",
text = "Clear All"
)
),
buttons = list(
edit = "Edit layers",
editDisabled = "No layers to edit",
remove = "Delete layers",
removeDisabled = "No layers to delete"
)) {

leaflet::filterNULL(list(
actions = actions,
buttons = buttons
))
}
14 changes: 14 additions & 0 deletions inst/htmlwidgets/bindings/lfx-draw-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId,

editOptions.featureGroup = editableFeatureGroup;
options.edit = editOptions;

if (options && options.edittoolbar) {
var rtool = options.edittoolbar;
var tooldef = L.drawLocal.draw.toolbar;
L.drawLocal.edit.toolbar.buttons = Object.assign({}, tooldef.buttons, rtool.buttons);
L.drawLocal.edit.toolbar.actions = Object.assign({}, tooldef.actions, rtool.actions);
}

if (options && options.edithandlers) {
var rhand = options.edithandlers;
L.drawLocal.edit.handlers.edit = Object.assign({}, rhand.buttons, rtool.edit);
L.drawLocal.edit.handlers.remove = Object.assign({}, rhand.actions, rtool.remove);
}
}

// Set Toolbar / Handlers options if provided. Changes the default values.
Expand All @@ -114,6 +127,7 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId,
L.drawLocal.draw.handlers.polygon = Object.assign({}, handldef.polygon, rhand.polygon);
L.drawLocal.draw.handlers.polyline = Object.assign({}, handldef.polyline, rhand.polyline);
L.drawLocal.draw.handlers.rectangle = Object.assign({}, handldef.rectangle, rhand.rectangle);
L.drawLocal.draw.handlers.simpleshape = Object.assign({}, handldef.simpleshape, rhand.simpleshape);
}

// Create new Drawing Control
Expand Down
2 changes: 1 addition & 1 deletion inst/htmlwidgets/build/lfx-draw/lfx-draw-bindings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit efd435f

Please sign in to comment.