2023-09-04 06:06:12 +00:00
|
|
|
import { Switch, Match, createSignal, Show, onMount, onCleanup, createEffect } from 'solid-js'
|
2023-05-04 12:16:39 +00:00
|
|
|
import type { Editor } from '@tiptap/core'
|
|
|
|
import styles from './TextBubbleMenu.module.scss'
|
|
|
|
import { Icon } from '../../_shared/Icon'
|
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import { createEditorTransaction } from 'solid-tiptap'
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
2023-05-29 17:14:58 +00:00
|
|
|
import { Popover } from '../../_shared/Popover'
|
2023-07-24 08:58:07 +00:00
|
|
|
import { InsertLinkForm } from '../InsertLinkForm'
|
2023-08-28 11:48:54 +00:00
|
|
|
import SimplifiedEditor from '../SimplifiedEditor'
|
|
|
|
import { Button } from '../../_shared/Button'
|
|
|
|
import { showModal } from '../../../stores/ui'
|
2023-05-04 12:16:39 +00:00
|
|
|
|
|
|
|
type BubbleMenuProps = {
|
|
|
|
editor: Editor
|
2023-05-11 11:43:14 +00:00
|
|
|
isCommonMarkup: boolean
|
2023-05-04 12:16:39 +00:00
|
|
|
ref: (el: HTMLDivElement) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TextBubbleMenu = (props: BubbleMenuProps) => {
|
|
|
|
const { t } = useLocalize()
|
|
|
|
|
|
|
|
const isActive = (name: string, attributes?: unknown) =>
|
|
|
|
createEditorTransaction(
|
|
|
|
() => props.editor,
|
2023-09-04 06:06:12 +00:00
|
|
|
(editor) => editor && editor.isActive(name, attributes)
|
2023-05-04 12:16:39 +00:00
|
|
|
)
|
2023-09-04 06:06:12 +00:00
|
|
|
|
2023-07-24 08:58:07 +00:00
|
|
|
const [textSizeBubbleOpen, setTextSizeBubbleOpen] = createSignal(false)
|
|
|
|
const [listBubbleOpen, setListBubbleOpen] = createSignal(false)
|
|
|
|
const [linkEditorOpen, setLinkEditorOpen] = createSignal(false)
|
2023-08-28 11:48:54 +00:00
|
|
|
const [footnoteEditorOpen, setFootnoteEditorOpen] = createSignal(false)
|
|
|
|
const [footNote, setFootNote] = createSignal<string>()
|
2023-05-04 12:16:39 +00:00
|
|
|
|
|
|
|
const isBold = isActive('bold')
|
|
|
|
const isItalic = isActive('italic')
|
2023-06-24 14:45:57 +00:00
|
|
|
const isH1 = isActive('heading', { level: 2 })
|
|
|
|
const isH2 = isActive('heading', { level: 3 })
|
|
|
|
const isH3 = isActive('heading', { level: 4 })
|
2023-09-04 06:06:12 +00:00
|
|
|
const isQuote = isActive('blockquote', { 'data-type': 'quote' })
|
|
|
|
const isPunchLine = isActive('blockquote', { 'data-type': 'punchline' })
|
2023-05-04 12:16:39 +00:00
|
|
|
const isOrderedList = isActive('isOrderedList')
|
|
|
|
const isBulletList = isActive('isBulletList')
|
|
|
|
const isLink = isActive('link')
|
2023-05-09 17:31:28 +00:00
|
|
|
const isHighlight = isActive('highlight')
|
2023-08-28 11:48:54 +00:00
|
|
|
const isFootnote = isActive('footnote')
|
2023-09-04 06:06:12 +00:00
|
|
|
const isIncut = isActive('article')
|
2023-05-04 12:16:39 +00:00
|
|
|
|
|
|
|
const toggleTextSizePopup = () => {
|
|
|
|
if (listBubbleOpen()) {
|
|
|
|
setListBubbleOpen(false)
|
|
|
|
}
|
|
|
|
setTextSizeBubbleOpen((prev) => !prev)
|
|
|
|
}
|
|
|
|
const toggleListPopup = () => {
|
|
|
|
if (textSizeBubbleOpen()) {
|
|
|
|
setTextSizeBubbleOpen(false)
|
|
|
|
}
|
|
|
|
setListBubbleOpen((prev) => !prev)
|
|
|
|
}
|
2023-07-28 19:53:21 +00:00
|
|
|
const handleKeyDown = async (event) => {
|
|
|
|
if (event.code === 'KeyK' && (event.metaKey || event.ctrlKey) && !props.editor.state.selection.empty) {
|
|
|
|
event.preventDefault()
|
|
|
|
setLinkEditorOpen(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:48:54 +00:00
|
|
|
const currentFootnoteValue = createEditorTransaction(
|
|
|
|
() => props.editor,
|
2023-09-04 06:06:12 +00:00
|
|
|
(ed) => (ed && ed.getAttributes('footnote').value) || ''
|
2023-08-28 11:48:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const handleAddFootnote = (footnote) => {
|
|
|
|
if (footNote()) {
|
|
|
|
props.editor.chain().focus().updateFootnote(footnote).run()
|
|
|
|
} else {
|
|
|
|
props.editor.chain().focus().setFootnote({ value: footnote }).run()
|
|
|
|
}
|
|
|
|
setFootNote()
|
|
|
|
setFootnoteEditorOpen(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleOpenFootnoteEditor = () => {
|
|
|
|
setFootNote(currentFootnoteValue())
|
|
|
|
setFootnoteEditorOpen(true)
|
|
|
|
}
|
|
|
|
|
2023-07-28 19:53:21 +00:00
|
|
|
onMount(() => {
|
|
|
|
window.addEventListener('keydown', handleKeyDown)
|
|
|
|
})
|
|
|
|
|
|
|
|
onCleanup(() => {
|
|
|
|
window.removeEventListener('keydown', handleKeyDown)
|
|
|
|
})
|
2023-05-04 12:16:39 +00:00
|
|
|
|
|
|
|
return (
|
2023-08-28 11:48:54 +00:00
|
|
|
<div ref={props.ref} class={clsx(styles.TextBubbleMenu, { [styles.growWidth]: footnoteEditorOpen() })}>
|
2023-05-04 12:16:39 +00:00
|
|
|
<Switch>
|
|
|
|
<Match when={linkEditorOpen()}>
|
2023-07-28 19:53:21 +00:00
|
|
|
<InsertLinkForm editor={props.editor} onClose={() => setLinkEditorOpen(false)} />
|
2023-05-04 12:16:39 +00:00
|
|
|
</Match>
|
2023-08-28 11:48:54 +00:00
|
|
|
<Match when={footnoteEditorOpen()}>
|
|
|
|
<Button size={'S'} onClick={() => showModal('uploadImage')} value={'img'} />
|
|
|
|
<SimplifiedEditor
|
|
|
|
imageEnabled={true}
|
|
|
|
placeholder={t('Enter footnote text')}
|
|
|
|
onSubmit={(value) => handleAddFootnote(value)}
|
|
|
|
variant={'bordered'}
|
|
|
|
initialContent={currentFootnoteValue().value ?? null}
|
|
|
|
onCancel={() => {
|
|
|
|
setFootnoteEditorOpen(false)
|
|
|
|
}}
|
|
|
|
submitButtonText={t('Send')}
|
|
|
|
/>
|
|
|
|
</Match>
|
|
|
|
<Match when={!linkEditorOpen() || !footnoteEditorOpen()}>
|
2023-05-04 12:16:39 +00:00
|
|
|
<>
|
2023-05-11 11:43:14 +00:00
|
|
|
<Show when={!props.isCommonMarkup}>
|
|
|
|
<>
|
|
|
|
<div class={styles.dropDownHolder}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: textSizeBubbleOpen()
|
|
|
|
})}
|
|
|
|
onClick={toggleTextSizePopup}
|
|
|
|
>
|
|
|
|
<Icon name="editor-text-size" />
|
|
|
|
<Icon name="down-triangle" class={styles.triangle} />
|
|
|
|
</button>
|
|
|
|
<Show when={textSizeBubbleOpen()}>
|
|
|
|
<div class={styles.dropDown}>
|
|
|
|
<header>{t('Headers')}</header>
|
|
|
|
<div class={styles.actions}>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Header 1')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isH1()
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
2023-06-24 14:45:57 +00:00
|
|
|
props.editor.chain().focus().toggleHeading({ level: 2 }).run()
|
2023-05-29 17:14:58 +00:00
|
|
|
toggleTextSizePopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-h1" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
<Popover content={t('Header 2')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isH2()
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
2023-06-24 14:45:57 +00:00
|
|
|
props.editor.chain().focus().toggleHeading({ level: 3 }).run()
|
2023-05-29 17:14:58 +00:00
|
|
|
toggleTextSizePopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-h2" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
<Popover content={t('Header 3')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isH3()
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
2023-06-24 14:45:57 +00:00
|
|
|
props.editor.chain().focus().toggleHeading({ level: 4 }).run()
|
2023-05-29 17:14:58 +00:00
|
|
|
toggleTextSizePopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-h3" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-11 11:43:14 +00:00
|
|
|
</div>
|
|
|
|
<header>{t('Quotes')}</header>
|
|
|
|
<div class={styles.actions}>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Quote')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
2023-09-04 06:06:12 +00:00
|
|
|
[styles.bubbleMenuButtonActive]: isQuote()
|
2023-05-29 17:14:58 +00:00
|
|
|
})}
|
|
|
|
onClick={() => {
|
2023-09-04 06:06:12 +00:00
|
|
|
if (isPunchLine()) return
|
2023-05-29 17:14:58 +00:00
|
|
|
props.editor.chain().focus().toggleBlockquote('quote').run()
|
|
|
|
toggleTextSizePopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-blockquote" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
<Popover content={t('Punchline')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
2023-09-04 06:06:12 +00:00
|
|
|
[styles.bubbleMenuButtonActive]: isPunchLine()
|
2023-05-29 17:14:58 +00:00
|
|
|
})}
|
|
|
|
onClick={() => {
|
2023-09-04 06:06:12 +00:00
|
|
|
if (isQuote()) return
|
2023-05-29 17:14:58 +00:00
|
|
|
props.editor.chain().focus().toggleBlockquote('punchline').run()
|
|
|
|
toggleTextSizePopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-quote" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-11 11:43:14 +00:00
|
|
|
</div>
|
2023-05-29 10:09:44 +00:00
|
|
|
<header>{t('squib')}</header>
|
|
|
|
<div class={styles.actions}>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Incut')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
2023-09-04 06:06:12 +00:00
|
|
|
[styles.bubbleMenuButtonActive]: isIncut()
|
2023-05-29 17:14:58 +00:00
|
|
|
})}
|
|
|
|
onClick={() => {
|
|
|
|
props.editor.chain().focus().toggleArticle().run()
|
|
|
|
toggleTextSizePopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-squib" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-29 10:09:44 +00:00
|
|
|
</div>
|
2023-05-11 11:43:14 +00:00
|
|
|
</div>
|
|
|
|
</Show>
|
2023-05-04 12:16:39 +00:00
|
|
|
</div>
|
2023-05-11 11:43:14 +00:00
|
|
|
<div class={styles.delimiter} />
|
|
|
|
</>
|
|
|
|
</Show>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Bold')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isBold()
|
|
|
|
})}
|
|
|
|
onClick={() => props.editor.chain().focus().toggleBold().run()}
|
|
|
|
>
|
|
|
|
<Icon name="editor-bold" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
<Popover content={t('Italic')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isItalic()
|
|
|
|
})}
|
|
|
|
onClick={() => props.editor.chain().focus().toggleItalic().run()}
|
|
|
|
>
|
|
|
|
<Icon name="editor-italic" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-11 11:43:14 +00:00
|
|
|
|
|
|
|
<Show when={!props.isCommonMarkup}>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Highlight')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isHighlight()
|
|
|
|
})}
|
|
|
|
onClick={() => props.editor.chain().focus().toggleHighlight({ color: '#f6e3a1' }).run()}
|
|
|
|
>
|
|
|
|
<div class={styles.toggleHighlight} />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-07-27 17:38:38 +00:00
|
|
|
<div class={styles.delimiter} />
|
2023-05-11 11:43:14 +00:00
|
|
|
</Show>
|
2023-08-23 05:37:18 +00:00
|
|
|
<Popover content={<div class={styles.noWrap}>{t('Add url')}</div>}>
|
2023-05-29 17:14:58 +00:00
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
2023-07-24 08:58:07 +00:00
|
|
|
onClick={() => setLinkEditorOpen(true)}
|
2023-05-29 17:14:58 +00:00
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isLink()
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<Icon name="editor-link" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-11 11:43:14 +00:00
|
|
|
<Show when={!props.isCommonMarkup}>
|
|
|
|
<>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Insert footnote')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
2023-08-28 11:48:54 +00:00
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isFootnote()
|
|
|
|
})}
|
|
|
|
onClick={handleOpenFootnoteEditor}
|
|
|
|
>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Icon name="editor-footnote" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-11 11:43:14 +00:00
|
|
|
<div class={styles.delimiter} />
|
|
|
|
<div class={styles.dropDownHolder}>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: listBubbleOpen()
|
|
|
|
})}
|
|
|
|
onClick={toggleListPopup}
|
|
|
|
>
|
|
|
|
<Icon name="editor-ul" />
|
|
|
|
<Icon name="down-triangle" class={styles.triangle} />
|
|
|
|
</button>
|
|
|
|
<Show when={listBubbleOpen()}>
|
|
|
|
<div class={styles.dropDown}>
|
|
|
|
<header>{t('Lists')}</header>
|
|
|
|
<div class={styles.actions}>
|
2023-05-29 17:14:58 +00:00
|
|
|
<Popover content={t('Bullet list')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isBulletList()
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
|
|
|
props.editor.chain().focus().toggleBulletList().run()
|
|
|
|
toggleListPopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-ul" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
|
|
|
<Popover content={t('Ordered list')}>
|
|
|
|
{(triggerRef: (el) => void) => (
|
|
|
|
<button
|
|
|
|
ref={triggerRef}
|
|
|
|
type="button"
|
|
|
|
class={clsx(styles.bubbleMenuButton, {
|
|
|
|
[styles.bubbleMenuButtonActive]: isOrderedList()
|
|
|
|
})}
|
|
|
|
onClick={() => {
|
|
|
|
props.editor.chain().focus().toggleOrderedList().run()
|
|
|
|
toggleListPopup()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="editor-ol" />
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</Popover>
|
2023-05-11 11:43:14 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Show>
|
2023-05-04 12:16:39 +00:00
|
|
|
</div>
|
2023-05-11 11:43:14 +00:00
|
|
|
</>
|
|
|
|
</Show>
|
2023-05-04 12:16:39 +00:00
|
|
|
</>
|
|
|
|
</Match>
|
|
|
|
</Switch>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|