🤷‍♂️ RStudio Addin to Search and Copy Emoji
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

140 lines
3.9KB

  1. #' Emoji Picker
  2. #'
  3. #' An emoji picker gadget that lets you search and find emoji and insert either
  4. #' the unicode, HTML, or `emo::ji()` versions of the emoji. Built using the
  5. #' [Emoji Button](https://emoji-button.js.org/) JavaScript library.
  6. #'
  7. #' @return A list with the `emoji`, the emoji `name` and the `html` entity.
  8. #'
  9. #' @param gadget If `TRUE` the app is run as a gadget, otherwise it's run as a
  10. #' standalone app.
  11. #'
  12. #' @export
  13. emoji_picker <- function(gadget = TRUE) {
  14. shiny::addResourcePath("eb", system.file("picker", package = "ermoji"))
  15. if (gadget) {
  16. shiny::runGadget(
  17. emoji_picker_ui(),
  18. emoji_picker_server(quick_add = TRUE),
  19. viewer = shiny::dialogViewer("ermoji", width = 375, height = 463)
  20. )
  21. } else {
  22. shiny::shinyApp(emoji_picker_ui(), emoji_picker_server(quick_add = FALSE))
  23. }
  24. }
  25. emoji_picker_ui <- function() {
  26. shiny::fluidPage(
  27. id = "picker-gadget",
  28. class = paste(
  29. if (is_rstudio_dark()) "dark-theme",
  30. get_picker_type()
  31. ),
  32. shiny::div(id = "emoji-picker", style = "width: 100%; min-height: 425; position: relative;"),
  33. rstudio_style(),
  34. shiny::tags$script(src = "eb/emoji-picker.js", type = "module"),
  35. shiny::tags$head(
  36. shiny::tags$script(src = 'eb/he.js'),
  37. shiny::tags$style(
  38. '.emoji-picker__plugin-container { justify-content: space-between; }
  39. .emoji-picker { border-radius: 0 !important; }'
  40. )
  41. )
  42. )
  43. }
  44. is_rstudio_dark <- function() {
  45. if (rstudioapi::hasFun("getThemeInfo")) {
  46. rstudioapi::getThemeInfo()$dark
  47. } else {
  48. FALSE
  49. }
  50. }
  51. rstudio_style <- function() {
  52. if (!rstudioapi::hasFun("getThemeInfo")) {
  53. return(NULL)
  54. }
  55. theme <- rstudioapi::getThemeInfo()
  56. shiny::tags$style(shiny::HTML(paste0(
  57. ".emoji-picker {\n",
  58. if (theme$dark) " --dark-background-color: " else " --background-color: ", theme$background, ";\n",
  59. if (theme$dark) " --dark-text-color: " else " --text-color: ", theme$foreground, ";\n",
  60. "}\n",
  61. "body { background-color: ", theme$background, "; color: ", theme$foreground, ";}\n",
  62. "#picker_type {\n",
  63. " display: flex;\n",
  64. " justify-content: space-between;\n",
  65. " width: 100%;\n",
  66. "}"
  67. )))
  68. }
  69. set_picker_type <- function(style = c("unicode", "html", "emo_ji")) {
  70. if (!rstudioapi::hasFun("setPersistentValue")) {
  71. return()
  72. }
  73. style <- match.arg(style)
  74. rstudioapi::setPersistentValue("ermoji.picker_type", style)
  75. invisible(style)
  76. }
  77. get_picker_type <- function() {
  78. if (!rstudioapi::hasFun("setPersistentValue")) {
  79. return("unicode")
  80. }
  81. style <- rstudioapi::getPersistentValue("ermoji.picker_type")
  82. if (is.null(style)) {
  83. return("unicode")
  84. }
  85. if (!style %in% c("unicode", "html", "emo_ji")) {
  86. return("unicode")
  87. }
  88. style
  89. }
  90. emoji_picker_server <- function(quick_add = TRUE, document_id = NULL) {
  91. if (is.null(document_id)) {
  92. document_id <- rstudioapi::getActiveDocumentContext()$id
  93. }
  94. function(input, output, session) {
  95. shiny::observeEvent(input$close, {
  96. shiny::stopApp(invisible(input$emoji))
  97. })
  98. shiny::observe({
  99. session$sendCustomMessage('update_picker_type', get_picker_type())
  100. })
  101. shiny::observeEvent(input$picker_type, {
  102. set_picker_type(input$picker_type)
  103. })
  104. shiny::observeEvent(input$emoji, {
  105. emoji <- switch(
  106. input$picker_type,
  107. "unicode" = input$emoji$emoji,
  108. "html" = input$emoji$html,
  109. "emo_ji" = {
  110. tryCatch({
  111. emo::ji(input$emoji$name)
  112. paste0('emo::ji("', input$emoji$name, '")')
  113. },
  114. error = function(e) {
  115. message("{emo} doesn't know the emoji ", shQuote(input$emoji$name))
  116. NULL
  117. }
  118. )
  119. }
  120. )
  121. if (!is.null(emoji)) {
  122. rstudioapi::insertText(emoji, id = document_id)
  123. if (isTRUE(quick_add)) {
  124. shiny::stopApp(invisible(input$emoji))
  125. }
  126. }
  127. })
  128. }
  129. }