fix file/folder drop

This commit is contained in:
Labhansh Agrawal 2023-02-20 23:13:43 +05:30
parent cf97f8db3a
commit d6b7414890

View file

@ -276,22 +276,33 @@ export function newWindow(
} }
}); });
const handleDrop = (event: Event, url: string) => { const handleDroppedURL = (url: string) => {
const protocol = typeof url === 'string' && new URL(url).protocol; const protocol = typeof url === 'string' && new URL(url).protocol;
if (protocol === 'file:') { if (protocol === 'file:') {
event.preventDefault();
const path = fileURLToPath(url); const path = fileURLToPath(url);
rpc.emit('session data send', {data: path, escaped: true}); return {data: path, escaped: true};
} else if (protocol === 'http:' || protocol === 'https:') { } else if (protocol === 'http:' || protocol === 'https:') {
event.preventDefault(); return {data: url};
rpc.emit('session data send', {data: url});
} }
}; };
// If file is dropped onto the terminal window, navigate and new-window events are prevented // If file is dropped onto the terminal window, navigate and new-window events are prevented
// and his path is added to active session. // and it's path is added to active session.
window.webContents.on('will-navigate', handleDrop); window.webContents.on('will-navigate', (event, url) => {
window.webContents.on('new-window', handleDrop); const data = handleDroppedURL(url);
if (data) {
event.preventDefault();
rpc.emit('session data send', data);
}
});
window.webContents.setWindowOpenHandler(({url}) => {
const data = handleDroppedURL(url);
if (data) {
rpc.emit('session data send', data);
return {action: 'deny'};
}
return {action: 'allow'};
});
// expose internals to extension authors // expose internals to extension authors
window.rpc = rpc; window.rpc = rpc;