123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <template>
- <SmartModal
- v-if="show"
- dialog
- :title="`${t('import.curl')}`"
- @close="hideModal"
- >
- <template #body>
- <div class="px-2 h-46">
- <div
- ref="curlEditor"
- class="h-full border rounded border-dividerLight"
- ></div>
- </div>
- </template>
- <template #footer>
- <span class="flex">
- <ButtonPrimary
- ref="importButton"
- :label="`${t('import.title')}`"
- @click.native="handleImport"
- />
- <ButtonSecondary
- :label="`${t('action.cancel')}`"
- @click.native="hideModal"
- />
- </span>
- <span class="flex">
- <ButtonSecondary
- :svg="pasteIcon"
- :label="`${t('action.paste')}`"
- filled
- @click.native="handlePaste"
- />
- </span>
- </template>
- </SmartModal>
- </template>
- <script setup lang="ts">
- import { ref, watch } from "@nuxtjs/composition-api"
- import { refAutoReset } from "@vueuse/core"
- import { useCodemirror } from "~/helpers/editor/codemirror"
- import { setRESTRequest } from "~/newstore/RESTSession"
- import { useI18n, useToast } from "~/helpers/utils/composables"
- import { parseCurlToHoppRESTReq } from "~/helpers/curl"
- const t = useI18n()
- const toast = useToast()
- const curl = ref("")
- const curlEditor = ref<any | null>(null)
- const props = defineProps<{ show: boolean; text: string }>()
- useCodemirror(curlEditor, curl, {
- extendedEditorConfig: {
- mode: "application/x-sh",
- placeholder: `${t("request.enter_curl")}`,
- },
- linter: null,
- completer: null,
- environmentHighlights: false,
- })
- watch(
- () => props.show,
- () => {
- if (props.show) {
- curl.value = props.text.toString()
- }
- },
- { immediate: false }
- )
- const emit = defineEmits<{
- (e: "hide-modal"): void
- }>()
- const hideModal = () => {
- emit("hide-modal")
- }
- const handleImport = () => {
- const text = curl.value
- try {
- const req = parseCurlToHoppRESTReq(text)
- setRESTRequest(req)
- } catch (e) {
- console.error(e)
- toast.error(`${t("error.curl_invalid_format")}`)
- }
- hideModal()
- }
- const pasteIcon = refAutoReset<"clipboard" | "check">("clipboard", 1000)
- const handlePaste = async () => {
- try {
- const text = await navigator.clipboard.readText()
- if (text) {
- curl.value = text
- pasteIcon.value = "check"
- }
- } catch (e) {
- console.error("Failed to copy: ", e)
- toast.error(t("profile.no_permission").toString())
- }
- }
- </script>
|