import { assertExists } from '@blockeditor/global/utils'; import { flip, offset } from '@floating-ui/dom'; import { html } from 'lit'; import { createRef, ref, type RefOrCallback } from 'lit/directives/ref.js'; import { createLitPortal } from '../../_common/components/portal.js'; import { BookmarkIcon, CaptionIcon, EditIcon, EmbedWebIcon, LinkIcon, MoreIcon, ViewIcon, } from '../../_common/icons/index.js'; import { stopPropagation } from '../../_common/utils/event.js'; import type { AttachmentBlockComponent } from '../attachment-block.js'; import type { AttachmentBlockModel } from '../attachment-model.js'; import { allowEmbed, turnIntoEmbedAction } from '../embed.js'; import { MoreMenu } from './more-menu.js'; import { RenameModal } from './rename-model.js'; import { styles } from './styles.js'; export function AttachmentOptionsTemplate({ anchor, model, showCaption, downloadAttachment, abortController, ref: refOrCallback = createRef(), }: { anchor: AttachmentBlockComponent; model: AttachmentBlockModel; downloadAttachment: (model: AttachmentBlockModel) => void | Promise; showCaption: () => void; abortController: AbortController; ref?: RefOrCallback; }) { let containerEl: Element | undefined; const refCallback = (el: Element | undefined) => { containerEl = el; if (!refCallback) return; // See also https://github.com/lit/lit/blob/c134604f178e36444261d83eabe9e578c1ed90c4/packages/lit-html/src/directives/ref.ts typeof refOrCallback === 'function' ? refOrCallback(el) : (( refOrCallback as { // RefInternal value: Element | undefined; } ).value = el); }; const disableEmbed = !allowEmbed(model); const readonly = model.page.readonly; let moreMenuAbortController: AbortController | null = null; return html`
${ViewIcon} Preview
${LinkIcon} Turn into Link view ${BookmarkIcon} Turn into Card view ${EmbedWebIcon} Turn into Embed view
${EditIcon} Rename { showCaption(); }} > ${CaptionIcon} Caption
{ if (moreMenuAbortController) { moreMenuAbortController.abort(); moreMenuAbortController = null; return; } moreMenuAbortController = new AbortController(); assertExists(containerEl); createLitPortal({ container: containerEl, template: MoreMenu({ model, abortController, downloadAttachment }), abortController: moreMenuAbortController, computePosition: { referenceElement: containerEl, placement: 'top-end', middleware: [flip(), offset(4)], }, }); }} > ${MoreIcon} More
`; }