import type { Editor } from '@tiptap/core' import styles from './EditorBubbleMenu.module.scss' import { Icon } from '../_shared/Icon' import { clsx } from 'clsx' import { createEditorTransaction } from 'solid-tiptap' import { createSignal } from 'solid-js' import { useLocalize } from '../../context/localize' import validateUrl from '../../utils/validateUrl' type BubbleMenuProps = { editor: Editor ref: (el: HTMLDivElement) => void } export const EditorBubbleMenu = (props: BubbleMenuProps) => { const { t } = useLocalize() const [linkEditorOpen, setLinkEditorOpen] = createSignal(false) const [url, setUrl] = createSignal('') const [prevUrl, setPrevUrl] = createSignal(null) const [linkError, setLinkError] = createSignal(null) const isBold = createEditorTransaction( () => props.editor, (editor) => editor && editor.isActive('bold') ) const isLink = createEditorTransaction( () => props.editor, (editor) => { editor && editor.isActive('link') setPrevUrl(editor && editor.getAttributes('link').href) } ) const clearLinkForm = () => { setUrl('') setLinkEditorOpen(false) } const handleSubmitLink = (e) => { e.preventDefault() if (url().length === 0) { props.editor.chain().focus().unsetLink().run() clearLinkForm() return } if (url().length > 1 && validateUrl(url())) { props.editor.commands.toggleLink({ href: url() }) clearLinkForm() } else { setLinkError(t('Invalid url format')) } } return ( <>
{linkEditorOpen() ? ( <>
handleSubmitLink(e)} class={styles.linkForm}> setUrl(e.currentTarget.value)} />
{linkError() &&
{linkError()}
} ) : ( <>
D
)}
) }