PreRequestScript.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <AppSection id="script" :label="$t('preRequest.script')">
  3. <div
  4. class="
  5. bg-primary
  6. border-b border-dividerLight
  7. flex flex-1
  8. top-upperSecondaryStickyFold
  9. pl-4
  10. z-10
  11. sticky
  12. items-center
  13. justify-between
  14. "
  15. >
  16. <label class="font-semibold text-secondaryLight">
  17. {{ $t("preRequest.javascript_code") }}
  18. </label>
  19. <div class="flex">
  20. <ButtonSecondary
  21. v-tippy="{ theme: 'tooltip' }"
  22. to="https://docs.hoppscotch.io/features/pre-request-script"
  23. blank
  24. :title="$t('app.wiki')"
  25. svg="help-circle"
  26. />
  27. <ButtonSecondary
  28. v-tippy="{ theme: 'tooltip' }"
  29. :title="$t('action.clear')"
  30. svg="trash-2"
  31. @click.native="clearContent"
  32. />
  33. </div>
  34. </div>
  35. <div class="border-b border-dividerLight flex">
  36. <div class="border-r border-dividerLight w-2/3">
  37. <SmartJsEditor
  38. v-model="preRequestScript"
  39. :options="{
  40. maxLines: Infinity,
  41. minLines: 16,
  42. autoScrollEditorIntoView: true,
  43. showPrintMargin: false,
  44. useWorker: false,
  45. }"
  46. complete-mode="pre"
  47. />
  48. </div>
  49. <div
  50. class="
  51. bg-primary
  52. h-full
  53. top-upperTertiaryStickyFold
  54. min-w-46
  55. max-w-1/3
  56. p-4
  57. z-9
  58. sticky
  59. overflow-auto
  60. "
  61. >
  62. <div class="text-secondaryLight pb-2">
  63. {{ $t("helpers.pre_request_script") }}
  64. </div>
  65. <SmartAnchor
  66. :label="$t('preRequest.learn')"
  67. to="https://docs.hoppscotch.io/features/pre-request-script"
  68. blank
  69. />
  70. <h4 class="font-bold text-secondaryLight pt-6">
  71. {{ $t("preRequest.snippets") }}
  72. </h4>
  73. <div class="flex flex-col pt-4">
  74. <TabSecondary
  75. v-for="(snippet, index) in snippets"
  76. :key="`snippet-${index}`"
  77. :label="snippet.name"
  78. active
  79. @click.native="useSnippet(snippet.script)"
  80. />
  81. </div>
  82. </div>
  83. </div>
  84. </AppSection>
  85. </template>
  86. <script lang="ts">
  87. import { defineComponent } from "@nuxtjs/composition-api"
  88. import { usePreRequestScript } from "~/newstore/RESTSession"
  89. import preRequestScriptSnippets from "~/helpers/preRequestScriptSnippets"
  90. export default defineComponent({
  91. setup() {
  92. const preRequestScript = usePreRequestScript()
  93. const useSnippet = (script: string) => {
  94. preRequestScript.value += script
  95. }
  96. const clearContent = () => {
  97. preRequestScript.value = ""
  98. }
  99. return {
  100. preRequestScript,
  101. snippets: preRequestScriptSnippets,
  102. useSnippet,
  103. clearContent,
  104. }
  105. },
  106. })
  107. </script>