Skip to content

Commit 91f386e

Browse files
authored
Merge pull request #1273 from nextcloud/enh/open_with
Add `openWith` function to OCA.Viewer
2 parents dbe5c75 + 46f07fb commit 91f386e

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

viewer/src/services/Viewer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default class Viewer {
4040
this._state.onClose = () => {}
4141
this._state.canLoop = true
4242
this._state.handlers = []
43+
this._state.overrideHandlerId = null
4344

4445
// ! built-in handlers
4546
this.registerHandler(Images)
@@ -150,6 +151,15 @@ export default class Viewer {
150151
return this._state.canLoop
151152
}
152153

154+
/**
155+
* If this handler is set, it should be used for viewing the next file.
156+
*
157+
* @memberof Viewer
158+
*/
159+
get overrideHandlerId() {
160+
return this._state.overrideHandlerId
161+
}
162+
153163
/**
154164
* Open the path into the viewer
155165
*
@@ -189,6 +199,25 @@ export default class Viewer {
189199
this._state.canLoop = canLoop
190200
}
191201

202+
/**
203+
* Open the path into the viewer
204+
*
205+
* @memberof Viewer
206+
* @param {object} handlerId ID of the handler with which to open the files
207+
* @param {object} options Options for opening the viewer
208+
* @param {string} options.path path of the file to open
209+
* @param {object[]} [options.list] the list of files as objects (fileinfo) format
210+
* @param {Function} options.loadMore callback for loading more files
211+
* @param {boolean} options.canLoop can the viewer loop over the array
212+
* @param {Function} options.onPrev callback when navigating back to previous file
213+
* @param {Function} options.onNext callback when navigation forward to next file
214+
* @param {Function} options.onClose callback when closing the viewer
215+
*/
216+
openWith(handlerId, options = {}) {
217+
this._state.overrideHandlerId = handlerId
218+
this.open(options)
219+
}
220+
192221
/**
193222
* Close the opened file
194223
*
@@ -199,6 +228,7 @@ export default class Viewer {
199228
this._state.files = []
200229
this._state.canLoop = true
201230
this._state.loadMore = () => ([])
231+
this._state.overrideHandlerId = null
202232
}
203233

204234
}

viewer/src/views/Viewer.vue

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export default {
253253
// we got a valid path! Load file...
254254
if (path.trim() !== '') {
255255
logger.info('Opening viewer for file ', { path })
256-
this.openFile(path)
256+
this.openFile(path, OCA.Viewer.overrideHandlerId)
257257
} else {
258258
// path is empty, we're closing!
259259
this.cleanup()
@@ -340,8 +340,9 @@ export default {
340340
* Open the view and display the clicked file
341341
*
342342
* @param {string} path the file path to open
343+
* @param {string|null} overrideHandlerId the ID of the handler with which to view the files, if any
343344
*/
344-
async openFile(path) {
345+
async openFile(path, overrideHandlerId = null) {
345346
// cancel any previous requests
346347
this.cancelRequestFile()
347348
this.cancelRequestFolder()
@@ -380,13 +381,24 @@ export default {
380381
// retrieve and store the file info
381382
let fileInfo = await fileRequest(path)
382383
383-
// get original mime
384-
let mime = fileInfo.mime
384+
// get original mime and alias
385+
const mime = fileInfo.mime
386+
const alias = mime.split('/')[0]
385387
386-
this.theme = this.registeredHandlers[mime].theme ?? 'dark'
388+
let handler
389+
// Try provided handler, if any
390+
if (overrideHandlerId !== null) {
391+
const overrideHandler = Object.values(this.registeredHandlers).find(h => h.id === overrideHandlerId)
392+
handler = overrideHandler ?? handler
393+
}
394+
// If no provided handler, or provided handler not found: try a supported handler with mime/mime-alias
395+
if (!handler) {
396+
handler = this.registeredHandlers[mime] ?? this.registeredHandlers[alias]
397+
}
387398
399+
this.theme = handler.theme ?? 'dark'
388400
// if we don't have a handler for this mime, abort
389-
if (!(mime in this.components)) {
401+
if (!handler) {
390402
logger.error('The following file could not be displayed', { fileName, fileInfo })
391403
showError(t('viewer', 'There is no plugin available to display this file type'))
392404
this.close()
@@ -428,13 +440,8 @@ export default {
428440
// get saved fileInfo
429441
fileInfo = this.fileList[this.currentIndex]
430442
431-
// override mimetype if existing alias
432-
if (!this.components[mime]) {
433-
mime = mime.split('/')[0]
434-
}
435-
436443
// show file
437-
this.currentFile = new File(fileInfo, mime, this.components[mime])
444+
this.currentFile = new File(fileInfo, mime, handler.component)
438445
this.updatePreviousNext()
439446
440447
// if sidebar was opened before, let's update the file

0 commit comments

Comments
 (0)