Skip to content

Commit 78259ac

Browse files
committed
fix: Make loading the viewer an init script
Signed-off-by: Ferdinand Thiessen <[email protected]> Signed-off-by: Louis Chemineau <[email protected]>
1 parent 6018252 commit 78259ac

File tree

7 files changed

+66
-45
lines changed

7 files changed

+66
-45
lines changed

lib/Listener/LoadViewerScript.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public function handle(Event $event): void {
5454
return;
5555
}
5656

57+
Util::addStyle(Application::APP_ID, 'viewer-init');
58+
Util::addStyle(Application::APP_ID, 'viewer-main');
59+
Util::addInitScript(Application::APP_ID, 'viewer-init', 'files');
5760
Util::addScript(Application::APP_ID, 'viewer-main', 'files');
5861
$this->initialStateService->provideInitialState('enabled_preview_providers', array_keys($this->previewManager->getProviders()));
5962
}

src/init.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
import { registerViewerAction } from './services/FilesActionHandler'
6+
import ViewerService from './services/Viewer.js'
7+
8+
// Register the files action
9+
registerViewerAction()
10+
11+
// Init Viewer Service
12+
window.OCA = window.OCA ?? {}
13+
window.OCA.Viewer = new ViewerService()
14+
window.OCA.Viewer.version = appVersion

src/main.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import { translate as t } from '@nextcloud/l10n'
2424
import Vue from 'vue'
2525

2626
import ViewerComponent from './views/Viewer.vue'
27-
import ViewerService from './services/Viewer.js'
27+
import { translate as t } from '@nextcloud/l10n'
28+
29+
import { generateFilePath } from '@nextcloud/router'
2830

