Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add openWith function to OCA.Viewer
Signed-off-by: Raul <[email protected]>
Signed-off-by: nextcloud-command <[email protected]>
  • Loading branch information
Raudius authored and Raul committed Jul 25, 2022
commit d2bacbd5239a105091cca338e5a4a40497e9a293
30 changes: 30 additions & 0 deletions src/services/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Viewer {
this._state.onClose = () => {}
this._state.canLoop = true
this._state.handlers = []
this._state.overrideHandlerId = null

// ! built-in handlers
this.registerHandler(Images)
Expand Down Expand Up @@ -150,6 +151,15 @@ export default class Viewer {
return this._state.canLoop
}

/**
* If this handler is set, it should be used for viewing the next file.
*
* @memberof Viewer
*/
get overrideHandlerId() {
return this._state.overrideHandlerId
}

/**
* Open the path into the viewer
*
Expand Down Expand Up @@ -189,6 +199,25 @@ export default class Viewer {
this._state.canLoop = canLoop
}

/**
* Open the path into the viewer
*
* @memberof Viewer
* @param {object} handlerId ID of the handler with which to open the files
* @param {object} options Options for opening the viewer
* @param {string} options.path path of the file to open
* @param {object[]} [options.list] the list of files as objects (fileinfo) format
* @param {Function} options.loadMore callback for loading more files
* @param {boolean} options.canLoop can the viewer loop over the array
* @param {Function} options.onPrev callback when navigating back to previous file
* @param {Function} options.onNext callback when navigation forward to next file
* @param {Function} options.onClose callback when closing the viewer
*/
openWith(handlerId, options = {}) {
this._state.overrideHandlerId = handlerId
this.open(options)
}

/**
* Close the opened file
*
Expand All @@ -199,6 +228,7 @@ export default class Viewer {
this._state.files = []
this._state.canLoop = true
this._state.loadMore = () => ([])
this._state.overrideHandlerId = null
}

}
36 changes: 22 additions & 14 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export default {
// we got a valid path! Load file...
if (path.trim() !== '') {
console.info('Opening viewer for file ', path)
this.openFile(path)
this.openFile(path, OCA.Viewer.overrideHandlerId)
} else {
// path is empty, we're closing!
this.cleanup()
Expand Down Expand Up @@ -359,8 +359,9 @@ export default {
* Open the view and display the clicked file
*
* @param {string} path the file path to open
* @param {string|null} overrideHandlerId the ID of the handler with which to view the files, if any
*/
async openFile(path) {
async openFile(path, overrideHandlerId = null) {
// cancel any previous requests
this.cancelRequestFile()
this.cancelRequestFolder()
Expand Down Expand Up @@ -399,14 +400,26 @@ export default {
// retrieve and store the file info
let fileInfo = await fileRequest(path)

// get original mime
let mime = fileInfo.mime

this.theme = this.registeredHandlers[mime].theme ?? 'dark'
// get original mime and alias
const mime = fileInfo.mime
const alias = mime.split('/')[0]

let handler
// Try provided handler, if any
if (overrideHandlerId !== null) {
const overrideHandler = Object.values(this.registeredHandlers).find((handler) => {
return handler.id === overrideHandlerId
})
handler = overrideHandler ?? handler
}
// If no provided handler, or provided handler not found: try a supported handler with mime/mime-alias
if (!handler) {
handler = this.registeredHandlers[mime] ?? this.registeredHandlers[alias]
}

this.theme = handler.theme ?? 'dark'
// if we don't have a handler for this mime, abort
if (!(mime in this.components)) {
console.error('The following file could not be displayed', fileName, fileInfo)
if (!handler) {
showError(t('viewer', 'There is no plugin available to display this file type'))
this.close()
return
Expand Down Expand Up @@ -447,13 +460,8 @@ export default {
// get saved fileInfo
fileInfo = this.fileList[this.currentIndex]

// override mimetype if existing alias
if (!this.components[mime]) {
mime = mime.split('/')[0]
}

// show file
this.currentFile = new File(fileInfo, mime, this.components[mime])
this.currentFile = new File(fileInfo, mime, handler.component)
this.updatePreviousNext()

// if sidebar was opened before, let's update the file
Expand Down