diff --git a/src/content/pages/home/DropContainer.tsx b/src/content/pages/home/DropContainer.tsx index f2dc600..4947d6a 100644 --- a/src/content/pages/home/DropContainer.tsx +++ b/src/content/pages/home/DropContainer.tsx @@ -1,6 +1,7 @@ import React, { FC } from 'react' import styled from 'styled-components/macro' import { ConnectDropTarget } from 'react-dnd' +import { Portal } from '../components/Portal' const Container = styled.div<{ active?: boolean }>` position: fixed; @@ -35,14 +36,16 @@ type BlockDropContainerProps = { const DropContainer: FC = ({ drop, active }) => { return ( - - 拖动到此 -
- 加入黑名单 - - - -
+ + + 拖动到此 +
+ 加入黑名单 + + + +
+
) } diff --git a/src/content/pages/home/GlobalStyle.tsx b/src/content/pages/home/GlobalStyle.tsx index 83ebb77..70b56c9 100644 --- a/src/content/pages/home/GlobalStyle.tsx +++ b/src/content/pages/home/GlobalStyle.tsx @@ -23,16 +23,5 @@ const GlobalStyle = styled.createGlobalStyle` .refined-leetcode-block.refined-leetcode-temp:hover { filter: none; } - - /* 设置当前鼠标悬浮的帖子元素,以显示拖拽手柄和定位 */ - .css-1pej3s6-FeedContainer:hover { - position: relative; - & > span { - display: block; - } - } - .css-1pej3s6-FeedContainer > span { - display: none; - } ` export default GlobalStyle diff --git a/src/content/pages/home/Mask.tsx b/src/content/pages/home/Mask.tsx index ea5b2a4..0898e27 100644 --- a/src/content/pages/home/Mask.tsx +++ b/src/content/pages/home/Mask.tsx @@ -1,5 +1,6 @@ import { useEffect, FC } from 'react' import 'styled-components/macro' +import { Portal } from '../components/Portal' import { isValid } from './DragAndDrop' @@ -19,32 +20,34 @@ const Mask: FC = ({ open, setOpen }) => { el = e.target as HTMLElement if (!(el instanceof HTMLAnchorElement)) return if (!isValid(el.href)) return - setOpen(true) + setTimeout(() => setOpen(true), 100) } const handleDragend = () => { setOpen(false) } - document.body.addEventListener('drag', handleDragstart) + document.body.addEventListener('dragstart', handleDragstart) document.body.addEventListener('dragend', handleDragend) return () => { - document.body.removeEventListener('drag', handleDragstart) + document.body.removeEventListener('dragstart', handleDragstart) document.body.removeEventListener('dragend', handleDragend) } }, []) if (!open) return null return ( -
+ +
+ ) } diff --git a/src/content/pages/home/PostItem.tsx b/src/content/pages/home/PostItem.tsx index 8a70cd3..5902566 100644 --- a/src/content/pages/home/PostItem.tsx +++ b/src/content/pages/home/PostItem.tsx @@ -3,6 +3,7 @@ import { css } from 'styled-components/macro' import { useDrag } from 'react-dnd' import Popper from '../components/PopperUnstyled' +import { useHover } from '../hooks' export const ItemTypes = { POST: 'post', @@ -22,15 +23,26 @@ const PostItem: FC<{ }> = ({ setOpen }) => { const [container, setContainer] = useState(null) const postElements = useRef([]) + const [hoverRef, hoverIcon] = useHover() + const [hoverContainer, setHoverContainer] = useState(false) + const timer = useRef>() const handleMouseEnter = (e: MouseEvent) => { const el = e.target as HTMLDivElement if (el && !el.classList.contains('refined-leetcode-block')) { - setContainer(() => el ?? null) + if (timer.current) clearTimeout(timer.current) + setContainer(el) + setHoverContainer(true) } } + const handleMouseLeave = () => { + timer.current = setTimeout(() => { + setHoverContainer(false) + }, 100) + } + useEffect(() => { postElements.current.push( ...document.querySelectorAll( @@ -39,11 +51,13 @@ const PostItem: FC<{ ) postElements.current.forEach(el => { el.addEventListener('mouseenter', handleMouseEnter) + el.addEventListener('mouseleave', handleMouseLeave) }) return () => { postElements.current.forEach(el => { el.removeEventListener('mouseenter', handleMouseEnter) + el.removeEventListener('mouseleave', handleMouseLeave) }) postElements.current = [] } @@ -58,6 +72,7 @@ const PostItem: FC<{ if (type === 'QUESTION' || type === 'SOLUTION') { postElements.current.push(node) node.addEventListener('mouseenter', handleMouseEnter) + node.addEventListener('mouseleave', handleMouseLeave) } } } @@ -104,15 +119,17 @@ const PostItem: FC<{ } }, [container, isDragging, setOpen]) - if (!container) return null + if (!hoverContainer && !hoverIcon && !isDragging) return null preview(container) return ( { + hoverRef(ref) + drag(ref) + }} offset={{ top: 36, left: 339 }} >