Skip to content

Commit

Permalink
fix: Make loading the viewer an init script
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Oct 3, 2024
1 parent 940afcf commit 235cc6f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 48 deletions.
2 changes: 2 additions & 0 deletions lib/Listener/LoadViewerScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function handle(Event $event): void {
return;
}

Util::addStyle(Application::APP_ID, 'viewer-init');
Util::addStyle(Application::APP_ID, 'viewer-main');
Util::addInitScript(Application::APP_ID, 'viewer-init', 'files');
Util::addScript(Application::APP_ID, 'viewer-main', 'files');
$this->initialStateService->provideInitialState('enabled_preview_providers', array_keys($this->previewManager->getProviders()));
}
Expand Down
14 changes: 14 additions & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { registerViewerAction } from './services/FilesActionHandler'
import ViewerService from './services/Viewer.js'

// Register the files action
registerViewerAction()

// Init Viewer Service
window.OCA = window.OCA ?? {}
window.OCA.Viewer = new ViewerService()
window.OCA.Viewer.version = appVersion
7 changes: 0 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { translate as t } from '@nextcloud/l10n'
import Vue from 'vue'

import ViewerComponent from './views/Viewer.vue'
import ViewerService from './services/Viewer.js'

Vue.mixin({
methods: {
Expand All @@ -34,12 +33,6 @@ Vue.mixin({
Vue.prototype.OC = window.OC
Vue.prototype.OCA = window.OCA

// Init Viewer Service
if (window.OCA) {
Object.assign(window.OCA, { Viewer: new ViewerService() })
window.OCA.Viewer.version = appVersion
}

// Create document root
const ViewerRoot = document.createElement('div')
ViewerRoot.id = 'viewer'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
*
*/

import EyeSvg from '@mdi/svg/svg/eye.svg?raw'

import { DefaultType, FileAction, Permission, registerFileAction } from "@nextcloud/files"

Check failure on line 25 in src/services/FilesActionHandler.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Strings must use singlequote
import { translate as t } from '@nextcloud/l10n'


Check failure on line 28 in src/services/FilesActionHandler.ts

View workflow job for this annotation

GitHub Actions / NPM lint

More than 1 blank line not allowed
/**
* @param {Node} node The file to open
* @param {any} view any The files view
* @param {string} dir the directory path
*/
export default function(node, view, dir) {
function filesActionHandler(node, view, dir) {
// replace potential leading double slashes
const path = `${node.dirname}/${node.basename}`.replace(/^\/\//, '/')
const onClose = () => {
Expand All @@ -51,3 +57,29 @@ function pushToHistory(node, view, dir) {
true,
)
}

/**
*
*/
export function registerViewerAction() {
registerFileAction(new FileAction({
id: 'view',
displayName() {
return t('viewer', 'View')
},
iconSvgInline: () => EyeSvg,
default: DefaultType.DEFAULT,
enabled: (nodes) => {
// Disable if not located in user root
if (nodes.some(node => !(node.isDavRessource && node.root?.startsWith('/files')))) {
return false
}
// Faster to check if at least one node doesn't match the requirements
return !nodes.some(node => (
(node.permissions & Permission.READ) === 0
|| !window.OCA.Viewer.mimetypes.includes(node.mime)
))
},
exec: filesActionHandler,
}))
}
9 changes: 5 additions & 4 deletions src/services/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import Images from '../models/images.js'
import Videos from '../models/videos.js'
import Audios from '../models/audios.js'
import logger from './logger.js'

/**
* Handler type definition
Expand Down Expand Up @@ -78,7 +79,7 @@ export default class Viewer {
this.registerHandler(Videos)
this.registerHandler(Audios)

console.debug('OCA.Viewer initialized')
logger.debug('OCA.Viewer initialized')
}

/**
Expand All @@ -99,9 +100,9 @@ export default class Viewer {
* @param {Handler} handler a new unregistered handler
*/
registerHandler(handler) {
const err = this.validateHandler(handler)
if (err) {
console.error(err, handler)
const error = this.validateHandler(handler)
if (error) {
logger.error('Could not register handler', { error, handler })
return
}

Expand Down
43 changes: 7 additions & 36 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ import Vue from 'vue'
import axios from '@nextcloud/axios'
import { showError } from '@nextcloud/dialogs'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { registerFileAction, FileAction, Permission, DefaultType, Node } from '@nextcloud/files'
import getSortingConfig from '../services/FileSortingConfig.ts'
import isFullscreen from '@nextcloud/vue/dist/Mixins/isFullscreen.js'
Expand All @@ -206,7 +205,6 @@ import canDownload from '../utils/canDownload.js'
import cancelableRequest from '../utils/CancelableRequest.js'
import Error from '../components/Error.vue'
import File from '../models/file.js'
import filesActionHandler from '../services/FilesActionHandler.js'
import legacyFilesActionHandler from '../services/LegacyFilesActionHandler.js'
import getFileInfo from '../services/FileInfo.ts'
import getFileList from '../services/FileList.ts'
Expand All @@ -215,7 +213,6 @@ import logger from '../services/logger.js'
import Delete from 'vue-material-design-icons/Delete.vue'
import Download from 'vue-material-design-icons/Download.vue'
import EyeSvg from '@mdi/svg/svg/eye.svg?raw'
import Fullscreen from 'vue-material-design-icons/Fullscreen.vue'
import FullscreenExit from 'vue-material-design-icons/FullscreenExit.vue'
import Pencil from 'vue-material-design-icons/Pencil.vue'
Expand Down Expand Up @@ -287,8 +284,7 @@ export default {
isSidebarShown: false,
isFullscreenMode: false,
canSwipe: true,
// TODO: remove OCA?.Files?.fileActions when public Files is Vue
isStandalone: OCP?.Files === undefined && OCA?.Files?.fileActions === undefined,
isStandalone: false,
theme: null,
root: getRootPath(),
handlerId: '',
Expand Down Expand Up @@ -524,6 +520,12 @@ export default {
},
beforeMount() {
this.isStandalone = window.OCP?.Files === undefined && window.OCA?.Files?.fileActions === undefined
if (this.isStandalone) {
logger.info('No OCP.Files app found, viewer is now in standalone mode')
}
// register on load
document.addEventListener('DOMContentLoaded', () => {
// register all primary components mimes
Expand All @@ -543,16 +545,10 @@ export default {
this.Sidebar = OCA.Files.Sidebar.state
}
this.registerFileActions()
logger.info(`${this.handlers.length} viewer handlers registered`, { handlers: this.handlers })
})
window.addEventListener('resize', this.onResize)
if (this.isStandalone) {
logger.info('No OCP.Files app found, viewer is now in standalone mode')
}
},
mounted() {
Expand Down Expand Up @@ -940,31 +936,6 @@ export default {
}
},
registerFileActions() {
if (!this.isStandalone) {
registerFileAction(new FileAction({
id: 'view',
displayName() {
return t('viewer', 'View')
},
iconSvgInline: () => EyeSvg,
default: DefaultType.DEFAULT,
enabled: (nodes) => {
// Disable if not located in user root
if (nodes.some(node => !(node.isDavRessource && node.root?.startsWith('/files')))) {
return false
}
// Faster to check if at least one node doesn't match the requirements
return !nodes.some(node => (
(node.permissions & Permission.READ) === 0
|| !this.Viewer.mimetypes.includes(node.mime)
))
},
exec: filesActionHandler,
}))
}
},
/**
* Close the viewer
*/
Expand Down
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const plyrIcons = readFileSync(join(__dirname, 'node_modules', 'plyr', 'dist', '

export default createAppConfig({
main: 'src/main.js',
init: 'src/init.ts',
}, {
replace: {
PLYR_ICONS: JSON.stringify(plyrIcons),
Expand Down

0 comments on commit 235cc6f

Please sign in to comment.