From 8b114ea75fa4bcb73abe34d2c1c517db8a7d6c75 Mon Sep 17 00:00:00 2001 From: Ilya Y <75578537+ilya-bkv@users.noreply.github.com> Date: Wed, 15 Nov 2023 17:52:10 +0300 Subject: [PATCH] Feature/refactoring simplyfy editor (#318) add Linkbubble menu --- .../LinkBubbleMenu/LinkBubbleMenu.module.scss | 4 ++ .../LinkBubbleMenu/LinkBubbleMenu.module.tsx | 20 ++++++++++ src/components/Editor/LinkBubbleMenu/index.ts | 1 + src/components/Editor/SimplifiedEditor.tsx | 40 ++++++++++++++----- src/stores/ui.ts | 2 - 5 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.scss create mode 100644 src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.tsx create mode 100644 src/components/Editor/LinkBubbleMenu/index.ts diff --git a/src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.scss b/src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.scss new file mode 100644 index 00000000..27f3ca77 --- /dev/null +++ b/src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.scss @@ -0,0 +1,4 @@ +.LinkBubbleMenu { + background: var(--editor-bubble-menu-background); + box-shadow: 0 4px 10px rgba(#000, 0.25); +} diff --git a/src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.tsx b/src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.tsx new file mode 100644 index 00000000..7b80de09 --- /dev/null +++ b/src/components/Editor/LinkBubbleMenu/LinkBubbleMenu.module.tsx @@ -0,0 +1,20 @@ +import type { Editor } from '@tiptap/core' + +import { InsertLinkForm } from '../InsertLinkForm' + +import styles from './LinkBubbleMenu.module.scss' + +type Props = { + editor: Editor + ref: (el: HTMLDivElement) => void + shouldShow: boolean + onClose: () => void +} + +export const LinkBubbleMenuModule = (props: Props) => { + return ( +
+ +
+ ) +} diff --git a/src/components/Editor/LinkBubbleMenu/index.ts b/src/components/Editor/LinkBubbleMenu/index.ts new file mode 100644 index 00000000..7d3e3ace --- /dev/null +++ b/src/components/Editor/LinkBubbleMenu/index.ts @@ -0,0 +1 @@ +export { LinkBubbleMenuModule } from './LinkBubbleMenu.module' diff --git a/src/components/Editor/SimplifiedEditor.tsx b/src/components/Editor/SimplifiedEditor.tsx index c2a5c988..786e7eb4 100644 --- a/src/components/Editor/SimplifiedEditor.tsx +++ b/src/components/Editor/SimplifiedEditor.tsx @@ -31,7 +31,7 @@ import { Modal } from '../Nav/Modal' import { Figcaption } from './extensions/Figcaption' import { Figure } from './extensions/Figure' -import { InsertLinkForm } from './InsertLinkForm' +import { LinkBubbleMenuModule } from './LinkBubbleMenu' import { TextBubbleMenu } from './TextBubbleMenu' import { UploadModalContent } from './UploadModalContent' @@ -64,7 +64,7 @@ export const MAX_DESCRIPTION_LIMIT = 400 const SimplifiedEditor = (props: Props) => { const { t } = useLocalize() const [counter, setCounter] = createSignal() - + const [shouldShowLinkBubbleMenu, setShouldShowLinkBubbleMenu] = createSignal(false) const isCancelButtonVisible = createMemo(() => props.isCancelButtonVisible !== false) const wrapperEditorElRef: { current: HTMLElement @@ -84,6 +84,12 @@ const SimplifiedEditor = (props: Props) => { current: null, } + const linkBubbleMenuRef: { + current: HTMLDivElement + } = { + current: null, + } + const { actions: { setEditor }, } = useEditorContext() @@ -129,6 +135,15 @@ const SimplifiedEditor = (props: Props) => { return view.hasFocus() && !empty }, }), + BubbleMenu.configure({ + pluginKey: 'linkBubbleMenu', + element: linkBubbleMenuRef.current, + shouldShow: ({ state }) => { + const { selection } = state + const { empty } = selection + return !empty && shouldShowLinkBubbleMenu() + }, + }), ImageFigure, Image, Figcaption, @@ -213,7 +228,7 @@ const SimplifiedEditor = (props: Props) => { if (event.code === 'KeyK' && (event.metaKey || event.ctrlKey) && !editor().state.selection.empty) { event.preventDefault() - showModal('simplifiedEditorInsertLink') + setShouldShowLinkBubbleMenu(true) } } @@ -231,8 +246,6 @@ const SimplifiedEditor = (props: Props) => { }) } - const handleInsertLink = () => !editor().state.selection.empty && showModal('simplifiedEditorInsertLink') - createEffect(() => { if (html()) { setCounter(editor().storage.characterCount.characters()) @@ -244,6 +257,10 @@ const SimplifiedEditor = (props: Props) => { 'max-height': `${props.maxHeight}px`, } + const handleShowLinkBubble = () => { + editor().chain().focus().run() + setShouldShowLinkBubbleMenu(true) + } return (
(wrapperEditorElRef.current = el)} @@ -294,7 +311,7 @@ const SimplifiedEditor = (props: Props) => {
- - - hideModal()} /> - - @@ -369,6 +381,12 @@ const SimplifiedEditor = (props: Props) => { ref={(el) => (textBubbleMenuRef.current = el)} /> + (linkBubbleMenuRef.current = el)} + onClose={() => setShouldShowLinkBubbleMenu(false)} + /> ) } diff --git a/src/stores/ui.ts b/src/stores/ui.ts index b4edecd2..aa555272 100644 --- a/src/stores/ui.ts +++ b/src/stores/ui.ts @@ -21,7 +21,6 @@ export type ModalType = | 'simplifiedEditorUploadImage' | 'uploadCoverImage' | 'editorInsertLink' - | 'simplifiedEditorInsertLink' | 'followers' | 'following' @@ -37,7 +36,6 @@ export const MODALS: Record = { simplifiedEditorUploadImage: 'simplifiedEditorUploadImage', uploadCoverImage: 'uploadCoverImage', editorInsertLink: 'editorInsertLink', - simplifiedEditorInsertLink: 'simplifiedEditorInsertLink', followers: 'followers', following: 'following', }