Skip to content

Commit

Permalink
fix: request body does not reset, fix #730 (#749)
Browse files Browse the repository at this point in the history
* fix: request body does not reset

* docs(changeset): fix: request body doesn’t reset on navigating to a request without a body
  • Loading branch information
hanspagel committed Jan 8, 2024
1 parent c2c9339 commit c171c9d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/ninety-pugs-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@scalar/use-codemirror': patch
'@scalar/api-client': patch
---

fix: request body doesn’t reset on navigating to a request without a body
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ const updateActiveRequest = (value: string) => {
<template v-else-if="formData && formData.length > 0">
<Grid :items="formData" />
</template>
<CodeMirror
v-else
:content="activeRequest.body"
:languages="['json']"
lineNumbers
@change="updateActiveRequest" />
<template v-else>
<CodeMirror
:content="activeRequest.body"
:languages="['json']"
lineNumbers
@change="updateActiveRequest" />
</template>
</CollapsibleSection>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ const {
watch(
() => props.content,
() => {
if (props.content?.length) {
setCodeMirrorContent(props.content)
}
setCodeMirrorContent(props.content)
},
)
Expand Down
10 changes: 5 additions & 5 deletions packages/use-codemirror/src/hooks/useCodeMirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const useCodeMirror = (
value: Ref<string>
codeMirrorRef: Ref<HTMLDivElement | null>
codeMirror: Ref<EditorView | null>
setCodeMirrorContent: (content: string) => void
setCodeMirrorContent: (content?: string) => void
reconfigureCodeMirror: (newExtensions: Extension[]) => void
restartCodeMirror: (newExtensions: Extension[]) => void
} => {
Expand Down Expand Up @@ -144,7 +144,7 @@ export const useCodeMirror = (
codeMirror.value?.destroy()
}

const setCodeMirrorContent = (newValue: string) => {
const setCodeMirrorContent = (newValue?: string) => {
// Check whether CodeMirror is mounted properly.
if (!codeMirror.value) {
return
Expand All @@ -155,9 +155,9 @@ export const useCodeMirror = (
return
}

value.value = newValue
value.value = newValue ?? ''

if (codeMirror.value.state.doc.toString() === newValue) {
if (codeMirror.value.state.doc.toString() === value.value) {
// No need to set the CodeMirror content
return
}
Expand All @@ -171,7 +171,7 @@ export const useCodeMirror = (
selection: {
anchor: Math.min(
codeMirror.value.state.selection.main.anchor,
newValue.length,
value.value.length,
),
},
})
Expand Down

0 comments on commit c171c9d

Please sign in to comment.