diff --git a/plugins/view-resources/src/actions.ts b/plugins/view-resources/src/actions.ts index 3c0cbc5d1e..6a7d291573 100644 --- a/plugins/view-resources/src/actions.ts +++ b/plugins/view-resources/src/actions.ts @@ -41,6 +41,18 @@ import view from './plugin' import { type FocusSelection, type SelectionStore } from './selection' import { restrictionStore } from './utils' +/** + * Collapses a single-element array to the element, otherwise returns the original value. + * + * @public + */ +export function normalizeActionContext (doc: T | T[] | undefined): T | T[] | undefined { + if (Array.isArray(doc) && doc.length === 1) { + return doc[0] + } + return doc +} + /** * @public */ @@ -114,7 +126,7 @@ export async function filterAvailableActions ( } else { const visibilityTester = await getResource(action.visibilityTester) - if (await visibilityTester(doc)) { + if (await visibilityTester(normalizeActionContext(doc))) { result.push(action) } } @@ -132,7 +144,7 @@ export async function invokeAction ( ): Promise { const impl = await getResource(action.action) Analytics.handleEvent(action.analyticsEvent ?? action._id) - await impl(Array.isArray(object) && object.length === 1 ? object[0] : object, evt, { + await impl(normalizeActionContext(object), evt, { ...action.actionProps, ...props }) diff --git a/plugins/view-resources/src/components/ActionsPopup.svelte b/plugins/view-resources/src/components/ActionsPopup.svelte index 8719fa8206..0b11ffed81 100644 --- a/plugins/view-resources/src/components/ActionsPopup.svelte +++ b/plugins/view-resources/src/components/ActionsPopup.svelte @@ -51,7 +51,7 @@ } from '@hcengineering/ui' import { Action, ActionCategory, ViewContext } from '@hcengineering/view' import { createEventDispatcher, onMount, tick } from 'svelte' - import { filterActions, getSelection } from '../actions' + import { filterActions, getSelection, normalizeActionContext } from '../actions' import view from '../plugin' import { focusStore, selectionStore } from '../selection' import { openDoc } from '../utils' @@ -99,7 +99,7 @@ } else { const visibilityTester = await getResource(action.visibilityTester) - if (await visibilityTester(docs)) { + if (await visibilityTester(normalizeActionContext(docs))) { resultActions.push(action) } }