import type { EditorHost } from '@blockeditor/lit'; import { ShadowlessElement, WithDisposable } from '@blockeditor/lit'; import { html } from 'lit'; import { customElement, property, query, state } from 'lit/decorators.js'; import { classMap } from 'lit/directives/class-map.js'; import type { BookmarkBlockModel } from '../../../../bookmark-block/bookmark-model.js'; import type { EmbedFigmaModel } from '../../../../embed-figma-block/embed-figma-model.js'; import type { EmbedGithubModel } from '../../../../embed-github-block/embed-github-model.js'; import type { EmbedYoutubeModel } from '../../../../embed-youtube-block/embed-youtube-model.js'; import { toast } from '../../toast.js'; import { embedCardModalStyles } from './styles.js'; type EmbedCardModel = | BookmarkBlockModel | EmbedGithubModel | EmbedYoutubeModel | EmbedFigmaModel; @customElement('embed-card-edit-modal') export class EmbedCardEditModal extends WithDisposable(ShadowlessElement) { static override styles = embedCardModalStyles; @property({ attribute: false }) model!: EmbedCardModel; @property({ attribute: false }) host!: EditorHost; @query('.title') titleInput!: HTMLInputElement; @query('.description') descInput!: HTMLInputElement; @state() private _titleInputValue = ''; override connectedCallback() { super.connectedCallback(); this.updateComplete .then(() => { this.titleInput.focus(); this.titleInput.setSelectionRange(0, this.titleInput.value.length); }) .catch(console.error); this.disposables.addFromEvent(this, 'keydown', this._onDocumentKeydown); this._titleInputValue = this.model.title ?? ''; } private _handleInput(e: InputEvent) { const target = e.target as HTMLInputElement; this._titleInputValue = target.value; } private _onDocumentKeydown(e: KeyboardEvent) { e.stopPropagation(); if (e.key === 'Enter' && !e.isComposing) { this._onSave(); } if (e.key === 'Escape') { this.remove(); } } private _onSave() { const title = this.titleInput.value; if (title.length === 0) { toast(this.host, 'Link title can not be empty'); return; } this.model.page.updateBlock(this.model, { title, description: this.descInput.value, }); this.remove(); } override render() { return html`
this.remove()}>
Edit Link
this.remove()} > Cancel
this._onSave()} > Save
`; } } export function toggleEmbedCardEditModal( host: EditorHost, embedCardModel: EmbedCardModel ) { host.selection.clear(); const embedCardEditModal = new EmbedCardEditModal(); embedCardEditModal.model = embedCardModel; embedCardEditModal.host = host; document.body.appendChild(embedCardEditModal); } declare global { interface HTMLElementTagNameMap { 'embed-card-edit-modal': EmbedCardEditModal; } }