123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <template>
- <div class="flex flex-col flex-1">
- <div v-if="showEventField" class="flex items-center justify-between p-4">
- <input
- id="event_name"
- v-model="eventName"
- class="input"
- name="event_name"
- :placeholder="`${t('socketio.event_name')}`"
- type="text"
- autocomplete="off"
- />
- </div>
- <div
- class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperSecondaryStickyFold"
- >
- <span class="flex items-center">
- <label class="font-semibold text-secondaryLight">
- {{ $t("websocket.message") }}
- </label>
- <tippy
- ref="contentTypeOptions"
- interactive
- trigger="click"
- theme="popover"
- arrow
- >
- <template #trigger>
- <span class="select-wrapper">
- <ButtonSecondary
- :label="contentType || $t('state.none').toLowerCase()"
- class="pr-8 ml-2 rounded-none"
- />
- </span>
- </template>
- <div class="flex flex-col" role="menu">
- <SmartItem
- v-for="(contentTypeItem, index) in validContentTypes"
- :key="`contentTypeItem-${index}`"
- :label="contentTypeItem"
- :info-icon="contentTypeItem === contentType ? 'done' : ''"
- :active-info-icon="contentTypeItem === contentType"
- @click.native="
- () => {
- contentType = contentTypeItem
- $refs.contentTypeOptions.tippy().hide()
- }
- "
- />
- </div>
- </tippy>
- </span>
- <div class="flex">
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip', delay: [500, 20], allowHTML: true }"
- :title="`${t('action.send')}`"
- :label="`${t('action.send')}`"
- :disabled="!communicationBody || !isConnected"
- svg="send"
- class="rounded-none !text-accent !hover:text-accentDark"
- @click.native="sendMessage()"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- to="https://docs.hoppscotch.io/features/body"
- blank
- :title="t('app.wiki')"
- svg="help-circle"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('state.linewrap')"
- :class="{ '!text-accent': linewrapEnabled }"
- svg="wrap-text"
- @click.native.prevent="linewrapEnabled = !linewrapEnabled"
- />
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('action.clear')"
- svg="trash-2"
- @click.native="clearContent"
- />
- <ButtonSecondary
- v-if="contentType && contentType == 'JSON'"
- ref="prettifyRequest"
- v-tippy="{ theme: 'tooltip' }"
- :title="t('action.prettify')"
- :svg="prettifyIcon"
- @click.native="prettifyRequestBody"
- />
- <label for="payload">
- <ButtonSecondary
- v-tippy="{ theme: 'tooltip' }"
- :title="t('import.title')"
- svg="file-plus"
- @click.native="$refs.payload.click()"
- />
- </label>
- <input
- ref="payload"
- class="input"
- name="payload"
- type="file"
- @change="uploadPayload"
- />
- </div>
- </div>
- <div ref="wsCommunicationBody" class="flex flex-col flex-1"></div>
- </div>
- </template>
- <script setup lang="ts">
- import { computed, reactive, ref } from "@nuxtjs/composition-api"
- import { pipe } from "fp-ts/function"
- import * as TO from "fp-ts/TaskOption"
- import * as O from "fp-ts/Option"
- import { refAutoReset } from "@vueuse/core"
- import { useCodemirror } from "~/helpers/editor/codemirror"
- import jsonLinter from "~/helpers/editor/linting/json"
- import { readFileAsText } from "~/helpers/functional/files"
- import { useI18n, useToast } from "~/helpers/utils/composables"
- import { isJSONContentType } from "~/helpers/utils/contenttypes"
- defineProps({
- showEventField: {
- type: Boolean,
- default: false,
- },
- isConnected: {
- type: Boolean,
- default: false,
- },
- })
- const emit = defineEmits<{
- (
- e: "send-message",
- body: {
- eventName: string
- message: string
- }
- ): void
- }>()
- const t = useI18n()
- const toast = useToast()
- const linewrapEnabled = ref(true)
- const wsCommunicationBody = ref<HTMLElement>()
- const prettifyIcon = refAutoReset<"wand" | "check" | "info">("wand", 1000)
- const knownContentTypes = {
- JSON: "application/ld+json",
- Raw: "text/plain",
- } as const
- const validContentTypes = Object.keys(knownContentTypes)
- const contentType = ref<keyof typeof knownContentTypes>("JSON")
- const eventName = ref("")
- const communicationBody = ref("")
- const rawInputEditorLang = computed(() => knownContentTypes[contentType.value])
- const langLinter = computed(() =>
- isJSONContentType(contentType.value) ? jsonLinter : null
- )
- useCodemirror(
- wsCommunicationBody,
- communicationBody,
- reactive({
- extendedEditorConfig: {
- lineWrapping: linewrapEnabled,
- mode: rawInputEditorLang,
- placeholder: t("websocket.message").toString(),
- },
- linter: langLinter,
- completer: null,
- environmentHighlights: true,
- })
- )
- const clearContent = () => {
- communicationBody.value = ""
- }
- const sendMessage = () => {
- if (!communicationBody.value) return
- emit("send-message", {
- eventName: eventName.value,
- message: communicationBody.value,
- })
- communicationBody.value = ""
- }
- const uploadPayload = async (e: InputEvent) => {
- const result = await pipe(
- (e.target as HTMLInputElement).files?.[0],
- TO.fromNullable,
- TO.chain(readFileAsText)
- )()
- if (O.isSome(result)) {
- communicationBody.value = result.value
- toast.success(`${t("state.file_imported")}`)
- } else {
- toast.error(`${t("action.choose_file")}`)
- }
- }
- const prettifyRequestBody = () => {
- try {
- const jsonObj = JSON.parse(communicationBody.value)
- communicationBody.value = JSON.stringify(jsonObj, null, 2)
- prettifyIcon.value = "check"
- } catch (e) {
- console.error(e)
- prettifyIcon.value = "info"
- toast.error(`${t("error.json_prettify_invalid_body")}`)
- }
- }
- </script>
|