From 65252c0a0f1f3f094a6cfd170e4846afb8440349 Mon Sep 17 00:00:00 2001 From: Aslam H Date: Sun, 18 Aug 2024 11:33:29 +0700 Subject: [PATCH] fix: remove selection update, instead whenever show, update the link state --- .../bubble-menu/link-bubble-menu.tsx | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/components/minimal-tiptap/components/bubble-menu/link-bubble-menu.tsx b/src/components/minimal-tiptap/components/bubble-menu/link-bubble-menu.tsx index db29ea8..01323e2 100644 --- a/src/components/minimal-tiptap/components/bubble-menu/link-bubble-menu.tsx +++ b/src/components/minimal-tiptap/components/bubble-menu/link-bubble-menu.tsx @@ -1,4 +1,4 @@ -import React, { useState, useCallback, useEffect } from 'react' +import React, { useState, useCallback } from 'react' import { Editor } from '@tiptap/core' import { BubbleMenu } from '@tiptap/react' import { LinkEditBlock } from '../link/link-edit-block' @@ -28,21 +28,21 @@ export const LinkBubbleMenu: React.FC = ({ editor }) => { setSelectedText(text) }, [editor]) - useEffect(() => { - editor.on('selectionUpdate', updateLinkState) + const shouldShow = useCallback( + ({ editor, from, to }: ShouldShowProps) => { + if (from === to) { + return false + } + const { href } = editor.getAttributes('link') - return () => { - editor.off('selectionUpdate', updateLinkState) - } - }, [editor, updateLinkState]) - - const shouldShow = useCallback(({ editor, from, to }: ShouldShowProps) => { - if (from === to) { + if (href) { + updateLinkState() + return true + } return false - } - const link = editor.getAttributes('link') - return typeof link.href === 'string' && link.href !== '' - }, []) + }, + [updateLinkState] + ) const handleEdit = useCallback(() => { setShowEdit(true) @@ -79,7 +79,6 @@ export const LinkBubbleMenu: React.FC = ({ editor }) => { editor.chain().focus().extendMarkRange('link').unsetLink().run() setShowEdit(false) updateLinkState() - return null }, [editor, updateLinkState]) return (