diff --git a/src/components/Editor/EditorBubbleMenu.tsx b/src/components/Editor/EditorBubbleMenu.tsx index 392630a2..fd99e803 100644 --- a/src/components/Editor/EditorBubbleMenu.tsx +++ b/src/components/Editor/EditorBubbleMenu.tsx @@ -1,4 +1,4 @@ -import { createEffect, createSignal, Show } from 'solid-js' +import { Match, Switch, createSignal, Show } from 'solid-js' import type { Editor } from '@tiptap/core' import styles from './EditorBubbleMenu.module.scss' import { Icon } from '../_shared/Icon' @@ -7,8 +7,6 @@ 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 @@ -23,20 +21,26 @@ export const EditorBubbleMenu = (props: BubbleMenuProps) => { const [prevUrl, setPrevUrl] = createSignal(null) const [linkError, setLinkError] = createSignal(null) - const activeControl = (action: ActionName, actionLevel?: HeadingLevel, prevLink?: boolean) => + const isActive = (name: string, attributes?: {}, checkPrevUrl?: boolean) => createEditorTransaction( () => props.editor, - (editor) => editor && editor.isActive(action, actionLevel && { actionLevel }) + (editor) => { + editor && editor.isActive(name, attributes) + if (checkPrevUrl) { + setPrevUrl(editor && editor.getAttributes('link').href) + } + } ) - const isLink = createEditorTransaction( - // вызов этой ф-ии обусловлен не через хэлпер activeControl только проверкой установленна ли ссылка - () => props.editor, - (editor) => { - editor && editor.isActive('link') - setPrevUrl(editor && editor.getAttributes('link').href) - } - ) + const isBold = isActive('bold') + const isItalic = isActive('italic') + const isH1 = isActive('heading', { level: 1 }) + const isH2 = isActive('heading', { level: 2 }) + const isH3 = isActive('heading', { level: 3 }) + const isBlockQuote = isActive('blockquote') + const isOrderedList = isActive('isOrderedList') + const isBulletList = isActive('isBulletList') + const isLink = isActive('link', {}, true) const clearLinkForm = () => { setUrl('') @@ -45,8 +49,14 @@ export const EditorBubbleMenu = (props: BubbleMenuProps) => { 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() }) + props.editor.chain().focus().toggleLink({ href: url() }).run() clearLinkForm() } else { setLinkError(t('Invalid url format')) @@ -65,160 +75,171 @@ export const EditorBubbleMenu = (props: BubbleMenuProps) => { return ( <>
- {linkEditorOpen() ? ( - <> -
handleSubmitLink(e)} class={styles.linkForm}> - setUrl(e.currentTarget.value)} - /> - - -
- {linkError() &&
{linkError()}
} - - ) : ( - <> -
+ + + <> +
handleSubmitLink(e)} class={styles.linkForm}> + setUrl(e.currentTarget.value)} + /> + + +
+ {linkError() &&
{linkError()}
} + +
+ + <> +
+ + +
+
{t('Headers')}
+
+ + + +
+
{t('Quotes')}
+
+ + +
+
+
+
+
- -
-
{t('Headers')}
-
- - - -
-
{t('Quotes')}
-
- -
-
-
-
-
- - -
- - - -
-
- -
-
{t('Lists')}
-
- - +
+ + +
+
+ + +
+
{t('Lists')}
+
+ + +
-
- -
- - )} + +
+ + +
)