Skip to content

Commit eba053b

Browse files
silverkszlobackportbot[bot]
authored andcommitted
feat: register a downloadCallback
Signed-off-by: silver <s.szmajduch@posteo.de> [skip ci]
1 parent 16d2910 commit eba053b

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,16 @@ If you want to make your app compatible with this app, you can use the methods p
175175
],
176176

177177
// your vue component view
178-
component: VideoView
178+
component: VideoView,
179+
180+
// optional: callback to be called before download
181+
// useful for saving unsaved changes, validation, logging, etc.
182+
// if not provided, defaults to an empty function
183+
downloadCallback: async (fileInfo) => {
184+
// perform any pre-download operations
185+
// e.g., save current editor state
186+
await saveCurrentDocument(fileInfo)
187+
}
179188
})
180189
```
181190
3. Make sure your script is loaded with `\OCP\Util::addInitScript` so that the handler is registered **before** the viewer is loaded.
@@ -213,7 +222,16 @@ If you want to make your app compatible with this app, you can use the `OCA.View
213222
],
214223

215224
// your vue component view
216-
component: VideoView
225+
component: VideoView,
226+
227+
// optional: callback to be called before download
228+
// useful for saving unsaved changes, validation, logging, etc.
229+
// if not provided, defaults to an empty function
230+
downloadCallback: async (fileInfo) => {
231+
// perform any pre-download operations
232+
// e.g., save current editor state
233+
await saveCurrentDocument(fileInfo)
234+
}
217235
})
218236
```
219237
3. Make sure your script is loaded with `\OCP\Util::addScript` (in contrast to using the API package)!

src/services/Viewer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import logger from './logger.js'
1818
* @property {string} group group identifier to combine for navigating to the next/previous files
1919
* @property {?string} theme viewer modal theme (one of 'dark', 'light', 'default')
2020
* @property {boolean} canCompare Indicate support for comparing two files
21+
* @property {?Function} downloadCallback Optional callback to be called before download
2122
*/
2223

2324
/**
@@ -89,6 +90,11 @@ export default class Viewer {
8990
return
9091
}
9192

93+
// Set default empty function for downloadCallback if not provided
94+
if (!handler.downloadCallback) {
95+
handler.downloadCallback = () => {}
96+
}
97+
9298
this._state.handlers.push(handler)
9399
const handledMimes = [
94100
...handler.mimes,

src/views/Viewer.vue

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,32 +1191,26 @@ export default defineComponent({
11911191
},
11921192
11931193
/**
1194-
* Save active Text editors before downloading
1194+
* Call handler's downloadCallback before downloading
11951195
*/
11961196
async onDownload() {
11971197
if (!this.canDownload) {
11981198
return
11991199
}
12001200
1201-
// Check if Text app is available and has active editor components
1202-
if (window.OCA?.Text?.editorComponents) {
1201+
// Get the current handler for this file
1202+
const mime = this.currentFile.mime
1203+
const alias = mime?.split('/')[0]
1204+
const handler = this.registeredHandlers[mime] ?? this.registeredHandlers[alias]
1205+
1206+
if (handler?.downloadCallback && typeof handler.downloadCallback === 'function') {
12031207
try {
1204-
logger.debug('Text app detected, attempting to save before download')
1205-
1206-
const editorComponents = window.OCA.Text.editorComponents
1207-
1208-
// Text app uses a Set to store editor components
1209-
if (editorComponents instanceof Set) {
1210-
for (const component of editorComponents) {
1211-
if (component && typeof component.save === 'function') {
1212-
logger.debug('Calling save on Text editor component')
1213-
await component.save()
1214-
}
1215-
}
1216-
}
1208+
logger.debug('Calling handler downloadCallback before download')
1209+
await handler.downloadCallback(this.currentFile)
12171210
} catch (error) {
1218-
logger.warn('Failed to save Text editor before download', { error })
1219-
// Continue with download anyway
1211+
logger.error('Failed to execute downloadCallback', { error })
1212+
showError(t('viewer', 'Failed to save file before download'))
1213+
return
12201214
}
12211215
}
12221216

0 commit comments

Comments
 (0)