Skip to content

Commit

Permalink
fix: remove selection update, instead whenever show, update the link …
Browse files Browse the repository at this point in the history
…state
  • Loading branch information
Aslam97 committed Aug 18, 2024
1 parent 734be69 commit 65252c0
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -28,21 +28,21 @@ export const LinkBubbleMenu: React.FC<LinkBubbleMenuProps> = ({ 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)
Expand Down Expand Up @@ -79,7 +79,6 @@ export const LinkBubbleMenu: React.FC<LinkBubbleMenuProps> = ({ editor }) => {
editor.chain().focus().extendMarkRange('link').unsetLink().run()
setShowEdit(false)
updateLinkState()
return null
}, [editor, updateLinkState])

return (
Expand Down

0 comments on commit 65252c0

Please sign in to comment.