|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { IFolder, INode } from '../node/index.ts' |
| 7 | +import logger from '../utils/logger.ts' |
| 8 | + |
| 9 | +interface SidebarUpdateContext { |
| 10 | + /** |
| 11 | + * The currently selected node for which the sidebar is shown. |
| 12 | + */ |
| 13 | + node: INode |
| 14 | + |
| 15 | + /** |
| 16 | + * The parent of the current selected node. |
| 17 | + * This is not necessarily the real parent folder of the node in means of the real filesystem tree, |
| 18 | + * but rather the parent folder in the current view of the files app. |
| 19 | + */ |
| 20 | + parent: IFolder |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Implementation of a custom sidebar tab within the files app. |
| 25 | + */ |
| 26 | +export interface ISidebarTab { |
| 27 | + /** |
| 28 | + * Unique id of the sidebar tab. |
| 29 | + * This has to conform to the HTML id attribute specification. |
| 30 | + */ |
| 31 | + id: string |
| 32 | + |
| 33 | + /** |
| 34 | + * The localized name of the sidebar tab. |
| 35 | + */ |
| 36 | + displayName: string |
| 37 | + |
| 38 | + /** |
| 39 | + * The icon, as SVG, of the sidebar tab. |
| 40 | + */ |
| 41 | + iconSvg: string |
| 42 | + |
| 43 | + /** |
| 44 | + * The order of this tab. |
| 45 | + * Use a low number to make this tab ordered in front. |
| 46 | + */ |
| 47 | + order: number |
| 48 | + |
| 49 | + /** |
| 50 | + * Callback to check if the sidebar tab should be shown for the selected node. |
| 51 | + * |
| 52 | + * @param node - The currently selected node |
| 53 | + */ |
| 54 | + enabled: (node: INode) => boolean |
| 55 | + |
| 56 | + /** |
| 57 | + * Called by the files app if this tab has become the active tab or was deactivated. |
| 58 | + * |
| 59 | + * @param active - The new active state of this tab |
| 60 | + */ |
| 61 | + setActive: (active: boolean) => void | Promise<void> |
| 62 | + |
| 63 | + /** |
| 64 | + * The lifecylce method for mounting the sidebar tab onto the sidebar. |
| 65 | + * |
| 66 | + * @param el - The element to mount the sidebar tab to |
| 67 | + * @param context - The current sidebar context |
| 68 | + */ |
| 69 | + mount: (el: HTMLElement, context: SidebarUpdateContext) => void | Promise<void> |
| 70 | + |
| 71 | + /** |
| 72 | + * The lifecycle method for updating the sidebar tab. |
| 73 | + * This is called if the currently selected node changes. |
| 74 | + * |
| 75 | + * @param context - The current sidebar context |
| 76 | + */ |
| 77 | + update: (context: SidebarUpdateContext) => void | Promise<void> |
| 78 | + |
| 79 | + /** |
| 80 | + * The lifecycle method for unmounting the sidebar tab. |
| 81 | + * This is called if the sidebar is unmounted from the files app and thus the sidebar tab needs to do its cleanup and unmounting. |
| 82 | + */ |
| 83 | + unmount: () => void | Promise<void> |
| 84 | + |
| 85 | + /** |
| 86 | + * Called when the bottom of the sidebar was reached during scrolling. |
| 87 | + */ |
| 88 | + onScrollBottomReached?: () => void | Promise<void> |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Register a new sidebar tab for the files app. |
| 93 | + * |
| 94 | + * @param tab - The sidebar tab to register |
| 95 | + * @throws If the provided tab is not a valid sidebar tab and thus cannot be registered. |
| 96 | + */ |
| 97 | +export function registerSidebarTab(tab: ISidebarTab): void { |
| 98 | + validateSidebarTab(tab) |
| 99 | + |
| 100 | + window._nc_files_sidebar_tabs ??= new Map<string, ISidebarTab>() |
| 101 | + if (window._nc_files_sidebar_tabs.has(tab.id)) { |
| 102 | + logger.warn(`Sidebar tab with id "${tab.id}" already registered. Skipping.`) |
| 103 | + return |
| 104 | + } |
| 105 | + window._nc_files_sidebar_tabs.set(tab.id, tab) |
| 106 | + logger.debug(`New sidebar tab with id "${tab.id}" registered.`) |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Get all currently registered sidebar tabs. |
| 111 | + */ |
| 112 | +export function getSidebarTabs(): ISidebarTab[] { |
| 113 | + if (window._nc_files_sidebar_tabs) { |
| 114 | + return [...window._nc_files_sidebar_tabs.values()] |
| 115 | + } |
| 116 | + return [] |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Check if a given sidebar tab objects implements all necessary fields. |
| 121 | + * |
| 122 | + * @param tab - The sidebar tab to validate |
| 123 | + */ |
| 124 | +function validateSidebarTab(tab: ISidebarTab): void { |
| 125 | + if (typeof tab !== 'object') { |
| 126 | + throw new Error('Sidebar tab is not an object') |
| 127 | + } |
| 128 | + |
| 129 | + if (!tab.id || (typeof tab.id !== 'string') || tab.id !== CSS.escape(tab.id)) { |
| 130 | + throw new Error('Sidebar tabs need to have an id conforming to the HTML id attribute specifications') |
| 131 | + } |
| 132 | + |
| 133 | + if (!tab.displayName || typeof tab.displayName !== 'string') { |
| 134 | + throw new Error('Sidebar tabs need to have a name set') |
| 135 | + } |
| 136 | + |
| 137 | + if (typeof tab.iconSvg !== 'string' || !tab.iconSvg.match(/^<svg.+<\/svg>$/i)) { |
| 138 | + throw new Error('Sidebar tabs need to have an valid SVG icon') |
| 139 | + } |
| 140 | + |
| 141 | + if (typeof tab.order !== 'number') { |
| 142 | + throw new Error('Sidebar tabs need to have a numeric order set') |
| 143 | + } |
| 144 | + |
| 145 | + if (typeof tab.enabled !== 'function') { |
| 146 | + throw new Error('Sidebar tabs need to have an "enabled" method') |
| 147 | + } |
| 148 | + |
| 149 | + if (typeof tab.setActive !== 'function') { |
| 150 | + throw new Error('Sidebar tabs need to have a "setActive" method') |
| 151 | + } |
| 152 | + |
| 153 | + if (typeof tab.mount !== 'function' |
| 154 | + || typeof tab.update !== 'function' |
| 155 | + || typeof tab.unmount !== 'function' |
| 156 | + ) { |
| 157 | + throw new Error('Sidebar tab is missing a required lifecycle method') |
| 158 | + } |
| 159 | + |
| 160 | + if (tab.onScrollBottomReached && typeof tab.onScrollBottomReached !== 'function') { |
| 161 | + throw new Error('"onScrollBottomReached" of the sidebar tab needs to be a function') |
| 162 | + } |
| 163 | +} |
0 commit comments