123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <template>
- <AppSection label="headers">
- <div
- class="bg-primary border-b border-dividerLight flex flex-1 top-upperSecondaryStickyFold pl-4 z-10 sticky items-center justify-between"
- >
- <label class="font-semibold text-secondaryLight">
- {{ t("request.header_list") }}
- </label>
- <div class="flex">
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- to="https://docs.hoppscotch.io/features/headers"
- blank
- :title="t('app.wiki')"
- svg="help-circle"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('action.clear_all')"
- svg="trash-2"
- @click.native="clearContent()"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('state.bulk_mode')"
- svg="edit"
- :class="{ '!text-accent': bulkMode }"
- @click.native="bulkMode = !bulkMode"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('add.new')"
- svg="plus"
- :disabled="bulkMode"
- @click.native="addHeader"
- />
- </div>
- </div>
- <div v-if="bulkMode" ref="bulkEditor"></div>
- <div v-else>
- <div
- v-for="(header, index) in headers$"
- :key="`header-${index}`"
- class="divide-dividerLight divide-x border-b border-dividerLight flex"
- >
- <SmartAutoComplete
- :placeholder="`${t('count.header', { count: index + 1 })}`"
- :source="commonHeaders"
- :spellcheck="false"
- :value="header.key"
- autofocus
- styles="
- bg-transparent
- flex
- flex-1
- py-1
- px-4
- truncate
- "
- class="flex-1 !flex"
- @input="
- updateHeader(index, {
- key: $event,
- value: header.value,
- active: header.active,
- })
- "
- />
- <SmartEnvInput
- v-model="header.value"
- :placeholder="`${t('count.value', { count: index + 1 })}`"
- styles="
- bg-transparent
- flex
- flex-1
- py-1
- px-4
- "
- @change="
- updateHeader(index, {
- key: header.key,
- value: $event,
- active: header.active,
- })
- "
- />
- <span>
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="
- header.hasOwnProperty('active')
- ? header.active
- ? t('action.turn_off')
- : t('action.turn_on')
- : t('action.turn_off')
- "
- :svg="
- header.hasOwnProperty('active')
- ? header.active
- ? 'check-circle'
- : 'circle'
- : 'check-circle'
- "
- color="green"
- @click.native="
- updateHeader(index, {
- key: header.key,
- value: header.value,
- active: header.hasOwnProperty('active')
- ? !header.active
- : false,
- })
- "
- />
- </span>
- <span>
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('action.remove')"
- svg="trash"
- color="red"
- @click.native="deleteHeader(index)"
- />
- </span>
- </div>
- <div
- v-if="headers$.length === 0"
- class="flex flex-col text-secondaryLight p-4 items-center justify-center"
- >
- <img
- :src="`/images/states/${$colorMode.value}/add_category.svg`"
- loading="lazy"
- class="flex-col object-contain object-center h-16 my-4 w-16 inline-flex"
- :alt="`${t('empty.headers')}`"
- />
- <span class="text-center pb-4">
- {{ t("empty.headers") }}
- </span>
- <ButtonSecondary
- filled
- :label="`${t('add.new')}`"
- svg="plus"
- class="mb-4"
- @click.native="addHeader"
- />
- </div>
- </div>
- </AppSection>
- </template>
- <script setup lang="ts">
- import { onBeforeUpdate, ref, watch } from "@nuxtjs/composition-api"
- import { HoppRESTHeader } from "@hoppscotch/data"
- import { useCodemirror } from "~/helpers/editor/codemirror"
- import {
- addRESTHeader,
- deleteAllRESTHeaders,
- deleteRESTHeader,
- restHeaders$,
- setRESTHeaders,
- updateRESTHeader,
- } from "~/newstore/RESTSession"
- import { commonHeaders } from "~/helpers/headers"
- import {
- useReadonlyStream,
- useI18n,
- useToast,
- } from "~/helpers/utils/composables"
- const t = useI18n()
- const toast = useToast()
- const bulkMode = ref(false)
- const bulkHeaders = ref("")
- const bulkEditor = ref<any | null>(null)
- useCodemirror(bulkEditor, bulkHeaders, {
- extendedEditorConfig: {
- mode: "text/x-yaml",
- placeholder: `${t("state.bulk_mode_placeholder")}`,
- },
- linter: null,
- completer: null,
- })
- watch(bulkHeaders, () => {
- try {
- const transformation = bulkHeaders.value.split("\n").map((item) => ({
- key: item.substring(0, item.indexOf(":")).trim().replace(/^\/\//, ""),
- value: item.substring(item.indexOf(":") + 1).trim(),
- active: !item.trim().startsWith("//"),
- }))
- setRESTHeaders(transformation as HoppRESTHeader[])
- } catch (e) {
- toast.error(`${t("error.something_went_wrong")}`)
- console.error(e)
- }
- })
- const headers$ = useReadonlyStream(restHeaders$, [])
- watch(
- headers$,
- (newValue) => {
- if (!bulkMode.value)
- if (
- (newValue[newValue.length - 1]?.key !== "" ||
- newValue[newValue.length - 1]?.value !== "") &&
- newValue.length
- )
- addHeader()
- },
- { deep: true }
- )
- onBeforeUpdate(() => editBulkHeadersLine(-1, null))
- const editBulkHeadersLine = (index: number, item?: HoppRESTHeader | null) => {
- const headers = headers$.value
- bulkHeaders.value = headers
- .reduce((all, header, pIndex) => {
- const current =
- index === pIndex && item != null
- ? `${item.active ? "" : "//"}${item.key}: ${item.value}`
- : `${header.active ? "" : "//"}${header.key}: ${header.value}`
- return [...all, current]
- }, [])
- .join("\n")
- }
- const clearBulkEditor = () => {
- bulkHeaders.value = ""
- }
- const addHeader = () => {
- const empty = { key: "", value: "", active: true }
- const index = headers$.value.length
- addRESTHeader(empty)
- editBulkHeadersLine(index, empty)
- }
- const updateHeader = (index: number, item: HoppRESTHeader) => {
- updateRESTHeader(index, item)
- editBulkHeadersLine(index, item)
- }
- const deleteHeader = (index: number) => {
- const headersBeforeDeletion = headers$.value
- deleteRESTHeader(index)
- editBulkHeadersLine(index, null)
- const deletedItem = headersBeforeDeletion[index]
- if (deletedItem.key || deletedItem.value) {
- toast.success(`${t("state.deleted")}`, {
- action: [
- {
- text: `${t("action.undo")}`,
- onClick: (_, toastObject) => {
- setRESTHeaders(headersBeforeDeletion as HoppRESTHeader[])
- editBulkHeadersLine(index, deletedItem)
- toastObject.goAway(0)
- },
- },
- ],
- })
- }
- }
- const clearContent = () => {
- deleteAllRESTHeaders()
- clearBulkEditor()
- }
- </script>
|