|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { AsyncComponent, Component } from 'vue' |
| 7 | + |
| 8 | +export interface IHandler { |
| 9 | + /** |
| 10 | + * Unique identifier for the handler |
| 11 | + */ |
| 12 | + id: string |
| 13 | + |
| 14 | + /** |
| 15 | + * Indicate support for comparing two files |
| 16 | + */ |
| 17 | + canCompare?: boolean |
| 18 | + |
| 19 | + /** |
| 20 | + * Vue 2 component to render the file. |
| 21 | + */ |
| 22 | + component: Component | AsyncComponent |
| 23 | + |
| 24 | + /** |
| 25 | + * Group identifier to combine for navigating to the next/previous files |
| 26 | + */ |
| 27 | + group?: string |
| 28 | + |
| 29 | + /** |
| 30 | + * List of mime types that are supported for opening |
| 31 | + */ |
| 32 | + mimes?: string[] |
| 33 | + |
| 34 | + /** |
| 35 | + * Aliases for mime types, used to map different mime types to the same handler. |
| 36 | + */ |
| 37 | + mimesAliases?: Record<string, string> |
| 38 | + |
| 39 | + /** |
| 40 | + * Viewer modal theme (one of 'dark', 'light', 'default') |
| 41 | + */ |
| 42 | + theme?: 'dark' | 'light' | 'default' |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Register a new handler for the viewer. |
| 47 | + * This needs to be called before the viewer is initialized to ensure the handler is available. |
| 48 | + * So this should be called from an initialization script (`OCP\Util::addInitScript`). |
| 49 | + * |
| 50 | + * @param handler - The handler to register |
| 51 | + * @throws Error if the handler is invalid |
| 52 | + */ |
| 53 | +export function registerHandler(handler: IHandler): void { |
| 54 | + validateHandler(handler) |
| 55 | + |
| 56 | + window._oca_viewer_handlers ??= new Map<string, IHandler>() |
| 57 | + if (window._oca_viewer_handlers.has(handler.id)) { |
| 58 | + console.warn(`Handler with id ${handler.id} is already registered.`) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + window._oca_viewer_handlers.set(handler.id, handler) |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Validate the handler object. |
| 67 | + * |
| 68 | + * @param handler - The handler to validate |
| 69 | + */ |
| 70 | +function validateHandler(handler: IHandler) { |
| 71 | + const { id, mimes, mimesAliases, component } = handler |
| 72 | + |
| 73 | + // checking valid handler id |
| 74 | + if (!id || id.trim() === '' || typeof id !== 'string') { |
| 75 | + throw new Error('The handler does not have a valid id') |
| 76 | + } |
| 77 | + |
| 78 | + // Nothing available to process! Failure |
| 79 | + if ((!mimes || !Array.isArray(mimes)) && !mimesAliases) { |
| 80 | + throw new Error('Handler needs a valid mime array or mimesAliases') |
| 81 | + } |
| 82 | + |
| 83 | + // checking valid handler component data |
| 84 | + if ((!component || (typeof component !== 'object' && typeof component !== 'function'))) { |
| 85 | + throw new Error('The handler does not have a valid component') |
| 86 | + } |
| 87 | +} |
0 commit comments