選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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