Headers.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <AppSection label="headers">
  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("request.header_list") }}
  18. </label>
  19. <div class="flex">
  20. <ButtonSecondary
  21. v-tippy="{ theme: 'tooltip' }"
  22. to="https://docs.hoppscotch.io/features/headers"
  23. blank
  24. :title="$t('app.wiki')"
  25. svg="help-circle"
  26. />
  27. <ButtonSecondary
  28. v-tippy="{ theme: 'tooltip' }"
  29. :title="$t('action.clear_all')"
  30. svg="trash-2"
  31. :disabled="bulkMode"
  32. @click.native="clearContent"
  33. />
  34. <ButtonSecondary
  35. v-tippy="{ theme: 'tooltip' }"
  36. :title="$t('state.bulk_mode')"
  37. svg="edit"
  38. :class="{ '!text-accent': bulkMode }"
  39. @click.native="bulkMode = !bulkMode"
  40. />
  41. <ButtonSecondary
  42. v-tippy="{ theme: 'tooltip' }"
  43. :title="$t('add.new')"
  44. svg="plus"
  45. :disabled="bulkMode"
  46. @click.native="addHeader"
  47. />
  48. </div>
  49. </div>
  50. <div v-if="bulkMode" ref="bulkEditor"></div>
  51. <div v-else>
  52. <div
  53. v-for="(header, index) in headers$"
  54. :key="`header-${index}`"
  55. class="divide-x divide-dividerLight border-b border-dividerLight flex"
  56. >
  57. <SmartAutoComplete
  58. :placeholder="`${$t('count.header', { count: index + 1 })}`"
  59. :source="commonHeaders"
  60. :spellcheck="false"
  61. :value="header.key"
  62. autofocus
  63. styles="
  64. bg-transparent
  65. flex
  66. flex-1
  67. py-1
  68. px-4
  69. truncate
  70. "
  71. class="!flex flex-1"
  72. @input="
  73. updateHeader(index, {
  74. key: $event,
  75. value: header.value,
  76. active: header.active,
  77. })
  78. "
  79. />
  80. <SmartEnvInput
  81. v-model="header.value"
  82. :placeholder="`${$t('count.value', { count: index + 1 })}`"
  83. styles="
  84. bg-transparent
  85. flex
  86. flex-1
  87. py-1
  88. px-4
  89. "
  90. @change="
  91. updateHeader(index, {
  92. key: header.key,
  93. value: $event,
  94. active: header.active,
  95. })
  96. "
  97. />
  98. <span>
  99. <ButtonSecondary
  100. v-tippy="{ theme: 'tooltip' }"
  101. :title="
  102. header.hasOwnProperty('active')
  103. ? header.active
  104. ? $t('action.turn_off')
  105. : $t('action.turn_on')
  106. : $t('action.turn_off')
  107. "
  108. :svg="
  109. header.hasOwnProperty('active')
  110. ? header.active
  111. ? 'check-circle'
  112. : 'circle'
  113. : 'check-circle'
  114. "
  115. color="green"
  116. @click.native="
  117. updateHeader(index, {
  118. key: header.key,
  119. value: header.value,
  120. active: header.hasOwnProperty('active')
  121. ? !header.active
  122. : false,
  123. })
  124. "
  125. />
  126. </span>
  127. <span>
  128. <ButtonSecondary
  129. v-tippy="{ theme: 'tooltip' }"
  130. :title="$t('action.remove')"
  131. svg="trash"
  132. color="red"
  133. @click.native="deleteHeader(index)"
  134. />
  135. </span>
  136. </div>
  137. <div
  138. v-if="headers$.length === 0"
  139. class="
  140. flex flex-col
  141. text-secondaryLight
  142. p-4
  143. items-center
  144. justify-center
  145. "
  146. >
  147. <span class="text-center pb-4">
  148. {{ $t("empty.headers") }}
  149. </span>
  150. <ButtonSecondary
  151. filled
  152. :label="`${$t('add.new')}`"
  153. svg="plus"
  154. @click.native="addHeader"
  155. />
  156. </div>
  157. </div>
  158. </AppSection>
  159. </template>
  160. <script setup lang="ts">
  161. import { ref, useContext, watch } from "@nuxtjs/composition-api"
  162. import { useCodemirror } from "~/helpers/editor/codemirror"
  163. import {
  164. addRESTHeader,
  165. deleteAllRESTHeaders,
  166. deleteRESTHeader,
  167. restHeaders$,
  168. setRESTHeaders,
  169. updateRESTHeader,
  170. } from "~/newstore/RESTSession"
  171. import { commonHeaders } from "~/helpers/headers"
  172. import { useReadonlyStream } from "~/helpers/utils/composables"
  173. import { HoppRESTHeader } from "~/helpers/types/HoppRESTRequest"
  174. const {
  175. $toast,
  176. app: { i18n },
  177. } = useContext()
  178. const t = i18n.t.bind(i18n)
  179. const bulkMode = ref(false)
  180. const bulkHeaders = ref("")
  181. const bulkEditor = ref<any | null>(null)
  182. useCodemirror(bulkEditor, bulkHeaders, {
  183. extendedEditorConfig: {
  184. mode: "text/x-yaml",
  185. placeholder: `${t("state.bulk_mode_placeholder")}`,
  186. },
  187. linter: null,
  188. completer: null,
  189. })
  190. watch(bulkHeaders, () => {
  191. try {
  192. const transformation = bulkHeaders.value.split("\n").map((item) => ({
  193. key: item.substring(0, item.indexOf(":")).trim().replace(/^\/\//, ""),
  194. value: item.substring(item.indexOf(":") + 1).trim(),
  195. active: !item.trim().startsWith("//"),
  196. }))
  197. setRESTHeaders(transformation)
  198. } catch (e) {
  199. $toast.error(`${t("error.something_went_wrong")}`, {
  200. icon: "error_outline",
  201. })
  202. console.error(e)
  203. }
  204. })
  205. const headers$ = useReadonlyStream(restHeaders$, [])
  206. watch(
  207. headers$,
  208. (newValue) => {
  209. if (
  210. (newValue[newValue.length - 1]?.key !== "" ||
  211. newValue[newValue.length - 1]?.value !== "") &&
  212. newValue.length
  213. )
  214. addHeader()
  215. },
  216. { deep: true }
  217. )
  218. const addHeader = () => {
  219. addRESTHeader({ key: "", value: "", active: true })
  220. }
  221. const updateHeader = (index: number, item: HoppRESTHeader) => {
  222. updateRESTHeader(index, item)
  223. }
  224. const deleteHeader = (index: number) => {
  225. deleteRESTHeader(index)
  226. }
  227. const clearContent = () => {
  228. deleteAllRESTHeaders()
  229. }
  230. </script>