2931
Vue.mixin({
3032
methods: {
@@ -46,12 +48,6 @@ __webpack_nonce__ = btoa(OC.requestToken)
4648
// eslint-disable-next-line
4749
__webpack_public_path__ = generateFilePath('viewer', '', 'js/')
4850

49-
// Init Viewer Service
50-
if (window.OCA) {
51-
Object.assign(window.OCA, { Viewer: new ViewerService() })
52-
window.OCA.Viewer.version = appVersion
53-
}
54-
5551
// Create document root
5652
const ViewerRoot = document.createElement('div')
5753
ViewerRoot.id = 'viewer'
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020
*
2121
*/
2222

23+
import EyeSvg from '@mdi/svg/svg/eye.svg?raw'
24+
25+
import { DefaultType, FileAction, Permission, registerFileAction } from '@nextcloud/files'
26+
import { translate as t } from '@nextcloud/l10n'
27+
2328
/**
2429
* @param {Node} node The file to open
2530
* @param {any} view any The files view
2631
* @param {string} dir the directory path
2732
*/
28-
export default function(node, view, dir) {
33+
function filesActionHandler(node, view, dir) {
2934
// replace potential leading double slashes
3035
const path = `${node.dirname}/${node.basename}`.replace(/^\/\//, '/')
3136
const onClose = () => {
@@ -51,3 +56,29 @@ function pushToHistory(node, view, dir) {
5156
true,
5257
)
5358
}
59+
60+
/**
61+
*
62+
*/
63+
export function registerViewerAction() {
64+
registerFileAction(new FileAction({
65+
id: 'view',
66+
displayName() {
67+
return t('viewer', 'View')
68+
},
69+
iconSvgInline: () => EyeSvg,
70+
default: DefaultType.DEFAULT,
71+
enabled: (nodes) => {
72+
// Disable if not located in user root
73+
if (nodes.some(node => !(node.isDavRessource && node.root?.startsWith('/files')))) {
74+
return false
75+
}
76+
// Faster to check if at least one node doesn't match the requirements
77+
return !nodes.some(node => (
78+
(node.permissions & Permission.READ) === 0
79+
|| !window.OCA.Viewer.mimetypes.includes(node.mime)
80+
))
81+
},
82+
exec: filesActionHandler,
83+
}))
84+
}

src/services/Viewer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import Images from '../models/images.js'
2424
import Videos from '../models/videos.js'
2525
import Audios from '../models/audios.js'
26+
import logger from './logger.js'
2627

2728
/**
2829
* Handler type definition
@@ -78,7 +79,7 @@ export default class Viewer {
7879
this.registerHandler(Videos)
7980
this.registerHandler(Audios)
8081

81-
console.debug('OCA.Viewer initialized')
82+
logger.debug('OCA.Viewer initialized')
8283
}
8384

8485
/**

src/views/Viewer.vue

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ import Vue from 'vue'
193193
import axios from '@nextcloud/axios'
194194
import { showError } from '@nextcloud/dialogs'
195195
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
196-
import { registerFileAction, FileAction, Permission, DefaultType } from '@nextcloud/files'
197196
import getSortingConfig from '../services/FileSortingConfig.ts'
198197
199198
import isFullscreen from '@nextcloud/vue/dist/Mixins/isFullscreen.js'
@@ -205,7 +204,6 @@ import canDownload from '../utils/canDownload.js'
205204
import cancelableRequest from '../utils/CancelableRequest.js'
206205
import Error from '../components/Error.vue'
207206
import File from '../models/file.js'
208-
import filesActionHandler from '../services/FilesActionHandler.js'
209207
import legacyFilesActionHandler from '../services/LegacyFilesActionHandler.js'
210208
import getFileInfo from '../services/FileInfo.ts'
211209
import getFileList from '../services/FileList.ts'
@@ -214,7 +212,6 @@ import logger from '../services/logger.js'
214212
215213
import Delete from 'vue-material-design-icons/Delete.vue'
216214
import Download from 'vue-material-design-icons/Download.vue'
217-
import EyeSvg from '@mdi/svg/svg/eye.svg?raw'
218215
import Fullscreen from 'vue-material-design-icons/Fullscreen.vue'
219216
import FullscreenExit from 'vue-material-design-icons/FullscreenExit.vue'
220217
import Pencil from 'vue-material-design-icons/Pencil.vue'
@@ -286,8 +283,7 @@ export default {
286283
isSidebarShown: false,
287284
isFullscreenMode: false,
288285
canSwipe: true,
289-
// TODO: remove OCA?.Files?.fileActions when public Files is Vue
290-
isStandalone: OCP?.Files === undefined && OCA?.Files?.fileActions === undefined,
286+
isStandalone: false,
291287
theme: null,
292288
root: getRootPath(),
293289
handlerId: '',
@@ -523,6 +519,12 @@ export default {
523519
},
524520
525521
beforeMount() {
522+
this.isStandalone = window.OCP?.Files === undefined && window.OCA?.Files?.fileActions === undefined
523+
524+
if (this.isStandalone) {
525+
logger.info('No OCP.Files app found, viewer is now in standalone mode')
526+
}
527+
526528
// register on load
527529
document.addEventListener('DOMContentLoaded', () => {
528530
// register all primary components mimes
@@ -542,16 +544,10 @@ export default {
542544
this.Sidebar = OCA.Files.Sidebar.state
543545
}
544546
545-
this.registerFileActions()
546-
547547
logger.info(`${this.handlers.length} viewer handlers registered`, { handlers: this.handlers })
548548
})
549549
550550
window.addEventListener('resize', this.onResize)
551-
552-
if (this.isStandalone) {
553-
logger.info('No OCP.Files app found, viewer is now in standalone mode')
554-
}
555551
},
556552
557553
mounted() {
@@ -939,31 +935,6 @@ export default {
939935
}
940936
},
941937
942-
registerFileActions() {
943-
if (!this.isStandalone) {
944-
registerFileAction(new FileAction({
945-
id: 'view',
946-
displayName() {
947-
return t('viewer', 'View')
948-
},
949-
iconSvgInline: () => EyeSvg,
950-
default: DefaultType.DEFAULT,
951-
enabled: (nodes) => {
952-
// Disable if not located in user root
953-
if (nodes.some(node => !(node.isDavRessource && node.root?.startsWith('/files')))) {
954-
return false
955-
}
956-
// Faster to check if at least one node doesn't match the requirements
957-
return !nodes.some(node => (
958-
(node.permissions & Permission.READ) === 0
959-
|| !this.Viewer.mimetypes.includes(node.mime)
960-
))
961-
},
962-
exec: filesActionHandler,
963-
}))
964-
}
965-
},
966-
967938
/**
968939
* Close the viewer
969940
*/

webpack.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ if (isTesting) {
1515
console.debug('TESTING MODE ENABLED')
1616
}
1717

18+
webpackConfig.entry = {
19+
main: path.join(__dirname, 'src', 'main.js'),
20+
init: path.join(__dirname, 'src', 'init.js'),
21+
}
22+
1823
// vue-plyr uses .mjs file
1924
webpackRules.RULE_JS.test = /\.m?js$/
2025
webpackRules.RULE_JS.exclude = BabelLoaderExcludeNodeModulesExcept([

0 commit comments

Comments
 (0)