Skip to content

Commit

Permalink
WIP: render viewer content in element instead of modal
Browse files Browse the repository at this point in the history
Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud authored and vanpertsch committed May 18, 2022
1 parent c24f5b2 commit dc8006f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions js/viewer-main.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ if (window.OCA) {
OCA.Viewer.version = appVersion
}


// Create document root
const ViewerRoot = document.createElement('div')
ViewerRoot.id = 'viewer'
Expand Down
15 changes: 14 additions & 1 deletion src/services/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class Viewer {
this._state.onNext = () => {}
this._state.onClose = () => {}
this._state.canLoop = true
this._state.el = null
this._state.handlers = []

// ! built-in handlers
Expand Down Expand Up @@ -90,6 +91,16 @@ export default class Viewer {
return this._state.files
}

/**
* Get the element to render the current file in
*
* @memberof Viewer
* @return {string} selector of the element
*/
get el() {
return this._state.el
}

/**
* Get the supported mimetypes that can be opened with the viewer
*
Expand Down Expand Up @@ -157,13 +168,14 @@ export default class Viewer {
* @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 {string} options.el selector of the element to render the file in
* @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
*/
open({ path, list = [], loadMore = () => ([]), canLoop = true, onPrev = () => {}, onNext = () => {}, onClose = () => {} } = {}) {
open({ path, list = [], el = null, loadMore = () => ([]), canLoop = true, onPrev = () => {}, onNext = () => {}, onClose = () => {} } = {}) {
if (typeof arguments[0] === 'string') {
throw new Error('Opening the viewer with a single string parameter is deprecated. Please use a destructuring object instead', `OCA.Viewer.open({ path: '${path}' })`)
}
Expand All @@ -187,6 +199,7 @@ export default class Viewer {
this._state.onNext = onNext
this._state.onClose = onClose
this._state.canLoop = canLoop
this._state.el = el
}

/**
Expand Down
39 changes: 38 additions & 1 deletion src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@
-->

<template>
<Modal v-if="initiated || currentFile.modal"
<div v-if="el" id="viewer">
<component :is="currentFile.modal"
v-if="!currentFile.failed"
:key="currentFile.fileid"
ref="content"
:active="true"
:can-swipe="false"
v-bind="currentFile"
:file-list="[currentFile]"
:is-full-screen="false"
:loaded.sync="currentFile.loaded"
:is-sidebar-shown="false"
class="viewer__file viewer__file--active"
@error="currentFailed" />
<Error v-else
:name="currentFile.basename" />
</div>
<Modal v-else-if="initiated || currentFile.modal"
id="viewer"
size="full"
:class="{'icon-loading': !currentFile.loaded && !currentFile.failed,
Expand Down Expand Up @@ -203,6 +220,9 @@ export default {
files() {
return this.Viewer.files
},
el() {
return this.Viewer.el
},
loadMore() {
return this.Viewer.loadMore
},
Expand Down Expand Up @@ -249,6 +269,23 @@ export default {
},
watch: {
el(element) {
logger.info(element)
this.$nextTick(() => {
const viewerRoot = document.getElementById('viewer')
if (element) {
const el = document.querySelector(element)
if (el) {
el.appendChild(viewerRoot)
} else {
logger.warn('Could not find element ', { element })
}
} else {
document.body.appendChild(viewerRoot)
}
})
},
file(path) {
// we got a valid path! Load file...
if (path.trim() !== '') {
Expand Down

0 comments on commit dc8006f

Please sign in to comment.