-
-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
14 changed files
with
222 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ export interface CommentBaseProps { | |
refId: string | ||
|
||
afterSubmit?: () => void | ||
initialValue?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { ModalContentComponent } from '~/providers/root/modal-stack-provider' | ||
|
||
import { CommentBoxRoot } from '../comment/CommentBox' | ||
import { Comments } from '../comment/Comments' | ||
|
||
export interface CommentModalProps { | ||
title: string | ||
refId: string | ||
|
||
initialValue?: string | ||
} | ||
|
||
export const CommentModal: ModalContentComponent<CommentModalProps> = ( | ||
props, | ||
) => { | ||
const { refId, title, dismiss, initialValue } = props | ||
|
||
return ( | ||
<div className="max-w-95vw w-[700px] overflow-y-auto overflow-x-hidden"> | ||
<span> | ||
回复: <h1 className="mt-4 text-lg font-medium">{title}</h1> | ||
</span> | ||
|
||
<CommentBoxRoot | ||
initialValue={initialValue} | ||
className="my-12" | ||
refId={refId} | ||
afterSubmit={dismiss} | ||
/> | ||
|
||
<Comments refId={refId} /> | ||
</div> | ||
) | ||
} |
108 changes: 108 additions & 0 deletions
108
src/components/widgets/shared/WithArticleSelectionAction.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import { AnimatePresence, m } from 'framer-motion' | ||
|
||
import { useIsMobile } from '~/atoms' | ||
import { MotionButtonBase } from '~/components/ui/button' | ||
import { useIsClient } from '~/hooks/common/use-is-client' | ||
import { useModalStack } from '~/providers/root/modal-stack-provider' | ||
|
||
import { CommentModal } from './CommentModal' | ||
|
||
export const WithArticleSelectionAction: Component<{ | ||
refId: string | ||
title: string | ||
}> = ({ refId, title, children }) => { | ||
const isMobile = useIsMobile() | ||
const [pos, setPos] = useState({ | ||
x: 0, | ||
y: 0, | ||
}) | ||
const [show, setShow] = useState(false) | ||
const [selectedText, setSelectedText] = useState('') | ||
const ref = useRef<HTMLDivElement>(null) | ||
|
||
useEffect(() => { | ||
const handler = (e: MouseEvent): void => { | ||
if (window.getSelection()?.toString().length === 0) { | ||
setShow(false) | ||
return | ||
} | ||
if (ref.current?.contains(e.target as Node)) return | ||
setShow(false) | ||
} | ||
document.addEventListener('mousedown', handler) | ||
document.addEventListener('mouseup', handler) | ||
return () => { | ||
document.removeEventListener('mousedown', handler) | ||
document.removeEventListener('mouseup', handler) | ||
} | ||
}, []) | ||
|
||
const isClient = useIsClient() | ||
const { present } = useModalStack() | ||
|
||
if (isMobile || !isClient) return children | ||
return ( | ||
<div | ||
className="relative" | ||
ref={ref} | ||
onMouseUp={(e) => { | ||
const $ = ref.current | ||
if (!$) return | ||
|
||
const selection = window.getSelection() | ||
if (!selection) return | ||
const selectedText = selection.toString() | ||
if (selectedText.length === 0) return | ||
const { top, left } = $.getBoundingClientRect() | ||
setShow(true) | ||
setSelectedText(selectedText) | ||
setPos({ | ||
x: e.clientX - left + 10, | ||
y: e.clientY - top + 10, | ||
}) | ||
}} | ||
> | ||
{children} | ||
|
||
<AnimatePresence> | ||
{show && ( | ||
<m.div | ||
className="absolute z-10 rounded-md border border-slate-200/90 bg-slate-100 px-4 py-3 text-sm dark:border-neutral-800/90 dark:bg-zinc-900" | ||
data-event="selection-action" | ||
style={{ | ||
left: pos.x, | ||
top: pos.y, | ||
}} | ||
initial={{ y: 10, opacity: 0.6, scale: 0.96 }} | ||
animate={{ y: 0, opacity: 1, scale: 1 }} | ||
exit={{ | ||
y: 20, | ||
opacity: 0, | ||
}} | ||
> | ||
<MotionButtonBase | ||
onClick={() => { | ||
present({ | ||
title: '评论', | ||
content: (rest) => ( | ||
<CommentModal | ||
refId={refId} | ||
title={title} | ||
initialValue={`> ${selectedText | ||
?.split('\n') | ||
.join('')}\n\n`} | ||
{...rest} | ||
/> | ||
), | ||
}) | ||
}} | ||
> | ||
引用评论 | ||
</MotionButtonBase> | ||
</m.div> | ||
)} | ||
</AnimatePresence> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.
4a94877
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
shiro – ./
shiro-git-main-innei.vercel.app
springtide.vercel.app
shiro-innei.vercel.app
innei.in