{"version":3,"file":"fileAction.mjs","sources":["../../../lib/ui/actions/fileAction.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { ActionContext, ActionContextSingle } from '../types.ts'\n\nimport { scopedGlobals } from '../../globalScope.ts'\nimport logger from '../../utils/logger.ts'\nimport { getRegistry } from '../registry.ts'\n\nexport const DefaultType = Object.freeze({\n\tDEFAULT: 'default',\n\tHIDDEN: 'hidden',\n})\n\nexport type TDefaultType = typeof DefaultType[keyof typeof DefaultType]\n\nexport interface IHotkeyConfig {\n\t/**\n\t * Short, translated, description what this action is doing.\n\t * This will be used as the description next to the hotkey in the shortcuts overview.\n\t */\n\tdescription: string\n\n\t/**\n\t * The key to be pressed.\n\t *\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key\n\t */\n\tkey: string\n\n\t/**\n\t * If set then the callback is only called when the shift key is (not) pressed.\n\t * When left `undefined` a pressed shift key is ignored (callback is run with and without shift pressed).\n\t */\n\tshift?: boolean\n\n\t/**\n\t * Only execute the action if the control key is pressed.\n\t * On Mac devices the command key is used instead.\n\t */\n\tctrl?: true\n\n\t/**\n\t * Only execute the action if the alt key is pressed\n\t */\n\talt?: true\n}\n\nexport interface IFileAction {\n\t/** Unique ID */\n\tid: string\n\t/** Translatable string displayed in the menu */\n\tdisplayName: (context: ActionContext) => string\n\t/** Translatable title for of the action */\n\ttitle?: (context: ActionContext) => string\n\t/** Svg as inline string. <svg><path fill=\"...\" /></svg> */\n\ticonSvgInline: (context: ActionContext) => string\n\t/** Condition wether this action is shown or not */\n\tenabled?: (context: ActionContext) => boolean\n\n\t/**\n\t * Function executed on single file action\n\t *\n\t * @return true if the action was executed successfully,\n\t * false otherwise and null if the action is silent/undefined.\n\t * @throws {Error} If the action failed\n\t */\n\texec: (context: ActionContextSingle) => Promise<boolean | null>\n\t/**\n\t * Function executed on multiple files action\n\t *\n\t * @return true if the action was executed successfully,\n\t * false otherwise and null if the action is silent/undefined.\n\t * @throws {Error} If the action failed\n\t */\n\texecBatch?: (context: ActionContext) => Promise<(boolean | null)[]>\n\n\t/** This action order in the list */\n\torder?: number\n\n\t/**\n\t * Allows to define a hotkey which will trigger this action for the selected node.\n\t */\n\thotkey?: IHotkeyConfig\n\n\t/**\n\t * Set to true if this action is a destructive action, like \"delete\".\n\t * This will change the appearance in the action menu more prominent (e.g. red colored)\n\t */\n\tdestructive?: boolean\n\n\t/**\n\t * This action's parent id in the list.\n\t * If none found, will be displayed as a top-level action.\n\t */\n\tparent?: string\n\n\t/**\n\t * Make this action the default.\n\t *\n\t * If multiple actions are default, the first one will be used.\n\t * The other ones will be put as first entries in the actions menu iff `DefaultType.Hidden` is not used.\n\t *\n\t * A `DefaultType.Hidden` action will never be shown\n\t * in the actions menu even if another action takes\n\t * its place as default.\n\t *\n\t * @see DefaultType\n\t */\n\tdefault?: TDefaultType\n\t/**\n\t * If true, the renderInline function will be called\n\t */\n\tinline?: (context: ActionContextSingle) => boolean\n\t/**\n\t * If defined, the returned html element will be\n\t * appended before the actions menu.\n\t */\n\trenderInline?: (context: ActionContextSingle) => Promise<HTMLElement | null>\n}\n\n/**\n * Register a new file action.\n *\n * @param action - The file list action to register\n */\nexport function registerFileAction(action: IFileAction): void {\n\tvalidateAction(action)\n\n\tscopedGlobals.fileActions ??= new Map<string, IFileAction>()\n\tif (scopedGlobals.fileActions.has(action.id)) {\n\t\tlogger.error(`FileAction ${action.id} already registered`, { action })\n\t\treturn\n\t}\n\n\tscopedGlobals.fileActions.set(action.id, action)\n\tgetRegistry()\n\t\t.dispatchTypedEvent('register:action', new CustomEvent('register:action', { detail: action }))\n}\n\n/**\n * Get all registered file actions.\n */\nexport function getFileActions(): IFileAction[] {\n\tif (scopedGlobals.fileActions) {\n\t\treturn [...scopedGlobals.fileActions.values()]\n\t}\n\treturn []\n}\n\n/**\n * Validate a file action.\n *\n * @param action - The action to validate\n */\nfunction validateAction(action: IFileAction) {\n\tif (!action.id || typeof action.id !== 'string') {\n\t\tthrow new Error('Invalid id')\n\t}\n\n\tif (!action.displayName || typeof action.displayName !== 'function') {\n\t\tthrow new Error('Invalid displayName function')\n\t}\n\n\tif ('title' in action && typeof action.title !== 'function') {\n\t\tthrow new Error('Invalid title function')\n\t}\n\n\tif (!action.iconSvgInline || typeof action.iconSvgInline !== 'function') {\n\t\tthrow new Error('Invalid iconSvgInline function')\n\t}\n\n\tif (!action.exec || typeof action.exec !== 'function') {\n\t\tthrow new Error('Invalid exec function')\n\t}\n\n\t// Optional properties --------------------------------------------\n\tif ('enabled' in action && typeof action.enabled !== 'function') {\n\t\tthrow new Error('Invalid enabled function')\n\t}\n\n\tif ('execBatch' in action && typeof action.execBatch !== 'function') {\n\t\tthrow new Error('Invalid execBatch function')\n\t}\n\n\tif ('order' in action && typeof action.order !== 'number') {\n\t\tthrow new Error('Invalid order')\n\t}\n\n\tif (action.destructive !== undefined && typeof action.destructive !== 'boolean') {\n\t\tthrow new Error('Invalid destructive flag')\n\t}\n\n\tif ('parent' in action && typeof action.parent !== 'string') {\n\t\tthrow new Error('Invalid parent')\n\t}\n\n\tif (action.default && !Object.values(DefaultType).includes(action.default)) {\n\t\tthrow new Error('Invalid default')\n\t}\n\n\tif ('inline' in action && typeof action.inline !== 'function') {\n\t\tthrow new Error('Invalid inline function')\n\t}\n\n\tif ('renderInline' in action && typeof action.renderInline !== 'function') {\n\t\tthrow new Error('Invalid renderInline function')\n\t}\n\n\tif ('hotkey' in action && action.hotkey !== undefined) {\n\t\tif (typeof action.hotkey !== 'object') {\n\t\t\tthrow new Error('Invalid hotkey configuration')\n\t\t}\n\n\t\tif (typeof action.hotkey.key !== 'string' || !action.hotkey.key) {\n\t\t\tthrow new Error('Missing or invalid hotkey key')\n\t\t}\n\n\t\tif (typeof action.hotkey.description !== 'string' || !action.hotkey.description) {\n\t\t\tthrow new Error('Missing or invalid hotkey description')\n\t\t}\n\t}\n}\n"],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