Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api_package/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ declare global {
*/
// eslint-disable-next-line camelcase
_oca_viewer_handlers: Map<string, IHandler>

OCA?: {
Viewer?: {
registerHandler?: (handler: IHandler) => void
}
}
}
}
14 changes: 12 additions & 2 deletions src/api_package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

/**
Expand Down
Loading