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 { useCodemirror } from "~/helpers/editor/codemirror"
  42. import { setRESTRequest } from "~/newstore/RESTSession"
  43. import { useI18n, useToast } from "~/helpers/utils/composables"
  44. import { parseCurlToHoppRESTReq } from "~/helpers/curl"
  45. const t = useI18n()
  46. const toast = useToast()
  47. const curl = ref("")
  48. const curlEditor = ref<any | null>(null)
  49. const props = defineProps<{ show: boolean; text: string }>()
  50. useCodemirror(curlEditor, curl, {
  51. extendedEditorConfig: {
  52. mode: "application/x-sh",
  53. placeholder: `${t("request.enter_curl")}`,
  54. },
  55. linter: null,
  56. completer: null,
  57. environmentHighlights: false,
  58. })
  59. watch(
  60. () => props.show,
  61. () => {
  62. if (props.show) {
  63. curl.value = props.text.toString()
  64. }
  65. },
  66. { immediate: false }
  67. )
  68. const emit = defineEmits<{
  69. (e: "hide-modal"): void
  70. }>()
  71. const hideModal = () => {
  72. emit("hide-modal")
  73. }
  74. const handleImport = () => {
  75. const text = curl.value
  76. try {
  77. const req = parseCurlToHoppRESTReq(text)
  78. setRESTRequest(req)
  79. } catch (e) {
  80. console.error(e)
  81. toast.error(`${t("error.curl_invalid_format")}`)
  82. }
  83. hideModal()
  84. }
  85. const pasteIcon = ref("clipboard")
  86. const handlePaste = async () => {
  87. try {
  88. const text = await navigator.clipboard.readText()
  89. if (text) {
  90. curl.value = text
  91. pasteIcon.value = "check"
  92. setTimeout(() => (pasteIcon.value = "clipboard"), 1000)
  93. }
  94. } catch (e) {
  95. console.error("Failed to copy: ", e)
  96. toast.error(t("profile.no_permission").toString())
  97. }
  98. }
  99. </script>