From dd0ae33ddad21ce2f1270399955165fcad7df7dd Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Wed, 21 Jul 2021 13:13:01 +0530 Subject: [PATCH] Fix file/directory drag and drop --- app/package.json | 1 - app/ui/window.ts | 29 ++++++++++------------------- app/yarn.lock | 5 ----- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/app/package.json b/app/package.json index f9e9b7c6..35741057 100644 --- a/app/package.json +++ b/app/package.json @@ -18,7 +18,6 @@ "electron-fetch": "1.7.3", "electron-is-dev": "2.0.0", "electron-store": "8.0.0", - "file-uri-to-path": "2.0.0", "fs-extra": "10.0.0", "git-describe": "4.0.4", "lodash": "4.17.21", diff --git a/app/ui/window.ts b/app/ui/window.ts index 2f001460..cbe89bfa 100644 --- a/app/ui/window.ts +++ b/app/ui/window.ts @@ -1,8 +1,7 @@ -import {app, BrowserWindow, shell, Menu, BrowserWindowConstructorOptions} from 'electron'; +import {app, BrowserWindow, shell, Menu, BrowserWindowConstructorOptions, Event} from 'electron'; import {isAbsolute, normalize, sep} from 'path'; -import {parse as parseUrl} from 'url'; +import {URL, fileURLToPath} from 'url'; import {v4 as uuidv4} from 'uuid'; -import fileUriToPath from 'file-uri-to-path'; import isDev from 'electron-is-dev'; import updater from '../updater'; import toElectronBackgroundColor from '../utils/to-electron-background-color'; @@ -259,30 +258,22 @@ export function newWindow( } }); - // If file is dropped onto the terminal window, navigate event is prevented - // and his path is added to active session. - window.webContents.on('will-navigate', (event, url) => { - const protocol = typeof url === 'string' && parseUrl(url).protocol; + const handleDrop = (event: Event, url: string) => { + const protocol = typeof url === 'string' && new URL(url).protocol; if (protocol === 'file:') { event.preventDefault(); - - const path = fileUriToPath(url); - + const path = fileURLToPath(url); rpc.emit('session data send', {data: path, escaped: true}); } else if (protocol === 'http:' || protocol === 'https:') { event.preventDefault(); rpc.emit('session data send', {data: url}); } - }); + }; - // xterm makes link clickable - window.webContents.on('new-window', (event, url) => { - const protocol = typeof url === 'string' && parseUrl(url).protocol; - if (protocol === 'http:' || protocol === 'https:') { - event.preventDefault(); - void shell.openExternal(url); - } - }); + // If file is dropped onto the terminal window, navigate and new-window events are prevented + // and his path is added to active session. + window.webContents.on('will-navigate', handleDrop); + window.webContents.on('new-window', handleDrop); // expose internals to extension authors window.rpc = rpc; diff --git a/app/yarn.lock b/app/yarn.lock index a60e9dfa..8626a3bd 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -244,11 +244,6 @@ fast-deep-equal@^3.1.1: resolved "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== -file-uri-to-path@2.0.0: - version "2.0.0" - resolved "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-2.0.0.tgz#7b415aeba227d575851e0a5b0c640d7656403fba" - integrity sha512-hjPFI8oE/2iQPVe4gbrJ73Pp+Xfub2+WI2LlXDbsaJBwT5wuMh35WNWVYYTpnz895shtwfyutMFLFywpQAFdLg== - fill-range@^7.0.1: version "7.0.1" resolved "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"