|
| 1 | + |
| 2 | +#' Proxy for editor htmlwidget |
| 3 | +#' |
| 4 | +#' @param shinyId single-element character vector indicating the output ID of the |
| 5 | +#' chart to modify (if invoked from a Shiny module, the namespace will be added |
| 6 | +#' automatically). |
| 7 | +#' @param session the Shiny session object to which the chart belongs; usually the |
| 8 | +#' default value will suffice. |
| 9 | +#' |
| 10 | +#' @return A `editor_proxy` object. |
| 11 | +#' |
| 12 | +#' @family editor proxy methods |
| 13 | +#' |
| 14 | +#' @export |
| 15 | +#' |
| 16 | +#' @importFrom shiny getDefaultReactiveDomain |
| 17 | +#' |
| 18 | +#' @examples |
| 19 | +#' \dontrun{ |
| 20 | +#' |
| 21 | +#' # Consider having created a editor widget with |
| 22 | +#' editorOutput("my_editor") # UI |
| 23 | +#' output$my_editor <- renderEditor({}) # Server |
| 24 | +#' |
| 25 | +#' # Then you can call proxy methods in observer: |
| 26 | +#' |
| 27 | +#' # set editor proxy then call a cal_proxy_* function |
| 28 | +#' editor_proxy("my_editor") %>% |
| 29 | +#' cal_proxy_today() |
| 30 | +#' |
| 31 | +#' # or directly |
| 32 | +#' cal_proxy_today("my_editor") |
| 33 | +#' |
| 34 | +#' } |
| 35 | +editor_proxy <- function(shinyId, session = shiny::getDefaultReactiveDomain()) { |
| 36 | + if (is.null(session)) { |
| 37 | + stop("editor_proxy must be called from the server function of a Shiny app") |
| 38 | + } |
| 39 | + if (!is.null(session$ns) && nzchar(session$ns(NULL)) && substring(shinyId, 1, nchar(session$ns(""))) != session$ns("")) { |
| 40 | + shinyId <- session$ns(shinyId) |
| 41 | + } |
| 42 | + structure( |
| 43 | + list( |
| 44 | + session = session, |
| 45 | + id = shinyId, |
| 46 | + x = list() |
| 47 | + ), |
| 48 | + class = c("editor_proxy", "htmlwidgetProxy") |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +#' Change editor's preview style |
| 54 | +#' |
| 55 | +#' @param proxy A [editor_proxy()] or `outputId` of the editor |
| 56 | +#' @param style Style for the editor : 'tab' or 'vertical'. |
| 57 | +#' |
| 58 | +#' @return A `editor_proxy` object. |
| 59 | +#' @export |
| 60 | +#' |
| 61 | +#' @family editor proxy methods |
| 62 | +#' |
| 63 | +#' @example examples/editor-proxy.R |
| 64 | +editor_proxy_change_preview <- function(proxy, style = c("tab", "vertical")) { |
| 65 | + if (is.character(proxy)) { |
| 66 | + proxy <- editor_proxy(proxy) |
| 67 | + } |
| 68 | + .call_proxy( |
| 69 | + proxy = proxy, |
| 70 | + name = "editor-change-preview", |
| 71 | + style = match.arg(style) |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +#' Insert text in an editor |
| 76 | +#' |
| 77 | +#' @param proxy A [editor_proxy()] or `outputId` of the editor |
| 78 | +#' @param text Text to insert. |
| 79 | +#' |
| 80 | +#' @return A `editor_proxy` object. |
| 81 | +#' @export |
| 82 | +#' |
| 83 | +#' @family editor proxy methods |
| 84 | +#' |
| 85 | +#' @example examples/editor-proxy.R |
| 86 | +editor_proxy_insert <- function(proxy, text) { |
| 87 | + if (is.character(proxy)) { |
| 88 | + proxy <- editor_proxy(proxy) |
| 89 | + } |
| 90 | + .call_proxy( |
| 91 | + proxy = proxy, |
| 92 | + name = "editor-insert-text", |
| 93 | + text = text |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +#' @title Show/hide an editor |
| 99 | +#' |
| 100 | +#' @param proxy A [editor_proxy()] `htmlwidget` object. |
| 101 | +#' |
| 102 | +#' @export |
| 103 | +#' |
| 104 | +#' @return A `editor_proxy` object. |
| 105 | +#' |
| 106 | +#' @name editor-proxy-show-hide |
| 107 | +#' @family editor proxy methods |
| 108 | +#' |
| 109 | +#' @example examples/editor-proxy.R |
| 110 | +editor_proxy_show <- function(proxy) { |
| 111 | + if (is.character(proxy)) { |
| 112 | + proxy <- editor_proxy(proxy) |
| 113 | + } |
| 114 | + .call_proxy( |
| 115 | + proxy = proxy, |
| 116 | + name = "editor-show" |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +#' @export |
| 121 | +#' @rdname editor-proxy-show-hide |
| 122 | +editor_proxy_hide <- function(proxy) { |
| 123 | + if (is.character(proxy)) { |
| 124 | + proxy <- editor_proxy(proxy) |
| 125 | + } |
| 126 | + .call_proxy( |
| 127 | + proxy = proxy, |
| 128 | + name = "editor-hide" |
| 129 | + ) |
| 130 | +} |
0 commit comments