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 <opensource@fthiessen.de>
  • Loading branch information
susnux authored and artonge committed Oct 2, 2024
1 parent 6afc4e6 commit fe36bc1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 44 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');
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 './files_actions/viewerAction'
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 @@ -21,7 +21,6 @@
*/
import Vue from 'vue'
import ViewerComponent from './views/Viewer.vue'
import ViewerService from './services/Viewer.js'
import { translate as t } from '@nextcloud/l10n'

Vue.mixin({
Expand All @@ -37,12 +36,6 @@ INJECT_CYPRESS_FONT
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
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
39 changes: 6 additions & 33 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,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 +523,11 @@ export default {
},

beforeMount() {
this.isStandalone = window.OCP?.Files === 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 +547,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 @@ -933,31 +931,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 fe36bc1

Please sign in to comment.