Skip to content

Commit bfc9af1

Browse files
authored
Merge pull request #2958 from nextcloud/backport/2922/stable31
[stable31] feat: add API package to register handlers in init scripts
2 parents ad1a6ed + 7748f1a commit bfc9af1

16 files changed

+290
-18
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,46 @@ OCA.Viewer.close()
146146
```
147147

148148
### 🔍 Add you own file view
149+
150+
If you want to make your app compatible with this app, you can use the methods provided by the [`@nextcloud/viewer`](https://www.npmjs.com/package/@nextcloud/viewer) npm.js package:
151+
1. Create a vue component which use the `path` and `mime` props (they will be automatically passed by the viewer)
152+
2. Register your mime viewer with the following:
153+
``` js
154+
import { registerHandler } from '@nextcloud/viewer'
155+
import VideoView from 'VideoView.vue'
156+
157+
registerHandler({
158+
// unique id
159+
id: 'video',
160+
161+
// optional, it will group every view of this group and
162+
// use the proper view when building the file list
163+
// of the slideshow.
164+
// e.g. you open an image/jpeg that have the `media` group
165+
// you will be able to see the video/mpeg from the `video` handler
166+
// files that also have the `media` group set.
167+
group: 'media',
168+
169+
// the list of mimes your component is able to display
170+
mimes: [
171+
'video/mpeg',
172+
'video/ogg',
173+
'video/webm',
174+
'video/mp4'
175+
],
176+
177+
// your vue component view
178+
component: VideoView
179+
})
180+
```
181+
3. Make sure your script is loaded with `\OCP\Util::addInitScript` so that the handler is registered **before** the viewer is loaded.
182+
4. if you feel like your mime should be integrated on this repo, you can also create a pull request with your object on the `models` directory and the view on the `components` directory. Please have a look at what's already here and take example of it. 🙇‍♀️
183+
184+
185+
### Legacy handler registration
186+
> [!CAUTION]
187+
> Using OCA.Viewer for registering your handlers is not recommended as this might break depending on the script loading order
188+
149189
If you want to make your app compatible with this app, you can use the `OCA.Viewer` methods
150190
1. Create a vue component which use the `path` and `mime` props (they will be automatically passed by the viewer)
151191
2. Register your mime viewer with the following:
@@ -176,4 +216,4 @@ If you want to make your app compatible with this app, you can use the `OCA.View
176216
component: VideoView
177217
})
178218
```
179-
3. if you feel like your mime should be integrated on this repo, you can also create a pull request with your object on the `models` directory and the view on the `components` directory. Please have a look at what's already here and take example of it. 🙇‍♀️
219+
3. Make sure your script is loaded with `\OCP\Util::addScript` (in contrast to using the API package)!

css/main-zfQgoLdI.chunk.css renamed to css/main-C3PMk74T.chunk.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css/viewer-main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/* extracted by css-entry-points-plugin */
2-
@import './main-zfQgoLdI.chunk.css';
2+
@import './main-C3PMk74T.chunk.css';
33
@import './viewerAction-ziP2nhPq.chunk.css';

js/viewer-main.mjs

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

js/viewer-main.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api_package/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
/dist
5+
/package-lock.json

src/api_package/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!--
2+
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
3+
- SPDX-License-Identifier: AGPL-3.0-or-later
4+
-->
5+
# Nextcloud Viewer integration
6+
7+
[![REUSE status](https://api.reuse.software/badge/github.com/nextcloud/viewer)](https://api.reuse.software/info/github.com/nextcloud/viewer)
8+
9+
10+
## Usage
11+
### 🔍 Add you own file view
12+
13+
If you want to make your app compatible with this app, you can use the methods provided by the [`@nextcloud/viewer`](https://www.npmjs.com/package/@nextcloud/viewer) npm.js package:
14+
1. Create a vue component which use the `path` and `mime` props (they will be automatically passed by the viewer)
15+
2. Register your mime viewer with the following:
16+
``` js
17+
import { registerHandler } from '@nextcloud/viewer'
18+
import VideoView from 'VideoView.vue'
19+
20+
registerHandler({
21+
// unique id
22+
id: 'video',
23+
24+
// optional, it will group every view of this group and
25+
// use the proper view when building the file list
26+
// of the slideshow.
27+
// e.g. you open an image/jpeg that have the `media` group
28+
// you will be able to see the video/mpeg from the `video` handler
29+
// files that also have the `media` group set.
30+
group: 'media',
31+
32+
// the list of mimes your component is able to display
33+
mimes: [
34+
'video/mpeg',
35+
'video/ogg',
36+
'video/webm',
37+
'video/mp4'
38+
],
39+
40+
// your vue component view
41+
component: VideoView
42+
})
43+
```
44+
3. Make sure your script is loaded with `\OCP\Util::addInitScript` so that the handler is registered **before** the viewer is loaded.
45+
46+
> [!TIP]
47+
> If you feel like your mime should be integrated on this repo, you can also create a pull request with your object on the `models` directory and the view on the `components` directory. Please have a look at what's already here and take example of it. 🙇‍♀️

src/api_package/REUSE.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
2+
# SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
version = 1
5+
SPDX-PackageName = "@nextcloud/viewer"
6+
SPDX-PackageSupplier = "2025 Nextcloud GmbH and Nextcloud contributors"
7+
SPDX-PackageDownloadLocation = "https://github.com/nextcloud/viewer"
8+
9+
[[annotations]]
10+
path = ["package.json", "tsconfig.json"]
11+
precedence = "aggregate"
12+
SPDX-FileCopyrightText = "2025 Nextcloud GmbH and Nextcloud contributors"
13+
SPDX-License-Identifier = "AGPL-3.0-or-later"

src/api_package/global.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*!
2+
* SPDX-License-Identifier: AGPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
4+
*/
5+
6+
import type { IHandler } from './index.ts'
7+
8+
declare global {
9+
interface Window {
10+
/**
11+
* Registered viewer handlers.
12+
*/
13+
// eslint-disable-next-line camelcase
14+
_oca_viewer_handlers: Map<string, IHandler>
15+
}
16+
}

0 commit comments

Comments
 (0)