diff --git a/src/api_package/global.d.ts b/src/api_package/global.d.ts index ccd66138f..119493447 100644 --- a/src/api_package/global.d.ts +++ b/src/api_package/global.d.ts @@ -12,5 +12,11 @@ declare global { */ // eslint-disable-next-line camelcase _oca_viewer_handlers: Map + + OCA?: { + Viewer?: { + registerHandler?: (handler: IHandler) => void + } + } } } diff --git a/src/api_package/index.ts b/src/api_package/index.ts index bdb215809..58c7f9159 100644 --- a/src/api_package/index.ts +++ b/src/api_package/index.ts @@ -44,8 +44,9 @@ export interface IHandler { /** * Register a new handler for the viewer. - * This needs to be called before the viewer is initialized to ensure the handler is available. - * So this should be called from an initialization script (`OCP\Util::addInitScript`). + * Should be called from an initialization script (`OCP\Util::addInitScript`). + * If the ViewerService is already initialized the handler is registered + * immediately, otherwise it will be picked up on DOMContentLoaded. * * @param handler - The handler to register * @throws Error if the handler is invalid @@ -60,6 +61,15 @@ export function registerHandler(handler: IHandler): void { } window._oca_viewer_handlers.set(handler.id, handler) + + // If the ViewerService is already initialized, register the handler + // directly so its mimetypes are available immediately for file actions. + // Without this, mimetypes are only bridged on DOMContentLoaded which + // can race with the file list rendering — causing downloads instead of + // opening files in the viewer. + if (window.OCA?.Viewer?.registerHandler) { + window.OCA.Viewer.registerHandler(handler) + } } /**