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(desktop): prevent navigation on file drops (#5176)

Shreyas5ece4821af29
diff --git a/packages/hoppscotch-selfhost-web/src/main.ts b/packages/hoppscotch-selfhost-web/src/main.ts
index 2d28517f8..cb236e99e 100644
--- a/packages/hoppscotch-selfhost-web/src/main.ts
+++ b/packages/hoppscotch-selfhost-web/src/main.ts
@@ -126,7 +126,86 @@ async function initApp() {
headerPaddingTop.value = "0px"
headerPaddingLeft.value = "80px"
- // Add backspace prevention for non-text inputs
+ const ALLOWED_DROP_SELECTORS = [
+ '[draggable="true"]',
+ ".draggable-content",
+ ".draggable-handle",
+ ".sortable-ghost",
+ ".sortable-drag",
+ ".sortable-chosen",
+ ".vue-draggable",
+
+ 'input[type="file"]',
+ 'label[for*="attachment"]',
+ ".file-chips-container",
+ ".file-chips-wrapper",
+
+ ".cm-editor",
+ ".cm-content",
+ ".cm-scroller",
+ ".ace_editor",
+
+ "[data-allow-drop]",
+ ".drop-zone",
+
+ "[ondrop]",
+ "[data-drop-handler]",
+ ].join(", ")
+
+ const isAllowedDropTarget = (target: EventTarget | null): boolean => {
+ if (!target || !(target instanceof HTMLElement)) {
+ return false
+ }
+
+ if (target.closest(ALLOWED_DROP_SELECTORS)) {
+ return true
+ }
+
+ const element = target as any
+ if (element._vei?.onDrop || element.__vueListeners__?.drop) {
+ return true
+ }
+
+ return false
+ }
+
+ document.addEventListener(
+ "drop",
+ (e) => {
+ if (!isAllowedDropTarget(e.target)) {
+ e.preventDefault()
+ e.stopPropagation()
+ }
+ },
+ false
+ )
+
+ document.addEventListener(
+ "dragover",
+ (e) => {
+ e.preventDefault()
+ },
+ false
+ )
+
+ document.addEventListener(
+ "dragstart",
+ (e) => {
+ if (!e.target || !(e.target instanceof HTMLElement)) {
+ return
+ }
+
+ const target = e.target as HTMLElement
+ if (
+ !target.draggable &&
+ !target.closest('[draggable="true"], .draggable-content')
+ ) {
+ e.preventDefault()
+ }
+ },
+ false
+ )
+
window.addEventListener(
"keydown",
function (e) {