import { createEffect, createSignal, Show } from 'solid-js' 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 { useLocalize } from '../../context/localize' import validateUrl from '../../utils/validateUrl' type HeadingLevel = 1 | 2 | 3 type ActionName = 'heading' | 'bold' | 'italic' | 'blockquote' | 'isOrderedList' | 'isBulletList' type BubbleMenuProps = { editor: Editor ref: (el: HTMLDivElement) => void } export const EditorBubbleMenu = (props: BubbleMenuProps) => { const { t } = useLocalize() const [textSizeBubbleOpen, setTextSizeBubbleOpen] = createSignal(false) const [listBubbleOpen, setListBubbleOpen] = createSignal(false) const [linkEditorOpen, setLinkEditorOpen] = createSignal(false) const [url, setUrl] = createSignal('') const [prevUrl, setPrevUrl] = createSignal(null) const [linkError, setLinkError] = createSignal(null) const activeControl = (action: ActionName, actionLevel?: HeadingLevel, prevLink?: boolean) => createEditorTransaction( () => props.editor, (editor) => editor && editor.isActive(action, actionLevel && { actionLevel }) ) const isLink = createEditorTransaction( // вызов этой ф-ии обусловлен не через хэлпер activeControl только проверкой установленна ли ссылка () => 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 > 1 && validateUrl(url())) { props.editor.commands.toggleLink({ href: url() }) clearLinkForm() } else { setLinkError(t('Invalid url format')) } } const toggleTextSizePopup = () => { if (listBubbleOpen()) setListBubbleOpen(false) setTextSizeBubbleOpen((prev) => !prev) } const toggleListPopup = () => { if (textSizeBubbleOpen()) setTextSizeBubbleOpen(false) setListBubbleOpen((prev) => !prev) } return ( <>
{linkEditorOpen() ? ( <>
handleSubmitLink(e)} class={styles.linkForm}> setUrl(e.currentTarget.value)} />
{linkError() &&
{linkError()}
} ) : ( <>
{t('Headers')}
{t('Quotes')}
{t('Lists')}
)}
) }