import type { Editor } from '@tiptap/core' import { clsx } from 'clsx' import { Switch, Match, createSignal, Show, onMount, onCleanup, createEffect, lazy } from 'solid-js' import { createEditorTransaction } from 'solid-tiptap' import { useLocalize } from '../../../context/localize' import { Icon } from '../../_shared/Icon' import { Popover } from '../../_shared/Popover' import { InsertLinkForm } from '../InsertLinkForm' import styles from './TextBubbleMenu.module.scss' const SimplifiedEditor = lazy(() => import('../../Editor/SimplifiedEditor')) type BubbleMenuProps = { editor: Editor isCommonMarkup: boolean ref: (el: HTMLDivElement) => void shouldShow: boolean } export const TextBubbleMenu = (props: BubbleMenuProps) => { const { t } = useLocalize() const isActive = (name: string, attributes?: unknown) => createEditorTransaction( () => props.editor, (editor) => editor && editor.isActive(name, attributes), ) const [textSizeBubbleOpen, setTextSizeBubbleOpen] = createSignal(false) const [listBubbleOpen, setListBubbleOpen] = createSignal(false) const [linkEditorOpen, setLinkEditorOpen] = createSignal(false) const [footnoteEditorOpen, setFootnoteEditorOpen] = createSignal(false) const [footNote, setFootNote] = createSignal() createEffect(() => { if (!props.shouldShow) { setFootNote() setFootnoteEditorOpen(false) setLinkEditorOpen(false) setTextSizeBubbleOpen(false) setListBubbleOpen(false) } }) const isBold = isActive('bold') const isItalic = isActive('italic') const isH1 = isActive('heading', { level: 2 }) const isH2 = isActive('heading', { level: 3 }) const isH3 = isActive('heading', { level: 4 }) const isQuote = isActive('blockquote', { 'data-type': 'quote' }) const isPunchLine = isActive('blockquote', { 'data-type': 'punchline' }) const isOrderedList = isActive('isOrderedList') const isBulletList = isActive('isBulletList') const isLink = isActive('link') const isHighlight = isActive('highlight') const isFootnote = isActive('footnote') const isIncut = isActive('article') const toggleTextSizePopup = () => { if (listBubbleOpen()) { setListBubbleOpen(false) } setTextSizeBubbleOpen((prev) => !prev) } const toggleListPopup = () => { if (textSizeBubbleOpen()) { setTextSizeBubbleOpen(false) } setListBubbleOpen((prev) => !prev) } const handleKeyDown = async (event) => { if (event.code === 'KeyK' && (event.metaKey || event.ctrlKey) && !props.editor.state.selection.empty) { event.preventDefault() setLinkEditorOpen(true) } } const updateCurrentFootnoteValue = createEditorTransaction( () => props.editor, (ed) => { if (!isFootnote()) { return } const value = ed.getAttributes('footnote').value setFootNote(value) }, ) const handleAddFootnote = (footnote) => { if (footNote()) { props.editor.chain().focus().updateFootnote(footnote).run() } else { props.editor.chain().focus().setFootnote({ value: footnote }).run() } setFootNote() setLinkEditorOpen(false) setFootnoteEditorOpen(false) } const handleOpenFootnoteEditor = () => { updateCurrentFootnoteValue() setLinkEditorOpen(false) setFootnoteEditorOpen(true) } const handleSetPunchline = () => { if (isPunchLine()) { props.editor.chain().focus().toggleBlockquote('punchline').run() } props.editor.chain().focus().toggleBlockquote('quote').run() toggleTextSizePopup() } const handleSetQuote = () => { if (isQuote()) { props.editor.chain().focus().toggleBlockquote('quote').run() } props.editor.chain().focus().toggleBlockquote('punchline').run() toggleTextSizePopup() } onMount(() => { window.addEventListener('keydown', handleKeyDown) onCleanup(() => { window.removeEventListener('keydown', handleKeyDown) setLinkEditorOpen(false) }) }) return (
setLinkEditorOpen(false)} /> handleAddFootnote(value)} variant={'bordered'} initialContent={footNote()} onCancel={() => { setFootnoteEditorOpen(false) }} submitButtonText={t('Send')} /> <> <>
{t('Headers')}
{(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )}
{t('Quotes')}
{(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )}
{t('squib')}
{(triggerRef: (el) => void) => ( )}
{(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )}
{t('Add url')}
}> {(triggerRef: (el) => void) => ( )} <> {(triggerRef: (el) => void) => ( )}
{t('Lists')}
{(triggerRef: (el) => void) => ( )} {(triggerRef: (el) => void) => ( )}
) }