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

84 lines
2.9KB

  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. return null
  22. }
  23. if (!this._timing && nchar > 0) {
  24. this._timing = Date.now()
  25. return null
  26. }
  27. let time = Date.now()
  28. let elapsed = (time - this._timing) / 1000
  29. return {
  30. wpm: nword / elapsed * 60,
  31. cps: nchar / elapsed,
  32. time,
  33. text: el.value
  34. }
  35. },
  36. setValue: function(el, value) {
  37. // This method is used for restoring the bookmarked state of your input
  38. // and allows you to set the input's state without triggering reactivity.
  39. // Basically, reverses .getValue()
  40. // e.g.; el.value = value
  41. console.error('typingSpeed.setValue() is not yet defined');
  42. },
  43. receiveMessage: function(el, data) {
  44. // Given the input's container and data, update the input
  45. // and its elements to reflect the given data.
  46. // The messages are sent from R/Shiny via
  47. // R> session$sendInputMessage(inputId, data)
  48. console.error('typingSpeed.receiveMessage() is not yet defined');
  49. // If you want the update to trigger reactivity, trigger a subscribed event
  50. // $(el).trigger("keyup")
  51. },
  52. subscribe: function(el, callback) {
  53. // Listen to events on your input element. The following block listens to
  54. // the change event, but you might want to listen to another event.
  55. // Repeat the block for each event type you want to subscribe to.
  56. $(el).on("keyup.typingSpeed", function(e) {
  57. // Use callback() or callback(true).
  58. // If using callback(true) the rate policy applies,
  59. // for example if you need to throttle or debounce
  60. // the values being sent back to the server.
  61. callback();
  62. });
  63. },
  64. getRatePolicy: function() {
  65. return {
  66. policy: 'debounce', // 'debounce', 'throttle' or 'direct' (default)
  67. delay: 100 // milliseconds for debounce or throttle
  68. };
  69. },
  70. unsubscribe: function(el) {
  71. $(el).off(".typingSpeed");
  72. }
  73. });
  74. Shiny.inputBindings.register(typingSpeed, 'js4shiny.typingSpeed');