Skip to content

Commit

Permalink
add generateViewerObject util
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Sep 20, 2023
1 parent d5be55a commit 4da92c2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ export default {
emits: ['remove-file'],

setup() {
const { openViewer } = useViewer()
return { openViewer }
const { openViewer, generateViewerObject } = useViewer()

return {
openViewer,
generateViewerObject,
}
},

data() {
Expand Down Expand Up @@ -411,11 +415,7 @@ export default {
},

internalAbsolutePath() {
if (this.path.startsWith('/')) {
return this.path
}

return '/' + this.path
return this.path.startsWith('/') ? this.path : '/' + this.path
},

isTemporaryUpload() {
Expand Down Expand Up @@ -472,44 +472,14 @@ export default {
event.stopPropagation()
event.preventDefault()

let permissions = ''
if (this.permissions) {
if (this.permissions & OC.PERMISSION_CREATE) {
permissions += 'CK'
}
if (this.permissions & OC.PERMISSION_READ) {
permissions += 'G'
}
if (this.permissions & OC.PERMISSION_UPDATE) {
permissions += 'W'
}
if (this.permissions & OC.PERMISSION_DELETE) {
permissions += 'D'
}
if (this.permissions & OC.PERMISSION_SHARE) {
permissions += 'R'
}
}

this.openViewer(this.internalAbsolutePath, [
{
fileid: parseInt(this.id, 10),
filename: this.internalAbsolutePath,
basename: this.name,
mime: this.mimetype,
hasPreview: this.previewAvailable === 'yes',
etag: this.etag,
permissions,
},
])
const fileInfo = this.generateViewerObject(this)
this.openViewer(this.internalAbsolutePath, [fileInfo], fileInfo)
},
},
}
</script>

<style lang="scss" scoped>
@import '../../../../../assets/variables';

.file-preview {
position: relative;
min-width: 0;
Expand Down
3 changes: 0 additions & 3 deletions src/components/NewMessage/NewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ import NewMessagePollEditor from './NewMessagePollEditor.vue'
import NewMessageTypingIndicator from './NewMessageTypingIndicator.vue'
import NewMessageUploadEditor from './NewMessageUploadEditor.vue'

import { useViewer } from '../../composables/useViewer.js'
import { CONVERSATION, PARTICIPANT, PRIVACY } from '../../constants.js'
import { EventBus } from '../../services/EventBus.js'
import { shareFile } from '../../services/filesSharingServices.js'
Expand Down Expand Up @@ -259,11 +258,9 @@ export default {
expose: ['focusInput'],

setup() {
const { openViewer } = useViewer()
const settingsStore = useSettingsStore()

return {
openViewer,
settingsStore,
supportTypingStatus,
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/NewMessage/NewMessageNewFileDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default {

await shareFile(filePath, this.token, '', '')

this.openViewer(filePath, [fileData])
this.openViewer(filePath, [fileData], fileData)

this.closeModal()
},
Expand Down
53 changes: 51 additions & 2 deletions src/composables/useViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,46 @@ import { useStore } from './useStore.js'
* @description Open files in the OCA.Viewer taking into account Talk's fullscreen mode and call view
* @see https://github.com/nextcloud/viewer
* @param {string} path - The path to the file to be open
* @param {object} list - The list of the files to be opened
* @param {Array<object>} list - The list of the files to be opened
* @param {object} [fileInfo] - The known file info
*/

/**
*
* @param {string} path path to file
* @return {string}
*/
function generateAbsolutePath(path) {
return path.startsWith('/') ? path : '/' + path
}

/**
*
* @param {number} filePermissions file permissions in a bit notation
* @return {string}
*/
function generatePermissions(filePermissions) {
let permissions = ''

if (filePermissions & OC.PERMISSION_CREATE) {
permissions += 'CK'
}
if (filePermissions & OC.PERMISSION_READ) {
permissions += 'G'
}
if (filePermissions & OC.PERMISSION_UPDATE) {
permissions += 'W'
}
if (filePermissions & OC.PERMISSION_DELETE) {
permissions += 'D'
}
if (filePermissions & OC.PERMISSION_SHARE) {
permissions += 'R'
}

return permissions
}

/**
* FIXME Remove this hack once it is possible to set the parent
* element of the viewer.
Expand Down Expand Up @@ -80,10 +117,20 @@ export function useViewer() {
}
})

const generateViewerObject = (file) => ({
fileid: parseInt(file.id, 10),
filename: generateAbsolutePath(file.path),
basename: file.name,
mime: file.mimetype,
hasPreview: file.previewAvailable === 'yes' || file['preview-available'] === 'yes',
etag: file.etag,
permissions: generatePermissions(file.permissions),
})

/**
* @type {OpenViewer}
*/
const openViewer = async (path, list) => {
const openViewer = async (path, list, fileInfo) => {
if (!OCA.Viewer) {
return false
}
Expand All @@ -100,6 +147,7 @@ export function useViewer() {
OCA.Viewer.open({
path,
list,
fileInfo,
onClose: () => {
isViewerOpen.value = false
store.dispatch('setCallViewMode', { isViewerOverlay: false })
Expand All @@ -119,5 +167,6 @@ export function useViewer() {
return {
isViewerOpen,
openViewer,
generateViewerObject,
}
}

0 comments on commit 4da92c2

Please sign in to comment.