Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

89 lines
3.0KB

  1. // Ref: https://shiny.rstudio.com/articles/building-inputs.html
  2. // Ref: https://github.com/rstudio/shiny/blob/master/srcjs/input_binding.js
  3. const typingSpeed = new Shiny.InputBinding();
  4. $.extend(typingSpeed, {
  5. _timing: null,
  6. find: function(scope) {
  7. // Specify the selector that identifies your input. `scope` is a general
  8. // parent of your input elements. This function should return the nodes of
  9. // ALL of the inputs that are inside `scope`. These elements should all
  10. // have IDs that are used as the inputId on the server side.
  11. return scope.querySelectorAll(".typing-speed textarea");
  12. },
  13. getValue: function(el) {
  14. // For a particular input, this function is given the element containing
  15. // your input. In this function, find or construct the value that will be
  16. // returned to Shiny. The ID of `el` is used for the inputId.
  17. const nchar = el.value.length;
  18. const nword = el.value.split(' ').length;
  19. if (nchar === 0) {
  20. this._timing = null
  21. Shiny.setInputValue(el.id + '_reset', Date.now())
  22. return null
  23. }
  24. if (!this._timing && nchar > 0) {
  25. this._timing = Date.now()
  26. return null
  27. }
  28. if (nword < 3) return null
  29. let time = Date.now()
  30. let elapsed = (time - this._timing) / 1000
  31. return {
  32. wpm: nword / elapsed * 60,
  33. cps: nchar / elapsed,
  34. time,
  35. text: el.value
  36. }
  37. },
  38. setValue: function(el, value) {
  39. // This method is used for restoring the bookmarked state of your input
  40. // and allows you to set the input's state without triggering reactivity.
  41. // Basically, reverses .getValue()
  42. // e.g.; el.value = value
  43. console.error('typingSpeed.setValue() is not yet defined');
  44. },
  45. receiveMessage: function(el, data) {
  46. // Given the input's container and data, update the input
  47. // and its elements to reflect the given data.
  48. // The messages are sent from R/Shiny via
  49. // R> session$sendInputMessage(inputId, data)
  50. // If you want the update to trigger reactivity, trigger a subscribed event
  51. if (typeof data === 'boolean' && data) {
  52. el.value = ''
  53. $(el).trigger('keyup')
  54. }
  55. },
  56. subscribe: function(el, callback) {
  57. // Listen to events on your input element. The following block listens to
  58. // the change event, but you might want to listen to another event.
  59. // Repeat the block for each event type you want to subscribe to.
  60. $(el).on("keyup.typingSpeed", function(e) {
  61. // Use callback() or callback(true).
  62. // If using callback(true) the rate policy applies,
  63. // for example if you need to throttle or debounce
  64. // the values being sent back to the server.
  65. callback(true);
  66. });
  67. },
  68. getRatePolicy: function() {
  69. return {
  70. policy: 'throttle', // 'debounce', 'throttle' or 'direct' (default)
  71. delay: 1000 // milliseconds for debounce or throttle
  72. };
  73. },
  74. unsubscribe: function(el) {
  75. $(el).off(".typingSpeed");
  76. }
  77. });
  78. Shiny.inputBindings.register(typingSpeed, 'js4shiny.typingSpeed');