🤷‍♂️ RStudio Addin to Search and Copy Emoji
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

180 lines
5.8KB

  1. #' The ermoji emoji gadget
  2. #'
  3. #' Opens a miniUI based Shiny gadget in the RStudio Viewer pane with a
  4. #' searchable table of emoji. Select a row and click the copy desired button.
  5. #'
  6. #' @param clipout Should the gadget attempt to write to the clipboard?
  7. #' @param ... Ignored at this time
  8. #' @name ermoji
  9. #' @return nothing
  10. #' @export
  11. ermoji_gadget <- function(clipout = clipr::clipr_available(), ...) {
  12. require(shiny)
  13. require(miniUI)
  14. runGadget(ermoji_ui, ermoji_server(clipout, ...), viewer = paneViewer(500), stopOnCancel = FALSE)
  15. }
  16. #' @rdname ermoji
  17. #' @export
  18. ermoji_shiny <- function(clipout = clipr::clipr_available(), ...) {
  19. require(shiny)
  20. require(miniUI)
  21. shinyApp(ui = ermoji_ui, server = ermoji_server(clipout, ...))
  22. }
  23. ermoji_ui <- miniPage(
  24. title = "ermoji",
  25. tags$head(
  26. tags$style(
  27. HTML("
  28. .dropdown-item {
  29. display: block;
  30. width: 100%;
  31. padding: .25rem 1.5rem;
  32. clear: both;
  33. font-weight: 400;
  34. color: #212529;
  35. text-align: inherit;
  36. white-space: nowrap;
  37. background-color: transparent;
  38. border: 0;
  39. }
  40. .dropdown-menu {
  41. color: #212529;
  42. text-align: left;
  43. list-style: none;
  44. }
  45. ")
  46. )
  47. ),
  48. gadgetTitleBar("ermoji"),
  49. miniContentPanel(
  50. padding = 10,
  51. DT::dataTableOutput('emojis', height = "100%", width = "98%")
  52. ),
  53. miniButtonBlock(
  54. actionButton("copy_name", "Copy :emoji_name:", class = "btn-success"),
  55. tags$div(class = "btn-group dropup", style = "width: 33%",
  56. tags$button(class = "btn btn-warning dropdown-toggle", href = "#",
  57. role = "button", id = "dropdownMenuLink", style = "width: 100%",
  58. "data-toggle" = "dropdown", "aria-haspopup" = "true",
  59. "aria-expanded" = "false",
  60. "Copy Unicode"),
  61. tags$div(class = "dropdown-menu", style = "width: 100%",
  62. "aria-labelledby"="dropdownMenuLink",
  63. actionLink("copy_utf", "Copy unicode", class = "dropdown-item"),
  64. actionLink("copy_html", "Copy HTML", class = "dropdown-item")
  65. )
  66. ),
  67. actionButton("copy_gliph", "Copy Emoji", class = "btn-primary")
  68. )
  69. )
  70. ermoji_server <- function(clipout = clipr::clipr_available()) {
  71. function(input, output, session) {
  72. output$emojis <- DT::renderDataTable({
  73. emojis <- emo::jis
  74. emojis <- emojis[, c('emoji', 'name', "group", "keywords", "aliases")]
  75. emojis$keywords <- purrr::map_chr(emojis$keywords, ~ paste(., collapse = ", "))
  76. emojis$aliases <- purrr::map_chr(emojis$aliases, ~ paste(., collapse = ", "))
  77. DT::datatable(
  78. emojis,
  79. rownames = FALSE,
  80. colnames = c("Emoji", "Name", "Group", "Keywords", "Aliases"),
  81. filter = "top",
  82. selection = "single",
  83. fillContainer = TRUE,
  84. # style = 'bootstrap',
  85. class = 'compact stripe nowrap hover',
  86. options = list(
  87. searchHighlight = TRUE,
  88. search = list(regex = TRUE, caseInsensitive = FALSE),
  89. columnDefs = list(list(
  90. className = "dt-center", targets = 0
  91. )),
  92. pageLength = 10,
  93. lengthMenu = c(4, 5, 10)
  94. )
  95. )
  96. })
  97. this_emoji <- reactive({
  98. req(input$emojis_rows_selected)
  99. as.list(emo::jis[input$emojis_rows_selected, ])
  100. })
  101. this_emoji_name <- reactive({
  102. # name <- this_emoji()$name
  103. name <- this_emoji()$aliases[[1]][1]
  104. paste0(":", gsub(" ", "_", name), ":")
  105. })
  106. this_emoji_uni <- reactive({
  107. uni <- paste0("\\U", this_emoji()$runes)
  108. gsub(" ", "\\\\U", uni)
  109. })
  110. this_emoji_html <- reactive({
  111. gsub("([0-9A-F]{4,8}) ?", "&#x\\1;", this_emoji()$runes)
  112. })
  113. truncate <- function(x, n = 10) {
  114. if (nchar(x) > n) {
  115. paste0(strtrim(x, n), "...")
  116. } else x
  117. }
  118. observeEvent(input$emojis_rows_selected, {
  119. if (!isTruthy(input$emojis_rows_selected)) {
  120. updateActionButton(session, "copy_name", "Copy :emoji_name:")
  121. updateActionButton(session, "copy_utf", "Copy Unicode")
  122. updateActionButton(session, "copy_html", "Copy HTML")
  123. updateActionButton(session, "copy_gliph", "Copy Emoji")
  124. } else {
  125. updateActionButton(session, "copy_name", paste0("Copy <code>", this_emoji_name(), "</code>"))
  126. updateActionButton(session, "copy_utf", paste("Copy Unicode: <code>", truncate(this_emoji_uni()), "</code>"))
  127. updateActionButton(session, "copy_html", paste("Copy HTML: <code>", escape_html(truncate(this_emoji_html())), "</code>"))
  128. updateActionButton(session, "copy_gliph", paste("Copy", this_emoji()$emoji))
  129. }
  130. })
  131. copy_modal <- function(text) {
  132. showModal(
  133. modalDialog(
  134. title = "Select and Copy",
  135. tags$p("I don't have access to your clipboard. Select the text and", tags$kbd("Ctrl/Cmd"), "+", tags$kbd("c"), "to copy."),
  136. tags$pre(text),
  137. easyClose = TRUE
  138. )
  139. )
  140. }
  141. observeEvent(input$copy_name, {
  142. if (clipout) clipr::write_clip(this_emoji_name()) else copy_modal(this_emoji_name())
  143. })
  144. observeEvent(input$copy_utf, {
  145. if (clipout) clipr::write_clip(this_emoji_uni()) else copy_modal(this_emoji_uni())
  146. })
  147. observeEvent(input$copy_html, {
  148. if (clipout) clipr::write_clip(this_emoji_html()) else copy_modal(this_emoji_html())
  149. })
  150. observeEvent(input$copy_gliph, {
  151. if (clipout) clipr::write_clip(this_emoji()$emoji) else copy_modal(this_emoji()$emoji)
  152. })
  153. observeEvent(input$done, {
  154. stopApp(invisible())
  155. })
  156. observeEvent(input$cancel, {
  157. stopApp(invisible())
  158. })
  159. }
  160. }
  161. escape_html <- function(x) {
  162. x = gsub('&', '&amp;', x)
  163. x = gsub('<', '&lt;', x)
  164. x = gsub('>', '&gt;', x)
  165. x = gsub('"', '&quot;', x)
  166. x
  167. }