Commit 425db4d4 by liimin

feat(components): 优化复制按钮并更新外部链接对话框

- 更新 CopyBtn 组件,使用 Remix 图标替换原有图标
- 改进 ExternalLink 组件,增加复制链接功能和新窗口打开按钮
- 调整 VoiceInput 组件的录音状态管理
- 优化 Markdown 组件的渲染逻辑
- 更新多语言翻译文件,添加复制功能相关文本
1 parent ae3743ab
...@@ -5,6 +5,7 @@ import { debounce } from 'lodash-es' ...@@ -5,6 +5,7 @@ import { debounce } from 'lodash-es'
import copy from 'copy-to-clipboard' import copy from 'copy-to-clipboard'
import s from './style.module.css' import s from './style.module.css'
import Tooltip from '@/app/components/base/tooltip' import Tooltip from '@/app/components/base/tooltip'
import { RiCheckboxMultipleFill, RiClipboardLine, RiFileCopy2Fill, RiFileCopy2Line, RiFileCopyFill } from '@remixicon/react'
type ICopyBtnProps = { type ICopyBtnProps = {
value: string value: string
...@@ -31,11 +32,11 @@ const CopyBtn = ({ ...@@ -31,11 +32,11 @@ const CopyBtn = ({
return ( return (
<div className={`${className}`}> <div className={`${className}`}>
<Tooltip <Tooltip
popupContent={(isCopied ? t('appApi.copied') : t('appApi.copy'))} popupContent={(isCopied ? t('share.common.copied') : t('share.common.copy'))}
asChild={false} asChild={false}
> >
<div <div
onMouseLeave={onMouseLeave} // onMouseLeave={onMouseLeave}
className={'box-border flex cursor-pointer items-center justify-center rounded-md bg-components-button-secondary-bg p-0.5'} className={'box-border flex cursor-pointer items-center justify-center rounded-md bg-components-button-secondary-bg p-0.5'}
style={!isPlain style={!isPlain
? { ? {
...@@ -44,7 +45,9 @@ const CopyBtn = ({ ...@@ -44,7 +45,9 @@ const CopyBtn = ({
: {}} : {}}
onClick={onClickCopy} onClick={onClickCopy}
> >
<div className={`h-6 w-6 rounded-md hover:bg-components-button-secondary-bg-hover ${s.copyIcon} ${isCopied ? s.copied : ''}`}></div> {!isCopied && <RiFileCopy2Line className="h-4 w-4" />}
{isCopied && <RiCheckboxMultipleFill className="h-4 w-4" />}
{/* <div className={`h-6 w-6 rounded-md hover:bg-components-button-secondary-bg-hover ${s.copied} ${isCopied ? s.copied : ''}`}></div> */}
</div> </div>
</Tooltip> </Tooltip>
</div> </div>
......
...@@ -3,7 +3,7 @@ import Button from '@/app/components/base/button' ...@@ -3,7 +3,7 @@ import Button from '@/app/components/base/button'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { isValidUrl } from './utils' import { isValidUrl } from './utils'
import { InputMessageType } from '@/app/components/base/chat/types' import { InputMessageType } from '@/app/components/base/chat/types'
const MarkdownButton = ({ node }:any) => { const MarkdownButton = ({ node }: any) => {
const { onSend } = useChatContext() const { onSend } = useChatContext()
const variant = node.properties.dataVariant const variant = node.properties.dataVariant
const message = node.properties.dataMessage const message = node.properties.dataMessage
...@@ -20,7 +20,7 @@ const MarkdownButton = ({ node }:any) => { ...@@ -20,7 +20,7 @@ const MarkdownButton = ({ node }:any) => {
return return
} }
if (message) { if (message) {
onSend?.(message,InputMessageType.QUESTION_IN_ANSWER ) onSend?.(message, InputMessageType.QUESTION_IN_ANSWER)
// if (messageId) { // if (messageId) {
// document.getElementById(messageId)?.classList.add('pointer-events-none', 'opacity-50') // document.getElementById(messageId)?.classList.add('pointer-events-none', 'opacity-50')
// } // }
......
...@@ -3,6 +3,8 @@ import { InputMessageType } from '../../../chat/types' ...@@ -3,6 +3,8 @@ import { InputMessageType } from '../../../chat/types'
import { useChatContext } from '@/app/components/base/chat/chat-with-history/context/chat-context' import { useChatContext } from '@/app/components/base/chat/chat-with-history/context/chat-context'
import type { LinkProps } from './index' import type { LinkProps } from './index'
import { Dialog, DialogPanel, DialogTitle } from '@headlessui/react' import { Dialog, DialogPanel, DialogTitle } from '@headlessui/react'
import Copy from '@/app/components/base/chat/chat/answer/components/operation/copy'
import CopyBtn from '@/app/components/base/copy-btn'
const ExternalLink: FC<LinkProps> = ({ node, children }) => { const ExternalLink: FC<LinkProps> = ({ node, children }) => {
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false)
const [isLoading, setIsLoading] = useState(true) const [isLoading, setIsLoading] = useState(true)
...@@ -23,24 +25,40 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => { ...@@ -23,24 +25,40 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => {
{children || 'Open'} {children || 'Open'}
</a> </a>
<Dialog open={isOpen} onClose={() => setIsOpen(false)} className="relative z-50"> <Dialog open={isOpen} onClose={() => setIsOpen(false)} className="relative z-50">
<div className="fixed inset-0 bg-black/30" aria-hidden="true" /> <div className="fixed inset-0 bg-black/30" aria-hidden="true" />
<div className="fixed inset-0 flex items-center justify-center p-1"> <div className="fixed inset-0 flex items-center justify-center h-full">
<DialogPanel className="w-full h-full bg-gray-800/80 backdrop-blur-sm rounded-lg flex flex-col border-8 border-t-0 border-transparent"> <DialogPanel className="w-full h-full backdrop-blur-sm rounded-lg flex flex-col">
<div className="flex justify-between items-center border-b border-gray-600 h-8"> <div className="flex bg-white h-8 border-b border-gray-200">
{/* <DialogTitle className="text-sm font-medium text-white/80">外部页面</DialogTitle> */} {/* 左侧:自适应宽度区域 */}
<div className="flex-1 flex items-center overflow-hidden">
<span className="text-sm text-gray-500 truncate">{node.properties.href}</span>
<CopyBtn value={node.properties.href} className="ml-2" />
</div>
{/* 右侧:按钮组固定宽度 */}
<div className="flex items-center gap-2">
<button
onClick={() => window.open(node.properties.href, '_blank')}
className="px-2 py-1 text-xs bg-primary-500 text-white rounded hover:bg-primary-600">
新窗口打开
</button>
<button <button
onClick={() => setIsOpen(false)} onClick={() => setIsOpen(false)}
className="ml-auto p-1 rounded-full hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-primary-500"> className="p-1 rounded-full bg-gray-400 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-primary-500">
<span className="sr-only">关闭</span> <span className="sr-only">关闭</span>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" className="w-5 h-5 text-gray-400 hover:text-white"> <svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="w-5 h-5 text-blue-500-600 hover:text-white">
<path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" /> <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />
</svg> </svg>
</button> </button>
</div> </div>
</div>
<div className="flex-1 relative flex items-center justify-center"> <div className="flex-1 relative flex items-center justify-center p-4 pt-2">
{isLoading && ( {isLoading && (
<div className="absolute inset-0 flex items-center justify-center"> <div className="absolute inset-0 flex items-center justify-center">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary-500" /> <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary-500" />
...@@ -59,7 +77,7 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => { ...@@ -59,7 +77,7 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => {
) : ( ) : (
<iframe <iframe
src={node.properties.href} src={node.properties.href}
className="w-full max-w-[800px] h-full border-0 mx-auto" className="w-full max-w-[800px] h-full bg-white border-0 mx-auto"
allow="fullscreen" allow="fullscreen"
loading="lazy" loading="lazy"
onLoad={() => setIsLoading(false)} onLoad={() => setIsLoading(false)}
...@@ -72,7 +90,7 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => { ...@@ -72,7 +90,7 @@ const ExternalLink: FC<LinkProps> = ({ node, children }) => {
</div> </div>
</DialogPanel> </DialogPanel>
</div> </div>
</Dialog> </Dialog>
</> </>
) )
} }
......
...@@ -26,7 +26,6 @@ type MarkdownProps = { ...@@ -26,7 +26,6 @@ type MarkdownProps = {
operationClassName?: string operationClassName?: string
} }
export function Markdown(props: MarkdownProps) { export function Markdown(props: MarkdownProps) {
const markdown = useMemo(() => {
const latexContent = flow([preprocessThinkTag, preprocessLaTeX])(props.content) const latexContent = flow([preprocessThinkTag, preprocessLaTeX])(props.content)
return ( return (
<div className={cn('markdown-body', '!text-text-primary', props.className)}> <div className={cn('markdown-body', '!text-text-primary', props.className)}>
...@@ -80,6 +79,4 @@ export function Markdown(props: MarkdownProps) { ...@@ -80,6 +79,4 @@ export function Markdown(props: MarkdownProps) {
</ReactMarkdown> </ReactMarkdown>
</div> </div>
) )
}, [props.content])
return markdown
} }
...@@ -24,8 +24,9 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -24,8 +24,9 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
const ctxRef = useRef<CanvasRenderingContext2D | null>(null) const ctxRef = useRef<CanvasRenderingContext2D | null>(null)
const drawRecordId = useRef<number | null>(null) const drawRecordId = useRef<number | null>(null)
const [originDuration, setOriginDuration] = useState(0) const [originDuration, setOriginDuration] = useState(0)
const [startRecord, setStartRecord] = useState(false) // const [startRecord, setStartRecord] = useState(false)
const [startConvert, setStartConvert] = useState(false) // const [startConvert, setStartConvert] = useState(false)
const [statement, setStatement] = useState<'recording' | 'converting' | 'none'>('none')
const pathname = usePathname() const pathname = usePathname()
const params = useParams() const params = useParams()
const clearInterval = useRafInterval(() => { const clearInterval = useRafInterval(() => {
...@@ -77,13 +78,12 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -77,13 +78,12 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
const handleStopRecorder = useCallback(async () => { const handleStopRecorder = useCallback(async () => {
clearInterval() clearInterval()
setStartRecord(false) // setStartRecord(false)
setStartConvert(true) // setStartConvert(true)
setStatement('converting')
// 修改点3: 安全访问recorder实例 // 修改点3: 安全访问recorder实例
if (recorder.current) { if (recorder.current) {
recorder.current.stop() recorder.current.stop()
drawRecordId.current && cancelAnimationFrame(drawRecordId.current) drawRecordId.current && cancelAnimationFrame(drawRecordId.current)
drawRecordId.current = null drawRecordId.current = null
const canvas = canvasRef.current! const canvas = canvasRef.current!
...@@ -110,9 +110,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -110,9 +110,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
try { try {
const audioResponse = await audioToText(url, isPublic, formData) const audioResponse = await audioToText(url, isPublic, formData)
onConverted(audioResponse.text) onConverted(audioResponse.text)
onCancel()
} catch (e) { } catch (e) {
onConverted('') onConverted('')
} finally {
setStatement('none')
onCancel() onCancel()
} }
} }
...@@ -122,21 +123,18 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -122,21 +123,18 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
try { try {
// 修改点4: 确保清理之前的录音实例 // 修改点4: 确保清理之前的录音实例
if (recorder.current) { if (recorder.current) {
try { // if (recorder.current.isRecording) {
if (recorder.current.isRecording) { recorder.current.stop()
await recorder.current.stop() // }
}
recorder.current.destroy() recorder.current.destroy()
} catch (e) {
console.error('Error destroying previous recorder:', e)
}
} }
// 创建新的录音实例 // 创建新的录音实例
recorder.current = createRecorder() recorder.current = createRecorder()
await recorder.current.start() await recorder.current.start()
setStartRecord(true) // setStartRecord(true)
setStartConvert(false) // setStartConvert(false)
setStatement('recording')
setOriginDuration(0) // 重置录音时间 setOriginDuration(0) // 重置录音时间
// 修改点5: 确保canvas正确初始化 // 修改点5: 确保canvas正确初始化
...@@ -180,10 +178,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -180,10 +178,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
// 修改点6: 添加录音超时处理 // 修改点6: 添加录音超时处理
useEffect(() => { useEffect(() => {
if (originDuration >= 600 && startRecord) { if (originDuration >= 600 && statement === 'recording') {
handleStopRecorder() handleStopRecorder()
} }
}, [originDuration, startRecord, handleStopRecorder]) }, [originDuration, statement, handleStopRecorder])
useEffect(() => { useEffect(() => {
handleStartRecord() handleStartRecord()
...@@ -191,14 +189,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -191,14 +189,10 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
// 修改点7: 清理函数确保释放资源 // 修改点7: 清理函数确保释放资源
return () => { return () => {
if (recorder.current) { if (recorder.current) {
try { // if (recorder.current.isRecording) {
if (recorder.current.isRecording) {
recorder.current.stop() recorder.current.stop()
} // }
recorder.current.destroy() recorder.current.destroy()
} catch (e) {
console.error('Cleanup error:', e)
}
recorder.current = null recorder.current = null
} }
...@@ -215,22 +209,27 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes) ...@@ -215,22 +209,27 @@ const VoiceInput = ({ onCancel, onConverted, wordTimestamps }: VoiceInputTypes)
{/* <div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 py-[14px] pl-[14.5px] pr-[6.5px]"></div> */} {/* <div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 py-[14px] pl-[14.5px] pr-[6.5px]"></div> */}
<div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 pl-[14.5px] pr-[6.5px]"> <div className="absolute inset-[1.5px] flex items-center overflow-hidden rounded-[10.5px] bg-primary-25 pl-[14.5px] pr-[6.5px]">
<canvas id="voice-input-record" className="absolute bottom-0 left-0 h-5 w-full" /> <canvas id="voice-input-record" className="absolute bottom-0 left-0 h-5 w-full" />
{startConvert && <RiLoader2Line className="mr-2 h-4 w-4 animate-spin text-primary-700" />} {statement === 'converting' && <RiLoader2Line className="mr-2 h-4 w-4 animate-spin text-primary-700" />}
<div className="grow"> <div className="grow">
{startRecord && <div className="text-sm text-gray-500">{t('common.voiceInput.speaking')}</div>} {statement === 'recording' && <div className="text-sm text-gray-500">{t('common.voiceInput.speaking')}</div>}
{startConvert && <div className={cn(s.convert, 'text-sm')}>{t('common.voiceInput.converting')}</div>} {statement === 'converting' && (
<div className={cn(s.convert, 'text-sm')}>{t('common.voiceInput.converting')}</div>
)}
</div> </div>
{startRecord && ( {statement === 'recording' && (
<div className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-primary-100" onClick={handleStopRecorder}> <div
className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-primary-100"
onClick={handleStopRecorder}>
<StopCircle className="h-5 w-5 text-primary-600" /> <StopCircle className="h-5 w-5 text-primary-600" />
</div> </div>
)} )}
{startConvert && ( {/* {statement==='converting' && (
<div className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-gray-200" onClick={onCancel}> <div className="mr-1 flex h-8 w-8 cursor-pointer items-center justify-center rounded-lg hover:bg-gray-200" onClick={onCancel}>
<RiCloseLine className="h-4 w-4 text-gray-500" /> <RiCloseLine className="h-4 w-4 text-gray-500" />
</div> </div>
)} )} */}
<div className={`w-[45px] pl-1 text-xs font-medium ${originDuration > 500 ? 'text-[#F04438]' : 'text-gray-700'}`}> <div
className={`w-[45px] pl-1 text-xs font-medium ${originDuration > 500 ? 'text-[#F04438]' : 'text-gray-700'}`}>
{`${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`} {`${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`}
</div> </div>
</div> </div>
......
...@@ -3,6 +3,8 @@ const translation = { ...@@ -3,6 +3,8 @@ const translation = {
welcome: '', welcome: '',
appUnavailable: 'App is unavailable', appUnavailable: 'App is unavailable',
appUnknownError: 'App is unavailable', appUnknownError: 'App is unavailable',
copy: 'Copy',
copied: 'Copied',
}, },
chat: { chat: {
newChat: 'Start New chat', newChat: 'Start New chat',
......
...@@ -3,6 +3,8 @@ const translation = { ...@@ -3,6 +3,8 @@ const translation = {
welcome: '', welcome: '',
appUnavailable: '应用不可用', appUnavailable: '应用不可用',
appUnknownError: '应用不可用', appUnknownError: '应用不可用',
copy: '复制',
copied: '已复制',
}, },
chat: { chat: {
newChat: '开启新对话', newChat: '开启新对话',
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!