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: context-menu position fixed while scrolling (#3340)

Nivedin7201147b55d9
diff --git a/packages/hoppscotch-common/src/components/smart/EnvInput.vue b/packages/hoppscotch-common/src/components/smart/EnvInput.vue
index 41efe2e42..246b14976 100644
--- a/packages/hoppscotch-common/src/components/smart/EnvInput.vue
+++ b/packages/hoppscotch-common/src/components/smart/EnvInput.vue
@@ -311,35 +311,36 @@ const envVars = computed(() =>
const envTooltipPlugin = new HoppReactiveEnvPlugin(envVars, view)
-const initView = (el: any) => {
- function handleTextSelection() {
- const selection = view.value?.state.selection.main
- if (selection) {
- const from = selection.from
- const to = selection.to
- const text = view.value?.state.doc.sliceString(from, to)
- const { top, left } = view.value?.coordsAtPos(from)
- if (text) {
- invokeAction("contextmenu.open", {
- position: {
- top,
- left,
- },
- text,
- })
- showSuggestionPopover.value = false
- } else {
- invokeAction("contextmenu.open", {
- position: {
- top,
- left,
- },
- text: null,
- })
- }
+function handleTextSelection() {
+ const selection = view.value?.state.selection.main
+ if (selection) {
+ const from = selection.from
+ const to = selection.to
+ if (from === to) return
+ const text = view.value?.state.doc.sliceString(from, to)
+ const { top, left } = view.value?.coordsAtPos(from)
+ if (text) {
+ invokeAction("contextmenu.open", {
+ position: {
+ top,
+ left,
+ },
+ text,
+ })
+ showSuggestionPopover.value = false
+ } else {
+ invokeAction("contextmenu.open", {
+ position: {
+ top,
+ left,
+ },
+ text: null,
+ })
}
}
+}
+const initView = (el: any) => {
// Debounce to prevent double click from selecting the word
const debounceFn = useDebounceFn(() => {
handleTextSelection()
@@ -381,6 +382,11 @@ const initView = (el: any) => {
drop(ev) {
ev.preventDefault()
},
+ scroll(event) {
+ if (event.target) {
+ handleTextSelection()
+ }
+ },
}),
ViewPlugin.fromClass(
class {
diff --git a/packages/hoppscotch-common/src/composables/codemirror.ts b/packages/hoppscotch-common/src/composables/codemirror.ts
index 4ea8b4111..57966d1c3 100644
--- a/packages/hoppscotch-common/src/composables/codemirror.ts
+++ b/packages/hoppscotch-common/src/composables/codemirror.ts
@@ -216,6 +216,33 @@ export function useCodemirror(
? new HoppEnvironmentPlugin(subscribeToStream, view)
: null
+ function handleTextSelection() {
+ const selection = view.value?.state.selection.main
+ if (selection) {
+ const from = selection.from
+ const to = selection.to
+ const text = view.value?.state.doc.sliceString(from, to)
+ const { top, left } = view.value?.coordsAtPos(from)
+ if (text) {
+ invokeAction("contextmenu.open", {
+ position: {
+ top,
+ left,
+ },
+ text,
+ })
+ } else {
+ invokeAction("contextmenu.open", {
+ position: {
+ top,
+ left,
+ },
+ text: null,
+ })
+ }
+ }
+ }
+
const initView = (el: any) => {
if (el) platform.ui?.onCodemirrorInstanceMount?.(el)
@@ -226,33 +253,6 @@ export function useCodemirror(
ViewPlugin.fromClass(
class {
update(update: ViewUpdate) {
- function handleTextSelection() {
- const selection = view.value?.state.selection.main
- if (selection) {
- const from = selection.from
- const to = selection.to
- const text = view.value?.state.doc.sliceString(from, to)
- const { top, left } = view.value?.coordsAtPos(from)
- if (text) {
- invokeAction("contextmenu.open", {
- position: {
- top,
- left,
- },
- text,
- })
- } else {
- invokeAction("contextmenu.open", {
- position: {
- top,
- left,
- },
- text: null,
- })
- }
- }
- }
-
// Debounce to prevent double click from selecting the word
const debounceFn = useDebounceFn(() => {
handleTextSelection()
@@ -296,6 +296,13 @@ export function useCodemirror(
}
}
),
+ EditorView.domEventHandlers({
+ scroll(event) {
+ if (event.target) {
+ handleTextSelection()
+ }
+ },
+ }),
EditorView.updateListener.of((update) => {
if (options.extendedEditorConfig.readOnly) {
update.view.contentDOM.inputMode = "none"