From 0360da07689455f9827b97efbcccaa727c1736f6 Mon Sep 17 00:00:00 2001 From: swordensen Date: Fri, 28 Jun 2024 09:58:24 -0700 Subject: [PATCH 1/2] preventing default on drop events to prevent files from opening in a new window --- .../contrib/webview/browser/pre/index.html | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/vs/workbench/contrib/webview/browser/pre/index.html b/src/vs/workbench/contrib/webview/browser/pre/index.html index f46e124042850..e55ebb78bd6ab 100644 --- a/src/vs/workbench/contrib/webview/browser/pre/index.html +++ b/src/vs/workbench/contrib/webview/browser/pre/index.html @@ -5,8 +5,7 @@ + content="default-src 'none'; script-src 'sha256-EnfegwWkkVl5XI5EPvw9QR4tqOkoO1Uaj+Ot/RVrckU=' 'self'; frame-src 'self'; style-src 'unsafe-inline';"> @@ -799,6 +798,12 @@ function handleInnerDragEvent(/** @type {DragEvent} */ e) { + /** + * To ensure that the drop event always fires as expected, you should always include a preventDefault() call in the part of your code which handles the dragover event. + * source: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event + **/ + e.preventDefault(); + if (!e.dataTransfer) { return; } @@ -808,6 +813,10 @@ }); } + function handleInnerDropEvent(/**@type {DragEvent} */e) { + e.preventDefault(); + } + /** * @param {() => void} callback */ @@ -900,6 +909,7 @@ window.addEventListener('dragenter', handleInnerDragStartEvent); window.addEventListener('dragover', handleInnerDragEvent); window.addEventListener('drag', handleInnerDragEvent); + window.addEventListener('drop', handleInnerDropEvent); onDomReady(() => { @@ -1184,6 +1194,7 @@ contentWindow.addEventListener('dragenter', handleInnerDragStartEvent); contentWindow.addEventListener('dragover', handleInnerDragEvent); contentWindow.addEventListener('drag', handleInnerDragEvent); + contentWindow.addEventListener('drop', handleInnerDropEvent); unloadMonitor.onIframeLoaded(newFrame); } From 69563ee77e31888d90d90bfa553da4e01dff124e Mon Sep 17 00:00:00 2001 From: swordensen Date: Wed, 3 Jul 2024 10:38:01 -0700 Subject: [PATCH 2/2] adding a fix that addresses https://github.com/microsoft/vscode/issues/219047 --- .../contrib/webview/browser/pre/index.html | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/contrib/webview/browser/pre/index.html b/src/vs/workbench/contrib/webview/browser/pre/index.html index e55ebb78bd6ab..6a1f3d459abf6 100644 --- a/src/vs/workbench/contrib/webview/browser/pre/index.html +++ b/src/vs/workbench/contrib/webview/browser/pre/index.html @@ -5,7 +5,7 @@ + content="default-src 'none'; script-src 'sha256-DXeP32g8BdMsVuVabYTmznoTH59F7M7UtV0vXemEFqc=' 'self'; frame-src 'self'; style-src 'unsafe-inline';"> @@ -808,9 +808,14 @@ return; } - hostMessaging.postMessage('drag', { - shiftKey: e.shiftKey - }); + + // Only handle drags from outside editor for now + if (e.dataTransfer.items.length && Array.prototype.every.call(e.dataTransfer.items, item => item.kind === 'file')) { + hostMessaging.postMessage('drag', { + shiftKey: e.shiftKey + }); + } + } function handleInnerDropEvent(/**@type {DragEvent} */e) {