import type { BlockStdScope } from '@blocksuite/std'; import { html, nothing } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { join } from 'lit/directives/join.js'; import { repeat } from 'lit/directives/repeat.js'; import { ToolbarMoreMenuConfigExtension } from './config.js'; import type { MenuContext } from './menu-context.js'; import type { FatMenuItems, MenuItem, MenuItemGroup, ToolbarMoreMenuConfig, } from './types.js'; export function groupsToActions( groups: MenuItemGroup[], context: T ): MenuItem[][] { return groups .filter(group => group.when?.(context) ?? true) .map(({ items }) => items .filter(item => item.when?.(context) ?? true) .map(({ type, label, tooltip, icon, action, disabled, generate }) => { if (action && typeof action === 'function') { return { type, label, tooltip, icon, action: () => { action(context)?.catch(console.error); }, disabled: typeof disabled === 'function' ? disabled(context) : disabled, }; } if (generate && typeof generate === 'function') { const result = generate(context); if (!result) return null; return { type, label, tooltip, icon, ...result, }; } return null; }) .filter(item => !!item) ); } export function renderActions( fatMenuItems: FatMenuItems, action?: (item: MenuItem) => Promise | void, selectedName?: string ) { return join( fatMenuItems .filter(g => g.length) .map(g => g.filter(a => a !== nothing) as MenuItem[]) .filter(g => g.length) .map(items => repeat( items, item => item.type, item => item.render?.(item) ?? html` action?.(item)} > ${item.icon}${item.label ? html`${item.label}` : nothing} ` ) ), () => html` ` ); } export function cloneGroups(groups: MenuItemGroup[]) { return groups.map(group => ({ ...group, items: [...group.items] })); } export function renderGroups(groups: MenuItemGroup[], context: T) { return renderActions(groupsToActions(groups, context)); } export function renderToolbarSeparator(orientation?: 'horizontal') { return html``; } export function getMoreMenuConfig(std: BlockStdScope): ToolbarMoreMenuConfig { return { configure: (groups: MenuItemGroup[]) => groups, ...std.getOptional(ToolbarMoreMenuConfigExtension.identifier), }; }