PreRequestScript.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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('state.linewrap')"
  30. :class="{ '!text-accent': linewrapEnabled }"
  31. svg="corner-down-left"
  32. @click.native.prevent="linewrapEnabled = !linewrapEnabled"
  33. />
  34. <ButtonSecondary
  35. v-tippy="{ theme: 'tooltip' }"
  36. :title="$t('action.clear')"
  37. svg="trash-2"
  38. @click.native="clearContent"
  39. />
  40. </div>
  41. </div>
  42. <div class="border-b border-dividerLight flex">
  43. <div class="border-r border-dividerLight w-2/3">
  44. <div ref="preRrequestEditor"></div>
  45. </div>
  46. <div
  47. class="
  48. bg-primary
  49. h-full
  50. top-upperTertiaryStickyFold
  51. min-w-46
  52. max-w-1/3
  53. p-4
  54. z-9
  55. sticky
  56. overflow-auto
  57. "
  58. >
  59. <div class="text-secondaryLight pb-2">
  60. {{ $t("helpers.pre_request_script") }}
  61. </div>
  62. <SmartAnchor
  63. :label="`${$t('preRequest.learn')}`"
  64. to="https://docs.hoppscotch.io/features/pre-request-script"
  65. blank
  66. />
  67. <h4 class="font-bold text-secondaryLight pt-6">
  68. {{ $t("preRequest.snippets") }}
  69. </h4>
  70. <div class="flex flex-col pt-4">
  71. <TabSecondary
  72. v-for="(snippet, index) in snippets"
  73. :key="`snippet-${index}`"
  74. :label="snippet.name"
  75. active
  76. @click.native="useSnippet(snippet.script)"
  77. />
  78. </div>
  79. </div>
  80. </div>
  81. </AppSection>
  82. </template>
  83. <script setup lang="ts">
  84. import { reactive, ref, useContext } from "@nuxtjs/composition-api"
  85. import { usePreRequestScript } from "~/newstore/RESTSession"
  86. import snippets from "~/helpers/preRequestScriptSnippets"
  87. import { useCodemirror } from "~/helpers/editor/codemirror"
  88. import linter from "~/helpers/editor/linting/preRequest"
  89. import completer from "~/helpers/editor/completion/preRequest"
  90. const {
  91. app: { i18n },
  92. } = useContext()
  93. const t = i18n.t.bind(i18n)
  94. const preRequestScript = usePreRequestScript()
  95. const preRrequestEditor = ref<any | null>(null)
  96. const linewrapEnabled = ref(true)
  97. useCodemirror(
  98. preRrequestEditor,
  99. preRequestScript,
  100. reactive({
  101. extendedEditorConfig: {
  102. mode: "application/javascript",
  103. lineWrapping: linewrapEnabled,
  104. placeholder: `${t("preRequest.javascript_code")}`,
  105. },
  106. linter,
  107. completer,
  108. })
  109. )
  110. const useSnippet = (script: string) => {
  111. preRequestScript.value += script
  112. }
  113. const clearContent = () => {
  114. preRequestScript.value = ""
  115. }
  116. </script>