Skip to content

Commit

Permalink
feat(app): handle download links
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <[email protected]>
  • Loading branch information
ShGKme committed Oct 23, 2024
1 parent 3bfe63b commit 99d502f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/app/externalLinkHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function isExternalLink(url) {
* @return {{action: 'deny'} | {action: 'allow', outlivesOpener?: boolean, overrideBrowserWindowOptions?: import('electron').BrowserWindowConstructorOptions}}
*/
function windowOpenExternalLinkHandler(details, browserWindowOptions = {}) {
// TODO: Should handle different types of details.disposition? I.e. save-to-disk?
if (isExternalLink(details.url)) {
shell.openExternal(details.url)
return { action: 'deny' }
Expand Down
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ app.whenReady().then(async () => {
isInWindowRelaunch = false
})

ipcMain.on('app:downloadURL', (event, url) => mainWindow.webContents.downloadURL(url))

// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ const TALK_DESKTOP = {
* @return {Promise<void>}
*/
setAppConfig: (key, value) => ipcRenderer.invoke('app:config:set', key, value),
/**
* Trigger download of a URL
* @param {string} url - URL to download
*/
downloadURL: (url) => ipcRenderer.send('app:downloadURL', url),
/**
* Send appData to main process on restore
*
Expand Down
14 changes: 14 additions & 0 deletions src/shared/setupWebPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ function applyHeaderHeight() {
document.documentElement.style.setProperty('--header-height', `${TITLE_BAR_HEIGHT}px`, 'important')
}

/**
* Handle download links
*/
function applyDownloadLinkHandler() {
document.addEventListener('click', (event) => {
const link = event.target.closest('a')
if (link && link.hasAttribute('download')) {
event.preventDefault()
window.TALK_DESKTOP.downloadURL(link.href)
}
})
}

/**
* Make all required initial setup for the web page for authorized user: server-rendered data, globals and ect.
*/
Expand All @@ -214,4 +227,5 @@ export async function setupWebPage() {
applyHeaderHeight()
applyAxiosInterceptors()
await applyL10n()
applyDownloadLinkHandler()
}

0 comments on commit 99d502f

Please sign in to comment.