Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix: close context menu when no text is selected (#4337)

Nivedin53839e1091a2
diff --git a/packages/hoppscotch-common/src/composables/codemirror.ts b/packages/hoppscotch-common/src/composables/codemirror.ts
index 0b491c837..713a82852 100644
--- a/packages/hoppscotch-common/src/composables/codemirror.ts
+++ b/packages/hoppscotch-common/src/composables/codemirror.ts
@@ -242,11 +242,27 @@ export function useCodemirror(
? new HoppEnvironmentPlugin(subscribeToStream, view)
: null
+ const closeContextMenu = () => {
+ invokeAction("contextmenu.open", {
+ position: {
+ top: 0,
+ left: 0,
+ },
+ text: null,
+ })
+ }
+
function handleTextSelection() {
const selection = view.value?.state.selection.main
if (selection) {
const { from, to } = selection
- if (from === to) return
+
+ // If the selection is empty, hide the context menu
+ if (from === to) {
+ closeContextMenu()
+ return
+ }
+
const text = view.value?.state.doc.sliceString(from, to)
const coords = view.value?.coordsAtPos(from)
const top = coords?.top ?? 0
@@ -260,13 +276,7 @@ export function useCodemirror(
text,
})
} else {
- invokeAction("contextmenu.open", {
- position: {
- top,
- left,
- },
- text: null,
- })
+ closeContextMenu()
}
}
}