ImportCurl.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <SmartModal
  3. v-if="show"
  4. dialog
  5. :title="`${t('import.curl')}`"
  6. @close="hideModal"
  7. >
  8. <template #body>
  9. <div class="px-2 h-46">
  10. <div
  11. ref="curlEditor"
  12. class="h-full border rounded border-dividerLight"
  13. ></div>
  14. </div>
  15. </template>
  16. <template #footer>
  17. <span class="flex">
  18. <ButtonPrimary
  19. ref="importButton"
  20. :label="`${t('import.title')}`"
  21. @click.native="handleImport"
  22. />
  23. <ButtonSecondary
  24. :label="`${t('action.cancel')}`"
  25. @click.native="hideModal"
  26. />
  27. </span>
  28. <span class="flex">
  29. <ButtonSecondary
  30. :svg="pasteIcon"
  31. :label="`${t('action.paste')}`"
  32. filled
  33. @click.native="handlePaste"
  34. />
  35. </span>
  36. </template>
  37. </SmartModal>
  38. </template>
  39. <script setup lang="ts">
  40. import { ref, watch } from "@nuxtjs/composition-api"
  41. import { refAutoReset } from "@vueuse/core"
  42. import { useCodemirror } from "~/helpers/editor/codemirror"
  43. import { setRESTRequest } from "~/newstore/RESTSession"
  44. import { useI18n, useToast } from "~/helpers/utils/composables"
  45. import { parseCurlToHoppRESTReq } from "~/helpers/curl"
  46. const t = useI18n()
  47. const toast = useToast()
  48. const curl = ref("")
  49. const curlEditor = ref<any | null>(null)
  50. const props = defineProps<{ show: boolean; text: string }>()
  51. useCodemirror(curlEditor, curl, {
  52. extendedEditorConfig: {
  53. mode: "application/x-sh",
  54. placeholder: `${t("request.enter_curl")}`,
  55. },
  56. linter: null,
  57. completer: null,
  58. environmentHighlights: false,
  59. })
  60. watch(
  61. () => props.show,
  62. () => {
  63. if (props.show) {
  64. curl.value = props.text.toString()
  65. }
  66. },
  67. { immediate: false }
  68. )
  69. const emit = defineEmits<{
  70. (e: "hide-modal"): void
  71. }>()
  72. const hideModal = () => {
  73. emit("hide-modal")
  74. }
  75. const handleImport = () => {
  76. const text = curl.value
  77. try {
  78. const req = parseCurlToHoppRESTReq(text)
  79. setRESTRequest(req)
  80. } catch (e) {
  81. console.error(e)
  82. toast.error(`${t("error.curl_invalid_format")}`)
  83. }
  84. hideModal()
  85. }
  86. const pasteIcon = refAutoReset<"clipboard" | "check">("clipboard", 1000)
  87. const handlePaste = async () => {
  88. try {
  89. const text = await navigator.clipboard.readText()
  90. if (text) {
  91. curl.value = text
  92. pasteIcon.value = "check"
  93. }
  94. } catch (e) {
  95. console.error("Failed to copy: ", e)
  96. toast.error(t("profile.no_permission").toString())
  97. }
  98. }
  99. </script>