Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,16 @@ If you want to make your app compatible with this app, you can use the methods p
],

// your vue component view
component: VideoView
component: VideoView,

// optional: callback to be called before download
// useful for saving unsaved changes, validation, logging, etc.
// if not provided, defaults to an empty function
downloadCallback: async (fileInfo) => {
// perform any pre-download operations
// e.g., save current editor state
await saveCurrentDocument(fileInfo)
}
})
```
3. Make sure your script is loaded with `\OCP\Util::addInitScript` so that the handler is registered **before** the viewer is loaded.
Expand Down Expand Up @@ -207,7 +216,16 @@ If you want to make your app compatible with this app, you can use the `OCA.View
],

// your vue component view
component: VideoView
component: VideoView,

// optional: callback to be called before download
// useful for saving unsaved changes, validation, logging, etc.
// if not provided, defaults to an empty function
downloadCallback: async (fileInfo) => {
// perform any pre-download operations
// e.g., save current editor state
await saveCurrentDocument(fileInfo)
}
})
```
3. Make sure your script is loaded with `\OCP\Util::addScript` (in contrast to using the API package)!

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/viewer-main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* extracted by css-entry-points-plugin */
@import './main-BDyUgWoK.chunk.css';
@import './main-OQPT9EaA.chunk.css';
@import './viewerAction-6cpbKhU6.chunk.css';
2 changes: 1 addition & 1 deletion js/viewer-init.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-init.mjs.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/viewer-main.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-main.mjs.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/services/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import logger from './logger.js'
* @property {string} group group identifier to combine for navigating to the next/previous files
* @property {?string} theme viewer modal theme (one of 'dark', 'light', 'default')
* @property {boolean} canCompare Indicate support for comparing two files
* @property {?Function} downloadCallback Optional callback to be called before download
*/

/**
Expand Down Expand Up @@ -106,6 +107,11 @@ export default class Viewer {
return
}

// Set default empty function for downloadCallback if not provided
if (!handler.downloadCallback) {
handler.downloadCallback = () => {}
}

this._state.handlers.push(handler)
const handledMimes = [
...handler.mimes,
Expand Down
51 changes: 41 additions & 10 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@
@click="showSidebar">
{{ t('viewer', 'Open sidebar') }}
</NcActionButton>
<NcActionLink v-if="canDownload"
:download="currentFile.basename"
<NcActionButton v-if="canDownload"
:close-after-click="true"
:href="downloadPath">
@click="onDownload">
<template #icon>
<Download :size="24" />
</template>
{{ t('viewer', 'Download') }}
</NcActionLink>
</NcActionButton>
<NcActionButton v-if="canDelete"
:close-after-click="true"
@click="onDelete">
Expand Down Expand Up @@ -1027,12 +1026,7 @@ export default defineComponent({
if (event.key === 's' && event.ctrlKey === true) {
event.preventDefault()
if (this.canDownload) {
const a = document.createElement('a')
a.href = this.currentFile.source ?? this.currentFile.davPath
a.download = this.currentFile.basename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
this.onDownload()
}
}
},
Expand Down Expand Up @@ -1215,6 +1209,43 @@ export default defineComponent({
this.toggleEditor(true)
},

/**
* Call handler's downloadCallback before downloading
*/
async onDownload() {
if (!this.canDownload) {
return
}

// Get the current handler for this file
const mime = this.currentFile.mime
const alias = mime?.split('/')[0]
const handler = this.registeredHandlers[mime] ?? this.registeredHandlers[alias]

if (handler?.downloadCallback && typeof handler.downloadCallback === 'function') {
try {
logger.debug('Calling handler downloadCallback before download')
await handler.downloadCallback(this.currentFile)
} catch (error) {
logger.error('Failed to execute downloadCallback', { error })
showError(t('viewer', 'Failed to save file before download'))
return
}
}

this.performDownload()
},

performDownload() {
logger.debug('Performing download', { file: this.currentFile })
const a = document.createElement('a')
a.href = this.currentFile.source ?? this.currentFile.davPath
a.download = this.currentFile.basename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
},

handleTrapElementsChange(element) {
this.trapElements.push(element)
},
Expand Down
Loading