Headers.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. <img
  148. :src="`/images/states/${$colorMode.value}/add_category.svg`"
  149. loading="lazy"
  150. class="
  151. flex-col
  152. my-4
  153. object-contain object-center
  154. h-16
  155. w-16
  156. inline-flex
  157. "
  158. />
  159. <span class="text-center pb-4">
  160. {{ $t("empty.headers") }}
  161. </span>
  162. <ButtonSecondary
  163. filled
  164. :label="`${$t('add.new')}`"
  165. svg="plus"
  166. @click.native="addHeader"
  167. />
  168. </div>
  169. </div>
  170. </AppSection>
  171. </template>
  172. <script setup lang="ts">
  173. import { ref, useContext, watch } from "@nuxtjs/composition-api"
  174. import { useCodemirror } from "~/helpers/editor/codemirror"
  175. import {
  176. addRESTHeader,
  177. deleteAllRESTHeaders,
  178. deleteRESTHeader,
  179. restHeaders$,
  180. setRESTHeaders,
  181. updateRESTHeader,
  182. } from "~/newstore/RESTSession"
  183. import { commonHeaders } from "~/helpers/headers"
  184. import { useReadonlyStream } from "~/helpers/utils/composables"
  185. import { HoppRESTHeader } from "~/helpers/types/HoppRESTRequest"
  186. const {
  187. $toast,
  188. app: { i18n },
  189. } = useContext()
  190. const t = i18n.t.bind(i18n)
  191. const bulkMode = ref(false)
  192. const bulkHeaders = ref("")
  193. const bulkEditor = ref<any | null>(null)
  194. useCodemirror(bulkEditor, bulkHeaders, {
  195. extendedEditorConfig: {
  196. mode: "text/x-yaml",
  197. placeholder: `${t("state.bulk_mode_placeholder")}`,
  198. },
  199. linter: null,
  200. completer: null,
  201. })
  202. watch(bulkHeaders, () => {
  203. try {
  204. const transformation = bulkHeaders.value.split("\n").map((item) => ({
  205. key: item.substring(0, item.indexOf(":")).trim().replace(/^\/\//, ""),
  206. value: item.substring(item.indexOf(":") + 1).trim(),
  207. active: !item.trim().startsWith("//"),
  208. }))
  209. setRESTHeaders(transformation)
  210. } catch (e) {
  211. $toast.error(`${t("error.something_went_wrong")}`, {
  212. icon: "error_outline",
  213. })
  214. console.error(e)
  215. }
  216. })
  217. const headers$ = useReadonlyStream(restHeaders$, [])
  218. watch(
  219. headers$,
  220. (newValue) => {
  221. if (
  222. (newValue[newValue.length - 1]?.key !== "" ||
  223. newValue[newValue.length - 1]?.value !== "") &&
  224. newValue.length
  225. )
  226. addHeader()
  227. },
  228. { deep: true }
  229. )
  230. const addHeader = () => {
  231. addRESTHeader({ key: "", value: "", active: true })
  232. }
  233. const updateHeader = (index: number, item: HoppRESTHeader) => {
  234. updateRESTHeader(index, item)
  235. }
  236. const deleteHeader = (index: number) => {
  237. deleteRESTHeader(index)
  238. }
  239. const clearContent = () => {
  240. deleteAllRESTHeaders()
  241. }
  242. </script>