🤷‍♂️ 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.

134 lines
3.6KB

  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('.emoji-picker__plugin-container { justify-content: space-between; }')
  38. )
  39. )
  40. }
  41. is_rstudio_dark <- function() {
  42. if (rstudioapi::hasFun("getThemeInfo")) {
  43. rstudioapi::getThemeInfo()$dark
  44. } else {
  45. FALSE
  46. }
  47. }
  48. rstudio_style <- function() {
  49. if (!rstudioapi::hasFun("getThemeInfo")) {
  50. return(NULL)
  51. }
  52. theme <- rstudioapi::getThemeInfo()
  53. shiny::tags$style(shiny::HTML(paste0(
  54. ".emoji-picker {\n",
  55. " --background-color: ", theme$background, ";\n",
  56. " --text-color: ", theme$foreground, ";\n",
  57. "}\n",
  58. "body { background-color: ", theme$background, "; color: ", theme$foreground, ";}\n",
  59. "#picker_type {\n",
  60. " display: flex;\n",
  61. " justify-content: space-between;\n",
  62. " width: 100%;\n",
  63. "}"
  64. )))
  65. }
  66. set_picker_type <- function(style = c("unicode", "html", "emo_ji")) {
  67. if (!rstudioapi::hasFun("setPersistentValue")) {
  68. return()
  69. }
  70. style <- match.arg(style)
  71. rstudioapi::setPersistentValue("ermoji.picker_type", style)
  72. invisible(style)
  73. }
  74. get_picker_type <- function() {
  75. if (!rstudioapi::hasFun("setPersistentValue")) {
  76. return("unicode")
  77. }
  78. style <- rstudioapi::getPersistentValue("ermoji.picker_type")
  79. if (is.null(style)) {
  80. return("unicode")
  81. }
  82. if (!style %in% c("unicode", "html", "emo_ji")) {
  83. return("unicode")
  84. }
  85. style
  86. }
  87. emoji_picker_server <- function(quick_add = TRUE) {
  88. function(input, output, session) {
  89. shiny::observeEvent(input$close, {
  90. shiny::stopApp(invisible(input$emoji))
  91. })
  92. shiny::observe({
  93. session$sendCustomMessage('update_picker_type', get_picker_type())
  94. })
  95. shiny::observeEvent(input$picker_type, {
  96. set_picker_type(input$picker_type)
  97. })
  98. shiny::observeEvent(input$emoji, {
  99. emoji <- switch(
  100. input$picker_type,
  101. "unicode" = input$emoji$emoji,
  102. "html" = input$emoji$html,
  103. "emo_ji" = {
  104. tryCatch({
  105. emo::ji(input$emoji$name)
  106. paste0('emo::ji("', input$emoji$name, '")')
  107. },
  108. error = function(e) {
  109. message("{emo} doesn't know the emoji ", shQuote(input$emoji$name))
  110. NULL
  111. }
  112. )
  113. }
  114. )
  115. if (!is.null(emoji)) {
  116. rstudioapi::insertText(emoji, id = rstudioapi::documentId())
  117. if (isTRUE(quick_add)) {
  118. shiny::stopApp(invisible(input$emoji))
  119. }
  120. }
  121. })
  122. }
  123. }