fix: useCodemirror getting non strings as value (#2810)
This commit is contained in:
@@ -63,10 +63,10 @@ import IconFilePlus from "~icons/lucide/file-plus"
|
|||||||
import IconWand2 from "~icons/lucide/wand-2"
|
import IconWand2 from "~icons/lucide/wand-2"
|
||||||
import IconCheck from "~icons/lucide/check"
|
import IconCheck from "~icons/lucide/check"
|
||||||
import IconInfo from "~icons/lucide/info"
|
import IconInfo from "~icons/lucide/info"
|
||||||
import { computed, reactive, Ref, ref } from "vue"
|
import { computed, reactive, Ref, ref, watch } from "vue"
|
||||||
import * as TO from "fp-ts/TaskOption"
|
import * as TO from "fp-ts/TaskOption"
|
||||||
import { pipe } from "fp-ts/function"
|
import { pipe } from "fp-ts/function"
|
||||||
import { HoppRESTReqBody, ValidContentTypes } from "@hoppscotch/data"
|
import { ValidContentTypes } from "@hoppscotch/data"
|
||||||
import { refAutoReset } from "@vueuse/core"
|
import { refAutoReset } from "@vueuse/core"
|
||||||
import { useCodemirror } from "@composables/codemirror"
|
import { useCodemirror } from "@composables/codemirror"
|
||||||
import { getEditorLangForMimeType } from "@helpers/editorutils"
|
import { getEditorLangForMimeType } from "@helpers/editorutils"
|
||||||
@@ -92,14 +92,7 @@ const props = defineProps<{
|
|||||||
|
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
const rawParamsBody = pluckRef(
|
const rawParamsBody = pluckRef(useRESTRequestBody(), "body")
|
||||||
useRESTRequestBody() as Ref<
|
|
||||||
HoppRESTReqBody & {
|
|
||||||
contentType: PossibleContentTypes
|
|
||||||
}
|
|
||||||
>,
|
|
||||||
"body"
|
|
||||||
)
|
|
||||||
|
|
||||||
const prettifyIcon = refAutoReset<
|
const prettifyIcon = refAutoReset<
|
||||||
typeof IconWand2 | typeof IconCheck | typeof IconInfo
|
typeof IconWand2 | typeof IconCheck | typeof IconInfo
|
||||||
@@ -115,9 +108,27 @@ const langLinter = computed(() =>
|
|||||||
const linewrapEnabled = ref(true)
|
const linewrapEnabled = ref(true)
|
||||||
const rawBodyParameters = ref<any | null>(null)
|
const rawBodyParameters = ref<any | null>(null)
|
||||||
|
|
||||||
|
const codemirrorValue: Ref<string | undefined> =
|
||||||
|
typeof rawParamsBody.value == "string"
|
||||||
|
? ref(rawParamsBody.value)
|
||||||
|
: ref(undefined)
|
||||||
|
|
||||||
|
watch(rawParamsBody, (newVal) => {
|
||||||
|
typeof newVal == "string"
|
||||||
|
? (codemirrorValue.value = newVal)
|
||||||
|
: (codemirrorValue.value = undefined)
|
||||||
|
})
|
||||||
|
|
||||||
|
// propagate the edits from codemirror back to the body
|
||||||
|
watch(codemirrorValue, (updatedValue) => {
|
||||||
|
if (updatedValue && updatedValue != rawParamsBody.value) {
|
||||||
|
rawParamsBody.value = updatedValue
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
useCodemirror(
|
useCodemirror(
|
||||||
rawBodyParameters,
|
rawBodyParameters,
|
||||||
rawParamsBody,
|
codemirrorValue,
|
||||||
reactive({
|
reactive({
|
||||||
extendedEditorConfig: {
|
extendedEditorConfig: {
|
||||||
lineWrapping: linewrapEnabled,
|
lineWrapping: linewrapEnabled,
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ const getEditorLanguage = (
|
|||||||
|
|
||||||
export function useCodemirror(
|
export function useCodemirror(
|
||||||
el: Ref<any | null>,
|
el: Ref<any | null>,
|
||||||
value: Ref<string>,
|
value: Ref<string | undefined>,
|
||||||
options: CodeMirrorOptions
|
options: CodeMirrorOptions
|
||||||
): { cursor: Ref<{ line: number; ch: number }> } {
|
): { cursor: Ref<{ line: number; ch: number }> } {
|
||||||
const { subscribeToStream } = useStreamSubscriber()
|
const { subscribeToStream } = useStreamSubscriber()
|
||||||
@@ -289,6 +289,15 @@ export function useCodemirror(
|
|||||||
})
|
})
|
||||||
|
|
||||||
watch(value, (newVal, oldVal) => {
|
watch(value, (newVal, oldVal) => {
|
||||||
|
if (newVal === undefined) {
|
||||||
|
view.value?.destroy()
|
||||||
|
view.value = undefined
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!view.value && el.value) {
|
||||||
|
initView(el.value)
|
||||||
|
}
|
||||||
if (cachedValue.value !== newVal) {
|
if (cachedValue.value !== newVal) {
|
||||||
try {
|
try {
|
||||||
view.value?.dispatch({
|
view.value?.dispatch({
|
||||||
|
|||||||
Reference in New Issue
Block a user