import { EditorChevronDown } from '@blocksuite/affine-components/toolbar'; import type { ToolbarContext } from '@blocksuite/affine-shared/services'; import type { BlockComponent } from '@blocksuite/std'; import { html } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { repeat } from 'lit/directives/repeat.js'; import type { Menu, MenuItem } from './types'; export function renderCurrentMenuItemWith>( items: MenuItem[], currentValue: T, field: F ) { return items.find(({ value }) => value === currentValue)?.[field]; } export function renderMenu({ label, tooltip, icon, items, currentValue, onPick, }: Menu) { return html` ${icon ?? renderCurrentMenuItemWith(items, currentValue, 'icon')} ${EditorChevronDown} `} > ${renderMenuItems(items, currentValue, onPick)} `; } export function renderMenuItems( items: MenuItem[], currentValue: T, onPick: (value: T) => void ) { return repeat( items, item => item.value, ({ key, value, icon, disabled }) => html` onPick(value)} > ${icon} ` ); } export function getRootBlock(ctx: ToolbarContext): BlockComponent | null { const rootModel = ctx.store.root; if (!rootModel) return null; return ctx.view.getBlock(rootModel.id); }